2024-01-24 03:23:46 +00:00
|
|
|
from langchain_core.messages import AIMessage, AIMessageChunk, HumanMessage
|
|
|
|
|
|
|
|
from langchain_community.chat_models.sparkllm import ChatSparkLLM
|
|
|
|
|
|
|
|
|
2024-04-13 23:03:19 +00:00
|
|
|
def test_initialization() -> None:
|
|
|
|
"""Test chat model initialization."""
|
2024-05-21 00:11:36 +00:00
|
|
|
|
2024-04-13 23:03:19 +00:00
|
|
|
for model in [
|
2024-05-21 00:11:36 +00:00
|
|
|
ChatSparkLLM(
|
|
|
|
api_key="secret",
|
|
|
|
temperature=0.5,
|
|
|
|
timeout=30,
|
|
|
|
),
|
|
|
|
ChatSparkLLM(
|
|
|
|
spark_api_key="secret",
|
|
|
|
request_timeout=30,
|
|
|
|
), # type: ignore[call-arg]
|
2024-04-13 23:03:19 +00:00
|
|
|
]:
|
|
|
|
assert model.request_timeout == 30
|
2024-05-21 00:11:36 +00:00
|
|
|
assert model.spark_api_key == "secret"
|
|
|
|
assert model.temperature == 0.5
|
2024-04-13 23:03:19 +00:00
|
|
|
|
|
|
|
|
2024-01-24 03:23:46 +00:00
|
|
|
def test_chat_spark_llm() -> None:
|
2024-05-13 18:55:07 +00:00
|
|
|
chat = ChatSparkLLM() # type: ignore[call-arg]
|
2024-01-24 03:23:46 +00:00
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
response = chat.invoke([message])
|
2024-01-24 03:23:46 +00:00
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_spark_llm_streaming() -> None:
|
2024-05-13 18:55:07 +00:00
|
|
|
chat = ChatSparkLLM(streaming=True) # type: ignore[call-arg]
|
2024-01-24 03:23:46 +00:00
|
|
|
for chunk in chat.stream("Hello!"):
|
|
|
|
assert isinstance(chunk, AIMessageChunk)
|
|
|
|
assert isinstance(chunk.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_spark_llm_with_domain() -> None:
|
2024-05-13 18:55:07 +00:00
|
|
|
chat = ChatSparkLLM(spark_llm_domain="generalv3") # type: ignore[call-arg]
|
2024-01-24 03:23:46 +00:00
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
response = chat.invoke([message])
|
2024-02-10 00:13:30 +00:00
|
|
|
print(response) # noqa: T201
|
2024-01-24 03:23:46 +00:00
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_spark_llm_with_temperature() -> None:
|
2024-05-13 18:55:07 +00:00
|
|
|
chat = ChatSparkLLM(temperature=0.9, top_k=2) # type: ignore[call-arg]
|
2024-01-24 03:23:46 +00:00
|
|
|
message = HumanMessage(content="Hello")
|
2024-04-24 23:39:23 +00:00
|
|
|
response = chat.invoke([message])
|
2024-02-10 00:13:30 +00:00
|
|
|
print(response) # noqa: T201
|
2024-01-24 03:23:46 +00:00
|
|
|
assert isinstance(response, AIMessage)
|
|
|
|
assert isinstance(response.content, str)
|