🚀3分钟复刻Manus智能体!AutoGen+MCP Server+Cline构建最强AI智能体,支持ollama!轻松实现网络搜索+文件操作的AI Agen

🚀🚀🚀本篇笔记所对应的视频 www.bilibili.com/video/BV119... Manus是中国科技团队最近推出的AI智能体系统,但是这个智能体并不开源。

而且很多小伙伴都没有体验到Manus。

所以我们将使用开源方案来复刻Manus。

使用AutoGen+MCP Server打造超越Manus的智能体具有以下优势:

  1. 开放性:AutoGen是开源框架,允许更灵活的定制和扩展。
  2. 模块化架构:AutoGen采用分层设计,便于开发者根据需求选择合适的抽象层级。
  3. 多代理协作:AutoGen专门设计用于构建多代理AI应用,支持复杂的代理间交互。
  4. 代码生成与执行:AutoGen强调代码生成和执行能力,增强了代理的表达能力和适应性。
  5. 人机协作:支持人类干预模式,可在自主工作流程中融入人类反馈。
  6. MCP集成:结合Model Control Protocol,可实现更精细的模型控制和上下文管理。
  7. 跨平台支持:AutoGen支持多种编程语言和运行环境,增强了部署灵活性。

通过结合AutoGen的强大框架和MCP的精细控制,开发者有潜力创建出在自主性、适应性和协作能力上超越Manus的智能体系统。

安装

sql 复制代码
python3 -m venv venv
source venv/bin/activate

pip install "autogen-ext[openai,mcp]" autogen-agentchat mcp-server-fetch -U

pip install "autogen-ext[openai]"
pip install autogen-agentchat
pip install mcp-server-fetch autogen-ext[mcp]
pip install mcp-server-fetch
pip install -U "autogen-ext[mcp]"

ollama run phi4-mini

代码

ini 复制代码
# 代码1
import asyncio
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import TextMentionTermination
from autogen_core import CancellationToken
from autogen_ext.tools.mcp import StdioServerParams, mcp_server_tools

async def main():
    # Setup the MCP fetch server parameters
    fetch_mcp_server = StdioServerParams(command="node", args=["mcp-server-fetch"])
    
    # Get the fetch tool from the MCP server
    tools = await mcp_server_tools(fetch_mcp_server)
    
    # Create fetch agent with the MCP fetch tool
    fetch_agent = AssistantAgent(
        name="content_fetcher",
        model_client=OpenAIChatCompletionClient(model="gpt-4o-mini"),
        tools=tools,  # The MCP fetch tool will be included here
        system_message="你是一个网页内容获取助手。使用fetch工具获取网页内容。"
    )
    
    # Create rewriter Agent (unchanged)
    rewriter_agent = AssistantAgent(
        name="content_rewriter",
        model_client=OpenAIChatCompletionClient(model="gpt-4o-mini"),
        system_message="""你是一个内容改写专家。将提供给你的网页内容改写为科技资讯风格的文章。
        科技资讯风格特点:
        1. 标题简洁醒目
        2. 开头直接点明主题
        3. 内容客观准确但生动有趣
        4. 使用专业术语但解释清晰
        5. 段落简短,重点突出
        
        当你完成改写后,回复TERMINATE。"""
    )
    
    # Set up termination condition and team (unchanged)
    termination = TextMentionTermination("TERMINATE")
    team = RoundRobinGroupChat([fetch_agent, rewriter_agent], termination_condition=termination)
    
    # Run the workflow (unchanged)
    result = await team.run(
        task="获取https://www.aivi.fyi/llms/introduce-Claude-3.7-Sonnet的内容,然后将其改写为科技资讯风格的文章",
        cancellation_token=CancellationToken()
    )
    
    print("\n最终改写结果:\n")
    print(result.messages[-1].content)
    return result

# This is the correct way to run async code in a Python script
if __name__ == "__main__":
    asyncio.run(main())
    
    
    
 # 代码2
