ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

adk-python 配置驱动的多智能体实践:基于 YAML 委派的学习助手示例

adk-python 配置驱动的多智能体实践:基于 YAML 委派的学习助手示例 adk-python 配置驱动的多智能体实践基于 YAML 委派的学习助手示例【免费下载链接】adk-pythonAn open-source, code-first Python toolkit for building, evaluating, and deploying sophisticated AI agents with flexibility and control.项目地址: https://gitcode.com/GitHub_Trending/ad/adk-python本篇指南围绕 adk-python 仓库中的配置版多智能体示例 multi_agent_basic_config 展开它用三个纯 YAML 文件一个路由主智能体 两个专业辅导智能体构建了一个学习助手多智能体系统全程不写一行 Python 智能体代码、不挂载任何工具。读完本文你将掌握如何用agent_class、model、instruction、sub_agents等配置字段定义委派式多智能体理解root_agent.yaml目录约定与加载机制并能看懂 ADK 配置加载器from_config/_AgentConfigMapper在底层如何把 YAML 递归构造成真实的LlmAgent对象树。示例结构与文件清单该示例位于 contributing/samples/multi_agent/multi_agent_basic_config/目录内容如下文件角色root_agent.yaml主学习助手智能体负责将问题路由到对应的辅导智能体code_tutor_agent.yaml编程问题专项辅导智能体math_tutor_agent.yaml数学问题专项辅导智能体README.md示例说明文档官方 README 的定位是一个最小化的多智能体配置示例学习助手委派给专业的辅导智能体tutoring agents。核心委派规则是编程/代码类问题 → 委派给code_tutor_agent数学类问题 → 委派给math_tutor_agent这个例子刻意不使用任何工具专注展示清晰委派 专业分工这一多智能体基本形态。主智能体配置root_agent.yaml 逐字段解析完整的 root_agent.yaml 内容如下省略许可证头注释agent_class: LlmAgent model: gemini-2.5-flash name: root_agent description: Learning assistant that provides tutoring in code and math. instruction: | You are a learning assistant that helps students with coding and math questions. You delegate coding questions to the code_tutor_agent and math questions to the math_tutor_agent. Follow these steps: 1. If the user asks about programming or coding, delegate to the code_tutor_agent. 2. If the user asks about math concepts or problems, delegate to the math_tutor_agent. 3. Always provide clear explanations and encourage learning. sub_agents: - config_path: code_tutor_agent.yaml - config_path: math_tutor_agent.yaml各字段含义字段取值作用agent_classLlmAgent指定用哪个 ADK 智能体类来构建实例。从加载器源码看该字段可省略缺省值就是LlmAgent见下文 config_agent_utils.py 中config_data.get(agent_class, LlmAgent)的实现modelgemini-2.5-flash主智能体使用的模型。示例声明了该模型意味着运行时需要可访问 Gemini 的凭据如 Google AI Studio API key 或 Vertex AI 环境配置nameroot_agent智能体名称也是委派与对话中引用该智能体的标识description…智能体的一句话描述供路由/文档场景使用instruction多行文本系统提示词。这里用编号步骤的形式显式写明委派规则是驱动委派行为的核心sub_agents两个config_path引用声明子智能体。注意写法不是内联定义而是引用同目录下的其他 YAML 配置文件由加载器递归构建关于委派机制有两点值得注意委派由 instruction 驱动LlmAgent的子智能体路由依赖模型根据instruction与子智能体的name/description自行决定转发。示例中description: Coding tutor that helps with programming concepts and questions.这类描述文本就是模型做路由决策时的重要信号因此写清楚何时该找这个智能体比堆砌角色设定更有价值。子智能体通过config_path相对引用sub_agents列表里每一项是{config_path: xxx.yaml}形式的引用。加载器会相对于引用它的配置文件所在目录解析该路径见下文底层实现章节的路径拼接逻辑所以两个 tutor 配置与主配置放在同一目录即可直接生效。两个专业辅导智能体配置code_tutor_agent.yamlagent_class: LlmAgent name: code_tutor_agent description: Coding tutor that helps with programming concepts and questions. instruction: | You are a helpful coding tutor that specializes in teaching programming concepts. Your role is to: 1. Explain programming concepts clearly and simply 2. Help debug code issues 3. Provide code examples and best practices 4. Guide students through problem-solving approaches 5. Encourage good coding habits Always be patient, encouraging, and provide step-by-step explanations.math_tutor_agent.yamlagent_class: LlmAgent name: math_tutor_agent description: Math tutor that helps with mathematical concepts and problems. instruction: | You are a helpful math tutor that specializes in teaching mathematical concepts. Your role is to: 1. Explain mathematical concepts clearly with examples 2. Help solve math problems step by step 3. Provide different approaches to solving problems 4. Help students understand the reasoning behind solutions 5. Encourage mathematical thinking and problem-solving skills Always break down complex problems into manageable steps and be patient with explanations.两个子智能体配置的共同特征都声明agent_class: LlmAgent与主智能体同为纯 LLM 智能体均未声明model字段从源码结构看LlmAgent的model是可选字段未声明时使用框架为该模型字段准备的默认模型均未挂载tools符合示例无工具、专注委派的设计目标instruction采用角色 编号职责清单 风格约束的结构这是该系列示例提示词编写的通用范式可复用到自己的子智能体中。三个文件开头都有同一行 IDE 辅助注释# yaml-language-server: $schemahttps://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json它用于让支持 yaml-language-server 的编辑器按 Agent 配置 JSON Schema 做字段校验该 Schema 的源文件在仓库内同样存在AgentConfig.json。运行示例目录约定ADK 的命令行工具adk web/adk run识别智能体项目时会查找agents_dir/{agent_name}/root_agent.yaml作为根智能体配置。这一约定在加载器 agent_loader.py 中可以直接看到def _load_from_yaml_config(self, agent_name, agents_dir): # Load from the config file at agents_dir/{agent_name}/root_agent.yaml config_path os.path.join(agents_dir, agent_name, root_agent.yaml) ... agent config_agent_utils.from_config(config_path)当目录中没有root_agent.yaml或没有代码定义的__init__.py时加载器会打印明确的目录结构提示要求放入root_agent.yaml后执行adk web agents_dir见 agent_loader.py 的报错提示。操作步骤安装google-adk包以仓库 README.md 中的安装说明为准并配置示例所用gemini-2.5-flash模型所需的凭据如设置 Google AI Studio 的GOOGLE_API_KEY或配置 Vertex AI 相关环境变量后走 Vertex 通道把本示例目录整体作为一个智能体项目目录其下直接包含root_agent.yaml在示例的上一级目录中执行# 启动 Web 开发界面选择本示例对应的智能体目录进行对话 adk web 包含该示例的父目录 # 或在命令行中直接与智能体交互 adk run 示例目录推荐测试查询官方 README 给出的示例查询如下可直接用于验证委派是否按预期发生编程类问题预期委派给code_tutor_agentHow do I create a for loop in Python? Can you help me debug this function? What are the best practices for variable naming?数学类问题预期委派给math_tutor_agentCan you explain the quadratic formula? How do I solve this algebra problem: 2x 5 15? Whats the difference between mean and median?验证要点观察对话过程中响应是否来自对应 tutor 智能体如事件中的智能体标识以及回答风格是否符合各自instruction中定义的耐心、分步讲解特征。底层实现YAML 如何变成智能体对象树入口 from_config配置加载的总入口是 config_agent_utils.py 中的from_config(config_path)其流程为校验配置文件存在用yaml.safe_load解析安全解析器不会执行 YAML 标签拒绝空文件或非字典结构的配置读取agent_class缺省LlmAgent经_resolve_agent_class解析为真实类——裸类名会先在google.adk.agents、google.adk.workflow两个内置包中探测否则按完整限定名模块路径.类名导入并要求目标必须是BaseNode子类实例化_AgentConfigMapper并调用map()把 YAML 数据映射为构造参数最终agent_class(**kwargs)得到智能体实例。注意from_config带有experimental(FeatureName.AGENT_CONFIG)标记即基于配置的 Agent 目前是实验性特性使用前可留意该特性的可用性约定。反射式字段映射与 Schema 校验_AgentConfigMapper.map 采用反射 分类规则的方式把 YAML 键值映射到目标类字段先收集目标类的 Pydanticmodel_fields与构造签名参数作为合法键集合如果类声明了config_type配置 Schema会先用它model_validate整个数据——内置配置是extraforbid因此拼错的字段名会在校验阶段直接报错而不是被静默丢弃对_code后缀字段如model_code、回调的*_code写法做代码引用解析支持以.开头的相对模块名会拼接为配置文件所在包名 相对路径的形式导入。字段级的特殊处理在 _map_field 中按规则分发与本示例直接相关的是Rule 2Sub Agents# Rule 2: Sub Agents if _is_sub_agents_type(annotation) and isinstance(value, list): sub_agents [] for sub_agent_config in value: ref ( AgentRefConfig(**sub_agent_config) if isinstance(sub_agent_config, dict) else AgentRefConfig(config_pathsub_agent_config) ) sub_agents.append(resolve_agent_reference(ref, self.abs_path)) return sub_agents也就是说root_agent.yaml里sub_agents的每一项都被包装成AgentRefConfig然后交给resolve_agent_reference递归构建——这正是引用式子智能体能工作的原因。引用解析与路径安全resolve_agent_reference 是子智能体引用的解析器值得逐行理解因为它定义了config_path的完整语义if ref_config.config_path: if os.path.isabs(ref_config.config_path): raise ValueError(Absolute paths are not allowed in AgentRefConfig config_path: ...) agent_dir os.path.dirname(referencing_agent_config_abs_path) resolved_path os.path.realpath(os.path.join(agent_dir, ref_config.config_path)) canonical_agent_dir os.path.realpath(agent_dir) if os.path.commonpath([canonical_agent_dir, resolved_path]) ! canonical_agent_dir: raise ValueError(Path traversal detected: config_path ... resolves outside the agent directory) return from_config(resolved_path)要点相对路径基准是引用方配置文件所在目录本示例中root_agent.yaml与两个 tutor 配置同目录所以code_tutor_agent.yaml这样的裸文件名即可命中如果日后把 tutor 配置挪进子目录config_path要相应写成sub/code_tutor_agent.yaml禁止绝对路径并做commonpath检查防止../../形式的路径穿越出智能体目录命中后递归调用from_config因此引用链可以多层嵌套若config_path与code都未提供则直接报错AgentRefConfig must have either code or config_path除config_path外引用还支持code字段给定模块路径.智能体实例的完整限定名从 Python 代码中直接引用一个现成的智能体实例见 _resolve_agent_code_reference且该目标必须是BaseNode实例传类或可调用对象都会被明确拒绝。旧的 AgentConfig 类与当前机制的关系仓库中还存在一个 Pydantic 判别联合体 AgentConfigRootModel包装LlmAgentConfig/LoopAgentConfig/ParallelAgentConfig/SequentialAgentConfig/BaseAgentConfig但它已被标注为 deprecated配置现在通过反射加载单独的 config 类不再需要。本示例走的就是当前的反射加载路径写 YAML 时无需关心AgentConfig类只需让字段与LlmAgent的字段/配置 Schema 对齐即可。配置加载的安全边界由于 YAML 配置允许通过*_code字段和引用机制触发 Python 导入加载器内置了模块引用白/黑名单约束_validate_module_reference 会对每个代码引用的顶层包名做检查Python 标准库整体被阻断sys.stdlib_module_names 内置模块另有os、subprocess、socket、pickle、yaml等显式阻断清单覆盖cProfile.run、timeit、yaml.unsafe_load这类能执行字符串/反序列化任意对象的高危入口官方建议是只引用你自己的智能体包、google.adk或可信的第三方集成包YAML 解析统一使用yaml.safe_load避免 YAML 标签触发的对象构造另有一个针对args键的黑名单检查 _check_config_for_blocked_keys代码注释说明该键可执行任意代码当前默认开关_ENFORCE_YAML_KEY_DENYLIST为False可视为预留的加固点。这些约束意味着本示例零 Python 代码的配置方式并不是牺牲安全性的便利写法而是在受控导入规则下的正规路径。小结与延伸阅读本示例的最小价值在于用三个文件演示了 ADK 配置驱动多智能体的完整闭环instruction写委派规则 sub_agents用config_path相对引用 目录约定root_agent.yamladk web/adk run启动。从源码侧印证的关键链路是agent_loader发现root_agent.yaml→from_config反射构建主智能体 →_map_fieldRule 2 将sub_agents转为AgentRefConfig→resolve_agent_reference做路径安全校验后递归from_config。如果想在同一仓库中横向对比配置版与代码版的多智能体写法可以查看 contributing/samples/multi_agent/ 下的兄弟示例如hello_world_ma、multi_agent_llm_config等字段级 Schema 详见 AgentConfig.json。适用前提提醒配置式 Agent 在源码中标记为实验特性FeatureName.AGENT_CONFIG且示例硬编码了gemini-2.5-flash模型迁移到其他模型或平台时只需修改model字段并相应调整凭据配置。【免费下载链接】adk-pythonAn open-source, code-first Python toolkit for building, evaluating, and deploying sophisticated AI agents with flexibility and control.项目地址: https://gitcode.com/GitHub_Trending/ad/adk-python创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表