The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Litellm dev 10 22 2024 (#6384)
Krish Dholakiacb2563e3c0e4
diff --git a/docs/my-website/docs/realtime.md b/docs/my-website/docs/realtime.mdindex 2149387a669..28697f44b90 100644--- a/docs/my-website/docs/realtime.md+++ b/docs/my-website/docs/realtime.md@@ -83,4 +83,21 @@ ws.on("message", function incoming(message) {ws.on("error", function handleError(error) {console.error("Error: ", error);});-```\ No newline at end of file+```++## Logging++To prevent requests from being dropped, by default LiteLLM just logs these event types:++- `session.created`+- `response.create`+- `response.done`++You can override this by setting the `logged_real_time_event_types` parameter in the config. For example:++```yaml+litellm_settings:+ logged_real_time_event_types: "*" # Log all events+ ## OR ##+ logged_real_time_event_types: ["session.created", "response.create", "response.done"] # Log only these event types+```diff --git a/litellm/__init__.py b/litellm/__init__.pyindex 357057e4cc2..2aa89a03c88 100644--- a/litellm/__init__.py+++ b/litellm/__init__.py@@ -56,6 +56,7 @@ _custom_logger_compatible_callbacks_literal = Literal["opik","argilla",]+logged_real_time_event_types: Optional[Union[List[str], Literal["*"]]] = None_known_custom_logger_compatible_callbacks: List = list(get_args(_custom_logger_compatible_callbacks_literal))diff --git a/litellm/_redis.py b/litellm/_redis.pyindex 289a7d4aed3..c058a0d3a87 100644--- a/litellm/_redis.py+++ b/litellm/_redis.py@@ -69,6 +69,8 @@ def _get_redis_cluster_kwargs(client=None):available_args = [x for x in arg_spec.args if x not in exclude_args]available_args.append("password")+ available_args.append("username")+ available_args.append("ssl")return available_argsdiff --git a/litellm/litellm_core_utils/realtime_streaming.py b/litellm/litellm_core_utils/realtime_streaming.pyindex 922f90e3608..440deac1ccb 100644--- a/litellm/litellm_core_utils/realtime_streaming.py+++ b/litellm/litellm_core_utils/realtime_streaming.py@@ -26,15 +26,24 @@ async with websockets.connect( # type: ignoreimport asyncioimport concurrent.futures+import jsonimport tracebackfrom asyncio import Taskfrom typing import Any, Dict, List, Optional, Union+import litellm+from .litellm_logging import Logging as LiteLLMLogging# Create a thread pool with a maximum of 10 threadsexecutor = concurrent.futures.ThreadPoolExecutor(max_workers=10)+DefaultLoggedRealTimeEventTypes = [+ "session.created",+ "response.create",+ "response.done",+]+class RealTimeStreaming:def __init__(@@ -49,9 +58,27 @@ class RealTimeStreaming:self.messages: List = []self.input_message: Dict = {}+ _logged_real_time_event_types = litellm.logged_real_time_event_types++ if _logged_real_time_event_types is None:+ _logged_real_time_event_types = DefaultLoggedRealTimeEventTypes+ self.logged_real_time_event_types = _logged_real_time_event_types++ def _should_store_message(self, message: Union[str, bytes]) -> bool:+ if isinstance(message, bytes):+ message = message.decode("utf-8")+ message_obj = json.loads(message)+ _msg_type = message_obj["type"]+ if self.logged_real_time_event_types == "*":+ return True+ if _msg_type in self.logged_real_time_event_types:+ return True+ return False+def store_message(self, message: Union[str, bytes]):"""Store message in list"""- self.messages.append(message)+ if self._should_store_message(message):+ self.messages.append(message)def store_input(self, message: dict):"""Store input message"""diff --git a/litellm/llms/AzureOpenAI/chat/gpt_transformation.py b/litellm/llms/AzureOpenAI/chat/gpt_transformation.pyindex 271c5c467a6..fb0b21d3b3b 100644--- a/litellm/llms/AzureOpenAI/chat/gpt_transformation.py+++ b/litellm/llms/AzureOpenAI/chat/gpt_transformation.py@@ -198,9 +198,6 @@ class AzureOpenAIConfig:optional_params["json_mode"] = Trueelse:optional_params["response_format"] = value- elif param == "max_completion_tokens":- # TODO - Azure OpenAI will probably add support for this, we should pass it through when Azure adds support- optional_params["max_tokens"] = valueelif param in supported_openai_params:optional_params[param] = valuediff --git a/litellm/llms/AzureOpenAI/realtime/handler.py b/litellm/llms/AzureOpenAI/realtime/handler.pyindex bf45c53fbd0..a6c0f1967b4 100644--- a/litellm/llms/AzureOpenAI/realtime/handler.py+++ b/litellm/llms/AzureOpenAI/realtime/handler.py@@ -72,5 +72,5 @@ class AzureOpenAIRealtime(AzureChatCompletion):except websockets.exceptions.InvalidStatusCode as e: # type: ignoreawait websocket.close(code=e.status_code, reason=str(e))- except Exception as e:- await websocket.close(code=1011, reason=f"Internal server error: {str(e)}")+ except Exception:+ passdiff --git a/litellm/llms/ollama.py b/litellm/llms/ollama.pyindex e08e6f6938c..845d0e2ddd0 100644--- a/litellm/llms/ollama.py+++ b/litellm/llms/ollama.py@@ -398,6 +398,7 @@ def ollama_completion_stream(url, data, logging_obj):isinstance(content_chunk, StreamingChoices)and hasattr(content_chunk, "delta")and hasattr(content_chunk.delta, "content")+ and content_chunk.delta.content is not None):content_chunks.append(content_chunk.delta.content)response_content = "".join(content_chunks)diff --git a/litellm/llms/vertex_ai_and_google_ai_studio/vertex_ai_partner_models/anthropic/transformation.py b/litellm/llms/vertex_ai_and_google_ai_studio/vertex_ai_partner_models/anthropic/transformation.pyindex 44b8af27957..406314a5933 100644--- a/litellm/llms/vertex_ai_and_google_ai_studio/vertex_ai_partner_models/anthropic/transformation.py+++ b/litellm/llms/vertex_ai_and_google_ai_studio/vertex_ai_partner_models/anthropic/transformation.py@@ -177,3 +177,16 @@ class VertexAIAnthropicConfig:optional_params["json_mode"] = Truereturn optional_params++ @classmethod+ def is_supported_model(+ cls, model: str, custom_llm_provider: Optional[str] = None+ ) -> bool:+ """+ Check if the model is supported by the VertexAI Anthropic API.+ """+ if custom_llm_provider == "vertex_ai" and "claude" in model.lower():+ return True+ elif model in litellm.vertex_anthropic_models:+ return True+ return Falsediff --git a/litellm/main.py b/litellm/main.pyindex 0e62c9fa2fc..f239d2612c0 100644--- a/litellm/main.py+++ b/litellm/main.py@@ -4986,7 +4986,6 @@ def speech(litellm_call_id: Optional[str] = kwargs.get("litellm_call_id", None)proxy_server_request = kwargs.get("proxy_server_request", None)model_info = kwargs.get("model_info", None)- metadata = kwargs.get("metadata", {})model, custom_llm_provider, dynamic_api_key, api_base = get_llm_provider(model=model, custom_llm_provider=custom_llm_provider, api_base=api_base) # type: ignorekwargs.pop("tags", [])diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.jsonindex e6374180494..890ef868832 100644--- a/litellm/model_prices_and_context_window_backup.json+++ b/litellm/model_prices_and_context_window_backup.json@@ -1104,7 +1104,7 @@"litellm_provider": "azure_ai","mode": "chat"},- "azure_ai/Meta-Llama-31-8B-Instruct": {+ "azure_ai/Meta-Llama-3.1-8B-Instruct": {"max_tokens": 128000,"max_input_tokens": 128000,"max_output_tokens": 128000,@@ -1114,7 +1114,7 @@"mode": "chat","source":"https://azuremarketplace.microsoft.com/en-us/marketplace/apps/metagenai.meta-llama-3-1-8b-instruct-offer?tab=PlansAndPrice"},- "azure_ai/Meta-Llama-31-70B-Instruct": {+ "azure_ai/Meta-Llama-3.1-70B-Instruct": {"max_tokens": 128000,"max_input_tokens": 128000,"max_output_tokens": 128000,@@ -1124,7 +1124,7 @@"mode": "chat","source":"https://azuremarketplace.microsoft.com/en-us/marketplace/apps/metagenai.meta-llama-3-1-70b-instruct-offer?tab=PlansAndPrice"},- "azure_ai/Meta-Llama-31-405B-Instruct": {+ "azure_ai/Meta-Llama-3.1-405B-Instruct": {"max_tokens": 128000,"max_input_tokens": 128000,"max_output_tokens": 128000,@@ -6446,6 +6446,14 @@"litellm_provider": "voyage","mode": "embedding"},+ "voyage/voyage-finance-2": {+ "max_tokens": 4000,+ "max_input_tokens": 4000,+ "input_cost_per_token": 0.00000012,+ "output_cost_per_token": 0.000000,+ "litellm_provider": "voyage",+ "mode": "embedding"+ },"databricks/databricks-meta-llama-3-1-405b-instruct": {"max_tokens": 128000,"max_input_tokens": 128000,diff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yamlindex 105fabbdda6..00f4da8d91f 100644--- a/litellm/proxy/_new_secret_config.yaml+++ b/litellm/proxy/_new_secret_config.yaml@@ -1,8 +1,10 @@model_list:- - model_name: gpt-3.5-turbo- litellm_params:- model: gpt-3.5-turbo- api_key: os.environ/OPENAI_API_KEY+ - model_name: gpt-4o+ litellm_params:+ model: azure/gpt-4o-realtime-preview+ api_key: os.environ/AZURE_SWEDEN_API_KEY+ api_base: os.environ/AZURE_SWEDEN_API_BASElitellm_settings:- callbacks: ["prometheus"]\ No newline at end of file+ success_callback: ["langfuse"]+ # logged_real_time_event_types: "*"\ No newline at end of filediff --git a/litellm/utils.py b/litellm/utils.pyindex e82a937bb4e..6b3f0a80ca7 100644--- a/litellm/utils.py+++ b/litellm/utils.py@@ -126,6 +126,7 @@ except (ImportError, AttributeError):os.environ["TIKTOKEN_CACHE_DIR"] = os.getenv("CUSTOM_TIKTOKEN_CACHE_DIR", filename) # use local copy of tiktoken b/c of - https://github.com/BerriAI/litellm/issues/1071+from tiktoken import Encodingencoding = tiktoken.get_encoding("cl100k_base")from importlib import resources@@ -1278,7 +1279,10 @@ def encode(model="", text="", custom_tokenizer: Optional[dict] = None):enc: The encoded text."""tokenizer_json = custom_tokenizer or _select_tokenizer(model=model)- enc = tokenizer_json["tokenizer"].encode(text)+ if isinstance(tokenizer_json["tokenizer"], Encoding):+ enc = tokenizer_json["tokenizer"].encode(text, disallowed_special=())+ else:+ enc = tokenizer_json["tokenizer"].encode(text)return enc@@ -3045,8 +3049,8 @@ def get_optional_params( # noqa: PLR0915)if litellm.vertex_ai_safety_settings is not None:optional_params["safety_settings"] = litellm.vertex_ai_safety_settings- elif (- custom_llm_provider == "vertex_ai" and model in litellm.vertex_anthropic_models+ elif litellm.VertexAIAnthropicConfig.is_supported_model(+ model=model, custom_llm_provider=custom_llm_provider):supported_params = get_supported_openai_params(model=model, custom_llm_provider=custom_llm_providerdiff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.jsonindex e6374180494..890ef868832 100644--- a/model_prices_and_context_window.json+++ b/model_prices_and_context_window.json@@ -1104,7 +1104,7 @@"litellm_provider": "azure_ai","mode": "chat"},- "azure_ai/Meta-Llama-31-8B-Instruct": {+ "azure_ai/Meta-Llama-3.1-8B-Instruct": {"max_tokens": 128000,"max_input_tokens": 128000,"max_output_tokens": 128000,@@ -1114,7 +1114,7 @@"mode": "chat","source":"https://azuremarketplace.microsoft.com/en-us/marketplace/apps/metagenai.meta-llama-3-1-8b-instruct-offer?tab=PlansAndPrice"},- "azure_ai/Meta-Llama-31-70B-Instruct": {+ "azure_ai/Meta-Llama-3.1-70B-Instruct": {"max_tokens": 128000,"max_input_tokens": 128000,"max_output_tokens": 128000,@@ -1124,7 +1124,7 @@"mode": "chat","source":"https://azuremarketplace.microsoft.com/en-us/marketplace/apps/metagenai.meta-llama-3-1-70b-instruct-offer?tab=PlansAndPrice"},- "azure_ai/Meta-Llama-31-405B-Instruct": {+ "azure_ai/Meta-Llama-3.1-405B-Instruct": {"max_tokens": 128000,"max_input_tokens": 128000,"max_output_tokens": 128000,@@ -6446,6 +6446,14 @@"litellm_provider": "voyage","mode": "embedding"},+ "voyage/voyage-finance-2": {+ "max_tokens": 4000,+ "max_input_tokens": 4000,+ "input_cost_per_token": 0.00000012,+ "output_cost_per_token": 0.000000,+ "litellm_provider": "voyage",+ "mode": "embedding"+ },"databricks/databricks-meta-llama-3-1-405b-instruct": {"max_tokens": 128000,"max_input_tokens": 128000,diff --git a/tests/llm_translation/test_max_completion_tokens.py b/tests/llm_translation/test_max_completion_tokens.pyindex d0819e544dc..de335a3c559 100644--- a/tests/llm_translation/test_max_completion_tokens.py+++ b/tests/llm_translation/test_max_completion_tokens.py@@ -235,7 +235,7 @@ def test_all_model_configs():optional_params={},api_version="2022-12-01",drop_params=False,- ) == {"max_tokens": 10}+ ) == {"max_completion_tokens": 10}from litellm.llms.bedrock.chat.converse_transformation import AmazonConverseConfigdiff --git a/tests/llm_translation/test_optional_params.py b/tests/llm_translation/test_optional_params.pyindex 728ec1507f6..a0387ce1b2e 100644--- a/tests/llm_translation/test_optional_params.py+++ b/tests/llm_translation/test_optional_params.py@@ -775,3 +775,12 @@ def test_hosted_vllm_tool_param():)assert "tools" not in optional_paramsassert "tool_choice" not in optional_params+++def test_unmapped_vertex_anthropic_model():+ optional_params = get_optional_params(+ model="claude-3-5-sonnet-v250@20241022",+ custom_llm_provider="vertex_ai",+ max_retries=10,+ )+ assert "max_retries" not in optional_paramsdiff --git a/tests/local_testing/test_completion_cost.py b/tests/local_testing/test_completion_cost.pyindex fcd80650a6e..21659a4c1b3 100644--- a/tests/local_testing/test_completion_cost.py+++ b/tests/local_testing/test_completion_cost.py@@ -2587,3 +2587,50 @@ async def test_test_completion_cost_gpt4o_audio_output_from_model(stream):total_output_cost = output_audio_cost + output_text_costassert round(cost, 2) == round(total_input_cost + total_output_cost, 2)+++def test_completion_cost_azure_ai_meta():+ """+ Relevant issue: https://github.com/BerriAI/litellm/issues/6310+ """+ from litellm import ModelResponse++ os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"+ litellm.model_cost = litellm.get_model_cost_map(url="")++ litellm.set_verbose = True+ response = {+ "id": "cmpl-55db75e0b05344058b0bd8ee4e00bf84",+ "choices": [+ {+ "finish_reason": "stop",+ "index": 0,+ "logprobs": None,+ "message": {+ "content": 'Here\'s one:\n\nWhy did the Linux kernel go to therapy?\n\nBecause it had a lot of "core" issues!\n\nHope that one made you laugh!',+ "refusal": None,+ "role": "assistant",+ "audio": None,+ "function_call": None,+ "tool_calls": [],+ },+ }+ ],+ "created": 1729243714,+ "model": "azure_ai/Meta-Llama-3.1-70B-Instruct",+ "object": "chat.completion",+ "service_tier": None,+ "system_fingerprint": None,+ "usage": {+ "completion_tokens": 32,+ "prompt_tokens": 16,+ "total_tokens": 48,+ "completion_tokens_details": None,+ "prompt_tokens_details": None,+ },+ }++ model_response = ModelResponse(**response)+ cost = completion_cost(model_response, custom_llm_provider="azure_ai")++ assert cost > 0diff --git a/tests/local_testing/test_token_counter.py b/tests/local_testing/test_token_counter.pyindex 6dbf286e4e1..3ad73f2d8b4 100644--- a/tests/local_testing/test_token_counter.py+++ b/tests/local_testing/test_token_counter.py@@ -375,3 +375,7 @@ def test_img_url_token_counter(img_url):assert width is not Noneassert height is not None+++def test_token_encode_disallowed_special():+ encode(model="gpt-3.5-turbo", text="Hello, world! <|endoftext|>")