import asyncio
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import TextMentionTermination
from autogen_core import CancellationToken
from autogen_ext.tools.mcp import StdioServerParams, mcp_server_tools
from autogen_agentchat.ui import Console

async def main():
    # Setup the MCP fetch server parameters
    fetch_mcp_server = StdioServerParams(command="uvx", args=["mcp-server-fetch"])
    
    # Get the fetch tool from the MCP server
    tools = await mcp_server_tools(fetch_mcp_server)
    
    # Create fetch agent with the MCP fetch tool
    fetch_agent = AssistantAgent(
        name="content_fetcher",
        model_client=OpenAIChatCompletionClient(model="gpt-4o"),
        tools=tools,
        system_message="你是一个网页内容获取助手。使用fetch工具获取网页内容。"
    )
    
    # Create rewriter Agent
    rewriter_agent = AssistantAgent(
        name="content_rewriter",
        model_client=OpenAIChatCompletionClient(model="gpt-4o"),
        system_message="""你是一个内容改写专家。将提供给你的网页内容改写为科技资讯风格的文章。
        科技资讯风格特点:
        1. 标题简洁醒目
        2. 开头直接点明主题
        3. 内容客观准确但生动有趣
        4. 使用专业术语但解释清晰
        5. 段落简短,重点突出
        
        当你完成改写后,回复TERMINATE。"""
    )
    
    # Set up termination condition and team
    termination = TextMentionTermination("TERMINATE")
    team = RoundRobinGroupChat([fetch_agent, rewriter_agent], termination_condition=termination)
    
    task = "获取https://www.aivi.fyi/llms/introduce-Claude-3.7-Sonnet的内容,然后将其改写为科技资讯风格的文章"
    
    # 显示对话过程
    stream = team.run_stream(task=task, cancellation_token=CancellationToken())
    await Console(stream)
    
    # 获取最终结果(使用单独的run调用)
    result = await team.run(task=task, cancellation_token=CancellationToken())
    
    print("\n最终改写结果:\n")
    print(result.messages[-1].content)
    
    return result

# 在Python脚本中运行异步代码的正确方式
if __name__ == "__main__":
    asyncio.run(main())
    
    
    
# 代码3
import asyncio
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import TextMentionTermination
from autogen_core import CancellationToken
from autogen_ext.tools.mcp import StdioServerParams, mcp_server_tools
from datetime import datetime

