你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。

评估 AI 智能体(预览版)(经典版)

仅适用于:Foundry (经典版)门户。 本文不适用于新的 Foundry 门户。 详细了解新门户。

注意

本文中的链接可能会打开新 Microsoft Foundry 文档中的内容,而不是你现在正在查看的 Foundry (经典)文档。

重要

本文中标记为预览的项目目前为预览版。 此预览版在没有服务级别协议的情况下提供,Microsoft不建议将其用于生产工作负荷。 某些功能可能不受支持,或者可能具有受限功能。 有关详细信息,请参阅 Microsoft Azure 预览版的使用条款。

AI 代理是强大的生产力助手,可为业务需求创建工作流。 但是,可观测性具有挑战性,因为它们的复杂交互模式。 本文介绍如何使用内置计算器评估 Microsoft Foundry 代理或其他代理。

为了生成生产就绪的代理应用程序并确保可观测性和透明度,开发人员不仅需要工具来评估代理工作流的最终输出,还需要评估工作流的质量和效率。

类似于用户查询“明天天气”的事件会触发代理工作流。 为了生成最终响应,工作流包括通过用户意向、调用工具以及使用检索扩充生成进行推理。

注意

用于评估和 Foundry 门户的 Microsoft Foundry SDK 以公共预览版提供,但 API 已正式发布用于模型和数据集评估(代理评估仍为公共预览版)。 本文中的Azure AI 评估 SDK 和评估器(预览版)目前在任何地方都以公共预览版提供。

在此过程中,评估工作流的每个步骤以及最终输出的质量和安全性至关重要。 这些评估方面已制定为代理的以下评估器:

  • 意向解析:度量代理是否正确标识用户的意向。
  • 工具调用准确性:度量代理是否对用户的请求进行了正确的函数工具调用。
  • 任务符合性:根据代理的系统消息和先前步骤衡量代理的最终响应是否遵守其分配的任务。

使用全面的内置评估器套件评估代理工作流的其他质量和安全方面。 一般情况下,代理会发送代理消息。 将代理消息转换为评估人员的合适评估数据可能很困难。 如果通过 Foundry Agent 服务构建您的代理,您可以利用我们的转换器支持对其进行无缝评估。 如果在 Foundry 智能体服务外部生成智能体,仍可以通过解析智能体消息为所需的数据格式,根据智能体工作流的需要使用评估器。 请参阅 评估其他代理的示例。

开始

安装 Azure AI 评估 SDK 中的评估程序包:

pip install azure-ai-evaluation

评估 Microsoft Foundry 智能体

如果您使用 Foundry 代理服务,可以通过我们的转换器支持,无缝评估 Microsoft Foundry 代理和 语义内核 代理。 以下评估器支持转换器返回的评估数据:IntentResolution、ToolCallAccuracy、TaskAdherence、Relevance和Groundedness。

注意

如果要生成输出其他架构的其他代理,请将它们转换为常规 OpenAI 样式 的代理消息架构 ,并使用前面的计算器。 更普遍地说,如果可以将代理消息分析为 所需的数据格式,还可以使用我们的所有评估程序。

AI 辅助评估器的模型支持

AzureOpenAI 和 OpenAI 推理模型和非推理模型根据评估程序支持 LLM 判断:

评估器 推理模型作为评判者(示例:来自 Azure OpenAI / OpenAI 的 o系列模型) 非推理型模型充当评审(例如:gpt-4.1、gpt-4o 等) 启用
IntentResolution、TaskAdherence、ToolCallAccuracy、ResponseCompleteness、Coherence、Fluency、Similarity、Groundedness、Retrieval、Relevance 支持 支持 初始化计算器时设置其他参数is_reasoning_model=True
其他评估者 不支持 支持 --

对于需要精细推理的复杂评估,使用强大的推理模型,例如 4.1-mini,以平衡推理性能和成本效益。

工具调用评估支持

ToolCallAccuracyEvaluator支持在 Microsoft Foundry 代理中评估以下工具:

  • 文件搜索
  • Foundry IQ (Azure AI 搜索)
  • 必应上下文关联
  • 必应自定义搜索
  • SharePoint基础掌握
  • 代码解释器
  • Fabric数据代理
  • OpenAPI
  • 函数工具(用户定义的工具)

但是,如果在代理运行中使用不支持的工具,评估器会输出一个“通过”和一个理由,说明不支持对所调用工具进行评估,以便于更容易地筛选出这些情况。 若要启用评估,请将不支持的工具包装为用户定义的工具。

此示例演示如何生成和评估 Microsoft Foundry 代理。 除了评估之外,Foundry 代理服务还需要pip install azure-ai-projects azure-identity、Foundry 项目的连接字符串,以及支持的模型。

创建代理线程并运行

代理可以使用工具。 下面是为代理创建自定义工具的示例(使用模拟天气函数作为示例):

from azure.ai.projects.models import FunctionTool, ToolSet
from typing import Set, Callable, Any
import json

# Define a custom Python function.
def fetch_weather(location: str) -> str:
    """
    Fetches the weather information for the specified location.

    :param location (str): The location to fetch weather for.
    :return: Weather information as a JSON string.
    :rtype: str
    """
    # In a real-world scenario, you'd integrate with a weather API.
    # In the following code snippet, we mock the response.
    mock_weather_data = {"Seattle": "Sunny, 25°C", "London": "Cloudy, 18°C", "Tokyo": "Rainy, 22°C"}
    weather = mock_weather_data.get(location, "Weather data not available for this location.")
    weather_json = json.dumps({"weather": weather})
    return weather_json

user_functions: Set[Callable[..., Any]] = {
    fetch_weather,
}

# Add tools that the agent will use. 
functions = FunctionTool(user_functions)

toolset = ToolSet()
toolset.add(functions)

AGENT_NAME = "Seattle Tourist Assistant"

如果使用 Foundry(非中心)项目,请使用工具集创建代理,如下所示:

注意

如果使用基于 Foundry Hub 的项目(仅支持较低版本的 azure-ai-projects<1.0.0b10 和 azure-ai-agents<1.0.0b10),请迁移到 最新的 Foundry 代理服务 SDK Python 客户端库。 有关详细信息,请参阅 为记录批处理评估结果而设置的 Foundry 项目。

import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from dotenv import load_dotenv

load_dotenv()

# Create an Azure AI Client from an endpoint, copied from your Foundry project.
# You need to login to Azure subscription via Azure CLI and set the environment variables
# Foundry project endpoint, example: AZURE_AI_PROJECT=https://your-account.services.ai.azure.com/api/projects/your-project
project_endpoint = os.environ["AZURE_AI_PROJECT"]  # Ensure the PROJECT_ENDPOINT environment variable is set

# Create an AIProjectClient instance
project_client = AIProjectClient(
    endpoint=project_endpoint,
    credential=DefaultAzureCredential(),  # Use Azure Default Credential for authentication
)

# Create an agent with the toolset 
agent = project_client.agents.create_agent(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],  # Model deployment name
    name="my-agent",  # Name of the agent
    instructions="You are a helpful agent",  # Instructions for the agent
    toolset=toolset
)
print(f"Created agent, ID: {agent.id}")

# Create a thread for communication
thread = project_client.agents.threads.create()
print(f"Created thread, ID: {thread.id}")

# Add a message to the thread
message = project_client.agents.messages.create(
    thread_id=thread.id,
    role="user",  # Role of the message sender
    content="What is the weather in Seattle today?",  # Message content
)
print(f"Created message, ID: {message.id}")

# Create and process an agent run
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
print(f"Run finished with status: {run.status}")

# Check if the run failed
if run.status == "failed":
    print(f"Run failed: {run.last_error}")

# Fetch and log all messages
messages = project_client.agents.messages.list(thread_id=thread.id)
for message in messages:
    print(f"Role: {message.role}, Content: {message.content}")
    

评估单个代理运行

创建代理运行后,使用转换器将 Microsoft Foundry 代理线程数据转换为评估程序可以理解的所需评估数据。

import json, os
from azure.ai.evaluation import AIAgentConverter, IntentResolutionEvaluator

# Initialize the converter for Microsoft Foundry agents.
converter = AIAgentConverter(project_client)

# Specify the thread and run ID.
thread_id = thread.id
run_id = run.id

converted_data = converter.convert(thread_id, run_id)

就是这样! converted_data 包含这些评估器所需的所有输入。 无需读取每个计算器的输入要求或执行任何工作来分析输入。 选择您的评估器,并在此单次运行中调用评估器。 根据评估程序的判断,我们支持 Azure OpenAI 或 OpenAI 推理模型:

评估器 推理模型作为评判者(示例:来自 Azure OpenAI / OpenAI 的 o系列模型) 非推理型模型充当评审(例如:gpt-4.1、gpt-4o 等) 启用
所有质量评估程序,GroundednessProEvaluator 除外 支持 支持 在初始化计算器时设置其他参数is_reasoning_model=True
GroundednessProEvaluator 用户不需要支持模型 用户不需要支持模型 --

对于需要优化计算推理的复杂任务,请使用强大的推理模型,例如 o3-mini 或之后发布的 o 系列微型模型,以平衡推理性能和成本效益。

在quality_evaluators和safety_evaluators中设置质量和安全评估者列表。 在评估多个代理运行或线程时将其作为参考。

# This is specific to agentic workflows.
from azure.ai.evaluation import IntentResolutionEvaluator, TaskAdherenceEvaluator, ToolCallAccuracyEvaluator 
# Other quality, risk, and safety metrics:
from azure.ai.evaluation import RelevanceEvaluator, CoherenceEvaluator, CodeVulnerabilityEvaluator, ContentSafetyEvaluator, IndirectAttackEvaluator, FluencyEvaluator
from azure.identity import DefaultAzureCredential

import os
from dotenv import load_dotenv
load_dotenv()

model_config = {
    "azure_deployment": os.getenv("AZURE_DEPLOYMENT_NAME"),
    "api_key": os.getenv("AZURE_OPENAI_API_KEY"),
    "azure_endpoint": os.getenv("AZURE_OPENAI_ENDPOINT"),
    "api_version": os.getenv("AZURE_API_VERSION"),
}

# example config for a reasoning model
reasoning_model_config = {
    "azure_deployment": "o4-mini",
    "api_key": os.getenv("AZURE_OPENAI_API_KEY"),
    "azure_endpoint": os.getenv("AZURE_OPENAI_ENDPOINT"),
    "api_version": os.getenv("AZURE_API_VERSION"),
}

# Evaluators you might want to use with reasoning models 
quality_evaluators = {evaluator.__name__: evaluator(model_config=reasoning_model_config, is_reasoning_model=True) for evaluator in [IntentResolutionEvaluator, TaskAdherenceEvaluator, ToolCallAccuracyEvaluator]}

# Other evaluators you might NOT want to use with reasoning models 
quality_evaluators.update({ evaluator.__name__: evaluator(model_config=model_config) for evaluator in [CoherenceEvaluator, FluencyEvaluator, RelevanceEvaluator]})

## Using Foundry (non-Hub) project endpoint, example: AZURE_AI_PROJECT=https://your-account.services.ai.azure.com/api/projects/your-project
azure_ai_project = os.environ.get("AZURE_AI_PROJECT")

safety_evaluators = {evaluator.__name__: evaluator(azure_ai_project=azure_ai_project, credential=DefaultAzureCredential()) for evaluator in [ContentSafetyEvaluator, IndirectAttackEvaluator, CodeVulnerabilityEvaluator]}

# Reference the quality and safety evaluator list above.
quality_and_safety_evaluators = {**quality_evaluators, **safety_evaluators}

for name, evaluator in quality_and_safety_evaluators.items():
    result = evaluator(**converted_data)
    print(name)
    print(json.dumps(result, indent=4)) 

输出格式

