Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) using Anthropic response_format={"type": "json_object"} #6721

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add support for response_format=json anthropic
  • Loading branch information
ishaan-jaff committed Nov 12, 2024
commit 7ef3b680a25f893a7db3e5170d2a64beb6657bbc
58 changes: 42 additions & 16 deletions litellm/llms/anthropic/chat/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
AllAnthropicToolsValues,
AnthropicComputerTool,
AnthropicHostedTools,
AnthropicInputSchema,
AnthropicMessageRequestBase,
AnthropicMessagesRequest,
AnthropicMessagesTool,
Expand Down Expand Up @@ -159,15 +160,19 @@ def _map_tool_helper(
returned_tool: Optional[AllAnthropicToolsValues] = None

if tool["type"] == "function" or tool["type"] == "custom":
_input_function_parameters: dict = (
tool["function"].get("parameters", None) or {}
)
_tool_input_schema: AnthropicInputSchema = AnthropicInputSchema(
type=_input_function_parameters.get("type", "object"),
properties=_input_function_parameters.get("properties", {}),
additionalProperties=_input_function_parameters.get(
"additionalProperties", True
),
)
_tool = AnthropicMessagesTool(
name=tool["function"]["name"],
input_schema=tool["function"].get(
"parameters",
{
"type": "object",
"properties": {},
},
),
input_schema=_tool_input_schema,
)

_description = tool["function"].get("description")
Expand Down Expand Up @@ -304,17 +309,10 @@ def map_openai_params(
- You should set tool_choice (see Forcing tool use) to instruct the model to explicitly use that tool
- Remember that the model will pass the input to the tool, so the name of the tool and description should be from the model’s perspective.
"""
_tool_choice = None
_tool_choice = {"name": "json_tool_call", "type": "tool"}

_tool = AnthropicMessagesTool(
name="json_tool_call",
input_schema={
"type": "object",
"properties": {"values": json_schema}, # type: ignore
},
_tool = self._create_json_tool_call_for_response_format(
json_schema=json_schema,
)

optional_params["tools"] = [_tool]
optional_params["tool_choice"] = _tool_choice
optional_params["json_mode"] = True
Expand All @@ -341,6 +339,34 @@ def map_openai_params(

return optional_params

def _create_json_tool_call_for_response_format(
self,
json_schema: Optional[dict] = None,
) -> AnthropicMessagesTool:
"""
Handles creating a tool call for getting responses in JSON format.

Args:
json_schema (Optional[dict]): The JSON schema the response should be in

Returns:
AnthropicMessagesTool: The tool call to send to Anthropic API to get responses in JSON format
"""
_input_schema: AnthropicInputSchema = AnthropicInputSchema(
type="object",
)

if json_schema is None:
# Anthropic raises a 400 BadRequest error if properties is passed as None
# see usage with additionalProperties (Example 5) https://github.com/anthropics/anthropic-cookbook/blob/main/tool_use/extracting_structured_json.ipynb
_input_schema["additionalProperties"] = True
_input_schema["properties"] = {}
else:
_input_schema["properties"] = json_schema

_tool = AnthropicMessagesTool(name="json_tool_call", input_schema=_input_schema)
return _tool

def is_cache_control_set(self, messages: List[AllMessageValues]) -> bool:
"""
Return if {"cache_control": ..} in message content block
Expand Down
8 changes: 7 additions & 1 deletion litellm/types/llms/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ class AnthropicMessagesToolChoice(TypedDict, total=False):
disable_parallel_tool_use: bool # default is false


class AnthropicInputSchema(TypedDict, total=False):
type: Optional[str]
properties: Optional[dict]
additionalProperties: Optional[bool]


class AnthropicMessagesTool(TypedDict, total=False):
name: Required[str]
description: str
input_schema: Required[dict]
input_schema: Optional[AnthropicInputSchema]
type: Literal["custom"]
cache_control: Optional[Union[dict, ChatCompletionCachedContent]]

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy