跳转到主要内容

Patronus AI 评估

概述

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

主要特点

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

评估工具

Patronus 提供了三种主要的评估工具,适用于不同的用例
  1. PatronusEvalTool:允许智能体为评估任务选择最合适的评估器和标准。
  2. PatronusPredefinedCriteriaEvalTool:使用用户指定的预定义评估器和标准。
  3. PatronusLocalEvaluatorTool:使用用户定义的自定义函数评估器。

安装

要使用这些工具,您需要安装 Patronus 包
uv add patronus
您还需要将您的 Patronus API 密钥设置为环境变量
export PATRONUS_API_KEY="your_patronus_api_key"

开始步骤

要有效使用 Patronus 评估工具,请遵循以下步骤
  1. 安装 Patronus:使用上述命令安装 Patronus 包。
  2. 设置 API 密钥:将您的 Patronus API 密钥设置为环境变量。
  3. 选择合适的工具:根据您的需求选择合适的 Patronus 评估工具。
  4. 配置工具:使用必要的参数配置工具。

示例

使用 PatronusEvalTool

以下示例演示了如何使用 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

以下示例演示了如何使用 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

以下示例演示了如何使用 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

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

PatronusPredefinedCriteriaEvalTool

PatronusPredefinedCriteriaEvalTool 在初始化期间接受以下参数
  • evaluators:必需。包含要使用的评估器和标准的字典列表。例如:[{"evaluator": "judge", "criteria": "contains-code"}]

PatronusLocalEvaluatorTool

PatronusLocalEvaluatorTool 在初始化期间接受以下参数
  • patronus_client:必需。Patronus 客户端实例。
  • evaluator:可选。要使用的已注册本地评估器的名称。默认为空字符串。
  • evaluated_model_gold_answer:可选。用于评估的黄金标准答案。默认为空字符串。

用法

在使用 Patronus 评估工具时,您需要提供模型输入、输出和上下文,工具将返回来自 Patronus API 的评估结果。 对于 PatronusEvalToolPatronusPredefinedCriteriaEvalTool,调用工具时需要以下参数:
  • evaluated_model_input:智能体任务的纯文本描述。
  • evaluated_model_output:智能体完成任务的输出。
  • evaluated_model_retrieved_context:智能体的上下文。
对于 PatronusLocalEvaluatorTool,需要相同的参数,但在初始化期间指定评估器和黄金标准答案。

结论

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