- 单元测试使用内存中的模拟在隔离环境中练习智能体的小型确定性部分,以便您可以快速且确定性地断言确切行为。
- 集成测试使用真实网络调用测试智能体,以确认组件协同工作、凭据和模式对齐以及延迟可接受。
集成测试
许多智能体行为只有在使用真实 LLM 时才会出现,例如智能体决定调用哪个工具、如何格式化响应或提示修改是否影响整个执行轨迹。LangChain 的agentevals 包提供了专门为使用实时模型测试智能体轨迹而设计的评估器。
AgentEvals 让您可以通过执行轨迹匹配或使用 LLM 裁判轻松评估智能体的轨迹(包括工具调用的确切消息序列):
轨迹匹配
为给定输入硬编码参考轨迹,并通过逐步比较验证运行。非常适合测试您知道预期行为的明确定义的工作流程。当您对应该调用哪些工具以及按什么顺序有具体期望时使用。这种方法是确定性的、快速的且成本效益高,因为它不需要额外的 LLM 调用。
LLM-as-judge
使用 LLM 来定性验证智能体的执行轨迹。“裁判” LLM 根据提示标准(可以包括参考轨迹)审查智能体的决策。更灵活,可以评估效率和适当性等细微方面,但需要 LLM 调用且确定性较低。当您想要评估智能体轨迹的整体质量和合理性而不需要严格的工具调用或排序要求时使用。
安装 AgentEvals
轨迹匹配评估器
AgentEvals 提供createTrajectoryMatchEvaluator 函数,将您的智能体轨迹与参考轨迹进行匹配。有四种模式可供选择:
| 模式 | 描述 | 用例 |
|---|---|---|
strict | 相同顺序的消息和工具调用的精确匹配 | 测试特定序列(例如,在授权之前进行策略查找) |
unordered | 允许以任何顺序进行相同的工具调用 | 在顺序不重要时验证信息检索 |
subset | 智能体仅调用参考中的工具(无额外工具) | 确保智能体不超过预期范围 |
superset | 智能体至少调用参考工具(允许额外工具) | 验证是否采取了所需的最小操作 |
严格匹配
严格匹配
strict 模式确保轨迹包含相同顺序的相同消息和相同的工具调用,尽管它允许消息内容存在差异。当您需要强制执行特定的操作序列时(例如,在授权操作之前需要策略查找),这很有用。Unordered match
Unordered match
The
unordered mode allows the same tool calls in any order, which is helpful when you want to verify that specific information was retrieved but don’t care about the sequence. For example, an agent might need to check both weather and events for a city, but the order doesn’t matter.Subset and superset match
Subset and superset match
The
superset and subset modes match partial trajectories. The superset mode verifies that the agent called at least the tools in the reference trajectory, allowing additional tool calls. The subset mode ensures the agent did not call any tools beyond those in the reference.You can also set the
toolArgsMatchMode property and/or toolArgsMatchOverrides to customize how the evaluator considers equality between tool calls in the actual trajectory vs. the reference. By default, only tool calls with the same arguments to the same tool are considered equal. Visit the repository for more details.LLM-as-Judge 评估器
You can also use an LLM to evaluate the agent’s execution path with thecreateTrajectoryLLMAsJudge function. Unlike the trajectory match evaluators, it doesn’t require a reference trajectory, but one can be provided if available.
Without reference trajectory
Without reference trajectory
With reference trajectory
With reference trajectory
If you have a reference trajectory, you can add an extra variable to your prompt and pass in the reference trajectory. Below, we use the prebuilt
TRAJECTORY_ACCURACY_PROMPT_WITH_REFERENCE prompt and configure the reference_outputs variable:For more configurability over how the LLM evaluates the trajectory, visit the repository.
LangSmith 集成
为了长期跟踪实验,您可以将评估器结果记录到 LangSmith,这是一个用于构建生产级 LLM 应用程序的平台,包括跟踪、评估和实验工具。 首先,通过设置所需的环境变量来设置 LangSmith:evaluate function.
Using vitest/jest integration
Using vitest/jest integration
Using the evaluate function
Using the evaluate function
Alternatively, you can create a dataset in LangSmith and use the Results will be automatically logged to LangSmith.
evaluate function: