Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix proxy perf) use _read_request_body instead of ast.literal_eval to get better performance #7545

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions litellm/proxy/proxy_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ast
import asyncio
import copy
import inspect
Expand Down Expand Up @@ -3339,13 +3338,7 @@ async def chat_completion( # noqa: PLR0915

data = {}
try:
body = await request.body()
body_str = body.decode()
try:
data = ast.literal_eval(body_str)
except Exception:
data = json.loads(body_str)

data = await _read_request_body(request=request)
verbose_proxy_logger.debug(
"Request received by LiteLLM:\n{}".format(json.dumps(data, indent=4)),
)
Expand Down Expand Up @@ -3612,12 +3605,7 @@ async def completion( # noqa: PLR0915
global user_temperature, user_request_timeout, user_max_tokens, user_api_base
data = {}
try:
body = await request.body()
body_str = body.decode()
try:
data = ast.literal_eval(body_str)
except Exception:
data = json.loads(body_str)
data = await _read_request_body(request=request)

data["model"] = (
general_settings.get("completion_model", None) # server default
Expand Down Expand Up @@ -5350,12 +5338,7 @@ async def anthropic_response( # noqa: PLR0915
litellm.adapters = [{"id": "anthropic", "adapter": anthropic_adapter}]

global user_temperature, user_request_timeout, user_max_tokens, user_api_base
body = await request.body()
body_str = body.decode()
try:
request_data: dict = ast.literal_eval(body_str)
except Exception:
request_data = json.loads(body_str)
request_data = await _read_request_body(request=request)
data: dict = {**request_data, "adapter_id": "anthropic"}
try:
data["model"] = (
Expand Down
2 changes: 1 addition & 1 deletion tests/local_testing/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
from litellm.litellm_core_utils.prompt_templates.factory import anthropic_messages_pt

# litellm.num_retries = 3
# litellm.num_retries=3

litellm.cache = None
litellm.success_callback = []
Expand Down
Loading