fix(factory.py): support mapping openai 'tool' message to anthropic format

This commit is contained in:
Krrish Dholakia 2024-05-04 10:14:52 -07:00
parent 33472bfd2b
commit d9d5149aa1
2 changed files with 47 additions and 2 deletions

View file

@ -18,6 +18,7 @@ from litellm.types.completion import (
ChatCompletionMessageParam,
ChatCompletionFunctionMessageParam,
ChatCompletionMessageToolCallParam,
ChatCompletionToolMessageParam,
)
from litellm.types.llms.anthropic import *
import uuid
@ -1034,6 +1035,40 @@ def anthropic_messages_pt(messages: list):
msg_i += 1
## MERGE CONSECUTIVE TOOL CONTENT ##
while msg_i < len(messages) and messages[msg_i]["role"] == "tool":
"""
Anthropic function message: "role", "name", "input", "id"
OpenAI function message: "content", "name", "role"
- Check if received message is a tool call input or model text response
"""
tool_use_param = True
_message = ChatCompletionToolMessageParam(**messages[msg_i]) # type: ignore
anthropic_tool_message: Optional[
AnthropicMessagesAssistantMessageValues
] = None
try:
anthropic_tool_message = AnthopicMessagesAssistantMessageToolCallParam(
type="tool_use"
)
anthropic_tool_message["input"] = json.loads(_message["content"])
anthropic_tool_message["id"] = _message["tool_call_id"]
anthropic_tool_message["name"] = _message["name"]
except Exception as e:
litellm.print_verbose(
"Invalid dictionary content. Treating as text instead."
)
anthropic_tool_message = (
AnthopicMessagesAssistantMessageTextContentParam(type="text")
)
anthropic_tool_message["text"] = _message["content"]
assistant_content.append(anthropic_tool_message) # type: ignore
msg_i += 1
if assistant_content:
new_messages.append({"role": "assistant", "content": assistant_content})

View file

@ -2367,7 +2367,17 @@ def test_completion_with_fallbacks():
],
],
)
def test_completion_anthropic_hanging(function_call):
@pytest.mark.parametrize(
"tool_call",
[
[{"role": "tool", "tool_call_id": "1234", "content": "Kokoko"}],
[
{"role": "tool", "tool_call_id": "12344", "content": "Kokoko"},
{"role": "tool", "tool_call_id": "1214", "content": "Kokoko"},
],
],
)
def test_completion_anthropic_hanging(function_call, tool_call):
litellm.modify_params = True
messages = [
{
@ -2382,7 +2392,7 @@ def test_completion_anthropic_hanging(function_call):
},
},
]
messages = messages + function_call
messages = messages + function_call + tool_call
litellm.completion(
model="claude-3-haiku-20240307",
messages=messages,