多個不同的角色的Agent,共同完成一份復雜的工作。由一個統籌管理的智能體,自主規劃多個智能體分別做什么,以及執行的順序。
agent 應該包含的屬性
執行特定任務
根據其角色和目標做出決策
能夠使用工具來實現目標
與其他代理溝通和協作
保留互動記憶
在允許的情況下委派任務
如何協作是關鍵
- 條件自主:通過編碼,順序執行。
- 高度自主:能夠自行決定不同角色Agent的是否要執行,以及執行順序。
主要關注的點:
1、自主性
2、開發人員能夠控制的精度 (可編碼改造)
一、匯總
特性 | AutoGen | agentScope | Swarm | CrewAI | LangGraph |
時間 & star | 2023.05 star 36.4k | 2024.02 star 5.6k | 2024.10 star 17.2k | 2024年 star 22.9k | 2023.01 star 7.5k |
框架類型 | 對話智能體 | 角色智能體 | 角色智能體 | 角色智能體 | 基于圖的智能體 |
自主性 | 高度自主 | 條件自主 | 條件自主 | 高度自主 | 條件自主 |
協作 | 集中式群聊 | 群聊 | 任務傳遞 | 具有角色和目標的自主智能體 | 基于條件的循環圖 |
執行 | 由專用智能體管理 | 任務傳遞 | 動態委托,但可以定義層次化流程 | 所有智能體都可以執行 | |
適用場景 | 原型設計 | 從開發到生產 | 詳細控制場景 |
二、已有多智能體框架
2.1 AutoGen (微軟)
2023.05
star 36.4k
GitHub - microsoft/autogen: A programming framework for agentic AI 🤖 PyPi: autogen-agentchat Discord: https://aka.ms/autogen-discord Office Hour: https://aka.ms/autogen-officehour
AutoGen 專注于對話智能體,提供對話作為多智能體協作的能力。它的設計理念圍繞著模擬小組討論,智能體發送和接收消息以啟動或繼續對話。
AutoGen 是一個用于構建 AI 代理系統的開源框架。它簡化了事件驅動、分布式、可擴展且具有彈性的代理應用程序的創建。它允許您快速構建 AI 代理協作并自主或在人工監督下執行任務的系統。
autoGen 中定義的多智能體的模式
并發:通過傳遞消息給多種智能體。來執行
Concurrent Agents — AutoGen
順序工作流,按順序執行
Sequential Workflow — AutoGen
群聊:多個智能體,還有一個群聊管理器,群聊管理器,決定誰來發言。
群聊是一種設計模式,其中一組代理共享一個共同的消息線程:他們都訂閱并發布同一主題。每個參與代理都專門負責一項特定任務,例如在協作寫作任務中擔任作家、插畫師和編輯。您還可以添加一個代理來代表人類用戶,以便在需要時幫助指導代理。在群聊中,參與者輪流發布消息,并且該過程是連續的——每次只有一個代理在工作。在后臺,輪流順序由群聊管理器代理維護,該代理在收到消息后選擇下一個發言的代理。選擇下一個代理的具體算法可能因您的應用程序要求而異。通常,使用循環算法或具有 LLM 模型的選擇器。群聊可用于將復雜任務動態分解為較小的任務,這些任務可以由具有明確角色的專門代理來處理。還可以將群聊嵌套成層次結構,每個參與者都是一個遞歸群聊。
Group Chat — AutoGen
多智能體辯論
Multi-Agent Debate — AutoGen
2.2 AgentScope
2024.02
star 5.6k
文檔:
AgentScope 文檔 — AgentScope 文檔
實現狼人殺的示例代碼
樣例:狼人殺游戲 — AgentScope 文檔
https://img.alicdn.com/imgextra/i3/O1CN01n2Q2tR1aCFD2gpTdu_!!6000000003293-1-tps-960-482.gif
2.3 Swarm (openAI)
2024.10
star 17.2k
Swarm框架本身并不具備自我感知任務變化的能力。它依賴于開發者定義的智能體(Agent)和交接(Handoff)機制來管理和協調任務。智能體可以封裝指令和工具,并能夠獨立執行任務或與其他智能體協作。當一個智能體遇到無法獨立處理的任務時,它可以通過交接機制將任務傳遞給另一個智能體。這種設計允許智能體之間根據任務需求和能力匹配進行動態調整,確保任務能夠以最高效的方式完成,但這需要開發者預先定義智能體的行為和交接邏輯。因此,Swarm框架的靈活性和適應性取決于開發者如何設計和實現智能體及其交互,而不是框架自身具備感知任務變化的能力。
swarm 集成了langchain的工具
示例:
from swarm import Agent# 定義一個函數用于處理指定商品的退款
def process_refund(item_id, reason="NOT SPECIFIED"):"""
處理商品退款。 參數:
item_id (str): 要退款的商品唯一標識。
reason (str): 退款原因,默認值為 "NOT SPECIFIED"。 返回:
str: 表示退款成功的消息。
"""print(f"[mock] Refunding item {item_id} because {reason}...")return "Success!"# 定義一個函數用于對用戶的購物車應用折扣
def apply_discount():"""
對用戶的購物車應用折扣。 返回:
str: 表示折扣成功應用的消息。
"""print("[mock] Applying discount...")return "Applied discount of 11%"# 創建一個分診代理,用于將用戶請求分配給合適的代理
triage_agent = Agent(
name="Triage Agent",
instructions="判斷哪個代理最適合處理用戶的請求,并將對話轉移到該代理。",
)# 創建一個銷售代理,用于處理與銷售相關的用戶請求
sales_agent = Agent(
name="Sales Agent",
instructions="對銷售蜜蜂表現出極大的熱情。",
)# 創建一個退款代理,用于處理與退款相關的用戶請求
refunds_agent = Agent(
name="Refunds Agent",
instructions=("幫助用戶處理退款。如果退款原因是價格太貴,提供用戶一個退款代碼。""如果用戶堅持,則處理退款。"),
functions=[process_refund, apply_discount],
)# 定義一個函數,如果用戶提到的主題不在當前代理的職責范圍內,則調用該函數將請求轉回分診代理
def transfer_back_to_triage():"""
當用戶的請求超出當前代理的處理范圍時調用此函數,將請求轉回分診代理。 返回:
Agent: 分診代理實例。
"""return triage_agent# 定義一個函數,用于將請求轉移到銷售代理
def transfer_to_sales():"""
將請求轉移到銷售代理。 返回:
Agent: 銷售代理實例。
"""return sales_agent# 定義一個函數,用于將請求轉移到退款代理
def transfer_to_refunds():"""
將請求轉移到退款代理。 返回:
Agent: 退款代理實例。
"""return refunds_agent# 為分診代理添加轉移到銷售和退款代理的功能
triage_agent.functions = [transfer_to_sales, transfer_to_refunds]# 為銷售代理添加轉移回分診代理的功能
sales_agent.functions.append(transfer_back_to_triage)# 為退款代理添加轉移回分診代理的功能
refunds_agent.functions.append(transfer_back_to_triage)
2.4 CrewAI
2024年
star 22.9k
文檔:Introduction - CrewAI
CrewAI 是一個用于協調角色扮演、自主 AI 代理的框架。它使代理能夠無縫協作,通過協作智能處理復雜任務。
crewAI中支持的任務執行順序,順序執行,分層執行
串行執行示例
from crewai import Crew, Process, Agent, Task, TaskOutput, CrewOutput# Define your agents
researcher = Agent(
role='Researcher',
goal='Conduct foundational research',
backstory='An experienced researcher with a passion for uncovering insights'
)
analyst = Agent(
role='Data Analyst',
goal='Analyze research findings',
backstory='A meticulous analyst with a knack for uncovering patterns'
)
writer = Agent(
role='Writer',
goal='Draft the final report',
backstory='A skilled writer with a talent for crafting compelling narratives'
)# Define your tasks
research_task = Task(
description='Gather relevant data...',
agent=researcher,
expected_output='Raw Data'
)
analysis_task = Task(
description='Analyze the data...',
agent=analyst,
expected_output='Data Insights'
)
writing_task = Task(
description='Compose the report...',
agent=writer,
expected_output='Final Report'
)# Form the crew with a sequential process
report_crew = Crew(
agents=[researcher, analyst, writer],# 任務的順序
tasks=[research_task, analysis_task, writing_task],# 任務的執行順序 順序執行
process=Process.sequential
)# Execute the crew
result = report_crew.kickoff()# Accessing the type-safe output
task_output: TaskOutput = result.tasks[0].output
crew_output: CrewOutput = result.output
分層執行示例
from langchain_openai import ChatOpenAI
from crewai import Crew, Process, Agent# Agents are defined with attributes for backstory, cache, and verbose mode
researcher = Agent(
role='Researcher',
goal='Conduct in-depth analysis',
backstory='Experienced data analyst with a knack for uncovering hidden trends.',
cache=True,
verbose=False,
# tools=[] # This can be optionally specified; defaults to an empty list
use_system_prompt=True, # Enable or disable system prompts for this agent
max_rpm=30, # Limit on the number of requests per minute
max_iter=5 # Maximum number of iterations for a final answer
)
writer = Agent(
role='Writer',
goal='Create engaging content',
backstory='Creative writer passionate about storytelling in technical domains.',
cache=True,
verbose=False,
# tools=[] # Optionally specify tools; defaults to an empty list
use_system_prompt=True, # Enable or disable system prompts for this agent
max_rpm=30, # Limit on the number of requests per minute
max_iter=5 # Maximum number of iterations for a final answer
)# Establishing the crew with a hierarchical process and additional configurations
project_crew = Crew(
tasks=[...], # Tasks to be delegated and executed under the manager's supervision
agents=[researcher, writer],
manager_llm=ChatOpenAI(temperature=0, model="gpt-4"), # Mandatory if manager_agent is not set
process=Process.hierarchical, # Specifies the hierarchical management approach
respect_context_window=True, # Enable respect of the context window for tasks
memory=True, # Enable memory usage for enhanced task execution
manager_agent=None, # Optional: explicitly set a specific agent as manager instead of the manager_llm
planning=True, # Enable planning feature for pre-execution strategy
)
三、超級經典的案例-最近火熱的deep-research
二月份初,從openai公布了deep-research開始,deep-research就開始變得非常火熱。其實這個就是一個非常典型的多智能體協作的案例。定義了web查詢智能體、代碼執行智能體、文件檢索智能體、寫作智能體,還有規劃問題的智能體。
auto-deep-research
特點
Auto-Deep-Research 是基于 AutoAgent多智能體框架構建的deep research產品。
評測效果
在通用AI助手評測GAIA中位列全球第三,是開源方案中的最優解。
多智能體框架
https://github.com/HKUDS/AutoAgent
系統采用模塊化多Agent架構,通過Orchestrator Agent動態調度任務
模塊 | 功能 | 技術特性 |
Web Agent | 深度網絡搜索與文獻抓取 | 支持瀏覽器Cookies導入,突破學術資源訪問限制 |
Coding Agent | 代碼生成與優化(Python/R腳本調試、可視化優化、容器化部署) | 實現自動化編程與調試,支持Docker一鍵部署 |
Local File Agent | 多格式文件解析(PDF/CSV/XLSX等12種格式) | 自動轉換為Markdown統一處理,支持200+頁PDF深度解析 |
agent示例
def get_filesurfer_agent(model: str = "gpt-4o", **kwargs):def handle_mm_func(tool_name, tool_args):return f"After using tool {tool_name}({tool_args}), I have opened the image I want to see and prepared a question according to the image. Please answer the question based on the image."def instructions(context_variables):file_env: RequestsMarkdownBrowser = context_variables.get("file_env", None)assert file_env is not None, "file_env is required"return \
f"""
You are a file surfer agent that can handle local files.You can only access the files in the folder `{file_env.docker_workplace}` and when you want to open a file, you should use absolute path from root like `{file_env.docker_workplace}/...`.Note that `open_local_file` can read a file as markdown text and ask questions about it. And `open_local_file` can handle the following file extensions: [".html", ".htm", ".xlsx", ".pptx", ".wav", ".mp3", ".flac", ".pdf", ".docx"], and all other types of text files. But IT DOES NOT HANDLE IMAGES, you should use `visual_question_answering` to see the image. If the converted markdown text has more than 1 page, you can use `page_up`, `page_down`, `find_on_page_ctrl_f`, `find_next` to navigate through the pages.When you think you have completed the task the `System Triage Agent` asked you to do, you should use `transfer_back_to_triage_agent` to transfer the conversation back to the `System Triage Agent`. And you should not stop to try to solve the user's request by transferring to `System Triage Agent` only until the task is completed.If you are unable to open the file, you can transfer the conversation back to the `System Triage Agent`, and let the `Coding Agent` try to solve the problem by coding.
"""tool_list = [open_local_file, page_up_markdown, page_down_markdown, find_on_page_ctrl_f, find_next, visual_question_answering]return Agent(name="File Surfer Agent",model=model, instructions=instructions,functions=tool_list,handle_mm_func=handle_mm_func,tool_choice = "required", parallel_tool_calls = False)
優勢
在多智能體框架下,有非常好的擴展性,只需要定義和實現一些具有特色功能的agent,就能擴展功能。