2023-11-20 02:44:58 +00:00
|
|
|
"""Test Anthropic Chat API wrapper."""
|
|
|
|
from typing import List
|
2023-11-22 01:40:29 +00:00
|
|
|
from unittest.mock import MagicMock
|
2023-11-20 02:44:58 +00:00
|
|
|
|
|
|
|
import pytest
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_core.messages import (
|
|
|
|
AIMessage,
|
|
|
|
BaseMessage,
|
|
|
|
HumanMessage,
|
|
|
|
SystemMessage,
|
|
|
|
)
|
2023-11-20 02:44:58 +00:00
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.chat_models import BedrockChat
|
|
|
|
from langchain_community.chat_models.meta import convert_messages_to_prompt_llama
|
2023-11-20 02:44:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("messages", "expected"),
|
|
|
|
[
|
|
|
|
([HumanMessage(content="Hello")], "[INST] Hello [/INST]"),
|
|
|
|
(
|
|
|
|
[HumanMessage(content="Hello"), AIMessage(content="Answer:")],
|
|
|
|
"[INST] Hello [/INST]\nAnswer:",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[
|
|
|
|
SystemMessage(content="You're an assistant"),
|
|
|
|
HumanMessage(content="Hello"),
|
|
|
|
AIMessage(content="Answer:"),
|
|
|
|
],
|
|
|
|
"<<SYS>> You're an assistant <</SYS>>\n[INST] Hello [/INST]\nAnswer:",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_formatting(messages: List[BaseMessage], expected: str) -> None:
|
|
|
|
result = convert_messages_to_prompt_llama(messages)
|
|
|
|
assert result == expected
|
2023-11-22 01:40:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_anthropic_bedrock() -> None:
|
|
|
|
client = MagicMock()
|
|
|
|
respbody = MagicMock(
|
|
|
|
read=MagicMock(
|
|
|
|
return_value=MagicMock(
|
|
|
|
decode=MagicMock(return_value=b'{"completion":"Hi back"}')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
client.invoke_model.return_value = {"body": respbody}
|
|
|
|
model = BedrockChat(model_id="anthropic.claude-v2", client=client)
|
|
|
|
|
|
|
|
# should not throw an error
|
|
|
|
model.invoke("hello there")
|