跳转到内容

Patronus AI 评估

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

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

Patronus 提供三种主要评估工具,以覆盖不同用例:

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

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

Terminal window
uv add patronus

你还需要将 Patronus API key 设为环境变量:

Terminal window
export PATRONUS_API_KEY="your_patronus_api_key"

要有效使用 Patronus 评估工具,请按以下步骤操作:

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

下面的示例演示如何使用 PatronusEvalTool,它允许代理选择最合适的评估器和标准:

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,它使用预定义的评估器和标准:

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,它使用自定义函数评估器:

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 在初始化时不需要任何参数。它会自动从 Patronus API 获取可用的评估器和标准。

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

  • evaluators:必需。包含要使用的 evaluator 和 criteria 的字典列表。例如:[{"evaluator": "judge", "criteria": "contains-code"}]

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

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

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

对于 PatronusEvalToolPatronusPredefinedCriteriaEvalTool,调用工具时需要以下参数:

  • evaluated_model_input:代理任务描述的纯文本。
  • evaluated_model_output:代理对任务的输出。
  • evaluated_model_retrieved_context:代理的上下文。

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

Patronus 评估工具提供了一种强大的方式来使用 Patronus AI 平台评估和打分模型输入与输出。通过让代理评估自身输出或其他代理的输出,这些工具可以帮助提升 CrewAI 工作流的质量与可靠性。