Fix Anthropic system message handling

This commit is contained in:
Stefan Dobrev 2024-04-14 16:17:10 +03:00
parent 816c925bdd
commit 1cfea955aa
No known key found for this signature in database
GPG key ID: 99EEBEADBF78AFE4
2 changed files with 19 additions and 2 deletions

View file

@ -705,7 +705,7 @@ def anthropic_messages_pt_xml(messages: list):
if assistant_content:
new_messages.append({"role": "assistant", "content": assistant_content})
if new_messages[0]["role"] != "user":
if not new_messages or new_messages[0]["role"] != "user":
if litellm.modify_params:
new_messages.insert(
0, {"role": "user", "content": [{"type": "text", "text": "."}]}
@ -884,7 +884,7 @@ def anthropic_messages_pt(messages: list):
if assistant_content:
new_messages.append({"role": "assistant", "content": assistant_content})
if new_messages[0]["role"] != "user":
if not new_messages or new_messages[0]["role"] != "user":
if litellm.modify_params:
new_messages.insert(
0, {"role": "user", "content": [{"type": "text", "text": "."}]}

View file

@ -2,13 +2,16 @@
# This tests if prompts are being correctly formatted
import sys
import os
import pytest
sys.path.insert(0, os.path.abspath("../.."))
# from litellm.llms.prompt_templates.factory import prompt_factory
import litellm
from litellm import completion
from litellm.llms.prompt_templates.factory import (
anthropic_pt,
anthropic_messages_pt,
claude_2_1_pt,
llama_2_chat_pt,
)
@ -94,4 +97,18 @@ def test_anthropic_pt_formatting():
assert anthropic_pt(messages) == expected_prompt
def test_anthropic_messages_pt():
# Test case: No messages (filtered system messages only)
litellm.modify_params = True
messages = []
expected_messages = [{"role": "user", "content": [{"type": "text", "text": "."}]}]
assert anthropic_messages_pt(messages) == expected_messages
# Test case: No messages (filtered system messages only) when modify_params is False should raise error
litellm.modify_params = False
messages = []
with pytest.raises(Exception) as err:
anthropic_messages_pt(messages)
assert("Invalid first message." in str(err.value))
# codellama_prompt_format()