AI 辅助的质量评估器返回查询和响应对的结果。 结果是一个字典,其中包括:

  • {metric_name}:按 Likert 刻度(整数 1 到 5)或介于 0 和 1 之间的浮点数提供数值分数。
  • {metric_name}_label:如果指标自然输出二进制分数,则提供二进制标签。
  • {metric_name}_reason:说明为何为每个数据点提供特定分数或标签。
  • details:包含有关单个代理运行质量的调试信息的可选输出。

为了提高清晰度,所有计算器都接受二进制阈值(除非其输出已是二进制),并生成两个新键。 对于二进制化阈值,将设置默认值,用户可以替代该阈值。 这两个新密钥包括:

  • {metric_name}_result:基于二进制化阈值的“pass”或“fail”字符串。
  • {metric_name}_threshold:默认或用户设置的数字二进制化阈值。

请查看以下示例输出以了解一些评估器:

{
    "intent_resolution": 5.0, # likert scale: 1-5 integer 
    "intent_resolution_threshold": 3,
    "intent_resolution_result": "pass", # pass because 5 > 3 the threshold
    "intent_resolution_reason": "The assistant correctly understood the user's request to fetch the weather in Seattle. It used the appropriate tool to get the weather information and provided a clear and accurate response with the current weather conditions in Seattle. The response fully resolves the user's query with all necessary information."
}
{
    "task_adherence": 5.0, # likert scale: 1-5 integer 
    "task_adherence_threshold": 3,
    "task_adherence_result": "pass", # pass because 5 > 3 the threshold
    "task_adherence_reason": "The response accurately follows the instructions, fetches the correct weather information, and relays it back to the user without any errors or omissions."
}
{
    "tool_call_accuracy": 5,  # a score between 1-5, higher is better
    "tool_call_accuracy_threshold": 3,
    "tool_call_accuracy_result": "pass", # pass because 5 > 3 the threshold
    "details": { ... } # helpful details for debugging the tool calls made by the agent
}

评估多个代理运行或线程

若要评估多个代理运行或线程,请使用批处理 evaluate() API 进行异步评估。 首先,使用转换器支持将代理线程数据转换为文件:

import json
from azure.ai.evaluation import AIAgentConverter

# Initialize the converter.
converter = AIAgentConverter(project_client)

# Specify a file path to save the agent output (evaluation input data) to.
filename = os.path.join(os.getcwd(), "evaluation_input_data.jsonl")

evaluation_data = converter.prepare_evaluation_data(thread_ids=thread_id, filename=filename) 

print(f"Evaluation data saved to {filename}")

通过用一行代码准备评估数据,可以选择评估员来评估代理质量并提交批量评估任务。 在以下示例中,请引用 “评估单个代理执行quality_and_safety_evaluators”部分中相同的质量和安全评估者列表:

import os
from dotenv import load_dotenv
load_dotenv()

# Batch evaluation API (local):
from azure.ai.evaluation import evaluate

response = evaluate(
    data=filename,
    evaluation_name="agent demo - batch run",
    evaluators=quality_and_safety_evaluators,
    # optionally, log your results to your Foundry project for rich visualization 
    azure_ai_project=os.environ.get("AZURE_AI_PROJECT"),  # example: https://your-account.services.ai.azure.com/api/projects/your-project
)
# Inspect the average scores at a high level.
print(response["metrics"])
# Use the URL to inspect the results on the UI.
print(f'Foundry URL: {response.get("studio_url")}')

选择 URL 后,会重定向到 Foundry。 在 Foundry 项目中查看评估结果并调试应用程序。 使用原因字段和通过/失败结果来评估应用程序的质量和安全性能。 可以运行和比较多个运行以测试回归或改进。

使用 Azure AI 评估 SDK 客户端库,借助转换器功能评估 Microsoft Foundry 代理,增强对代理工作流的可观测性和透明度。

评估其他代理

如果使用代理服务外部的代理,仍可以通过为所选评估程序准备正确的数据来评估它们。

代理通常发送消息以与用户或其他代理交互。 内置评估器根据query,接受简单的数据类型,如response、ground_truth和中的字符串。 但是,由于代理和框架差异的复杂交互模式,从代理消息中提取这些简单数据类型可能会很困难。 例如,单个用户查询可能会触发一长串代理消息,通常会调用多个工具。

如以下示例所示,可以为以下内置计算器启用代理消息支持,以评估代理工作流的这些方面。 在评估代理时,这些计算器可能会采用 tool_calls 或 tool_definitions 作为代理唯一的参数。

评估者 query response tool_calls tool_definitions
IntentResolutionEvaluator 必填: Union[str, list[Message]] 必填: Union[str, list[Message]] 不适用 可选:list[ToolCall]
ToolCallAccuracyEvaluator 必填: Union[str, list[Message]] 可选:Union[str, list[Message]] 可选:Union[dict, list[ToolCall]] 必填: list[ToolDefinition]
TaskAdherenceEvaluator 必填: Union[str, list[Message]] 必填: Union[str, list[Message]] 不适用 可选:list[ToolCall]
GroundednessEvaluator 必填: Union[str, list[Message]] 必填: Union[str, list[Message]] 不适用 必填: list[ToolCall]
  • Message: dict 描述代理与用户的交互的 OpenAI 样式消息,其中 query 必须包含系统消息作为第一条消息。
  • ToolCall: dict 指定在代理与用户交互期间调用的工具调用。
  • ToolDefinition:dict,用于描述代理可用的工具。

对于 ToolCallAccuracyEvaluator,必须提供 response 或 tool_calls。

GroundednessEvaluator 要求 tool_definitions 评估代理响应相对于代理接收到的工具输出的有据可依性。

以下示例显示了两种数据格式:简单的代理数据和代理消息。 但是,由于这些计算器的独特要求,请参阅 示例笔记本,其中说明了每个计算器的可能输入路径。

所有内置的AI 辅助质量评估器都会为每个输入输出通过或失败的结果。

简单代理数据

在简单的代理数据格式中,query 和 response 是简单的Python字符串。 例如:

import os
import json
from azure.ai.evaluation import AzureOpenAIModelConfiguration
from azure.identity import DefaultAzureCredential
from azure.ai.evaluation import IntentResolutionEvaluator, ResponseCompletenessEvaluator
  
model_config = AzureOpenAIModelConfiguration(
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    api_version=os.environ["AZURE_OPENAI_API_VERSION"],
    azure_deployment=os.environ["MODEL_DEPLOYMENT_NAME"],
)
 
intent_resolution_evaluator = IntentResolutionEvaluator(model_config)

# Evaluate the query and response as strings.
# The following is a positive example. Intent is identified and understood and the response correctly resolves user intent.
result = intent_resolution_evaluator(
    query="What are the opening hours of the Eiffel Tower?",
    response="Opening hours of the Eiffel Tower are 9:00 AM to 11:00 PM.",
)
print(json.dumps(result, indent=4))
 

请参阅以下输出(有关详细信息,请参阅 输出格式 ):

{
    "intent_resolution": 5.0,
    "intent_resolution_result": "pass",
    "intent_resolution_threshold": 3,
    "intent_resolution_reason": "The response provides the opening hours of the Eiffel Tower, which directly addresses the user's query. The information is clear, accurate, and complete, fully resolving the user's intent.",
}

代理工具调用和定义

关于 tool_calls,请参阅以下 tool_definitions 和 ToolCallAccuracyEvaluator 示例:

import json 

query = "How is the weather in Seattle?"
tool_calls = [{
                    "type": "tool_call",
                    "tool_call_id": "call_CUdbkBfvVBla2YP3p24uhElJ",
                    "name": "fetch_weather",
                    "arguments": {
                        "location": "Seattle"
                    }
            },
            {
                    "type": "tool_call",
                    "tool_call_id": "call_CUdbkBfvVBla2YP3p24uhElJ",
                    "name": "fetch_weather",
                    "arguments": {
                        "location": "London"
                    }
            }]

