From 1fc9bcb18410f2c2c7434285159cc804eb3ec1a1 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Thu, 16 May 2024 14:38:17 -0700 Subject: [PATCH 1/4] feat use OpenAI extra_headers param --- litellm/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litellm/main.py b/litellm/main.py index 3429cab4d..f6014c2d3 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -552,7 +552,7 @@ def completion( model_info = kwargs.get("model_info", None) proxy_server_request = kwargs.get("proxy_server_request", None) fallbacks = kwargs.get("fallbacks", None) - headers = kwargs.get("headers", None) + headers = kwargs.get("headers", None) or extra_headers num_retries = kwargs.get("num_retries", None) ## deprecated max_retries = kwargs.get("max_retries", None) context_window_fallback_dict = kwargs.get("context_window_fallback_dict", None) From 23bcd03904ece1844a7a4d79e8e520e8e359e0c8 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Thu, 16 May 2024 14:40:31 -0700 Subject: [PATCH 2/4] feat: Anthropic allow users to set anthropic-beta in headers --- litellm/llms/anthropic.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm/llms/anthropic.py b/litellm/llms/anthropic.py index 97a473a2e..2e02ffe59 100644 --- a/litellm/llms/anthropic.py +++ b/litellm/llms/anthropic.py @@ -93,6 +93,7 @@ class AnthropicConfig: "max_tokens", "tools", "tool_choice", + "extra_headers", ] def map_openai_params(self, non_default_params: dict, optional_params: dict): @@ -504,7 +505,9 @@ class AnthropicChatCompletion(BaseLLM): ## Handle Tool Calling if "tools" in optional_params: _is_function_call = True - headers["anthropic-beta"] = "tools-2024-04-04" + if "anthropic-beta" not in headers: + # default to v1 of "anthropic-beta" + headers["anthropic-beta"] = "tools-2024-04-04" anthropic_tools = [] for tool in optional_params["tools"]: From e19e475c9f6d33e63354c3df10cd10fe839eff4f Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Thu, 16 May 2024 14:41:26 -0700 Subject: [PATCH 3/4] test - setting extra headers for anthropic tool use --- litellm/tests/test_completion.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.py index 120a65acb..439f549d1 100644 --- a/litellm/tests/test_completion.py +++ b/litellm/tests/test_completion.py @@ -278,7 +278,8 @@ def test_completion_claude_3_function_call(): model="anthropic/claude-3-opus-20240229", messages=messages, tools=tools, - tool_choice="auto", + tool_choice={"type": "tool", "name": "get_weather"}, + extra_headers={"anthropic-beta": "tools-2024-05-16"}, ) # Add any assertions, here to check response args print(response) From aa0863dd76452fa0a8ceb86f3d31ee8674520e2b Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Thu, 16 May 2024 14:55:29 -0700 Subject: [PATCH 4/4] docs - Setting `anthropic-beta` Header in Requests --- docs/my-website/docs/providers/anthropic.md | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/my-website/docs/providers/anthropic.md b/docs/my-website/docs/providers/anthropic.md index 5bb47d780..bf8f72bea 100644 --- a/docs/my-website/docs/providers/anthropic.md +++ b/docs/my-website/docs/providers/anthropic.md @@ -223,6 +223,34 @@ assert isinstance( ``` +### Setting `anthropic-beta` Header in Requests + +Pass the the `extra_headers` param to litellm, All headers will be forwarded to Anthropic API + +```python +response = completion( + model="anthropic/claude-3-opus-20240229", + messages=messages, + tools=tools, + extra_headers={"anthropic-beta": "tools-2024-05-16"}, +) +``` + +### Forcing Anthropic Tool Use + +If you want Claude to use a specific tool to answer the user’s question + +You can do this by specifying the tool in the `tool_choice` field like so: +```python +response = completion( + model="anthropic/claude-3-opus-20240229", + messages=messages, + tools=tools, + tool_choice={"type": "tool", "name": "get_weather"}, + extra_headers={"anthropic-beta": "tools-2024-05-16"}, +) +``` + ### Parallel Function Calling