【技术前沿】MetaGPT入门安装部署——用多个大语言模型解决任务!一键安装,只需填写OpenAI API

项目简介

MetaGPT 是一个多智能体框架,旨在构建全球首家 “AI 软件公司”。该项目通过为 GPT 分配不同的角色,模拟产品经理、架构师、工程师等职业,协同完成复杂的软件开发任务。MetaGPT 将一个简单的需求转化为完整的软件开发流程,包括用户故事、需求分析、数据结构设计和 API 文档生成等。其核心理念是将标准操作程序(SOP)应用于由多智能体组成的团队,使得自然语言编程成为可能。

项目地址:MetaGPT on GitHub


由于项目是英文的,博主在这里做一版中文教程,给后来人一些参考。
关注CSDN心若为城,获得计算机领域与人工智能领域的前沿技术。
博主碎碎念,可跳过:
打算重新做做自己这个老号,高中时候开始做CSDN,那会儿写的是NOIP/NOI相关的算法东西,纯粹是写给自己看的;现在时隔多年,我也在清华站稳了脚跟,在互联网开发和量化交易领域都算是小有成就了。
接下来这个号(也许也不止这个号)应该会做三个方向:
AI新技术(或者不局限于AI)的抢先浏览,会向大家说明当下热点论文、热点技术的部署等,以及做一些周报或者日报。(类似于AI Weekly)
量化交易相关,我在量化开发技术栈有着多年的开发经验,也拿过一些投资比赛的奖项。可以面向应届生给出就业规划,提供一些指导的同时分享一些含金量高的项目。
互联网面试相关,我应该会着重于分享一些面试的底层技术面,并且尽可能和2进行一些结合,让大家同时能handle住两边的技术。

在这里插入图片描述

安装说明

只需要一行!简单明了:

pip install --upgrade metagpt
# or `pip install --upgrade git+https://github.com/geekan/MetaGPT.git`
# or `git clone https://github.com/geekan/MetaGPT && cd MetaGPT && pip install --upgrade -e .`

然后运行下面的命令

# Check https://docs.deepwisdom.ai/main/en/guide/get_started/configuration.html for more details
metagpt --init-config  # it will create ~/.metagpt/config2.yaml, just modify it to your needs

这样会生成一个config文件,我们可以通过修改config文件来部署MetaGPT。

llm:
  api_type: "openai"  # or azure / ollama / groq etc. Check LLMType for more options
  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"

MetaGPT 支持一系列 LLM 模型。根据需要配置模型 API 密钥。
也可以配置Claude:

llm:
  api_type: 'claude' # or anthropic
  base_url: 'https://api.anthropic.com'
  api_key: 'YOUR_API_KEY'
  model: 'claude-3-opus-20240229'

额外工具的使用

除了 LLM 之外,我们还经常希望代理使用工具。这里将介绍这些工具的设置。

## Supported api_type: serpapi/google/serper/ddg
## serper: Visit https://serper.dev/ to get key.
## serpapi: Visit https://serpapi.com/ to get key.
## google: Visit https://console.cloud.google.com/apis/credentials to get key.
## ddg: it is free, no need to get key.
search:
  api_type: 'google' # serpapi/google/serper/ddg
  api_key: 'YOUR_API_KEY'
  cse_id: 'YOUR_CSE_ID' # only for google
 params:
   engine: google # google/bing/yahoo/baidu/yandex, check https://serpapi.com/bing-search-api for more details
   google_domain: 'google.com'
   gl: us
   hl: en

使用MetaGPT、导入已有的角色

我们可以用下面的代码来导入一个产品经理。
具体更复杂的用法,可以参考这个文档:Tutorials

import asyncio

from metagpt.context import Context
from metagpt.roles.product_manager import ProductManager
from metagpt.logs import logger

async def main():
    msg = "Write a PRD for a snake game"
    context = Context()  # The session Context object is explicitly created, and the Role object implicitly shares it automatically with its own Action object
    role = ProductManager(context=context)
    while msg:
        msg = await role.run(msg)
        logger.info(str(msg))

if __name__ == '__main__':
    asyncio.run(main())

使用MetaGPT进行数据分析与可视化

在这里,官方给了一些数据可视化的官方代码。

import asyncio
from metagpt.logs import logger
from metagpt.roles.di.data_interpreter import DataInterpreter
from metagpt.utils.recovery_util import save_history

async def main(requirement: str = ""):

    di = DataInterpreter()
    rsp = await di.run(requirement)
    logger.info(rsp)
    save_history(role=di)


if __name__ == "__main__":

    requirement = "Run data analysis on sklearn Iris dataset, include a plot"
    asyncio.run(main(requirement))

执行上述代码后,生成的计划和代码将分别保存在 data/output/current_time/plan.jsondata/output/current_time/code.ipynb 中。

执行结果

DataInterpreter 提出了以下解决方案任务:

[
  {
    "task_id": "1",
    "dependent_task_ids": [],
    "instruction": "Load the Iris dataset from sklearn."
  },
  {
    "task_id": "2",
    "dependent_task_ids": ["1"],
    "instruction": "Perform exploratory data analysis on the Iris dataset."
  },
  {
    "task_id": "3",
    "dependent_task_ids": ["2"],
    "instruction": "Create a plot visualizing the Iris dataset features."
  }
]

DataInterpreter 能够将问题划分为逻辑任务,并按照加载数据、分析数据和绘制图表的步骤运行。

DataInterpreter 写入以下代码:

# ----------------------------------task1------------------------------------
from sklearn.datasets import load_iris
iris_data = load_iris()
iris_data.keys()
!pip install scikit-learn
from sklearn.datasets import load_iris
iris_data = load_iris()
iris_data.keys()
# ----------------------------------task2------------------------------------
import pandas as pd

# Create a DataFrame from the iris dataset
iris_df = pd.DataFrame(iris_data['data'], columns=iris_data['feature_names'])
iris_df['species'] = pd.Categorical.from_codes(iris_data['target'], iris_data['target_names'])

# Summary statistics
summary_statistics = iris_df.describe()

# Check for missing values
missing_values = iris_df.isnull().sum()

(summary_statistics, missing_values)
# ----------------------------------task3------------------------------------
import matplotlib.pyplot as plt
import seaborn as sns

# Use seaborn's pairplot to visualize the dataset features
sns.set(style='whitegrid', context='notebook')
iris_pairplot = sns.pairplot(iris_df, hue='species', height=2.5)
plt.show()

在完成任务 1 的过程中,由于环境中没有安装 scikit-learn,第一次执行时发生了错误。不过, DataInterpreter 可以通过安装 scikit-learn 来分析并解决这个问题。在任务 3 中, DataInterpreter 使用 seaborn 中的 pairplot 函数创建散点图矩阵,该矩阵可视化数据集中不同特征之间的关系,并使用颜色区分不同物种的数据点。最后,使用 plt.show() 显示图表。
下图是 DataInterpreter 运行代码后绘制的图表。很明显,代码成功执行并生成了漂亮的可视化表格,可以帮助我们更有效地分析数据集的特征。
在这里插入图片描述希望查看更多内容,点这里进入官方文档查看。

总结

MetaGPT is all you need!

  • 18
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值