实战:让团队领导者把请求一键转派给专业成员)
Agno Team 路由模式Route Mode实战让团队领导者把请求一键转派给专业成员【免费下载链接】agnoBuild, run, and manage agent platforms.项目地址: https://gitcode.com/GitHub_Trending/ag/agno路由模式TeamMode.route是 Agno 中 Team 的四种执行模式之一。它的核心思路很直接团队领导者leader分析用户请求后只把任务转派给最匹配的一位专业成员specialist并把这个成员的回复原样返回给用户不再做二次合成。本文基于仓库cookbook/03_teams/02_modes/route/目录下的三个可运行示例展开分别演示语言路由、领域专家路由和带兜底fallback的路由。读完本文你将掌握 route mode 的语义、Team的搭建方式、成员角色的编写要点以及如何在自己的多智能体场景里用它实现专人专事的分发。什么是 Route Mode与其它 Team 模式的定位差异Agno 的 Team 支持四种执行模式定义位于 mode.py 的TeamMode枚举中模式枚举值领导者行为典型场景CoordinateTeamMode.coordinate挑选成员、派发任务并合成响应默认监督者模式通用编排RouteTeamMode.route只路由给一位专家直接返回该成员的回答专家选择、语言路由BroadcastTeamMode.broadcast把同一任务发给所有成员再合成结果多视角分析、共识TasksTeamMode.tasks把目标拆解成共享任务清单按依赖循环执行直到完成复杂多步工作流、并行执行从源码注释看route 模式的精确定义是Router pattern. Leader routes to a specialist and returns the members response directly.mode.py对比 coordinate/broadcast 需要领导者综合各路答案route 模式不合成——谁的活谁干回答是谁的就原样给谁。这种零合成分支的设计让它在语言分发、领域专送、客服工单分类等场景下响应路径最短、语义最不容易被中间层稀释。底层语义moderoute 在源码里做了什么选择modeTeamMode.route并不是一个黑盒开关它在 Team 初始化阶段_init.py会被确定性归一化成一组布尔配置if mode TeamMode.route: team.respond_directly True # 成员回答直接返回不合成 team.delegate_to_all_members False # 不广播给所有人也就是说 route 模式等价于只派单 直连返回。源码里还做了反向归一化如果你只设置了respond_directlyTrue而未指定modeTeam 会被自动归为route模式。这保证了同一套语义无论从哪个入口配置都不会互相冲突。而路由任务本身通过delegate_task_to_member这一团队工具完成。在 route 模式下系统注入给领导者的提示词会明确约束其行为见 _messages.py你工作在 route 模式必须把请求交给恰好一个成员调用delegate_task_to_member并把该成员的回答原样返回给用户随后结束本轮。结合三个示例中都会设置的show_members_responsesTrue运行时可直观看到领导者选人 → 成员作答 → 直接透传的完整链路。示例一语言路由01_basic.py第一个例子把三个只会说一种语言的 Agent 装进一个Language Router团队领导者先检测用户输入属于哪种语言再把问题转给对应的语言专家而不支持的输入默认兜底给英语专家。完整代码见 01_basic.py核心结构如下from agno.agent import Agent from agno.models.openai import OpenAIResponses from agno.team.mode import TeamMode from agno.team.team import Team # 1. 创建成员每个 Agent 用 name/role 表明身份与边界 english_agent Agent( nameEnglish Agent, roleResponds only in English, modelOpenAIResponses(idgpt-5.2), instructions[Always respond in English, regardless of the input language.], ) spanish_agent Agent( nameSpanish Agent, roleResponds only in Spanish, modelOpenAIResponses(idgpt-5.2), instructions[Always respond in Spanish, regardless of the input language.], ) french_agent Agent( nameFrench Agent, roleResponds only in French, modelOpenAIResponses(idgpt-5.2), instructions[Always respond in French, regardless of the input language.], ) # 2. 创建 Teammoderoute 是关键 team Team( nameLanguage Router, modeTeamMode.route, modelOpenAIResponses(idgpt-5.2), members[english_agent, spanish_agent, french_agent], instructions[ You are a language router., Detect the language of the users message and route to the matching agent., If the language is not supported, default to the English Agent., ], show_members_responsesTrue, markdownTrue, ) # 3. 连续提问分别用英语 / 西班牙语 / 法语 team.print_response(What is the capital of France?, streamTrue) team.print_response(Cual es la capital de Francia?, streamTrue) team.print_response(Quelle est la capitale de la France?, streamTrue)注意moderoute模式下领导者依然需要自己的model上例复用同一个gpt-5.2模型。团队成员用role自我声明职责、用instructions硬约束行为这是保证路由准确性的第一道防线——例如语言专家必须无论输入语言是什么都用指定语言回复即使领导者偶尔派错成员输出也依然守规矩。示例二领域专家路由02_specialist_router.py当问题不是按语言分类而是按学科派活时做法完全一致只是把成员换成数学、编程、科学三个领域专家。见 02_specialist_router.pymath_agent Agent( nameMath Specialist, roleSolves mathematical problems and explains concepts, modelOpenAIResponses(idgpt-5.2), instructions[ You are a mathematics expert., Solve problems step by step, showing your work clearly., Explain the underlying concepts when relevant., ], ) code_agent Agent( nameCode Specialist, roleWrites code and explains programming concepts, modelOpenAIResponses(idgpt-5.2), instructions[ You are a programming expert., Write clean, well-commented code., Explain your approach and any trade-offs., ], ) science_agent Agent( nameScience Specialist, roleExplains scientific concepts and phenomena, modelOpenAIResponses(idgpt-5.2), instructions[ You are a science expert covering physics, chemistry, and biology., Explain concepts clearly with real-world examples., ], ) team Team( nameExpert Router, modeTeamMode.route, modelOpenAIResponses(idgpt-5.2), members[math_agent, code_agent, science_agent], instructions[ You are an expert router., Analyze the users question and route it to the best specialist:, - Math questions - Math Specialist, - Programming questions - Code Specialist, - Science questions - Science Specialist, ], show_members_responsesTrue, markdownTrue, ) team.print_response( What is the time complexity of merge sort and why?, streamTrue, )这里值得学习的是Team 级instructions的路由规则表写法用一行一个- 类别 - 成员的显式映射把什么题找谁讲清楚。把分发逻辑写成确定性规则比让领导者自由发挥更能获得稳定的路由结果。示例三带兜底 Agent 的路由03_with_fallback.py真实场景中并非每个问题都命中专家。第三个示例在 SQL 专家、Python 专家之外增加了一个通用助手General Assistant专门接住不属于任何专家或拿不准的问题见 03_with_fallback.pysql_agent Agent( nameSQL Expert, roleWrites and optimizes SQL queries, modelOpenAIResponses(idgpt-5.2), instructions[ You are an SQL expert., Write correct, optimized SQL queries., Explain query plans and indexing strategies when asked., ], ) python_agent Agent( namePython Expert, roleWrites Python code and solves Python-specific problems, modelOpenAIResponses(idgpt-5.2), instructions[ You are a Python expert., Write idiomatic, well-structured Python code., Follow PEP 8 and use type hints., ], ) general_agent Agent( nameGeneral Assistant, roleHandles general questions that do not match a specialist, modelOpenAIResponses(idgpt-5.2), instructions[ You are a helpful general assistant., Answer questions clearly and concisely., If the question is about SQL or Python, still do your best., ], ) team Team( nameDev Help Router, modeTeamMode.route, modelOpenAIResponses(idgpt-5.2), members[sql_agent, python_agent, general_agent], instructions[ You route questions to the right expert., - SQL or database questions - SQL Expert, - Python questions - Python Expert, - Everything else - General Assistant, When in doubt, route to the General Assistant., ], show_members_responsesTrue, markdownTrue, ) # SQL 问题 - 路由给 SQL Expert team.print_response( Write a query to find the top 10 customers by total order value, joining the customers and orders tables., streamTrue, ) # 通用问题 - 兜底到 General Assistant team.print_response( What are some good practices for code review?, streamTrue, )兜底路由之所以高效靠的是两条写在规则里的兜底策略一是Everything else - General Assistant二是When in doubt, route to the General Assistant拿不准就交给兜底。这两句话合起来几乎消灭了无专家可派的分叉死路同时兜底成员的 instructions 也主动声明即使问 SQL/Python 我也会尽力回答进一步降低路由失败时的体验损失。Team 路由编排要点小结把三个示例放在一起可以提炼出 route 模式的标准编排配方模式选择Team(..., modeTeamMode.route)导入路径from agno.team.mode import TeamMode领导者大脑Team 自带model负责读懂问题 选人成员画像每个成员必须有清晰的name与role领导者正是依据这些信息判断把任务交给谁规则显式化在 Team 的instructions中写明- 条件 - 成员映射表并补充默认分支兜底兜死预留General Assistant之类的通用成员承接未知问题过程可视化show_members_responsesTrue在响应中展示每个成员被选/被派的情况便于调试路由决策。仓库父目录 02_modes/README.md 以对比表形式列出了四种模式——route 模式适合专家选择、语言路由这类天然只有一条正确执行路径的任务如果你的场景需要综合多方意见或把一个大目标拆成依赖链条则应分别考虑 coordinate / broadcast / tasks 模式。运行方式与环境说明三个示例的运行方式相同替换脚本文件名即可.venvs/demo/bin/python cookbook/03_teams/02_modes/route/01_basic.py .venvs/demo/bin/python cookbook/03_teams/02_modes/route/02_specialist_router.py .venvs/demo/bin/python cookbook/03_teams/02_modes/route/03_with_fallback.py运行前提与注意事项示例使用.venvs/demo这一仓库约定的虚拟环境解释器需先按仓库说明完成依赖安装agno 本体位于 libs/agno 下示例统一使用OpenAIResponses(idgpt-5.2)运行时需要配置对应的 OpenAI 服务凭据环境变量OPENAI_API_KEYprint_response(..., streamTrue)开启流式输出三份脚本都以分隔行如 * 60切分多个问题以便观察每次路由结果每个if __name__ __main__:块内按顺序发问建议一次只跑一个脚本逐个观察路由行为目录内的 TEST_LOG.md 记录了这三个脚本的自动化测试状态三个文件均可完成运行Run: completed仅在docstring 下划线样式这类静态风格校验上未通过不影响示例功能本身。总结Route Mode 是 Agno Team 中单点直派的执行模式领导者用delegate_task_to_member把请求交给恰好一个成员成员回答经respond_directly语义原样透传、不经过合成层。在实现上modeTeamMode.route会被归一化为respond_directlyTrue与delegate_to_all_membersFalse源码证据从行为模型上保证了只派一人、直连返回。无论是按语言分发01_basic.py、按学科派活02_specialist_router.py还是用通用成员兜底03_with_fallback.py核心都是把选人规则显式写进 Team 的 instructions、把职责边界写进成员的 role 与 instructions。掌握这一模式后你就能以极低的编排成本构建出专人专事、答即所问的多语言客服、领域问答或工单分发系统。【免费下载链接】agnoBuild, run, and manage agent platforms.项目地址: https://gitcode.com/GitHub_Trending/ag/agno创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考