基于MetaGPT的智能体理论与实践-Task01

Task01: MetaGPT环境配置

学习教程:https://github.com/datawhalechina/hugging-multi-agent

1 环境准备

1.1 安装python3.9+

通过:python3 --version, 查看此python版本为3.10.3

1.2 下载MetaGPT

开始,借用清华镜像,拉取metagpt==0.6.6,失败。

然后直接用pip install metagpt==0.6.6 进行下载与安装。

1.3 获取MetaGPT仓库源码

首先git clone命令获取源码

bash 复制代码
git clone https://github.com/geekan/MetaGPT.git

然后进入 MetaGPT 目录

bash 复制代码
cd MetaGPT/

最后安装该仓库环境依赖

bash 复制代码
pip install -e .

2 配置MetaGPT

下面使用ZHIPUAI为例,来MetaGPT

首先,需要在https://open.bigmodel.cn/ 获取智谱的api_key。

然后使用config.yaml文件进行配置。

在MetaGPT/config/ 文件下,创建config.yaml文件,然后在文件中,添加如下配置信息。

bash 复制代码
llm:
  api_type: "zhipuai"  
  model: "glm-3-turbo"  
  base_url: "https://open.bigmodel.cn/api/paas/v4/chat/completions"  
  api_key: "your api_key"

3 DEMO测试

异步相关的代码在ipython或者notebook环境下,asyncio.run(xxx)得改成await xxx

eg:asyncio.run(main())需要改成await main()

代码中创建了两个角色,分别代表民主党候选人Alex和共和党候选人Bob。他们将在一个名为"US election live broadcast"的环境中进行对话。您的代码还定义了两个动作,分别是"AlexSay"和"BobSay",以及一个团队,其中包括了这两个角色。目标是模拟两位候选人在直播环境中就气候变化这一话题进行对话。这将有助于模拟候选人在现实选举中的表现和对话。

python 复制代码
import asyncio

from metagpt.actions import Action
from metagpt.environment import Environment
from metagpt.roles import Role
from metagpt.team import Team

action1 = Action(name="AlexSay", instruction="Express your opinion with emotion and don't repeat it")
action2 = Action(name="BobSay", instruction="Express your opinion with emotion and don't repeat it")
alex = Role(name="Alex", profile="Democratic candidate", goal="Win the election", actions=[action1], watch=[action2])
bob = Role(name="Bob", profile="Republican candidate", goal="Win the election", actions=[action2], watch=[action1])
env = Environment(desc="US election live broadcast")
team = Team(investment=10.0, env=env, roles=[alex, bob])

asyncio.run(team.run(idea="Topic: climate change. Under 80 words per message.", send_to="Alex", n_round=5))

由于,这里使用的notebook,所以把上面代码最后一行改成:

python 复制代码
await team.run(idea="Topic: climate change. Under 80 words per message.", send_to="Alex", n_round=5)
相关推荐
Coffeeee2 分钟前
帮你快速理解AI Agent之我想招个Android实习生
android·人工智能·agent
新新技术迷9 分钟前
AI聊天自动跟随滚动,附回到底部按钮
人工智能
先锋部队9 分钟前
用Web Worker解析AI返回的大文本不卡UI
人工智能
把你拉进白名单13 分钟前
8.OpenClaw源码解析——三层洋葱重试
人工智能·llm·agent
用户6324150317815 分钟前
拖文档进AI对话框解析,前端要处理哪些脏活
人工智能
姗姗来迟了22 分钟前
AI回答里的引用来源卡片,前端怎么做
人工智能
用户71062077334023 分钟前
Codex-端口配置错误排查案例(stream disconnected before completion)
人工智能
IT_陈寒1 小时前
JavaScript的默认参数挖坑实录,我掉进去了
前端·人工智能·后端
米小虾2 小时前
多Agent系统编排详解:从架构设计到代码实现
人工智能·agent
米小虾2 小时前
多Agent系统的编排:架构、协议与企业级应用
人工智能·agent