Skip to content

用户模拟

Supported in ADKPython v1.18.0

在评估对话式智能体时,使用固定的用户提示集并不总是可行的,因为对话可能会以意想不到的方式进行。例如,如果智能体需要用户提供两个值来执行任务,它可能会一次请求一个值或一次请求两个值。为了解决这个问题,ADK 可以使用生成式 AI 模型动态生成用户提示。

要使用此功能,你必须指定一个 ConversationScenario,它规定了用户与智能体对话的目标。 下面展示了 hello_world 智能体的一个示例对话场景:

{
  "starting_prompt": "What can you do for me?", // 用户的起始提示
  "conversation_plan": "Ask the agent to roll a 20-sided die. After you get the result, ask the agent to check if it is prime." // 对话计划
}

对话场景中的 starting_prompt 指定了一个固定的初始提示,用户应该使用它来开始与智能体的对话。为后续与智能体的交互指定此类固定提示是不切实际的,因为智能体可能会以不同的方式响应。相反,conversation_plan 提供了关于与智能体进行其余对话的指导方针。LLM 使用此对话计划以及对话历史记录,动态生成用户提示,直到它判断对话完成。

在 Colab 中尝试

模拟用户对话以动态评估 ADK 智能体的交互式笔记本中亲自测试整个工作流。 你将定义一个对话场景,进行“演练”以检查对话,然后执行全面评估以对智能体的响应进行评分。

示例:使用对话场景评估 hello_world 智能体

要将包含对话场景的评估案例添加到新的或现有的 EvalSet 中,你需要首先创建要测试智能体的对话场景列表。

尝试将以下内容保存到 contributing/samples/hello_world/conversation_scenarios.json

{
  "scenarios": [
    {
      "starting_prompt": "What can you do for me?", // 用户的起始提示
      "conversation_plan": "Ask the agent to roll a 20-sided die. After you get the result, ask the agent to check if it is prime." // 对话计划
    },
    {
      "starting_prompt": "Hi, I'm running a tabletop RPG in which prime numbers are bad!", // 用户的起始提示
      "conversation_plan": "Say that you don't care about the value; you just want the agent to tell you if a roll is good or bad. Once the agent agrees, ask it to roll a 6-sided die. Finally, ask the agent to do the same with 2 20-sided dice." // 对话计划
    }
  ]
}

你还需要一个包含评估期间使用的信息的会话输入文件。 尝试将以下内容保存到 contributing/samples/hello_world/session_input.json

{
  "app_name": "hello_world", // 应用程序名称
  "user_id": "user" // 用户 ID
}

然后,你可以将对话场景添加到 EvalSet 中:

# (可选) 创建一个新的 EvalSet
adk eval_set create \
  contributing/samples/hello_world \
  eval_set_with_scenarios

# 将对话场景作为新的评估案例添加到 EvalSet 中
adk eval_set add_eval_case \
  contributing/samples/hello_world \
  eval_set_with_scenarios \
  --scenarios_file contributing/samples/hello_world/conversation_scenarios.json \
  --session_input_file contributing/samples/hello_world/session_input.json

默认情况下,ADK 使用需要指定智能体预期响应的指标运行评估。 由于动态对话场景并非如此,我们将使用一个 EvalConfig 以及一些替代支持的指标。

尝试将以下内容保存到 contributing/samples/hello_world/eval_config.json

{
  "criteria": {
    "hallucinations_v1": {
      "threshold": 0.5, // 阈值
      "evaluate_intermediate_nl_responses": true // 评估中间自然语言响应
    },
    "safety_v1": {
      "threshold": 0.8 // 阈值
    }
  }
}

最后,你可以使用 adk eval 命令运行评估:

adk eval \
    contributing/samples/hello_world \
    --config_file_path contributing/samples/hello_world/eval_config.json \
    eval_set_with_scenarios \
    --print_detailed_results

用户模拟器配置

你可以覆盖默认的用户模拟器配置,以更改模型、内部模型行为以及用户与智能体交互的最大次数。 下面的 EvalConfig 显示了默认的用户模拟器配置:

{
  "criteria": {
    # same as before // 与之前相同
  },
  "user_simulator_config": {
    "model": "gemini-2.5-flash", // 用户模拟器使用的模型
    "model_configuration": {
      "thinking_config": {
        "include_thoughts": true, // 是否包含思考过程
        "thinking_budget": 10240 // 思考预算
      }
    },
    "max_allowed_invocations": 20 // 允许的最大调用次数
  }
}
  • model: 支持用户模拟器的模型。
  • model_configuration: 一个 GenerateContentConfig,用于控制模型行为。
  • max_allowed_invocations: 在对话被强制终止之前允许的最大用户与智能体交互次数。此值应设置为大于 EvalSet 中最长的合理用户与智能体交互次数。