autogen_ext.teams.magentic_one#
- class MagenticOne(client: ChatCompletionClient, hil_mode: bool = False, input_func: Callable[[str], str] | Callable[[str, CancellationToken | None], Awaitable[str]] | None = None, code_executor: CodeExecutor | None = None, approval_func: Callable[[ApprovalRequest], ApprovalResponse] | Callable[[ApprovalRequest], Awaitable[ApprovalResponse]] | None = None)[源代码]#
-
MagenticOne是一个专门的群聊类,它集成了FileSurfer、WebSurfer、Coder和Executor等各种代理,以解决复杂的任务。要了解更多关于Magentic-One背后的科学原理,请参阅完整的博客文章:Magentic-One:一个解决复杂任务的通才多代理系统以及下面的参考文献。
安装
pip install "autogen-ext[magentic-one]"
- 参数:
client (ChatCompletionClient) – 用于模型交互的客户端。
hil_mode (bool) – 可选;如果设置为True,则将UserProxyAgent添加到代理列表中。
input_func (InputFuncType | None) – 可选;在人机协作模式下用于用户输入的函数。
code_executor (CodeExecutor | None) – 可选;要使用的代码执行器。如果为None,将优先使用Docker(如果可用),否则使用本地执行器。
approval_func (ApprovalFuncType | None) – 可选;在运行前批准代码执行的函数。如果为None,代码将无需批准即可执行。
警告
使用Magentic-One涉及与专为人类设计的数字世界互动,这本身就带有固有的风险。为了将这些风险降至最低,请考虑以下预防措施
使用容器:在docker容器中运行所有任务,以隔离代理并防止直接系统攻击。
虚拟环境:使用虚拟环境运行代理,防止它们访问敏感数据。
监控日志:在执行期间和执行后密切监控日志,以检测并减轻危险行为。
人工监督:在人机协作模式下运行示例,以监督代理并防止意外后果。
限制访问:限制代理对互联网和其他资源的访问,以防止未经授权的操作。
保护数据:确保代理无法访问可能被泄露的敏感数据或资源。请勿与代理共享敏感信息。
请注意,代理有时可能会尝试危险操作,例如寻求人类帮助或在没有人类参与的情况下接受cookie协议。始终确保代理受到监控并在受控环境中运行,以防止意外后果。此外,请警惕Magentic-One可能容易受到网页的提示注入攻击。
架构
Magentic-One是一个通才多代理系统,用于解决各种领域的开放式网页和文件任务。它代表着在开发能够完成人们在工作和个人生活中遇到的任务的代理方面迈出了重要一步。
Magentic-One的工作基于多代理架构,其中主要的Orchestrator代理负责高级规划、指导其他代理以及跟踪任务进度。Orchestrator首先制定一个计划来解决任务,并在维护的Task Ledger中收集所需的事实和有根据的猜测。在计划的每一步,Orchestrator都会创建一个Progress Ledger,在该 Ledger 中它会自我反思任务进度并检查任务是否完成。如果任务尚未完成,它会分配Magentic-One的其他代理之一来完成一个子任务。在分配的代理完成其子任务后,Orchestrator会更新Progress Ledger并以这种方式继续,直到任务完成。如果Orchestrator发现进度在足够多的步骤中没有取得进展,它可以更新Task Ledger并创建新计划。
总的来说,Magentic-One包含以下代理
Orchestrator:主要代理,负责任务分解和规划,指导其他代理执行子任务,跟踪总体进度,并根据需要采取纠正措施。
WebSurfer:一个基于LLM的代理,精通控制和管理基于Chromium的网络浏览器的状态。它在浏览器上执行操作并报告网页的新状态。
FileSurfer:一个基于LLM的代理,它控制一个基于markdown的文件预览应用程序,以读取大多数类型的文件。它还可以执行常见的导航任务,例如列出目录内容和导航文件夹结构。
Coder:一个基于LLM的代理,专门用于编写代码、分析从其他代理收集的信息或创建新工件。
ComputerTerminal:为团队提供对控制台shell的访问权限,Coder的程序可以在其中执行,并且可以安装新的编程库。
Magentic-One的代理共同为Orchestrator提供了解决各种开放式问题所需的工具和功能,以及自主适应动态和不断变化的网页和文件系统环境的能力。
示例
# Autonomously complete a coding task: import asyncio from autogen_ext.models.openai import OpenAIChatCompletionClient from autogen_ext.teams.magentic_one import MagenticOne from autogen_agentchat.ui import Console async def example_usage(): client = OpenAIChatCompletionClient(model="gpt-4o") m1 = MagenticOne(client=client) # Uses DockerCommandLineCodeExecutor by default task = "Write a Python script to fetch data from an API." result = await Console(m1.run_stream(task=task)) print(result) if __name__ == "__main__": asyncio.run(example_usage())
# Enable human-in-the-loop mode with explicit Docker executor and code approval import asyncio from autogen_ext.models.openai import OpenAIChatCompletionClient from autogen_ext.teams.magentic_one import MagenticOne from autogen_ext.code_executors.docker import DockerCommandLineCodeExecutor from autogen_agentchat.ui import Console from autogen_agentchat.agents import ApprovalRequest, ApprovalResponse def user_input_func(prompt: str) -> str: """Custom input function for user interaction.""" return input(prompt) def approval_func(request: ApprovalRequest) -> ApprovalResponse: """Simple approval function that requests user input.""" print(f"Code to execute:\n{request.code}") user_input = input("Do you approve this code execution? (y/n): ").strip().lower() if user_input == 'y': return ApprovalResponse(approved=True, reason="User approved the code execution") else: return ApprovalResponse(approved=False, reason="User denied the code execution") async def example_usage_hil(): client = OpenAIChatCompletionClient(model="gpt-4o") # Explicitly specify Docker code executor for better security async with DockerCommandLineCodeExecutor() as code_executor: m1 = MagenticOne( client=client, hil_mode=True, input_func=user_input_func, code_executor=code_executor, approval_func=approval_func ) task = "Write a Python script to fetch data from an API." result = await Console(m1.run_stream(task=task)) print(result) if __name__ == "__main__": asyncio.run(example_usage_hil())
# Enable code execution approval without human-in-the-loop mode import asyncio from autogen_ext.models.openai import OpenAIChatCompletionClient from autogen_ext.teams.magentic_one import MagenticOne from autogen_ext.code_executors.docker import DockerCommandLineCodeExecutor from autogen_agentchat.ui import Console from autogen_agentchat.agents import ApprovalRequest, ApprovalResponse def approval_func(request: ApprovalRequest) -> ApprovalResponse: """Simple approval function that requests user input.""" print(f"Code to execute:\n{request.code}") user_input = input("Do you approve this code execution? (y/n): ").strip().lower() if user_input == 'y': return ApprovalResponse(approved=True, reason="User approved the code execution") else: return ApprovalResponse(approved=False, reason="User denied the code execution") async def example_usage_with_approval(): client = OpenAIChatCompletionClient(model="gpt-4o") # Use approval_func for code approval only (hil_mode=False) async with DockerCommandLineCodeExecutor() as code_executor: m1 = MagenticOne( client=client, hil_mode=False, # No human-in-the-loop for general conversation code_executor=code_executor, approval_func=approval_func # But still ask for code execution approval ) task = "Write a Python script to fetch data from an API." result = await Console(m1.run_stream(task=task)) print(result) if __name__ == "__main__": asyncio.run(example_usage_with_approval())
参考资料
@article{fourney2024magentic, title={Magentic-one: A generalist multi-agent system for solving complex tasks}, author={Fourney, Adam and Bansal, Gagan and Mozannar, Hussein and Tan, Cheng and Salinas, Eduardo and Niedtner, Friederike and Proebsting, Grace and Bassman, Griffin and Gerrits, Jack and Alber, Jacob and others}, journal={arXiv preprint arXiv:2411.04468}, year={2024}, url={https://arxiv.org/abs/2411.04468} }