實戰:讓團隊領導者把請求一鍵轉派給專業成員)
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 下劃線樣式這類靜態風格校驗上未通過不影響示例功能本身??偨YRoute 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。掌握這一模式后你就能以極低的編排成本構建出專人專事、答即所問的多語言客服、領域問答或工單分發系統?!久赓M下載鏈接】agnoBuild, run, and manage agent platforms.項目地址: https://gitcode.com/GitHub_Trending/ag/agno創作聲明:本文部分內容由AI輔助生成(AIGC),僅供參考