mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-22 00:31:44 +00:00
Fix Anthropic system message handling
This commit is contained in:
parent
816c925bdd
commit
1cfea955aa
2 changed files with 19 additions and 2 deletions
|
|
@ -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": "."}]}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue