From fc0ced48c1a3c695186b1d433bb2d99234d6c67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Est=C3=A9vez?= Date: Fri, 3 May 2024 23:38:54 -0400 Subject: [PATCH 1/4] add_function_to_prompt bug fix This blows up when there's no "functions" in the dictionary even when tools is present because the inner function executes regardless (does not short circuit). --- litellm/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litellm/utils.py b/litellm/utils.py index ac8ec35d4..75d6f8b7f 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -4956,7 +4956,7 @@ def get_optional_params( litellm.add_function_to_prompt ): # if user opts to add it to prompt instead optional_params["functions_unsupported_model"] = non_default_params.pop( - "tools", non_default_params.pop("functions") + "tools", non_default_params.pop("functions", None) ) else: raise UnsupportedParamsError( From 1b811cd1529448c563ac11cb83d50eb1011816eb Mon Sep 17 00:00:00 2001 From: phact Date: Tue, 7 May 2024 13:24:28 -0400 Subject: [PATCH 2/4] unit test and list fix --- .../test_get_optional_params_functions_not_supported.py | 9 +++++++++ litellm/utils.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 litellm/tests/test_get_optional_params_functions_not_supported.py diff --git a/litellm/tests/test_get_optional_params_functions_not_supported.py b/litellm/tests/test_get_optional_params_functions_not_supported.py new file mode 100644 index 000000000..2abfbc41f --- /dev/null +++ b/litellm/tests/test_get_optional_params_functions_not_supported.py @@ -0,0 +1,9 @@ +import litellm +from litellm import get_optional_params + +litellm.add_function_to_prompt = True +optional_params = get_optional_params( + tools= [{'type': 'function', 'function': {'description': 'Get the current weather in a given location', 'name': 'get_current_weather', 'parameters': {'type': 'object', 'properties': {'location': {'type': 'string', 'description': 'The city and state, e.g. San Francisco, CA'}, 'unit': {'type': 'string', 'enum': ['celsius', 'fahrenheit']}}, 'required': ['location']}}}], + tool_choice= 'auto', +) +assert optional_params is not None \ No newline at end of file diff --git a/litellm/utils.py b/litellm/utils.py index 75d6f8b7f..c0241f1c3 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -5769,7 +5769,7 @@ def get_optional_params( optional_params["extra_body"] = extra_body else: # if user passed in non-default kwargs for specific providers/models, pass them along - for k in passed_params.keys(): + for k in list(passed_params.keys()): if k not in default_params.keys(): optional_params[k] = passed_params[k] print_verbose(f"Final returned optional params: {optional_params}") From 7c5c9a8152762d12fd515c163d42f9a30e79581d Mon Sep 17 00:00:00 2001 From: phact Date: Tue, 7 May 2024 13:41:05 -0400 Subject: [PATCH 3/4] looks like cohere does support function calling --- litellm/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/litellm/utils.py b/litellm/utils.py index c0241f1c3..75476edc7 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -4932,6 +4932,7 @@ def get_optional_params( and custom_llm_provider != "mistral" and custom_llm_provider != "anthropic" and custom_llm_provider != "cohere_chat" + and custom_llm_provider != "cohere" and custom_llm_provider != "bedrock" and custom_llm_provider != "ollama_chat" ): From 4c64e3da1062c5c8438cbda5433b6253ae3314dc Mon Sep 17 00:00:00 2001 From: phact Date: Tue, 7 May 2024 14:58:35 -0400 Subject: [PATCH 4/4] locals().copy() --- litellm/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index 75476edc7..db48d3617 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -4836,7 +4836,7 @@ def get_optional_params( **kwargs, ): # retrieve all parameters passed to the function - passed_params = locals() + passed_params = locals().copy() special_params = passed_params.pop("kwargs") for k, v in special_params.items(): if k.startswith("aws_") and ( @@ -5770,7 +5770,7 @@ def get_optional_params( optional_params["extra_body"] = extra_body else: # if user passed in non-default kwargs for specific providers/models, pass them along - for k in list(passed_params.keys()): + for k in passed_params.keys(): if k not in default_params.keys(): optional_params[k] = passed_params[k] print_verbose(f"Final returned optional params: {optional_params}")