大家好,MetaGPT 是基于大型語言模型(LLMs)的多智能體協(xié)作框架,GitHub star數(shù)量已經(jīng)達(dá)到31.3k+。
接下來我們聊一下快速上手
一、環(huán)境搭建
python 環(huán)境最好是 3.9
1.python 環(huán)境
利用 anaconda 創(chuàng)建 python3.9 的虛擬環(huán)境
conda create -n python39 python-3.9
2. MetaGpt 下載
git clone https://github.com/geekan/MetaGPT.git
cd /your/path/to/MetaGPT
pip install -e .
也可以采取以下方式
pip install metagpt
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple metagpt==0.5.2
pip install git+https://github.com/geekan/MetaGPT
二、MetaGPT配置
1.調(diào)用 ChatGPT API 服務(wù)
API 可以在萬能的某寶上獲取,再次就不多贅述。在此主要想和大家聊了聊怎么調(diào)用 AIP 。
在此之前我們需要:
- 安裝
openai
庫
pip install --upgrade openai
- 獲取 API 密鑰 (某寶,文章最后有介紹方法)
- 配置 python 科學(xué)代理
【控制面板】->【網(wǎng)絡(luò)和Internet】->【Internet選項】->【連接】->【局域網(wǎng)設(shè)置】
之后在python里運行下方的代碼,將冒號內(nèi)的內(nèi)容改為上圖紅色框框里的地址。
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
如下圖
打開我們親愛的 jupyter
(也而已是 pycharm
) 運行 簡單的測試代碼
import os
from openai import OpenAI
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
os.environ["OPENAI_API_KEY"] = "sk-*****"
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
{"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
]
)
print(completion.choices[0].message)
得到以下輸出, 調(diào)用API成功。
2.簡單使用
接下來通過二十一點和貪吃蛇兩個游戲來帶大家簡單運用一些 MetaGpt
二十一點游戲
該代碼在 jupter 運行
代碼如下
import asyncio
from metagpt.roles import (
Architect,
Engineer,
ProductManager,
ProjectManager,
)
from metagpt.team import Team
import os
from openai import OpenAI
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
os.environ["OPENAI_API_KEY"] = "sk-*“
async def startup(idea: str):
company = Team()
company.hire(
[
ProductManager(),
Architect(),
ProjectManager(),
Engineer(),
]
)
company.invest(investment=3.0)
company.start_project(idea=idea)
await company.run(n_round=5)
await startup(idea="write a cli blackjack game")
這段代碼體現(xiàn)了 MetaGPT 的設(shè)計理念,該設(shè)計理念認(rèn)為每個項目都可以抽象為一個標(biāo)準(zhǔn)流程(SOP)。在這個 SOP 下,不同的角色(Role)負(fù)責(zé)項目的不同方面,組成一個項目組(Team),共同完成項目。在上述代碼中,MetaGPT 利用了 ProductManager、Architect、ProjectManager 和 Engineer 四個角色組成了一個項目組(Team),并共同完成了一個游戲開發(fā)任務(wù)。
metagpt生成了所有的設(shè)計文件和代碼框架。
貪吃蛇游戲
接下來我們在 終端 運行
1.首先修改配置文件,拷貝樣例配置 config.yaml 中的內(nèi)容到你的新文件中,然后添加一下內(nèi)容
OPENAI_BASE_URL: "https://api.openai.com/v1"
OPENAI_PROXY: "http://127.0.0.1:7890"
OPENAI_API_KEY: "sk-***" # set the value to sk-xxx if you host the openai interface for open llm model
OPENAI_API_MODEL: "gpt-3.5-turbo"
# OPENAI_API_MODEL: "gpt-4-1106-preview"
MAX_TOKENS: 1500
RPM: 10
TIMEOUT: 60 # Timeout for llm invocation
2.然后再終端運行
metagpt "寫一個貪吃蛇游戲"
就會在終端顯示思路和代碼,隨后我們可以在 workspace
目錄下看到游戲的全部代碼
運行
MetaGPT\workspace\game_snake\game_snake> python .\main.py
啟動!
在這里講一講 openai api key 的獲取方法
在獲取OpenAI API key
之前我們需要openai官網(wǎng)中注冊一個賬號。這里假設(shè)我們已經(jīng)有了openai
賬號,先在openai官網(wǎng)登錄,然后選擇API
。(如下圖)
在OPENAI的API官網(wǎng)首頁左側(cè)邊欄找到API keys,點擊進(jìn)入:
文章來源:http://www.zghlxwxcb.cn/news/detail-813802.html
點擊 Create new secret key文章來源地址http://www.zghlxwxcb.cn/news/detail-813802.html
到了這里,關(guān)于MetaGPT前期準(zhǔn)備與快速上手的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!