2023-11-21 16:35:29 +00:00
|
|
|
|
from langchain_core.messages import AIMessage, HumanMessage
|
2023-11-20 21:09:30 +00:00
|
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
|
from langchain_community.chat_models.baichuan import ChatBaichuan
|
2023-10-19 18:37:41 +00:00
|
|
|
|
|
2024-01-24 01:01:57 +00:00
|
|
|
|
# For testing, run:
|
|
|
|
|
# TEST_FILE=tests/integration_tests/chat_models/test_baichuan.py make test
|
2023-10-19 18:37:41 +00:00
|
|
|
|
|
2024-01-24 01:01:57 +00:00
|
|
|
|
|
|
|
|
|
def test_chat_baichuan_default() -> None:
|
|
|
|
|
chat = ChatBaichuan(streaming=True)
|
|
|
|
|
message = HumanMessage(content="请完整背诵将进酒,背诵5遍")
|
2024-04-24 23:39:23 +00:00
|
|
|
|
response = chat.invoke([message])
|
2024-01-24 01:01:57 +00:00
|
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_baichuan_default_non_streaming() -> None:
|
2023-10-19 18:37:41 +00:00
|
|
|
|
chat = ChatBaichuan()
|
2024-01-24 01:01:57 +00:00
|
|
|
|
message = HumanMessage(content="请完整背诵将进酒,背诵5遍")
|
2024-04-24 23:39:23 +00:00
|
|
|
|
response = chat.invoke([message])
|
2024-01-24 01:01:57 +00:00
|
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_baichuan_turbo() -> None:
|
|
|
|
|
chat = ChatBaichuan(model="Baichuan2-Turbo", streaming=True)
|
2023-10-19 18:37:41 +00:00
|
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
|
response = chat.invoke([message])
|
2023-10-19 18:37:41 +00:00
|
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
2024-01-24 01:01:57 +00:00
|
|
|
|
def test_chat_baichuan_turbo_non_streaming() -> None:
|
|
|
|
|
chat = ChatBaichuan(model="Baichuan2-Turbo")
|
2023-10-19 18:37:41 +00:00
|
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
|
response = chat.invoke([message])
|
2023-10-19 18:37:41 +00:00
|
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_baichuan_with_temperature() -> None:
|
2024-01-24 01:01:57 +00:00
|
|
|
|
chat = ChatBaichuan(temperature=1.0)
|
2023-10-19 18:37:41 +00:00
|
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
|
response = chat.invoke([message])
|
2023-10-19 18:37:41 +00:00
|
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_baichuan_with_kwargs() -> None:
|
|
|
|
|
chat = ChatBaichuan()
|
2024-01-24 01:01:57 +00:00
|
|
|
|
message = HumanMessage(content="百川192K API是什么时候上线的?")
|
2024-04-24 23:39:23 +00:00
|
|
|
|
response = chat.invoke(
|
|
|
|
|
[message], temperature=0.88, top_p=0.7, with_search_enhance=True
|
|
|
|
|
)
|
2024-02-10 00:13:30 +00:00
|
|
|
|
print(response) # noqa: T201
|
2023-10-19 18:37:41 +00:00
|
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_extra_kwargs() -> None:
|
2024-01-24 01:01:57 +00:00
|
|
|
|
chat = ChatBaichuan(temperature=0.88, top_p=0.7, with_search_enhance=True)
|
2023-10-19 18:37:41 +00:00
|
|
|
|
assert chat.temperature == 0.88
|
|
|
|
|
assert chat.top_p == 0.7
|
2024-01-24 01:01:57 +00:00
|
|
|
|
assert chat.with_search_enhance is True
|