async def main():

    # 设置MCP fetch服务器参数
    # 这个服务器用于获取网页内容
    fetch_mcp_server = StdioServerParams(command="uvx", args=["mcp-server-fetch"])

    # 设置MCP文件系统服务器参数
    # 这个服务器用于写入本地文件
    write_mcp_server = StdioServerParams(command="npx", args=["-y", "@modelcontextprotocol/server-filesystem", "/Users/charlesqin/Code-Project/test-autogen"])
    
    # 从MCP服务器获取fetch工具
    tools_fetch = await mcp_server_tools(fetch_mcp_server)

    # 从MCP服务器获取filesystem工具
    tools_write = await mcp_server_tools(write_mcp_server)
    
    # 创建内容获取代理
    # 这个代理负责获取网页内容
    fetch_agent = AssistantAgent(
        name="content_fetcher",
        model_client=OpenAIChatCompletionClient(model="gpt-4o-mini"),
        tools=tools_fetch,
        system_message="你是一个网页内容获取助手。使用fetch工具获取网页内容。获取成功后请传递给content_rewriter。"
    )
    
    # 创建内容改写代理
    # 这个代理负责将网页内容改写为科技资讯风格
    # 注意:不再在完成时添加TERMINATE,而是将内容传递给下一个代理    
    rewriter_agent = AssistantAgent(
        name="content_rewriter",
        model_client=OpenAIChatCompletionClient(model="gpt-4o-mini"),
        system_message="""你是一个内容改写专家。将提供给你的网页内容改写为科技资讯风格的文章。
        科技资讯风格特点:
        1. 标题简洁醒目
        2. 开头直接点明主题
        3. 内容客观准确但生动有趣
        4. 使用专业术语但解释清晰
        5. 段落简短,重点突出
        
        当你完成改写后,请将内容传递给content_writer代理,让它将你的改写内容写入到文件中。"""
    )
    
    # 获取当前日期并格式化为YYYY-MM-DD
    current_date = datetime.now().strftime('%Y-%m-%d')

    # 创建文件写入代理
    # 这个代理负责将改写后的内容写入本地文件
    # 注意:这个代理会在完成任务后添加TERMINATE来结束对话
    write_agent = AssistantAgent(
        name="content_writer",
        model_client=OpenAIChatCompletionClient(model="gpt-4o-mini"),
        tools=tools_write,
        system_message="""你是一个文件助手。使用filesystem工具将content_rewriter提供的内容写入txt文件,文件以日期命名(格式为{current_date}.txt)。
        当你成功将文件写入后,回复"TERMINATE"以结束对话。"""
    )
    
    # 设置终止条件和团队
    # 当任何代理回复TERMINATE时,对话将结束
    termination = TextMentionTermination("TERMINATE")
    team = RoundRobinGroupChat([fetch_agent, rewriter_agent, write_agent], termination_condition=termination)
    
    task = "获取https://www.aivi.fyi/llms/introduce-Claude-3.7-Sonnet的内容,然后将其改写为科技资讯风格的文章,然后将改写的文章写入本地txt文件"
    
    # 只执行一次任务,使用run方法
    result = await team.run(task=task, cancellation_token=CancellationToken())
    
    # 遍历并打印所有消息,以显示整个过程
    print("\n整个对话过程:\n")
    print("-" * 60)
    
    for i, msg in enumerate(result.messages):
        # 判断消息的类型并相应地打印
        if hasattr(msg, 'source') and hasattr(msg, 'content'):
            print(f"\n---------- {msg.source} ----------")
            print(msg.content)
        elif hasattr(msg, 'source') and hasattr(msg, 'content') and isinstance(msg.content, list):
            print(f"\n---------- {msg.source} (工具调用) ----------")
            for item in msg.content:
                print(item)
        else:
            print(f"\n[消息 {i+1}] (类型: {type(msg).__name__})")
            print(msg)
        
        print("-" * 60)
    
    # 打印最终改写结果
    print("\n最终改写结果:\n")
    final_message = result.messages[-1]
    if hasattr(final_message, 'content'):
        print(final_message.content)
    
    return result

# 在Python脚本中运行异步代码的正确方式
if __name__ == "__main__":
    asyncio.run(main())
    
相关推荐
AiTop1001 小时前
腾讯推出AI CLI工具CodeBuddy,国内首家同时支持插件、IDE和CLI三种形态的AI编程工具厂商
ide·人工智能·ai·aigc·ai编程
伊玛目的门徒1 小时前
告别 OpenAI SDK:如何使用 Python requests 库调用大模型 API(例如百度的ernie-4.5-turbo)
python·openai·requests·大模型调用·ernie-4.5-turbo
老马啊老马2 小时前
NestJS 构建 AI 流式聊天服务:从 0 到 1 实现实时对话功能
aigc
安思派Anspire3 小时前
你不需要 GraphRAG!构建一个多策略图谱思维(Multi-Strategy Graph Thinking)AI Agent
aigc·openai·agent
阑梦清川4 小时前
mulerun上线了一个重磅级的智能体--educard generator(知识可视化)
aigc
Zhikes4 小时前
MOSS-TTSD: 文本对话式语音克隆
aigc
饼干哥哥4 小时前
Vibe Marketing|用Claude code+kimi k2爬取了120个 b站视频做内容选题数据分析
数据分析·aigc
用户5191495848455 小时前
揭秘LedgerCTF的AES白盒挑战:逆向工程与密码学分析
人工智能·aigc
用户5191495848455 小时前
SonicWall防火墙安全态势深度分析:固件解密与漏洞洞察
人工智能·aigc
沧海一粟青草喂马5 小时前
对口型视频怎么制作?从脚本到成片的全流程解析
aigc