mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-27 03:34:10 +00:00
feat(anthropic.py): support anthropic 'tool_choice' param
Closes https://github.com/BerriAI/litellm/issues/3752
This commit is contained in:
parent
9f4c04dce3
commit
4795c56f84
4 changed files with 23 additions and 3 deletions
|
@ -10,6 +10,7 @@ from .prompt_templates.factory import prompt_factory, custom_prompt
|
||||||
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
|
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
|
||||||
from .base import BaseLLM
|
from .base import BaseLLM
|
||||||
import httpx # type: ignore
|
import httpx # type: ignore
|
||||||
|
from litellm.types.llms.anthropic import AnthropicMessagesToolChoice
|
||||||
|
|
||||||
|
|
||||||
class AnthropicConstants(Enum):
|
class AnthropicConstants(Enum):
|
||||||
|
@ -102,6 +103,17 @@ class AnthropicConfig:
|
||||||
optional_params["max_tokens"] = value
|
optional_params["max_tokens"] = value
|
||||||
if param == "tools":
|
if param == "tools":
|
||||||
optional_params["tools"] = value
|
optional_params["tools"] = value
|
||||||
|
if param == "tool_choice":
|
||||||
|
_tool_choice: Optional[AnthropicMessagesToolChoice] = None
|
||||||
|
if value == "auto":
|
||||||
|
_tool_choice = {"type": "auto"}
|
||||||
|
elif value == "required":
|
||||||
|
_tool_choice = {"type": "any"}
|
||||||
|
elif isinstance(value, dict):
|
||||||
|
_tool_choice = {"type": "tool", "name": value["function"]["name"]}
|
||||||
|
|
||||||
|
if _tool_choice is not None:
|
||||||
|
optional_params["tool_choice"] = _tool_choice
|
||||||
if param == "stream" and value == True:
|
if param == "stream" and value == True:
|
||||||
optional_params["stream"] = value
|
optional_params["stream"] = value
|
||||||
if param == "stop":
|
if param == "stop":
|
||||||
|
|
|
@ -486,7 +486,7 @@ def completion(
|
||||||
response_format: Optional[dict] = None,
|
response_format: Optional[dict] = None,
|
||||||
seed: Optional[int] = None,
|
seed: Optional[int] = None,
|
||||||
tools: Optional[List] = None,
|
tools: Optional[List] = None,
|
||||||
tool_choice: Optional[str] = None,
|
tool_choice: Optional[Union[str, dict]] = None,
|
||||||
logprobs: Optional[bool] = None,
|
logprobs: Optional[bool] = None,
|
||||||
top_logprobs: Optional[int] = None,
|
top_logprobs: Optional[int] = None,
|
||||||
deployment_id=None,
|
deployment_id=None,
|
||||||
|
|
|
@ -278,9 +278,12 @@ def test_completion_claude_3_function_call():
|
||||||
model="anthropic/claude-3-opus-20240229",
|
model="anthropic/claude-3-opus-20240229",
|
||||||
messages=messages,
|
messages=messages,
|
||||||
tools=tools,
|
tools=tools,
|
||||||
tool_choice={"type": "tool", "name": "get_weather"},
|
tool_choice={
|
||||||
extra_headers={"anthropic-beta": "tools-2024-05-16"},
|
"type": "function",
|
||||||
|
"function": {"name": "get_current_weather"},
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add any assertions, here to check response args
|
# Add any assertions, here to check response args
|
||||||
print(response)
|
print(response)
|
||||||
assert isinstance(response.choices[0].message.tool_calls[0].function.name, str)
|
assert isinstance(response.choices[0].message.tool_calls[0].function.name, str)
|
||||||
|
|
|
@ -5,6 +5,11 @@ from pydantic import BaseModel, validator
|
||||||
from typing_extensions import Literal, Required, TypedDict
|
from typing_extensions import Literal, Required, TypedDict
|
||||||
|
|
||||||
|
|
||||||
|
class AnthropicMessagesToolChoice(TypedDict, total=False):
|
||||||
|
type: Required[Literal["auto", "any", "tool"]]
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class AnthopicMessagesAssistantMessageTextContentParam(TypedDict, total=False):
|
class AnthopicMessagesAssistantMessageTextContentParam(TypedDict, total=False):
|
||||||
type: Required[Literal["text"]]
|
type: Required[Literal["text"]]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue