Skip to content

Commit

Permalink
Anthropic system message fix (#11301)
Browse files Browse the repository at this point in the history
Removes human prompt prefix before system message for anthropic models

Bedrock anthropic api enforces that Human and Assistant messages must be
interleaved (cannot have same type twice in a row). We currently treat
System Messages as human messages when converting messages -> string
prompt. Our validation when using Bedrock/BedrockChat raises an error
when this happens. For ChatAnthropic we don't validate this so no error
is raised, but perhaps the behavior is still suboptimal
  • Loading branch information
baskaryan committed Oct 4, 2023
1 parent 34a6410 commit b499de2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
3 changes: 1 addition & 2 deletions libs/langchain/langchain/chat_models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _convert_one_message_to_text(
elif isinstance(message, AIMessage):
message_text = f"{ai_prompt} {message.content}"
elif isinstance(message, SystemMessage):
message_text = f"{human_prompt} <admin>{message.content}</admin>"
message_text = message.content
else:
raise ValueError(f"Got unknown type {message}")
return message_text
Expand All @@ -56,7 +56,6 @@ def convert_messages_to_prompt_anthropic(
"""

messages = messages.copy() # don't mutate the original list

if not isinstance(messages[-1], AIMessage):
messages.append(AIMessage(content=""))

Expand Down
4 changes: 2 additions & 2 deletions libs/langchain/langchain/llms/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def _human_assistant_format(input_text: str) -> str:
if count % 2 == 0:
count += 1
else:
raise ValueError(ALTERNATION_ERROR)
raise ValueError(ALTERNATION_ERROR + f" Received {input_text}")
if input_text[i : i + len(ASSISTANT_PROMPT)] == ASSISTANT_PROMPT:
if count % 2 == 1:
count += 1
else:
raise ValueError(ALTERNATION_ERROR)
raise ValueError(ALTERNATION_ERROR + f" Received {input_text}")

if count % 2 == 1: # Only saw Human, no Assistant
input_text = input_text + ASSISTANT_PROMPT # SILENT CORRECTION
Expand Down
29 changes: 21 additions & 8 deletions libs/langchain/tests/unit_tests/chat_models/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from langchain.chat_models import ChatAnthropic
from langchain.chat_models.anthropic import convert_messages_to_prompt_anthropic
from langchain.schema import AIMessage, BaseMessage, HumanMessage
from langchain.schema import AIMessage, BaseMessage, HumanMessage, SystemMessage

os.environ["ANTHROPIC_API_KEY"] = "foo"

Expand Down Expand Up @@ -50,11 +50,24 @@ def test_anthropic_initialization() -> None:
ChatAnthropic(model="test", anthropic_api_key="test")


def test_formatting() -> None:
messages: List[BaseMessage] = [HumanMessage(content="Hello")]
@pytest.mark.parametrize(
("messages", "expected"),
[
([HumanMessage(content="Hello")], "\n\nHuman: Hello\n\nAssistant:"),
(
[HumanMessage(content="Hello"), AIMessage(content="Answer:")],
"\n\nHuman: Hello\n\nAssistant: Answer:",
),
(
[
SystemMessage(content="You're an assistant"),
HumanMessage(content="Hello"),
AIMessage(content="Answer:"),
],
"You're an assistant\n\nHuman: Hello\n\nAssistant: Answer:",
),
],
)
def test_formatting(messages: List[BaseMessage], expected: str) -> None:
result = convert_messages_to_prompt_anthropic(messages)
assert result == "\n\nHuman: Hello\n\nAssistant:"

messages = [HumanMessage(content="Hello"), AIMessage(content="Answer:")]
result = convert_messages_to_prompt_anthropic(messages)
assert result == "\n\nHuman: Hello\n\nAssistant: Answer:"
assert result == expected

0 comments on commit b499de2

Please sign in to comment.