tool_definitions = [{
                    "name": "fetch_weather",
                    "description": "Fetches the weather information for the specified location.",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "location": {
                                "type": "string",
                                "description": "The location to fetch weather for."
                            }
                        }
                    }
                }]

from azure.ai.evaluation import ToolCallAccuracyEvaluator

tool_call_accuracy = ToolCallAccuracyEvaluator(model_config) # reuse the config defined above
response = tool_call_accuracy(query=query, tool_calls=tool_calls, tool_definitions=tool_definitions)
print(json.dumps(response, indent=4))

请参阅以下输出(有关详细信息,请参阅 输出格式 ):

{
    "tool_call_accuracy": 3,  # a score between 1-5, higher is better
    "tool_call_accuracy_result": "fail",
    "tool_call_accuracy_threshold": 4,
    "details": { ... } # helpful details for debugging the tool calls made by the agent
}

代理消息架构

在代理消息格式中,query 和 response 是 OpenAI 样式消息的列表。 具体而言,query携带导致用户最后一次查询的所有过去代理与用户的交互记录,并且系统消息(代理)需要位于列表顶部。 response 包含来自代理的最后一条消息,以响应最后一个用户查询。

评估器所需的输入格式是一个Python消息列表,如下所示:

[
  {
    "role": "system" | "user" | "assistant" | "tool",
    "createdAt": "ISO 8601 timestamp",     // Optional for 'system'
    "run_id": "string",                    // Optional, only for assistant/tool in tool call context
    "tool_call_id": "string",              // Optional, only for tool/tool_result
    "name": "string",                      // Present if it's a tool call
    "arguments": { ... },                  // Parameters passed to the tool (if tool call)
    "content": [
      {
        "type": "text" | "tool_call" | "tool_result",
        "text": "string",                  // if type == text
        "tool_call_id": "string",         // if type == tool_call
        "name": "string",                 // tool name if type == tool_call
        "arguments": { ... },             // tool args if type == tool_call
        "tool_result": { ... }            // result if type == tool_result
      }
    ]
  }
]

示例查询和响应对象:

query = [
    {
        "role": "system",
        "content": "You are an AI assistant interacting with Azure Maps services to serve user requests."
    },
    {
        "createdAt": "2025-04-25T23:55:43Z",
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "Find the address for coordinates 41.8781,-87.6298."
            }
        ]
    },
    {
        "createdAt": "2025-04-25T23:55:45Z",
        "run_id": "run_DGE8RWPS8A9SmfCg61waRx9u",
        "role": "assistant",
        "content": [
            {
                "type": "tool_call",
                "tool_call_id": "call_nqNyhOFRw4FmF50jaCCq2rDa",
                "name": "azure_maps_reverse_address_search",
                "arguments": {
                    "lat": "41.8781",
                    "lon": "-87.6298"
                }
            }
        ]
    },
    {
        "createdAt": "2025-04-25T23:55:47Z",
        "run_id": "run_DGE8RWPS8A9SmfCg61waRx9u",
        "tool_call_id": "call_nqNyhOFRw4FmF50jaCCq2rDa",
        "role": "tool",
        "content": [
            {
                "type": "tool_result",
                "tool_result": {
                    "address": "300 South Federal Street, Chicago, IL 60604",
                    "position": {
                        "lat": "41.8781",
                        "lon": "-87.6298"
                    }
                }
            }
        ]
    },
    {
        "createdAt": "2025-04-25T23:55:48Z",
        "run_id": "run_DGE8RWPS8A9SmfCg61waRx9u",
        "role": "assistant",
        "content": [
            {
                "type": "text",
                "text": "The address for the coordinates 41.8781, -87.6298 is 300 South Federal Street, Chicago, IL 60604."
            }
        ]
    },
    {
        "createdAt": "2025-04-25T23:55:50Z",
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "What timezone corresponds to 41.8781,-87.6298?"
            }
        ]
    },
]

