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
[Feat] Add tools support for Nvidia NIM (#10763)
Ishaan Jaff6de80a29cb57
diff --git a/litellm/__init__.py b/litellm/__init__.pyindex 241f2f712d9..d8ec5a747c6 100644--- a/litellm/__init__.py+++ b/litellm/__init__.py@@ -994,7 +994,7 @@ from .llms.openai.chat.gpt_audio_transformation import (openAIGPTAudioConfig = OpenAIGPTAudioConfig()-from .llms.nvidia_nim.chat import NvidiaNimConfig+from .llms.nvidia_nim.chat.transformation import NvidiaNimConfigfrom .llms.nvidia_nim.embed import NvidiaNimEmbeddingConfignvidiaNimConfig = NvidiaNimConfig()diff --git a/litellm/llms/nvidia_nim/chat.py b/litellm/llms/nvidia_nim/chat/transformation.pysimilarity index 81%rename from litellm/llms/nvidia_nim/chat.pyrename to litellm/llms/nvidia_nim/chat/transformation.pyindex eedac6e38fe..20478afb59f 100644--- a/litellm/llms/nvidia_nim/chat.py+++ b/litellm/llms/nvidia_nim/chat/transformation.py@@ -7,9 +7,6 @@ This file only contains param mapping logicAPI calling is done using the OpenAI SDK with an api_base"""--from typing import Optional, Union-from litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfig@@ -20,31 +17,6 @@ class NvidiaNimConfig(OpenAIGPTConfig):The class `NvidiaNimConfig` provides configuration for the Nvidia NIM's Chat Completions API interface. Below are the parameters:"""- temperature: Optional[int] = None- top_p: Optional[int] = None- frequency_penalty: Optional[int] = None- presence_penalty: Optional[int] = None- max_tokens: Optional[int] = None- stop: Optional[Union[str, list]] = None-- def __init__(- self,- temperature: Optional[int] = None,- top_p: Optional[int] = None,- frequency_penalty: Optional[int] = None,- presence_penalty: Optional[int] = None,- max_tokens: Optional[int] = None,- stop: Optional[Union[str, list]] = None,- ) -> None:- locals_ = locals().copy()- for key, value in locals_.items():- if key != "self" and value is not None:- setattr(self.__class__, key, value)-- @classmethod- def get_config(cls):- return super().get_config()-def get_supported_openai_params(self, model: str) -> list:"""Get the supported OpenAI params for the given model@@ -116,6 +88,9 @@ class NvidiaNimConfig(OpenAIGPTConfig):"max_completion_tokens","stop","seed",+ "tools",+ "tool_choice",+ "parallel_tool_calls",]def map_openai_params(diff --git a/tests/llm_translation/test_max_completion_tokens.py b/tests/llm_translation/test_max_completion_tokens.pyindex 9bab536467d..8b1a29aef20 100644--- a/tests/llm_translation/test_max_completion_tokens.py+++ b/tests/llm_translation/test_max_completion_tokens.py@@ -171,7 +171,7 @@ def test_all_model_configs():drop_params=False,) == {"max_tokens": 10}- from litellm.llms.nvidia_nim.chat import NvidiaNimConfig+ from litellm.llms.nvidia_nim.chat.transformation import NvidiaNimConfigassert "max_completion_tokens" in NvidiaNimConfig().get_supported_openai_params(model="llama3"diff --git a/tests/llm_translation/test_nvidia_nim.py b/tests/llm_translation/test_nvidia_nim.pyindex 307f27755d7..89513c58b07 100644--- a/tests/llm_translation/test_nvidia_nim.py+++ b/tests/llm_translation/test_nvidia_nim.py@@ -89,3 +89,95 @@ def test_embedding_nvidia_nim():assert request_body["model"] == "nvidia/nv-embedqa-e5-v5"assert request_body["extra_body"]["input_type"] == "passage"assert request_body["dimensions"] == 1024+++def test_chat_completion_nvidia_nim_with_tools():+ from openai import OpenAI++ litellm.set_verbose = True+ model_name = "nvidia_nim/meta/llama3-70b-instruct"+ client = OpenAI(+ api_key="fake-api-key",+ )++ # Define tools+ tools = [+ {+ "type": "function",+ "function": {+ "name": "get_weather",+ "description": "Get the current weather in a given location",+ "parameters": {+ "type": "object",+ "properties": {+ "location": {+ "type": "string",+ "description": "The city and state, e.g. San Francisco, CA",+ },+ "unit": {+ "type": "string",+ "enum": ["celsius", "fahrenheit"],+ "description": "The unit of temperature to use",+ },+ },+ "required": ["location"],+ },+ },+ },+ {+ "type": "function",+ "function": {+ "name": "get_current_time",+ "description": "Get the current time in a given timezone",+ "parameters": {+ "type": "object",+ "properties": {+ "timezone": {+ "type": "string",+ "description": "The timezone, e.g. EST, PST",+ },+ },+ "required": ["timezone"],+ },+ },+ },+ ]++ with patch.object(+ client.chat.completions.with_raw_response, "create"+ ) as mock_client:+ try:+ completion(+ model=model_name,+ messages=[+ {+ "role": "user",+ "content": "What's the weather like in Boston today and what time is it in EST?",+ }+ ],+ tools=tools,+ tool_choice="auto",+ parallel_tool_calls=True,+ temperature=0.7,+ client=client,+ )+ except Exception as e:+ print(e)++ # Add assertions to check the request+ mock_client.assert_called_once()+ request_body = mock_client.call_args.kwargs++ print("request_body: ", request_body)++ assert request_body["messages"] == [+ {+ "role": "user",+ "content": "What's the weather like in Boston today and what time is it in EST?",+ },+ ]+ assert request_body["model"] == "meta/llama3-70b-instruct"+ assert request_body["temperature"] == 0.7+ assert request_body["tools"] == tools+ assert request_body["tool_choice"] == "auto"+ assert request_body["parallel_tool_calls"] == True