Skip to content

Commit

Permalink
fix anthropic chat model mutating input list (#6457)
Browse files Browse the repository at this point in the history
<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.

Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.

After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.

Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->

<!-- Remove if not applicable -->

Fixes: ChatAnthropic was mutating the input message list during
formatting which isn't ideal bc you could be changing the behavior for
other chat models when using the same input

#### Before submitting

<!-- If you're adding a new integration, please include:

1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use


See contribution guidelines for more information on how to write tests,
lint
etc:


https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->

#### Who can review?

Tag maintainers/contributors who might be interested:
  • Loading branch information
agola11 committed Jun 20, 2023
1 parent bc0af67 commit a924633
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions langchain/chat_models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def _convert_messages_to_prompt(self, messages: List[BaseMessage]) -> str:
Returns:
str: Combined string with necessary HUMAN_PROMPT and AI_PROMPT tags.
"""
messages = messages.copy() # don't mutate the original list

if not self.AI_PROMPT:
raise NameError("Please ensure the anthropic package is loaded")

Expand Down
16 changes: 16 additions & 0 deletions tests/integration_tests/chat_models/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ def test_anthropic_call() -> None:
assert isinstance(response.content, str)


def test_anthropic_generate() -> None:
"""Test generate method of anthropic."""
chat = ChatAnthropic(model="test")
chat_messages: List[List[BaseMessage]] = [
[HumanMessage(content="How many toes do dogs have?")]
]
messages_copy = [messages.copy() for messages in chat_messages]
result: LLMResult = chat.generate(chat_messages)
assert isinstance(result, LLMResult)
for response in result.generations[0]:
assert isinstance(response, ChatGeneration)
assert isinstance(response.text, str)
assert response.text == response.message.content
assert chat_messages == messages_copy


def test_anthropic_streaming() -> None:
"""Test streaming tokens from anthropic."""
chat = ChatAnthropic(model="test", streaming=True)
Expand Down

1 comment on commit a924633

@vercel
Copy link

@vercel vercel bot commented on a924633 Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langchain – ./

langchain-git-master-langchain.vercel.app
python.langchain.com
langchain-langchain.vercel.app

Please sign in to comment.