response = [
    {
        "createdAt": "2025-04-25T23:55:52Z",
        "run_id": "run_DmnhUGqYd1vCBolcjjODVitB",
        "role": "assistant",
        "content": [
            {
                "type": "tool_call",
                "tool_call_id": "call_qi2ug31JqzDuLy7zF5uiMbGU",
                "name": "azure_maps_timezone",
                "arguments": {
                    "lat": 41.878100000000003,
                    "lon": -87.629800000000003
                }
            }
        ]
    },
    {
        "createdAt": "2025-04-25T23:55:54Z",
        "run_id": "run_DmnhUGqYd1vCBolcjjODVitB",
        "tool_call_id": "call_qi2ug31JqzDuLy7zF5uiMbGU",
        "role": "tool",
        "content": [
            {
                "type": "tool_result",
                "tool_result": {
                    "ianaId": "America/Chicago",
                    "utcOffset": None,
                    "abbreviation": None,
                    "isDaylightSavingTime": None
                }
            }
        ]
    },
    {
        "createdAt": "2025-04-25T23:55:55Z",
        "run_id": "run_DmnhUGqYd1vCBolcjjODVitB",
        "role": "assistant",
        "content": [
            {
                "type": "text",
                "text": "The timezone for the coordinates 41.8781, -87.6298 is America/Chicago."
            }
        ]
    }
]

注意

如果查询(到当前为止的会话历史记录)或代理响应(对查询的回应)未采用预期格式,则评估器会显示警告。

请参阅有关如何使用 ToolCallAccuracyEvaluator 评估代理消息的示例。

import json

# The user asked a question.
query = [
    {
        "role": "system",
        "content": "You are a friendly and helpful customer service agent."
    },
    # Past interactions are omitted. 
    # ...
    {
        "createdAt": "2025-03-14T06:14:20Z",
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "Hi, I need help with the last 2 orders on my account #888. Could you please update me on their status?"
            }
        ]
    }
]
# The agent emits multiple messages to fulfill the request.
response = [
    {
        "createdAt": "2025-03-14T06:14:30Z",
        "run_id": "0",
        "role": "assistant",
        "content": [
            {
                "type": "text",
                "text": "Hello! Let me quickly look up your account details."
            }
        ]
    },
    {
        "createdAt": "2025-03-14T06:14:35Z",
        "run_id": "0",
        "role": "assistant",
        "content": [
            {
                "type": "tool_call",
                "tool_call_id": "tool_call_20250310_001",
                "name": "get_orders",
                "arguments": {
                    "account_number": "888"
                }
            }
        ]
    },
    # Many more messages are omitted. 
    # ...
    # Here is the agent's final response:
    {
        "createdAt": "2025-03-14T06:15:05Z",
        "run_id": "0",
        "role": "assistant",
        "content": [
            {
                "type": "text",
                "text": "The order with ID 123 has been shipped and is expected to be delivered on March 15, 2025. However, the order with ID 124 is delayed and should now arrive by March 20, 2025. Is there anything else I can help you with?"
            }
        ]
    }
]

# An example of tool definitions available to the agent:
tool_definitions = [
    {
        "name": "get_orders",
        "description": "Get the list of orders for a given account number.",
        "parameters": {
            "type": "object",
            "properties": {
                "account_number": {
                    "type": "string",
                    "description": "The account number to get the orders for."
                }
            }
        }
    },
    # Other tool definitions are omitted. 
    # ...
]

result = tool_call_accuracy(
    query=query,
    response=response,
    tool_definitions=tool_definitions 
)
print(json.dumps(result, indent=4))

请参阅以下输出(有关详细信息,请参阅 输出格式 ):

{
    "tool_call_accuracy": 2,  # a score between 1-5, higher is better
    "tool_call_accuracy_result": "fail",
    "tool_call_accuracy_threshold": 3,
    "details": { ... } # helpful details for debugging the tool calls made by the agent
}

此评估架构有助于分析代理服务外部的代理数据,使内置评估程序能够支持代理工作流中的可观测性。

示例笔记本

针对以下每个评估者尝试一个示例: