跳转到内容

用户模拟(User Simulation)

在 ADK 中得到支持。Python v1.18.0

在评估对话式智能助手时,使用固定的用户提示语并不总是可行的,因为对话的进程可能会以意想不到的方式发展。例如,如果智能助手需要用户提供两个值才能完成任务,它可能会分别请求这两个值,或者一次性请求这两个值。为了解决这个问题,ADK 可以利用生成式人工智能模型动态生成用户提示语。

要使用此功能,您必须指定一个……(请根据实际情况填写具体内容)。 ConversationScenario 这决定了用户在与智能代理(agent)对话时的目标。您还可以指定用户应遵循的“用户角色”(user persona)。

A ConversationScenario 由以下组件组成:

  • starting_prompt:一个固定的初始提示语,用户应使用该提示语与代理开始对话。
  • conversation_plan:这是一份关于用户必须实现的目标的高层指导方针。
  • user_persona:对用户特征的描述,例如其技术专长或语言表达风格。

一个用于……的示例对话场景: 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."
}
大型语言模型(LLM)使用了…… conversation_plan这些数据(包括对话记录)会被用于动态生成用户提示(user prompts)。

您还可以指定一个预先构建好的(pre-built)选项。 user_persona 按照以下方式:

{
  "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.",
  "user_persona": "NOVICE"
}

虽然对话计划规定了需要完成的具体任务,但模型的“人格特征”(即其行为模式或表达方式)决定了模型应如何提出问题,以及如何对代理的回答作出反应。

用户角色(User Personas)

在 ADK 中得到支持。Python v1.26.0

用户角色(User Persona)是指模拟用户在对话过程中所扮演的角色。该角色由一组特定的属性或行为特征定义而成。 行为(Behaviors) 这些因素决定了用户与代理(agent)的交互方式,例如用户的沟通风格、提供信息的方式,以及对错误的反应方式。

A UserPersona 该字段包含以下内容:

  • id:该角色的唯一标识符。
  • description:对用户身份的总体描述,以及用户如何与代理(agent)进行交互。
  • behaviors:一个列表 UserBehavior 这些对象定义了特定的特征(attributes)。

每个 UserBehavior 包括:

  • name该行为的名称。
  • description:对预期行为的总结。
  • behavior_instructions:向模拟用户(即大型语言模型,LLM)提供的具体指令,告诉它们应该如何行动。
  • violation_rubrics:评估人员使用该工具来判断用户是否遵循了某种行为规范。如果…… 任何;任何一个 这些评分标准中的……(of these rubrics are……) 满意的评估者应确定该行为的具体表现(即该行为的具体含义或结果)。 不;并非 随后发生了……(“followed”表示某事在之前的事情之后发生)。

预先构建的人物角色模型(Personas)

ADK 提供了一组预先构建的角色模型(personas),这些角色模型包含了常见的行为特征。下表总结了每个角色模型的具体行为特征:

行为(Behavior) EXPERT persona 新手(NOVICE) persona EVALUATOR persona
高级版(Advanced) 注重细节(主动提供相关信息) 目标导向型(需要进一步询问具体细节) 以细节为导向(即注重细节的处理和展示)
答案 仅接受相关问题。 回答所有问题。 仅接受相关问题。
更正代理信息中的错误(或:修正代理数据中的不准确之处) 是的 没有。 没有。
排查代理程序中的错误 一旦 从不 从不
Tone 专业版(Professional) Conversational(对话式的) Conversational(对话式的)

示例:评估…… hello_world 具有对话场景功能的代理(agent with conversation scenarios)

要将包含对话场景的评估案例添加到新的或现有的项目中,请按照以下步骤操作: EvalSet首先,你需要创建一个对话场景列表,以便对这些对话系统进行测试。

试着将以下内容保存到…… contributing/samples/core/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.",
      "user_persona": "NOVICE"
    },
    {
      "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.",
      "user_persona": "EXPERT"
    }
  ]
}

您还需要一个会话输入文件,其中包含用于评估过程中的信息。 请尝试将以下内容保存到相应的文件中: contributing/samples/core/hello_world/session_input.json:

{
  "app_name": "hello_world",
  "user_id": "user"
}

然后,您可以将这些对话场景添加到某个系统中(或文档中)。 EvalSet:

# (optional) create a new EvalSet
adk eval_set create \
  contributing/samples/core/hello_world \
  eval_set_with_scenarios

