基于MetaGPT的LLM Agent学习实战(一)

前言

我最近一直在做基于AI Agent 的个人项目, 因为工作加班较多,设计思考时间不足,这里借着Datawhale的开源学习课程《MetaGPT智能体理论与实战》课程,来完善自己的思路,抛砖引玉,和各位开发者一起学习!

一、介绍

今天是打卡的第一天,先说说主要的学习内容:

  • 获取MetaGPT
    • 部署到本地环境
  • 配置MetaGPT
    • 申请ChatGPT API Key
    • 基于ChatGPT API构建调用代码
  • 运行MetaGPT案例代码进行测试

今天学习的内容较为简单,我会尽量以简洁的语言详细描述清楚这个流程,带着读者一起学习Agent开发;

二、配置MetaGPT运行环境

声明
  • python版本为3.9+
  • 为了方便学习,这里我使用jupyter notebook进行讲解;
  • 所有代码我都会同步提交到Github和Gitee ,如果各位读者觉得我写的不错,可以给我一个Star.
1. 查看Python版本

为了确保我们的Python环境正确,首先要检查Python的版本。可以使用以下命令来查看Python版本:

bash 复制代码
!python3 --version

如果上面的命令不起作用或者报错,可以尝试使用以下命令:

bash 复制代码
python --version

输出

bash 复制代码
Python 3.10.13
2. 安装MetaGPT

要安装MetaGPT,我们可以使用pip来获取它。以下是在终端中安装MetaGPT的命令:

bash 复制代码
pip install metagpt==0.6.6

如果你在国内环境,并且希望加速安装过程,可以使用清华源进行按照:

bash 复制代码
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple metagpt==0.6.6

也可以通过拉取官方仓库进行安装:

bash 复制代码
git clone https://github.com/geekan/MetaGPT.git
cd /your/path/to/MetaGPT
pip install -e .

这里有个重点,如果你的OpenAI API Key是直连且不限速版本,你只需要安装包即可,

如果你的API Key为免费API且有速率限制,我这里建议你直接clone MetaGPT的GitHub仓库,其可以在config2.yaml中自定义配置代理服务器和Key,我在运行MetaGPT的过程中遇到的最大问题就是API限速导致程序报错;所以一定要注意这一点;

作者因为使用的是中转的API Key,因此选择了方法3:

bash 复制代码
git clone https://github.com/geekan/MetaGPT.git
cd MetaGPT
pip install -e .

我们在config/config2.yaml中配置自己的api key和 base_url 以及选择的model:

yaml 复制代码
llm:
  api_type: "openai"  # or azure / ollama / groq etc.
  model: "gpt-4-turbo"  # or gpt-3.5-turbo
  base_url: "https://api.openai.com/v1"  # or forward url / other llm url
  api_key: "YOUR_API_KEY"
3. 配置MetaGPT

为了配置MetaGPT,你需要调用ChatGPT API服务。你可以在这里查看具体配置方式。如果你没有科学环境,也可以通过去tb buy 一个 中转的 API Key来实现。我们主要介绍官方申请方法:

中转方案修改的部分我在代码中也已经标出

① 登录自己的账号

②创建API Key


③本地配置环境变量

python 复制代码
import os
os.environ["OPENAI_API_KEY"] = "sk-..."  # 填入你自己的OpenAI API key
os.environ["OPENAI_API_MODEL"] = "gpt-3.5-turbo" # 选择你要使用的模型,例如:gpt-4, gpt-3.5-turbo
os.environ["OPENAI_API_BASE"] = "https://api.openai-forward.com/v1" # 调整API请求地址,设置访问中转代理服务器,如果是商家购买的,可以联系商家要代理服务器地址,这里并不是固定的

④验证配置是否成功:

python 复制代码
from openai import OpenAI

# client = OpenAI(api_key='sk-......') # 官网直连版本
client = OpenAI(base_url="https://xxxx.com", # 这里填写你的中转服务器地址
    api_key='sk-......') # 这里填写你的中转apikey
completion = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system", "content": "你是一个WebGIS开发者,测绘地理和全栈开发精通."},
    {"role": "user", "content": "聊聊国内外WebGIS开发与AI结合的场景现在我们可以聊聊国内外WebGIS开发与AI LLM Agent结合的场景吧"}
  ]
)
print(completion.choices[0].message.content)

运行结果如下:

🎉🎉🎉bingo!!运行成功,我们成功拿到了我们要的方案!

通过以上步骤,我们终于成功配置MetaGPT,并开始使用它进行各种任务了。

三. 使用MetaGPT

接下来,我们通过下面这个案例,我们用以验证环境配置是否成功,并初次体验多智能体框架中的指令 - 动作 - 角色 - 环境 - 团队的抽象概念。在这个示例中,我们创建了一个团队,其中包括产品经理、架构师、项目经理和工程师。然后,我们投资并运行一个项目,最后让团队运行五轮。

python 复制代码
import asyncio
from metagpt.roles import (
    Architect,
    Engineer,
    ProductManager,
    ProjectManager,
)
from metagpt.team import Team

async def startup(idea: str):
    company = Team()
    company.hire(
        [
            ProductManager(),
            Architect(),
            ProjectManager(),
            Engineer(),
        ]
    )
    company.invest(investment=3.0)
    company.run_project(idea=idea)

    await company.run(n_round=5)

await startup(idea="write a cli blackjack game")

这里我copy了其中几轮Agent的回答,可以看到我们的AI团队已经运行起来了;

bash 复制代码
[CONTENT]
{
    "Language": "en_us",
    "Programming Language": "Python",
    "Original Requirements": "write a cli blackjack game",
    "Project Name": "cli_blackjack_game",
    "Product Goals": [
        "Create an engaging and interactive gameplay experience",
        "Ensure smooth and intuitive user interface for seamless gameplay",
        "Implement various difficulty levels to cater to different player skills"
    ],
    "User Stories": [
        "As a player, I want to be able to start a new game easily",
        "As a player, I want to see my current score and progress during the game",
        "As a player, I want to have options to hit, stand, or double down during my turn",
        "As a player, I want to receive clear instructions on how to play the game",
        "As a player, I want to feel the excitement and challenge of a real blackjack game"
    ],
    "Competitive Analysis": [
        "Blackjack Game A: Basic interface, lacks interactive features",
        "Blackjack Pro: Offers advanced gameplay options and strategy guides",
        "Blackjack Master: Provides a realistic casino experience with multiplayer mode"
    ],
    "Competitive Quadrant Chart": "quadrantChart\n    title \"Engagement and User Experience\"\n    x-axis \"Low Engagement\" --> \"High Engagement\"\n    y-axis \"Low User Experience\" --> \"High User Experience\"\n    quadrant-1 \"Enhance Features\"\n    quadrant-2 \"Improve User Experience\"\n    quadrant-3 \"Optimize Engagement\"\n    quadrant-4 \"Maximize User Satisfaction\"\n    \"Blackjack Game A\": [0.3, 0.4]\n    \"Blackjack Pro\": [0.6, 0.7]\n    \"Blackjack Master\": [0.8, 0.9]\n    \"Our CLI Blackjack Game\": [0.5, 0.6]",
    "Requirement Analysis": "",
    "Requirement Pool": [
        [
            "P0",
            "Implement basic game logic for blackjack"
        ],
        [
            "P1",
            "Create a scoring system to track player progress"
        ],
        [
            "P2",
            "Develop a user-friendly interface for easy navigation"
        ],
        [
            "P2",
            "Incorporate different difficulty levels for player choice"
        ],
        [
            "P1",
            "Include clear instructions on how to play the game"
        ]
    ],
    "UI Design draft": "The UI will include options for hitting, standing, and doubling down. It will display the player's current score and provide clear instructions for gameplay.",
    "Anything
2024-05-12 17:36:48.720 | ERROR    | metagpt.utils.common:log_it:554 - Finished call to 'metagpt.actions.action_node.ActionNode._aask_v1' after 10.724(s), this was the 1st time calling it. exp: openai.types.completion_usage.CompletionUsage() argument after ** must be a mapping, not NoneType
 UNCLEAR": ""
}
[/CONTENT][CONTENT]
{
    "Language": "en_us",
    "Programming Language": "Python",
    "Original Requirements": "write a cli blackjack game",
    "Project Name": "cli_blackjack_game",
    "Product Goals": [
        "Create an engaging CLI experience for users",
        "Ensure smooth gameplay and fair card dealing logic",
        "Provide an enjoyable and interactive blackjack game"
    ],
    "User Stories": [
        "As a player, I want to be able to place bets and receive cards",
        "As a player, I want to have options like hit, stand, double down",
        "As a player, I want to see my current balance and game outcome"
    ],
    "Competitive Analysis": [
        "Blackjack Game A: Basic CLI interface, lacks interactive features",
        "cli-blackjack.io: Offers various betting options and clear game instructions",
        "blackjack-cli.com: Provides realistic card dealing but lacks betting flexibility"
    ],
    "Competitive Quadrant Chart": "quadrantChart\n    title \"Engagement and User Experience\"\n    x-axis \"Low Engagement\" --> \"High Engagement\"\n    y-axis \"Low User Experience\" --> \"High User Experience\"\n    quadrant-1 \"Enhance Features\"\n    quadrant-2 \"Improve User Experience\"\n    quadrant-3 \"Optimize Engagement\"\n    quadrant-4 \"Maintain Quality\"\n    \"Blackjack Game A\": [0.3, 0.6]\n    \"cli-blackjack.io\": [0.45, 0.23]\n    \"blackjack-cli.com\": [0.57, 0.69]\n    \"Our CLI Blackjack Game\": [0.5, 0.6]",
    "Requirement Analysis": "",
    "Requirement Pool": [
        [
            "P0",
            "Implement card dealing and betting system"
        ],
        [
            "P1",
            "Include game logic for hit, stand, and double down actions"
        ],
        [
            "P2",
            "Display player balance and game outcomes"
        ]
    ],
    "UI Design draft": "Simple text-based interface with clear instructions and game status
2024-05-12 17:36:57.136 | ERROR    | metagpt.utils.common:log_it:554 - Finished call to 'metagpt.actions.action_node.ActionNode._aask_v1' after 19.140(s), this was the 2nd time calling it. exp: openai.types.completion_usage.CompletionUsage() argument after ** must be a mapping, not NoneType
 updates.",
    "Anything UNCLEAR": ""
}
[/CONTENT]

