MetaGPT-打卡day01

MetaGPT是一个基于大型语言模型(LLMs)的多智能体协作框架。它利用SOP(Standard Operating Procedures,标准作业程序)来协调基于大语言模型的多智能体系统,从而实现元编程技术。该框架使用智能体模拟了一个虚拟软件团队,包含产品经理、架构师、项目经理、工程师、质量工程师等角色,并引入SOP成为框架的虚拟软件团队的开发流程。

安装部署

MetaGPT提供了两种部署方式:pip本地部署和docker部署。
这里使用pip进行了安装:
 

# 创建虚拟环境
conda create -n py39 python=3.9
conda activate py39
# clone项目到本地
git clone https://github.com/geekan/MetaGPT.git
cd /your/path/to/MetaGPT
pip install -e .

项目提供了一些例子,比如贪吃蛇展示MetaGPT的设计理念,即每个项目都可以抽象为一个标准流程(SOP),不同的角色负责项目的不同方面,组成一个项目组共同完成任务。

尝试

除了OpenAI外,MetaGPT还支持国内的智谱、讯飞星火等。这里使用智谱的开放平台,目前平台会提供大概200万的测试用的额度。基本可以满足学习需求。

【默认安装后的版本是0.6.0,直接使用的话,发现会失败,后来看到文档上说,目前需要使用0.5.2的版本,将版本回退后,便可以执行成功】

‍⁢⁤‬‌‌​⁤‍‌⁢⁤‬⁡‬⁢​⁣⁤​‌⁡‌⁢​⁡⁢​⁢‌‌‌⁡​‌​⁣⁢⁡⁤​‌‍适配其他国产模型 - 飞书云文档 (feishu.cn)

# 可导入任何角色,初始化它,用一个开始的消息运行它,完成!
import os

os.environ["ZHIPUAI_API_KEY"] = "申请的KEY"

import asyncio
import re
import subprocess

import fire

from metagpt.actions import Action
from metagpt.logs import logger
from metagpt.roles.role import Role, RoleReactMode
from metagpt.schema import Message


class SimpleWriteCode(Action):
    PROMPT_TEMPLATE: str = """
    Write a python function that can {instruction} and provide two runnnable test cases.
    Return ```python your_code_here ``` with NO other texts,
    your code:
    """

    name: str = "SimpleWriteCode"

    async def run(self, instruction: str):
        prompt = self.PROMPT_TEMPLATE.format(instruction=instruction)

        rsp = await self._aask(prompt)

        code_text = SimpleWriteCode.parse_code(rsp)

        return code_text

    @staticmethod
    def parse_code(rsp):
        pattern = r"```python(.*)```"
        match = re.search(pattern, rsp, re.DOTALL)
        code_text = match.group(1) if match else rsp
        return code_text


class SimpleRunCode(Action):
    name: str = "SimpleRunCode"

    async def run(self, code_text: str):
        result = subprocess.run(["python3", "-c", code_text], capture_output=True, text=True)
        code_result = result.stdout
        logger.info(f"{code_result=}")
        return code_result


class SimpleCoder(Role):
    name: str = "Alice"
    profile: str = "SimpleCoder"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._init_actions([SimpleWriteCode])

    async def _act(self) -> Message:
        logger.info(f"{self._setting}: to do {self._rc.todo}({self._rc.todo.name})")
        todo = self._rc.todo  # todo will be SimpleWriteCode()

        msg = self.get_memories(k=1)[0]  # find the most recent messages
        code_text = await todo.run(msg.content)
        msg = Message(content=code_text, role=self.profile, cause_by=type(todo))

        return msg


class RunnableCoder(Role):
    name: str = "Alice"
    profile: str = "RunnableCoder"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._init_actions([SimpleWriteCode, SimpleRunCode])
        self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value)

    async def _act(self) -> Message:
        logger.info(f"{self._setting}: to do {self._rc.todo}({self._rc.todo.name})")
        # By choosing the Action by order under the hood
        # todo will be first SimpleWriteCode() then SimpleRunCode()
        todo = self._rc.todo

        msg = self.get_memories(k=1)[0]  # find the most k recent messages
        result = await todo.run(msg.content)

        msg = Message(content=result, role=self.profile, cause_by=type(todo))
        self._rc.memory.add(msg)
        return msg


def main(msg="创建一个定时将目录下文件推送到某FTP的程序,每次推送完成后,将已经推送的文件迁移到其他目录进行备份,备份的文件保留2天"):
    # role = SimpleCoder()
    role = RunnableCoder()
    logger.info(msg)
    result = asyncio.run(role.run(msg))
    logger.info(result)


if __name__ == "__main__":
    fire.Fire(main)

关于智能体

智能体 = LLM+观察+思考+行动+记忆

可能对于程序员来说,我觉得可以用更加简单的方式去理解这个概念。所谓的智能体,有些像我们借助大模型的能力,模拟出多个角色,当我们提出一个问题后,可以通过这些角色分工合作来解决。然后,metagpt的功能,更像是让我们简化定义操作角色的方式。这样可以让我们可以通过相对简单的方式,模拟出N个角色,让智能体来解决问题。

通过Demo的体验,总的感觉来说是很不错的~

让我们快乐的折腾吧~

过程中的一些困惑

其实周围的人,对于大模型的态度差异很大,有的人觉得大模型不太靠谱,还需要继续发展;有的人觉得,大模型是未来的一个热点~~

嗯,对于我来说,整体来说是比较看好大模型的,但是使用过程中,也遇到了一些困惑。

  • 效率问题:目前测试的时候,也就执行了五六次代码,就消费了6~7w的token。消耗感觉还是挺大的,感觉如果是个人的话,有些消费不起。。在单位部署了ChatGLM3B,但是在家没法这么玩。
  • 高度的依赖LLM: 大模型的好坏,其实直接影响了后面的一切。个人其实没法很好的去开发定义大模型底层的东西,而且要求的能力也很高。
  • 有些任务很复杂,并不是很容易解决。对于需要多个步骤的任务,每一个决策,都会影响后面的决策,最终可能直接影响整个决策的质量,智能体在这方面,可能并不是很容易解决。而且,有的任务,并不是需要每个过程都完美,还是需要从全局去考虑的。
  • ~~

你可能感兴趣的:(GPT,MetaGPT,大模型)