mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-18 21:41:24 +00:00
Signed-off-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com> Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com> Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com> Co-authored-by: ZhangShenao <15201440436@163.com> Co-authored-by: Friso H. Kingma <fhkingma@gmail.com> Co-authored-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Morgante Pell <morgantep@google.com>
90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
import os
|
|
|
|
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
|
|
from langchain_core.tools import tool
|
|
from pydantic import BaseModel
|
|
|
|
from langchain_community.chat_models import MiniMaxChat
|
|
|
|
|
|
def test_chat_minimax_not_group_id() -> None:
|
|
if "MINIMAX_GROUP_ID" in os.environ:
|
|
del os.environ["MINIMAX_GROUP_ID"]
|
|
chat = MiniMaxChat() # type: ignore[call-arg]
|
|
response = chat.invoke("你好呀")
|
|
assert isinstance(response, AIMessage)
|
|
assert isinstance(response.content, str)
|
|
|
|
|
|
def test_chat_minimax_with_stream() -> None:
|
|
chat = MiniMaxChat() # type: ignore[call-arg]
|
|
for chunk in chat.stream("你好呀"):
|
|
assert isinstance(chunk, AIMessage)
|
|
assert isinstance(chunk.content, str)
|
|
|
|
|
|
@tool
|
|
def add(a: int, b: int) -> int:
|
|
"""Adds a and b."""
|
|
return a + b
|
|
|
|
|
|
@tool
|
|
def multiply(a: int, b: int) -> int:
|
|
"""Multiplies a and b."""
|
|
return a * b
|
|
|
|
|
|
def test_chat_minimax_with_tool() -> None:
|
|
"""Test MinimaxChat with bind tools."""
|
|
chat = MiniMaxChat() # type: ignore[call-arg]
|
|
tools = [add, multiply]
|
|
chat_with_tools = chat.bind_tools(tools)
|
|
|
|
query = "What is 3 * 12?"
|
|
messages = [HumanMessage(query)]
|
|
ai_msg = chat_with_tools.invoke(messages)
|
|
assert isinstance(ai_msg, AIMessage)
|
|
assert isinstance(ai_msg.tool_calls, list)
|
|
assert len(ai_msg.tool_calls) == 1
|
|
tool_call = ai_msg.tool_calls[0]
|
|
assert "args" in tool_call
|
|
messages.append(ai_msg) # type: ignore[arg-type]
|
|
for tool_call in ai_msg.tool_calls:
|
|
selected_tool = {"add": add, "multiply": multiply}[tool_call["name"].lower()]
|
|
tool_output = selected_tool.invoke(tool_call["args"]) # type: ignore[attr-defined]
|
|
messages.append(ToolMessage(tool_output, tool_call_id=tool_call["id"])) # type: ignore[arg-type]
|
|
response = chat_with_tools.invoke(messages)
|
|
assert isinstance(response, AIMessage)
|
|
|
|
|
|
class AnswerWithJustification(BaseModel):
|
|
"""An answer to the user question along with justification for the answer."""
|
|
|
|
answer: str
|
|
justification: str
|
|
|
|
|
|
def test_chat_minimax_with_structured_output() -> None:
|
|
"""Test MiniMaxChat with structured output."""
|
|
llm = MiniMaxChat() # type: ignore
|
|
structured_llm = llm.with_structured_output(AnswerWithJustification)
|
|
response = structured_llm.invoke(
|
|
"What weighs more a pound of bricks or a pound of feathers"
|
|
)
|
|
assert isinstance(response, AnswerWithJustification)
|
|
|
|
|
|
def test_chat_tongyi_with_structured_output_include_raw() -> None:
|
|
"""Test MiniMaxChat with structured output."""
|
|
llm = MiniMaxChat() # type: ignore
|
|
structured_llm = llm.with_structured_output(
|
|
AnswerWithJustification, include_raw=True
|
|
)
|
|
response = structured_llm.invoke(
|
|
"What weighs more a pound of bricks or a pound of feathers"
|
|
)
|
|
assert isinstance(response, dict)
|
|
assert isinstance(response.get("raw"), AIMessage)
|
|
assert isinstance(response.get("parsed"), AnswerWithJustification)
|