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

* add support for response_format=json anthropic

* add test_json_response_format to baseLLM ChatTest

* fix test_litellm_anthropic_prompt_caching_tools

* fix test_anthropic_function_call_with_no_schema

* test test_create_json_tool_call_for_response_format
This commit is contained in:
Ishaan Jaff 2024-11-12 19:06:00 -08:00 committed by GitHub
parent e7543378b8
commit 6d4cf2d908
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 105 additions and 17 deletions

View file

@ -53,6 +53,32 @@ class BaseLLMChatTest(ABC):
response = litellm.completion(**base_completion_call_args, messages=messages)
assert response is not None
def test_json_response_format(self):
"""
Test that the JSON response format is supported by the LLM API
"""
base_completion_call_args = self.get_base_completion_call_args()
litellm.set_verbose = True
messages = [
{
"role": "system",
"content": "Your output should be a JSON object with no additional properties. ",
},
{
"role": "user",
"content": "Respond with this in json. city=San Francisco, state=CA, weather=sunny, temp=60",
},
]
response = litellm.completion(
**base_completion_call_args,
messages=messages,
response_format={"type": "json_object"},
)
print(response)
@pytest.fixture
def pdf_messages(self):
import base64

View file

@ -627,6 +627,38 @@ def test_anthropic_tool_helper(cache_control_location):
assert tool["cache_control"] == {"type": "ephemeral"}
def test_create_json_tool_call_for_response_format():
"""
tests using response_format=json with anthropic
A tool call to anthropic is made when response_format=json is used.
"""
# Initialize AnthropicConfig
config = AnthropicConfig()
# Test case 1: No schema provided
# See Anthropics Example 5 on how to handle cases when no schema is provided https://github.com/anthropics/anthropic-cookbook/blob/main/tool_use/extracting_structured_json.ipynb
tool = config._create_json_tool_call_for_response_format()
assert tool["name"] == "json_tool_call"
_input_schema = tool.get("input_schema")
assert _input_schema is not None
assert _input_schema.get("type") == "object"
assert _input_schema.get("additionalProperties") is True
assert _input_schema.get("properties") == {}
# Test case 2: With custom schema
# reference: https://github.com/anthropics/anthropic-cookbook/blob/main/tool_use/extracting_structured_json.ipynb
custom_schema = {"name": {"type": "string"}, "age": {"type": "integer"}}
tool = config._create_json_tool_call_for_response_format(json_schema=custom_schema)
assert tool["name"] == "json_tool_call"
_input_schema = tool.get("input_schema")
assert _input_schema is not None
assert _input_schema.get("type") == "object"
assert _input_schema.get("properties") == custom_schema
assert "additionalProperties" not in _input_schema
from litellm import completion