文档索引
获取完整文档索引: https://docs.crewai.com.cn/llms.txt
在深入了解之前,请使用此文件来浏览所有可用页面。
Patronus AI 评估
Patronus AI 为 CrewAI 智能体提供全面的评估和监控功能,使您能够评估模型输出、智能体行为以及整体系统性能。此集成允许您实施持续评估工作流,从而帮助在生产环境中维持质量和可靠性。
主要特点
- 自动化评估:对智能体输出和行为进行实时评估
- 自定义标准:定义针对您使用场景的特定评估标准
- 性能监控:跟踪智能体随时间变化的性能指标
- 质量保证:确保不同场景下输出的一致性
- 安全与合规:监控潜在问题和政策违规情况
Patronus 针对不同的使用场景提供了三种主要的评估工具
- PatronusEvalTool:允许智能体根据评估任务选择最合适的评估器和标准。
- PatronusPredefinedCriteriaEvalTool:使用用户指定的预定义评估器和标准。
- PatronusLocalEvaluatorTool:使用用户定义的自定义函数评估器。
要使用这些工具,您需要安装 Patronus 包
您还需要将 Patronus API 密钥设置为环境变量
export PATRONUS_API_KEY="your_patronus_api_key"
开始步骤
要有效地使用 Patronus 评估工具,请遵循以下步骤
- 安装 Patronus:使用上述命令安装 Patronus 包。
- 设置 API 密钥:将您的 Patronus API 密钥设置为环境变量。
- 选择合适的工具:根据您的需求选择相应的 Patronus 评估工具。
- 配置工具:使用必要的参数配置工具。
以下示例展示了如何使用 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": "judge", "criteria": "contains-code"}]。
PatronusLocalEvaluatorTool 在初始化时接受以下参数
- patronus_client:必需。Patronus 客户端实例。
- evaluator:可选。要使用的已注册本地评估器的名称。默认为空字符串。
- evaluated_model_gold_answer:可选。用于评估的黄金答案(Gold Answer)。默认为空字符串。
使用 Patronus 评估工具时,您提供模型输入、输出和上下文,工具将返回来自 Patronus API 的评估结果。 对于 PatronusEvalTool 和 PatronusPredefinedCriteriaEvalTool,调用工具时需要以下参数:
- evaluated_model_input:以纯文本描述的智能体任务。
- evaluated_model_output:智能体完成任务后的输出。
- evaluated_model_retrieved_context:智能体的上下文。
对于 PatronusLocalEvaluatorTool,需要相同的参数,但评估器和黄金答案在初始化时指定。
Patronus 评估工具提供了一种强大的方式,通过 Patronus AI 平台对模型输入和输出进行评估和打分。通过支持智能体评估其自身或其他智能体的输出,这些工具可以帮助提升 CrewAI 工作流的质量和可靠性。