教程 9.2:循环智能体——迭代计划优化器 您将学到的内容 循环智能体的组合:以循环方式依次执行子智能体 有状态的迭代:在每次迭代中持久化计数器和标志位 终止条件:达到最大迭代次数或当某个子智能体升级时停止 Streamlit Web 界面:用于运行迭代优化的交互式 UI 核心概念:带条件的 LoopAgent 根据 ADK 工作流智能体文档,LoopAgent 会在每次迭代中重复执行一组子智能体,并在迭代之间共享相同的上下文/状态。本教程演示了一个迭代计划优化器,它会在多个迭代中逐步改进计划,并在满足特定条件时停止。 终止条件:如果可选的 is reached, or if any sub-agent returns an with in its .
根据 ADK 工作流智能体文档,LoopAgent 会在每次迭代中重复执行一组子智能体,并在迭代之间共享相同的上下文/状态。本教程演示了一个迭代计划优化器,它会在多个迭代中逐步改进计划,并在满足特定条件时停止。
Topic → LoopAgent → [Refine Plan] → [Increment Iteration] → [Check Completion] ↑ │ └──────────────────────────── Repeat until stop ─────────┘
终止条件:如果可选的 max_iterations is reached, or if any sub-agent returns an Event with escalate=True in its EventActions.
Context & State: The same InvocationContext and session.state are used across iterations, allowing values like iteration, target_iterations, and accepted 被设置为真,则循环会停止,以便持久化并控制循环流程。
9_2_loop agent/ ├── agent.py # LoopAgent with 3 sub-agents and session-state control ├── app.py # Streamlit UI to run the loop refinement └── README.md # This documentation
cd "9_2_loop agent" pip install -r ../9_1_sequential_agent/requirements.txt
创建一个 .env 文件,并填入您的 Google API 密钥(或复用顺序示例中的密钥):
echo "GOOGLE_API_KEY=your_ai_studio_key_here" > .env
streamlit run app.py
session.state['iteration'].accepted=True or iteration >= target_iterations.The LoopAgent sequences these sub-agents on every iteration, persisting and updating state until a stop condition is met.
True, the loop stops immediately.目标迭代次数 to 3–5.EventActions(escalate=True).GOOGLE_API_KEY is set in .env.app.py。免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文语言版本的文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。