Skip to content
CrewAI 文档版本 跟随上游 源快照 3a918014 更新于 2026-10-18

Patronus AI 评估

使用 Patronus AI 的全面评估平台监控和评估 CrewAI 智能体性能,针对 LLM 输出和智能体行为。

Patronus AI 评估

概述

Patronus AI 为 CrewAI 智能体提供全面的评估和监控能力,使你能够评估模型输出、智能体行为和整体系统性能。此集成允许你实现持续评估工作流,帮助在生产环境中保持质量和可靠性。

核心特性

  • 自动化评估:实时评估智能体输出和行为
  • 自定义标准:定义针对你的用例的特定评估标准
  • 性能监控:跟踪智能体随时间变化的性能指标
  • 质量保证:确保不同场景下输出质量一致
  • 安全与合规:监控潜在问题和策略违规

评估工具

Patronus 为不同用例提供三种主要评估工具:

  1. PatronusEvalTool:允许智能体为评估任务选择最合适的评估器和标准。
  2. PatronusPredefinedCriteriaEvalTool:使用用户指定的预定义评估器和标准。
  3. PatronusLocalEvaluatorTool:使用用户定义的自定义函数评估器。

安装

要使用这些工具,你需要安装 Patronus 包:

shell
uv add patronus

你还需要将 Patronus API 密钥设置为环境变量:

shell
export PATRONUS_API_KEY="your_patronus_api_key"

入门步骤

要有效使用 Patronus 评估工具,请遵循以下步骤:

  1. 安装 Patronus:使用上面的命令安装 Patronus 包。
  2. 设置 API 密钥:将你的 Patronus API 密钥设置为环境变量。
  3. 选择正确的工具:根据你的需求选择合适的 Patronus 评估工具。
  4. 配置工具:使用必要的参数配置该工具。

示例

使用 PatronusEvalTool

以下示例演示如何使用 PatronusEvalTool,它允许智能体选择最合适的评估器和标准:

python
from crewai import Agent, Task, Crew
from crewai_tools import PatronusEvalTool

# Initialize the tool
patronus_eval_tool = PatronusEvalTool()

# Define an agent that uses the tool
coding_agent = Agent(
    role="Coding Agent",
    goal="Generate high quality code and verify that the output is code",
    backstory="An experienced coder who can generate high quality python code.",
    tools=[patronus_eval_tool],
    verbose=True,
)

# Example task to generate and evaluate code
generate_code_task = Task(
    description="Create a simple program to generate the first N numbers in the Fibonacci sequence. Select the most appropriate evaluator and criteria for evaluating your output.",
    expected_output="Program that generates the first N numbers in the Fibonacci sequence.",
    agent=coding_agent,
)

# Create and run the crew
crew = Crew(agents=[coding_agent], tasks=[generate_code_task])
result = crew.kickoff()

使用 PatronusPredefinedCriteriaEvalTool

以下示例演示如何使用 PatronusPredefinedCriteriaEvalTool,它使用预定义评估器和标准:

python
from crewai import Agent, Task, Crew
from crewai_tools import PatronusPredefinedCriteriaEvalTool

# Initialize the tool with predefined criteria
patronus_eval_tool = PatronusPredefinedCriteriaEvalTool(
    evaluators=[{"evaluator": "judge", "criteria": "contains-code"}]
)

# Define an agent that uses the tool
coding_agent = Agent(
    role="Coding Agent",
    goal="Generate high quality code",
    backstory="An experienced coder who can generate high quality python code.",
    tools=[patronus_eval_tool],
    verbose=True,
)

# Example task to generate code
generate_code_task = Task(
    description="Create a simple program to generate the first N numbers in the Fibonacci sequence.",
    expected_output="Program that generates the first N numbers in the Fibonacci sequence.",
    agent=coding_agent,
)

# Create and run the crew
crew = Crew(agents=[coding_agent], tasks=[generate_code_task])
result = crew.kickoff()

使用 PatronusLocalEvaluatorTool

以下示例演示如何使用 PatronusLocalEvaluatorTool,它使用自定义函数评估器:

python
from crewai import Agent, Task, Crew
from crewai_tools import PatronusLocalEvaluatorTool
from patronus import Client, EvaluationResult
import random

# Initialize the Patronus client
client = Client()

# Register a custom evaluator
@client.register_local_evaluator("random_evaluator")
def random_evaluator(**kwargs):
    score = random.random()
    return EvaluationResult(
        score_raw=score,
        pass_=score >= 0.5,
        explanation="example explanation",
    )

# Initialize the tool with the custom evaluator
patronus_eval_tool = PatronusLocalEvaluatorTool(
    patronus_client=client,
    evaluator="random_evaluator",
    evaluated_model_gold_answer="example label",
)

# Define an agent that uses the tool
coding_agent = Agent(
    role="Coding Agent",
    goal="Generate high quality code",
    backstory="An experienced coder who can generate high quality python code.",
    tools=[patronus_eval_tool],
    verbose=True,
)

# Example task to generate code
generate_code_task = Task(
    description="Create a simple program to generate the first N numbers in the Fibonacci sequence.",
    expected_output="Program that generates the first N numbers in the Fibonacci sequence.",
    agent=coding_agent,
)

# Create and run the crew
crew = Crew(agents=[coding_agent], tasks=[generate_code_task])
result = crew.kickoff()

参数

PatronusEvalTool

PatronusEvalTool 在初始化时不需要任何参数。它会自动从 Patronus API 获取可用的评估器和标准。

PatronusPredefinedCriteriaEvalTool

PatronusPredefinedCriteriaEvalTool 在初始化时接受以下参数:

  • evaluators:必填。一个字典列表,包含要使用的评估器和标准。例如:[{"evaluator": "judge", "criteria": "contains-code"}]。

PatronusLocalEvaluatorTool

PatronusLocalEvaluatorTool 在初始化时接受以下参数:

  • patronus_client:必填。Patronus 客户端实例。
  • evaluator:可选。要使用的已注册本地评估器名称。默认值为空字符串。
  • evaluated_model_gold_answer:可选。用于评估的标准答案。默认值为空字符串。

用量

使用 Patronus 评估工具时,你提供模型输入、输出和上下文,工具会返回来自 Patronus API 的评估结果。

对于 PatronusEvalTool 和 PatronusPredefinedCriteriaEvalTool,调用工具时需要以下参数:

  • evaluated_model_input:以简单文本表示的智能体任务描述。
  • evaluated_model_output:智能体对任务的输出。
  • evaluated_model_retrieved_context:智能体的上下文。

对于 PatronusLocalEvaluatorTool,需要相同的参数,但评估器和标准答案在初始化时指定。

结论

Patronus 评估工具提供了一种强大的方式,使用 Patronus AI 平台评估和评分模型输入与输出。通过让智能体评估自身输出或其他智能体的输出,这些工具有助于提高 CrewAI 工作流的质量和可靠性。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。