MetaGPT的实际使用结果

本文最后更新于 2023年8月29日 上午

MetaGPT就是输入一句话的老板需求,输出用户故事 / 竞品分析 / 需求 / 数据结构 / APIs / 文件等。看着非常美好,但是实际情况怎么样呢,网上搜了搜,全是垃圾信息,只好花钱自己试试了,本文就是对其结果的记录。

1 生成个项目

我这里是使用docker

  • docker的映射文件夹

    1
    mkdir -p /opt/metagpt/{config,workspace}
  • 建立配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    vim /opt/metagpt/config.yaml

    填入:
    OPENAI_API_KEY: "sk-m你的gpt key"
    OPENAI_API_BASE: "https://你的openai代理/v1"
    OPENAI_API_MODEL: "gpt-4"

    cp /opt/metagpt/config/config.yaml /opt/metagpt/config/key.yaml
  • 拉取容器

    1
    docker pull metagpt/metagpt:v0.3
  • 启动容器

    1
    2
    3
    4
    5
    docker run --name metagpt -d \
    --privileged \
    -v /opt/metagpt/config:/app/metagpt/config \
    -v /opt/metagpt/workspace:/app/metagpt/workspace \
    metagpt/metagpt:v0.3
  • 进入容器生成项目

    1
    2
    docker exec -it metagpt /bin/bash
    python startup.py "使用python写一个API网关,支持负载均衡,拥有web管理平 台"

    首次运行时报错:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    Traceback (most recent call last):
    File "/app/metagpt/startup.py", line 29, in <module>
    fire.Fire(main)
    File "/usr/local/lib/python3.9/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
    File "/usr/local/lib/python3.9/site-packages/fire/core.py", line 466, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
    File "/usr/local/lib/python3.9/site-packages/fire/core.py", line 681, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
    File "/app/metagpt/startup.py", line 25, in main
    asyncio.run(startup(idea, investment, n_round))
    File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
    File "/usr/local/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
    File "/app/metagpt/startup.py", line 15, in startup
    await company.run(n_round=n_round)
    File "/app/metagpt/metagpt/software_company.py", line 60, in run
    await self.environment.run()
    File "/app/metagpt/metagpt/environment.py", line 64, in run
    await asyncio.gather(*futures)
    File "/app/metagpt/metagpt/roles/role.py", line 229, in run
    rsp = await self._react()
    File "/app/metagpt/metagpt/roles/role.py", line 200, in _react
    return await self._act()
    File "/app/metagpt/metagpt/roles/engineer.py", line 143, in _act
    return await self._act_sp()
    File "/app/metagpt/metagpt/roles/engineer.py", line 128, in _act_sp
    code_rsp = await WriteCode().run(
    File "/app/metagpt/metagpt/actions/write_code.py", line 71, in run
    self._save(context, filename, code_rsp)
    File "/app/metagpt/metagpt/actions/write_code.py", line 60, in _save
    code = CodeParser.parse_code(block="", text=code_rsp)
    File "/app/metagpt/metagpt/utils/common.py", line 172, in parse_code
    raise Exception
    Exception

    异常居然没有写个原因,我也懒得去看具体原因,盲猜流式接收接口应答途中挂了。再次运行成功结束。总共用了18分钟,花费1.3美元

2 生成结果

生成文件在workspace中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
├── [4.0K]  api_gateway
│   ├── [ 771] admin.py
│   ├── [ 557] load_balancer.py
│   ├── [1.4K] main.py
│   ├── [ 598] models.py
│   └── [1.4K] views.py
├── [4.0K] docs
│   ├── [2.6K] prd.md
│   └── [1.5K] system_design.md
└── [4.0K] resources
├── [ 425] competitive_analysis.mmd
├── [ 17K] competitive_analysis.pdf
├── [ 32K] competitive_analysis.png
├── [5.1K] competitive_analysis.svg
├── [ 475] data_api_design.mmd
├── [ 20K] data_api_design.pdf
├── [ 34K] data_api_design.png
├── [ 12K] data_api_design.svg
├── [ 319] seq_flow.mmd
├── [ 15K] seq_flow.pdf
├── [ 32K] seq_flow.png
└── [ 22K] seq_flow.svg

其中文档prd.md中包含最开始我提供的需求扩展,比如什么产品目标、竞品分析、需求分析、需求池什么的,看着像那么回事儿,并且内容符合我的初衷,但是肯定是没法和正式这类文档比较,属于大学课程设计水平吧。

文档system_design.md描述了数据结构和程序流程,从里面非常简单的表述我感到不妙。

3 运行

直接执行python main.py,果不其然,报错了,找不到Base。下列是main.py函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
## Required Python third-party packages
from flask import Flask
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import API
from load_balancer import LoadBalancer
from views import APIView
from admin import AdminInterface
from flask_admin.contrib.sqla import ModelView

## Flask application setup
app = Flask(__name__)

## Database setup
engine = create_engine('sqlite:///api_gateway.db')
Session = sessionmaker(bind=engine)

## Create tables
Base.metadata.create_all(engine)

## Create a session
session = Session()

## Create API instances
api1 = API(name='API1', endpoint='http://localhost:5000/api1', method='GET', is_active=True)
api2 = API(name='API2', endpoint='http://localhost:5000/api2', method='GET', is_active=True)

## Add API instances to the session
session.add(api1)
session.add(api2)
session.commit()

## Create LoadBalancer instance
load_balancer = LoadBalancer([api1, api2])

## Create AdminInterface instance
admin_interface = AdminInterface(app, session)

## Add API view to the admin interface
admin_interface.add_view(ModelView(API, session))

## Create APIView instance
api_view = APIView(session)

## Add API routes to the Flask application
app.add_url_rule('/api/<api_name>', view_func=api_view, methods=['GET', 'DELETE'])
app.add_url_rule('/api', view_func=api_view, methods=['POST'])

## Run the Flask application
if __name__ == '__main__':
app.run(debug=True)

很明显在使用前没定义,找了找在models.py中定义了Base,但是没有导入,于是添加后继续运行,继续报错,这次是在views.py中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
## Required Python third-party packages
from flask import Flask, request, jsonify
from flask.views import MethodView
from sqlalchemy.orm import Session

## Data structures and interface definitions
class APIView(MethodView):
def __init__(self, session: Session):
self.session = session

def get(self, api_name: str):
"""
This method returns the details of an API.
"""
api = self.session.query(API).filter_by(name=api_name).first()
if not api:
return jsonify({'error': 'API not found'}), 404
return jsonify(api.__dict__), 200

def post(self):
"""
This method adds a new API.
"""
data = request.get_json()
api = API(**data)
self.session.add(api)
self.session.commit()
return jsonify(api.__dict__), 201

def delete(self, api_name: str):
"""
This method deletes an API.
"""
api = self.session.query(API).filter_by(name=api_name).first()
if not api:
return jsonify({'error': 'API not found'}), 404
self.session.delete(api)
self.session.commit()
return jsonify({'success': 'API deleted'}), 200

## Flask application setup
app = Flask(__name__)
api_view = APIView(Session())

app.add_url_rule('/api/<api_name>', view_func=api_view, methods=['GET', 'DELETE'])
app.add_url_rule('/api', view_func=api_view, methods=['POST'])

报错说app.add_url_rule('/api/<api_name>', view_func=api_view, methods=['GET', 'DELETE'])中的api_view没有__name__属性。先不管错误,最后这四行全局运行的和主函数里面一摸一样,这明显结构有问题。懒得再继续改了,这些生成的文件最多也就40多行,实现了些及其简单的功能,连个简单架子都算不上,应该是没有任何用处的垃圾。

4 结论

平日里也经常用gpt生成代码来用,但也只能使其生成最末端的功能,该开源仓库的思路其实挺棒的,但就目前最优gpt4方案仍然达不到自我细分后,实现组装的能力,所有目前来看它不能实际用来做什么,更多的是提供一种研究思路。


MetaGPT的实际使用结果
https://blog.kala.love/posts/1f49ad76/
作者
久远·卡拉
发布于
2023年8月29日
许可协议