2023-08-16 07:48:42 +00:00
|
|
|
import pytest
|
2023-11-21 16:35:29 +00:00
|
|
|
from langchain_core.messages import AIMessage, HumanMessage
|
2023-08-16 07:48:42 +00:00
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.chat_models.ernie import ErnieBotChat
|
2023-08-15 08:05:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_chat_ernie_bot() -> None:
|
|
|
|
chat = ErnieBotChat()
|
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
response = chat.invoke([message])
|
2023-08-15 08:05:46 +00:00
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_ernie_bot_with_model_name() -> None:
|
|
|
|
chat = ErnieBotChat(model_name="ERNIE-Bot")
|
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
response = chat.invoke([message])
|
2023-08-15 08:05:46 +00:00
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_ernie_bot_with_temperature() -> None:
|
|
|
|
chat = ErnieBotChat(model_name="ERNIE-Bot", temperature=1.0)
|
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
response = chat.invoke([message])
|
2023-08-15 08:05:46 +00:00
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
assert isinstance(response.content, str)
|
2023-08-16 07:48:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_chat_ernie_bot_with_kwargs() -> None:
|
|
|
|
chat = ErnieBotChat()
|
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
response = chat.invoke([message], temperature=0.88, top_p=0.7)
|
2023-08-16 07:48:42 +00:00
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
def test_extra_kwargs() -> None:
|
|
|
|
chat = ErnieBotChat(temperature=0.88, top_p=0.7)
|
|
|
|
assert chat.temperature == 0.88
|
|
|
|
assert chat.top_p == 0.7
|
|
|
|
|
|
|
|
|
|
|
|
def test_wrong_temperature_1() -> None:
|
|
|
|
chat = ErnieBotChat()
|
|
|
|
message = HumanMessage(content="Hello")
|
2023-08-30 01:20:06 +00:00
|
|
|
with pytest.raises(ValueError) as e:
|
2024-04-24 23:39:23 +00:00
|
|
|
chat.invoke([message], temperature=1.2)
|
2023-08-30 01:20:06 +00:00
|
|
|
assert "parameter check failed, temperature range is (0, 1.0]" in str(e)
|
2023-08-16 07:48:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_wrong_temperature_2() -> None:
|
|
|
|
chat = ErnieBotChat()
|
|
|
|
message = HumanMessage(content="Hello")
|
2023-08-30 01:20:06 +00:00
|
|
|
with pytest.raises(ValueError) as e:
|
2024-04-24 23:39:23 +00:00
|
|
|
chat.invoke([message], temperature=0)
|
2023-08-30 01:20:06 +00:00
|
|
|
assert "parameter check failed, temperature range is (0, 1.0]" in str(e)
|