# add conversation scenarios to the EvalSet as new eval cases
adk eval_set add_eval_case \
  contributing/samples/core/hello_world \
  eval_set_with_scenarios \
  --scenarios_file contributing/samples/core/hello_world/conversation_scenarios.json \
  --session_input_file contributing/samples/core/hello_world/session_input.json

默认情况下,ADK 在进行评估时会使用那些需要指定代理预期响应的指标。 然而,在动态对话场景中,情况并非如此;因此,我们将使用另一种方法来处理这些评估。 EvalConfig 使用一些其他受支持的指标(alternative supported metrics)。

试着将以下内容保存到…… contributing/samples/core/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/core/hello_world \
    --config_file_path contributing/samples/core/hello_world/eval_config.json \
    eval_set_with_scenarios \
    --print_detailed_results

用户模拟器配置

您可以覆盖默认的用户模拟器配置,以更改模型、内部模型的行为以及用户代理(user-agent)之间的最大交互次数。 EvalConfig 显示默认的用户模拟器配置:

{
  "criteria": {
    # same as before
  },
  "user_simulator_config": {
    "model": "gemini-flash-latest",
    "model_configuration": {
      "thinking_config": {
        "include_thoughts": true,
        "thinking_budget": 10240
      }
    },
    "max_allowed_invocations": 20,
    "include_function_calls": false
  }
}
  • model:支持用户模拟器的模型。
  • model_configuration: A GenerateContentConfig 该参数用于控制模型的行为。
  • max_allowed_invocations:在会话被强制终止之前,允许的最大用户代理(user-agent)交互次数。该数值应设置为大于您系统中用户代理交互过程所需的最长时间。 EvalSet初始的固定提示文本也被视为一次调用(即一次程序的执行)。将这个值设置为…… -1 移除了“创新限制”功能,但这种做法并不被推荐。
  • include_function_calls:可选。是否在显示给用户模拟器的对话历史提示中包含函数调用及其返回结果。 默认值为:**包含**。 false.
  • custom_instructions:可选。此设置会覆盖用户模拟器的默认操作指令。指令字符串必须包含以下格式占位符: Jinja `syntax`(语法)请不要提前替换变量中的值!):
    • {{ stop_signal }}当用户模拟器判断对话结束时,将生成以下文本: : “对话结束。”
    • {{ conversation_plan }}这是用户模拟器在对话过程中必须遵循的总体计划。
    • {{ conversation_history }}:到目前为止,用户与代理之间的对话内容。
    • 您也可以访问该内容。 UserPersona “object through the” 这个短语在常规英语中并没有明确的含义,可能是一个拼写错误或者特定领域的术语。如果它是某个特定领域(如编程、技术文档等)中的术语,请提供更多背景信息,以便我能够给出更准确的解释。 {{ persona }} placeholder.

自定义人物角色(Custom personas)

你可以通过提供相应的信息来定义自己自定义的角色(或“ persona”)。 UserPersona 对象位于……之中 ConversationScenario.

自定义人物角色定义的示例:

{
  "starting_prompt": "I need help with my account.",
  "conversation_plan": "Ask the agent to reset your password.",
  "user_persona": {
    "id": "IMPATIENT_USER",
    "description": "A user who is in a rush and gets easily frustrated.",
    "behaviors": [
      {
        "name": "Short responses",
        "description": "The user should provide very short, sometimes incomplete responses.",
        "behavior_instructions": [
            "Keep your responses under 10 words.",
            "Omit polite phrases."
        ],
        "violation_rubrics": [
            "The user response is over 10 words.",
            "The user response is overly polite."
        ]
      }
    ]
  }
}

通过用户模拟来生成评估案例。

手动编写评估用例不仅耗时,而且可能无法涵盖所有潜在的故障模式。ADK 提供了一个命令,可以通过 Agent Platform Eval SDK 根据您的代理(agent)的定义自动生成多样且真实的对话场景。

先决条件:代理平台凭据(Agent Platform Credentials)

生成评估用例时使用了…… Vertex Gen AI 评估服务 API您必须拥有一个启用了 Agent Platform API 的 Google Cloud 项目,并且在您的环境中配置了有效的应用程序默认凭据(Application Default Credentials,简称 ADC)。

命令语法

