优化智能体提示

优化是质量飞轮的最后阶段。通过评估和故障聚类分析确定系统性问题后,您可以使用 Optimizer 服务以编程方式优化代理的系统指令或工具说明。此自动化流程可识别确切的故障点,并以迭代方式提出有针对性的更新,直到找到评估数据集的最佳提示为止。

优化飞轮

优化智能体通常遵循一个三步循环:

  1. 分析失败:找出具体的行为差距,例如代理未能验证用户身份的“损失集群”。
  2. 提出改进建议:优化器会针对系统指令(例如“在确认预订之前,始终要求提供飞行常客卡号”)生成“补丁”。
  3. 验证增益:系统会自动运行对照比较评估,以确保新指令可提高得分,而不会导致回归。

使用 SDK 以程序化方式进行优化

您可以使用 Agent Platform SDK 直接触发优化作业。优化器使用 GEPAMIPRO 等高级方法从测试用例中派生新规则。

# Automatically refine the system prompt to fix identified issues
optimize_result = client.optimizer.optimize(
    targets=["system_prompt"],    # Specify which resource to optimize
    benchmark=eval_result,       # Use results from a previous evaluation run
    tests=eval_dataset,          # The test cases the agent should pass
    optimization_params={
        "method": "gepa",        # Use the GEPA optimization method
        "num_steps": 5           # Number of iterations to perform
    }
)

# Preview the optimized instruction
print("Optimized Instruction:", optimize_result.instruction)

使用 CLI 进行优化

对于本地开发,您可以使用 adk optimize 命令直接从终端更新智能体代码。

  1. 运行优化命令,以本地代理模块为目标: sh adk optimize . --target instruction --eval_set my_test_suite
  2. 在终端中查看建议的更改。
  3. 输入 y,将优化后的指令应用到 agent.py 文件。

控制台中的视觉优化

在 Google Cloud 控制台中,您可以使用可视化分析洞见来触发优化:

  1. 在 Google Cloud 控制台中,前往 Agent Platform > Agents > Evaluation 页面。

    前往“评估”页面

  2. 选择失败的评估运行。

  3. 选择特定的损失聚类(例如“工具参数格式无效”)。

  4. 点击 Fix with Optimizer 按钮。

  5. 查看系统指令的之前/之后差异,然后点击接受以更新代理。