實(shí)戰(zhàn):讓團(tuán)隊(duì)領(lǐng)導(dǎo)者把請(qǐng)求一鍵轉(zhuǎn)派給專業(yè)成員)
Agno Team 路由模式Route Mode實(shí)戰(zhàn)讓團(tuán)隊(duì)領(lǐng)導(dǎo)者把請(qǐng)求一鍵轉(zhuǎn)派給專業(yè)成員【免費(fèi)下載鏈接】agnoBuild, run, and manage agent platforms.項(xiàng)目地址: https://gitcode.com/GitHub_Trending/ag/agno路由模式TeamMode.route是 Agno 中 Team 的四種執(zhí)行模式之一。它的核心思路很直接團(tuán)隊(duì)領(lǐng)導(dǎo)者leader分析用戶請(qǐng)求后只把任務(wù)轉(zhuǎn)派給最匹配的一位專業(yè)成員specialist并把這個(gè)成員的回復(fù)原樣返回給用戶不再做二次合成。本文基于倉(cāng)庫(kù)cookbook/03_teams/02_modes/route/目錄下的三個(gè)可運(yùn)行示例展開分別演示語言路由、領(lǐng)域?qū)<衣酚珊蛶Ф档譮allback的路由。讀完本文你將掌握 route mode 的語義、Team的搭建方式、成員角色的編寫要點(diǎn)以及如何在自己的多智能體場(chǎng)景里用它實(shí)現(xiàn)專人專事的分發(fā)。什么是 Route Mode與其它 Team 模式的定位差異Agno 的 Team 支持四種執(zhí)行模式定義位于 mode.py 的TeamMode枚舉中模式枚舉值領(lǐng)導(dǎo)者行為典型場(chǎng)景CoordinateTeamMode.coordinate挑選成員、派發(fā)任務(wù)并合成響應(yīng)默認(rèn)監(jiān)督者模式通用編排RouteTeamMode.route只路由給一位專家直接返回該成員的回答專家選擇、語言路由BroadcastTeamMode.broadcast把同一任務(wù)發(fā)給所有成員再合成結(jié)果多視角分析、共識(shí)TasksTeamMode.tasks把目標(biāo)拆解成共享任務(wù)清單按依賴循環(huán)執(zhí)行直到完成復(fù)雜多步工作流、并行執(zhí)行從源碼注釋看route 模式的精確定義是Router pattern. Leader routes to a specialist and returns the members response directly.mode.py對(duì)比 coordinate/broadcast 需要領(lǐng)導(dǎo)者綜合各路答案route 模式不合成——誰的活誰干回答是誰的就原樣給誰。這種零合成分支的設(shè)計(jì)讓它在語言分發(fā)、領(lǐng)域?qū)K?、客服工單分類等?chǎng)景下響應(yīng)路徑最短、語義最不容易被中間層稀釋。底層語義moderoute 在源碼里做了什么選擇modeTeamMode.route并不是一個(gè)黑盒開關(guān)它在 Team 初始化階段_init.py會(huì)被確定性歸一化成一組布爾配置if mode TeamMode.route: team.respond_directly True # 成員回答直接返回不合成 team.delegate_to_all_members False # 不廣播給所有人也就是說 route 模式等價(jià)于只派單 直連返回。源碼里還做了反向歸一化如果你只設(shè)置了respond_directlyTrue而未指定modeTeam 會(huì)被自動(dòng)歸為route模式。這保證了同一套語義無論從哪個(gè)入口配置都不會(huì)互相沖突。而路由任務(wù)本身通過delegate_task_to_member這一團(tuán)隊(duì)工具完成。在 route 模式下系統(tǒng)注入給領(lǐng)導(dǎo)者的提示詞會(huì)明確約束其行為見 _messages.py你工作在 route 模式必須把請(qǐng)求交給恰好一個(gè)成員調(diào)用delegate_task_to_member并把該成員的回答原樣返回給用戶隨后結(jié)束本輪。結(jié)合三個(gè)示例中都會(huì)設(shè)置的show_members_responsesTrue運(yùn)行時(shí)可直觀看到領(lǐng)導(dǎo)者選人 → 成員作答 → 直接透?jìng)鞯耐暾溌?。示例一語言路由01_basic.py第一個(gè)例子把三個(gè)只會(huì)說一種語言的 Agent 裝進(jìn)一個(gè)Language Router團(tuán)隊(duì)領(lǐng)導(dǎo)者先檢測(cè)用戶輸入屬于哪種語言再把問題轉(zhuǎn)給對(duì)應(yīng)的語言專家而不支持的輸入默認(rèn)兜底給英語專家。完整代碼見 01_basic.py核心結(jié)構(gòu)如下from agno.agent import Agent from agno.models.openai import OpenAIResponses from agno.team.mode import TeamMode from agno.team.team import Team # 1. 創(chuàng)建成員每個(gè) 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. 創(chuàng)建 Teammoderoute 是關(guān)鍵 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. 連續(xù)提問分別用英語 / 西班牙語 / 法語 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模式下領(lǐng)導(dǎo)者依然需要自己的model上例復(fù)用同一個(gè)gpt-5.2模型。團(tuán)隊(duì)成員用role自我聲明職責(zé)、用instructions硬約束行為這是保證路由準(zhǔn)確性的第一道防線——例如語言專家必須無論輸入語言是什么都用指定語言回復(fù)即使領(lǐng)導(dǎo)者偶爾派錯(cuò)成員輸出也依然守規(guī)矩。示例二領(lǐng)域?qū)<衣酚?2_specialist_router.py當(dāng)問題不是按語言分類而是按學(xué)科派活時(shí)做法完全一致只是把成員換成數(shù)學(xué)、編程、科學(xué)三個(gè)領(lǐng)域?qū)<?。?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, )這里值得學(xué)習(xí)的是Team 級(jí)instructions的路由規(guī)則表寫法用一行一個(gè)- 類別 - 成員的顯式映射把什么題找誰講清楚。把分發(fā)邏輯寫成確定性規(guī)則比讓領(lǐng)導(dǎo)者自由發(fā)揮更能獲得穩(wěn)定的路由結(jié)果。示例三帶兜底 Agent 的路由03_with_fallback.py真實(shí)場(chǎng)景中并非每個(gè)問題都命中專家。第三個(gè)示例在 SQL 專家、Python 專家之外增加了一個(gè)通用助手General Assistant專門接住不屬于任何專家或拿不準(zhǔn)的問題見 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, )兜底路由之所以高效靠的是兩條寫在規(guī)則里的兜底策略一是Everything else - General Assistant二是When in doubt, route to the General Assistant拿不準(zhǔn)就交給兜底。這兩句話合起來幾乎消滅了無專家可派的分叉死路同時(shí)兜底成員的 instructions 也主動(dòng)聲明即使問 SQL/Python 我也會(huì)盡力回答進(jìn)一步降低路由失敗時(shí)的體驗(yàn)損失。Team 路由編排要點(diǎn)小結(jié)把三個(gè)示例放在一起可以提煉出 route 模式的標(biāo)準(zhǔn)編排配方模式選擇Team(..., modeTeamMode.route)導(dǎo)入路徑from agno.team.mode import TeamMode領(lǐng)導(dǎo)者大腦Team 自帶model負(fù)責(zé)讀懂問題 選人成員畫像每個(gè)成員必須有清晰的name與role領(lǐng)導(dǎo)者正是依據(jù)這些信息判斷把任務(wù)交給誰規(guī)則顯式化在 Team 的instructions中寫明- 條件 - 成員映射表并補(bǔ)充默認(rèn)分支兜底兜死預(yù)留General Assistant之類的通用成員承接未知問題過程可視化show_members_responsesTrue在響應(yīng)中展示每個(gè)成員被選/被派的情況便于調(diào)試路由決策。倉(cāng)庫(kù)父目錄 02_modes/README.md 以對(duì)比表形式列出了四種模式——route 模式適合專家選擇、語言路由這類天然只有一條正確執(zhí)行路徑的任務(wù)如果你的場(chǎng)景需要綜合多方意見或把一個(gè)大目標(biāo)拆成依賴鏈條則應(yīng)分別考慮 coordinate / broadcast / tasks 模式。運(yùn)行方式與環(huán)境說明三個(gè)示例的運(yùn)行方式相同替換腳本文件名即可.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運(yùn)行前提與注意事項(xiàng)示例使用.venvs/demo這一倉(cāng)庫(kù)約定的虛擬環(huán)境解釋器需先按倉(cāng)庫(kù)說明完成依賴安裝agno 本體位于 libs/agno 下示例統(tǒng)一使用OpenAIResponses(idgpt-5.2)運(yùn)行時(shí)需要配置對(duì)應(yīng)的 OpenAI 服務(wù)憑據(jù)環(huán)境變量OPENAI_API_KEYprint_response(..., streamTrue)開啟流式輸出三份腳本都以分隔行如 * 60切分多個(gè)問題以便觀察每次路由結(jié)果每個(gè)if __name__ __main__:塊內(nèi)按順序發(fā)問建議一次只跑一個(gè)腳本逐個(gè)觀察路由行為目錄內(nèi)的 TEST_LOG.md 記錄了這三個(gè)腳本的自動(dòng)化測(cè)試狀態(tài)三個(gè)文件均可完成運(yùn)行Run: completed僅在docstring 下劃線樣式這類靜態(tài)風(fēng)格校驗(yàn)上未通過不影響示例功能本身??偨Y(jié)Route Mode 是 Agno Team 中單點(diǎn)直派的執(zhí)行模式領(lǐng)導(dǎo)者用delegate_task_to_member把請(qǐng)求交給恰好一個(gè)成員成員回答經(jīng)respond_directly語義原樣透?jìng)?、不?jīng)過合成層。在實(shí)現(xiàn)上modeTeamMode.route會(huì)被歸一化為respond_directlyTrue與delegate_to_all_membersFalse源碼證據(jù)從行為模型上保證了只派一人、直連返回。無論是按語言分發(fā)01_basic.py、按學(xué)科派活02_specialist_router.py還是用通用成員兜底03_with_fallback.py核心都是把選人規(guī)則顯式寫進(jìn) Team 的 instructions、把職責(zé)邊界寫進(jìn)成員的 role 與 instructions。掌握這一模式后你就能以極低的編排成本構(gòu)建出專人專事、答即所問的多語言客服、領(lǐng)域問答或工單分發(fā)系統(tǒng)。【免費(fèi)下載鏈接】agnoBuild, run, and manage agent platforms.項(xiàng)目地址: https://gitcode.com/GitHub_Trending/ag/agno創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考