通过以上步骤,我们可以开始使用MetaGPT进行各种任务,并看到AI Agent的强大潜力!

四、总结

本文是这个打卡系列的第一篇文章,也是后续学习的基础,通过这篇文章,我们了解了MetaGPT开发的基础环境配置方法,在下一篇文章中,我们将深入理解AI Agent的理论,并通过代码来实现Agent的每个模块需求,希望我的文章对各位读者和开发者有所帮助!期待后续学习!!😀😀😀

参考文献

项目地址


如果觉得我的文章对您有帮助,三连+关注便是对我创作的最大鼓励!或者一个star🌟也可以😂.

相关推荐
星幻元宇VR2 分钟前
消防数字展厅智能升级|AR消防巡检员体验系统
学习·安全·ar·虚拟现实
木卫二号Coding9 分钟前
第七十七篇-V100+llama-cpp-python-server+Qwen3-30B+GGUF
开发语言·python·llama
半夏知半秋10 分钟前
lua5.5版本新特性学习
开发语言·笔记·学习
木卫二号Coding10 分钟前
第七十六篇-V100+llama-cpp-python+Qwen3-30B+GGUF
开发语言·python·llama
-To be number.wan10 分钟前
为什么 pyecharts 在 Jupyter Notebook 里显示空白?
ide·python·jupyter·数据分析
zhang61839910 分钟前
Linux中不同服务器之间迁移python 虚拟环境-conda-pack
linux·运维·python
AI量化价值投资入门到精通11 分钟前
数据清洗:大数据领域的必备技能
大数据·开发语言·ai·php
阿杰学AI13 分钟前
AI核心知识83——大语言模型之 AI伦理审查员(简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·安全性测试·ai伦理审查员
User_芊芊君子13 分钟前
2026 AI Agent 风口必看|四大技术变革+多Agent实战
人工智能·microsoft·ai·ai agent
好奇龙猫13 分钟前
【大学院-筆記試験練習:线性代数和数据结构(24)】
学习