adk eval_set generate_eval_cases \
    <AGENT_MODULE_FILE_PATH> \
    <EVAL_SET_ID> \
    --user_simulation_config_file=<PATH_TO_CONFIG_FILE>

配置文件格式

The --user_simulation_config_file 期望接收一个符合要求的 JSON 文件。 ConversationGenerationConfig ```json { "schema": { "type": "object", "properties": { "name": "API Identifier", "description": "A unique identifier for the API", "version": "1.0", "status": "Active", "lastUpdated": "2021-12-31" } } } ```

{
  "count": 5,
  "generation_instruction": "Generate scenarios where the user asks to control home devices under different conditions.",
  "environment_context": "Available devices: device_1 (Light), device_2 (Thermostat).",
  "model_name": "gemini-flash-latest"
}

配置字段(Configuration Fields)

  • count (必填项):需要生成的对话场景的数量。
  • generation_instruction (可选):一个用于指导您想要测试的具体场景或目标的自然语言提示。
  • environment_context (可选):描述代理工具可以访问的后端数据或状态的上下文信息。这些信息有助于生成器创建基于真实数据的查询(例如,有效的设备 ID)。
  • model_name (必填):用于生成结果的 Gemini 模型(例如:) gemini-flash-latest).

用于实时客服人员的音频用户模拟功能

用户模拟器与被测试的代理是否为实时(语音)代理无关,因此可以使用相同的模拟器进行测试。 ConversationScenario (或者是一次固定的对话)可以推动文本交流以及实时评估的进行。对于在线客服人员来说,模拟用户的发言内容可以被合成(即被转换为实际可使用的文本或语音),以便他们能够继续与客户进行交流。 audio 然后这些数据被流式传输(streamed)到代理(agent)那里。

该配置是通过以下方式实现的: llm_audio 在你的评估配置(eval config)中使用了用户模拟器(user simulator)。test_config.json该工具封装了标准的文本模拟器,并利用文本转语音(Text-to-Speech)模型将用户生成的每一条指令转换为音频。默认情况下,它使用的是 Google Cloud 的文本转语音服务。cloud_tts可以使用 Gemini TTS 模型的名称作为替代。

{
  "criteria": {
    "tool_trajectory_avg_score": 1.0,
    "response_match_score": 0.5
  },
  "live_model_config": {
    "timeout_seconds": 300
  },
  "user_simulator_config": {
    "type": "llm_audio",
    "model": "gemini-2.5-flash",
    "audio_model": "cloud_tts",
    "audio_model_configuration": {
      "speech_config": {
        "voice_config": {
          "prebuilt_voice_config": { "voice_name": "en-US-Studio-O" }
        },
        "language_code": "en-US"
      }
    },
    "include_text_with_audio": true
  }
}

关键字段:

  • type: "llm_audio" 选择音频用户模拟器(audio user simulator)。
  • audio_model: "cloud_tts" 对于 Google Cloud 的文本转语音(Text-to-Speech)服务,或者 Gemini 语音模型: 语音模型的名称(例如:……) "gemini-2.5-flash-preview-tts").
  • audio_model_configuration.speech_config:选择语音和语言。
  • include_text_with_audio:用户是否也会在生成的音频文件中附带文本内容?

实时模型需要实时的推理(即需要立即处理输入数据并生成相应的结果)。

评估在线客服人员的表现需要使用实时(双向流式传输)推理技术; 不;并非 这是默认设置。可以通过添加相应的代码来启用该功能。 live_model_config 将相关内容添加到您的配置文件中。这些内容涉及实时 API 模型(例如……)。 gemini-*-live-*)不会通过“一元操作”(unary operation)来提供(即:这些内容不会通过某种特定的“一元操作”方式被传输或处理)。 generateContent 这是非实时评估(non-live evaluation)所使用的端点(endpoint);因此,如果在非实时模式下运行这些程序,将会导致失败(即程序无法正常执行)。

use_live 这是一个内部字段,其值是由其他系统或流程设置的。 live_model_config;换句话说,将相关配置信息放入配置文件中并不会产生任何实际效果(即这些配置信息不会被系统读取或使用)。

使用 cloud_tts 需要…… google-cloud-texttospeech `package`(包含在……中) google-adk[eval] (额外功能)以及使用 Cloud Text-to-Speech API 的权限。

请参阅示例: contributing/samples/live/non_blocking_tool_agent 为了获得一个完整且可运行的实时评估(live evaluation)配置……