Documentation Index
Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
单元测试在隔离环境中运行你的智能体中的小型、确定性部分。通过用内存中的虚拟模型(即 fixture)替换真实的大语言模型(LLM),你可以编写精确的响应脚本(文本、工具调用和错误),使测试快速、免费且可重复,无需 API 密钥。
模拟聊天模型
LangChain 提供了 GenericFakeChatModel 用于模拟文本响应。它接受一个响应迭代器(AIMessage 对象或字符串),每次调用返回一个。它支持常规和流式输出两种用法。
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
model = GenericFakeChatModel(messages=iter([
AIMessage(content="", tool_calls=[ToolCall(name="foo", args={"bar": "baz"}, id="call_1")]),
"bar"
]))
model.invoke("hello")
# AIMessage(content='', ..., tool_calls=[{'name': 'foo', 'args': {'bar': 'baz'}, 'id': 'call_1', 'type': 'tool_call'}])
如果我们再次调用模型,它将返回迭代器中的下一项:
model.invoke("hello, again!")
# AIMessage(content='bar', ...)
InMemorySaver 检查点器
要在测试期间启用持久化,你可以使用 InMemorySaver 检查点器。这允许你模拟多轮对话以测试依赖状态的行为:
from langgraph.checkpoint.memory import InMemorySaver
agent = create_agent(
model,
tools=[],
checkpointer=InMemorySaver()
)
# 第一次调用
agent.invoke(
{"messages": [HumanMessage(content="I live in Sydney, Australia")]},
config={"configurable": {"thread_id": "session-1"}}
)
# 第二次调用:第一条消息已持久化(悉尼位置),所以模型返回 GMT+10 时间
agent.invoke(
{"messages": [HumanMessage(content="What's my local time?")]},
config={"configurable": {"thread_id": "session-1"}}
)
后续步骤
了解如何使用真实模型提供商 API 测试你的智能体,请参阅集成测试。
连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。