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: 大模型的好坏,其实直接影响了后面的一切。个人其实没法很好的去开发定义大模型底层的东西,而且要求的能力也很高。
  • 有些任务很复杂,并不是很容易解决。对于需要多个步骤的任务,每一个决策,都会影响后面的决策,最终可能直接影响整个决策的质量,智能体在这方面,可能并不是很容易解决。而且,有的任务,并不是需要每个过程都完美,还是需要从全局去考虑的。
  • ~~
  • 20
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要找出某个日期的正确上下班打卡时间,你可以使用`datetime`模块进行日期时间的比较和筛选。下面是一个示例代码: ```python import datetime # 打卡数据 data = [ {'日期': '2023-06-10', '开始时间': '2023-06-10 16:27:00', '结束时间': '2023-06-10 17:39:00'}, {'日期': '2023-06-11', '开始时间': '2023-06-11 01:57:00', '结束时间': '2023-06-11 02:02:00'}, {'日期': '2023-06-11', '开始时间': '2023-06-11 15:57:00', '结束时间': '2023-06-12 01:51:00'}, {'日期': '2023-07-01', '开始时间': '2023-07-01 08:09:00', '结束时间': '2023-07-01 17:25:00'}, {'日期': '2023-07-01', '开始时间': '2023-07-01 17:32:00', '结束时间': '2023-07-01 17:41:00'} ] target_date = datetime.date(2023, 6, 11) # 指定目标日期 # 遍历打卡数据,找到目标日期的打卡记录 for record in data: record_date = datetime.datetime.strptime(record['日期'], '%Y-%m-%d').date() if record_date == target_date: start_time = datetime.datetime.strptime(record['开始时间'], '%Y-%m-%d %H:%M:%S') end_time = datetime.datetime.strptime(record['结束时间'], '%Y-%m-%d %H:%M:%S') print('开始时间:', start_time) print('结束时间:', end_time) ``` 在上面的示例代码中,我们首先指定目标日期为`2023-06-11`,然后遍历打卡数据,找到与目标日期匹配的记录。然后将开始时间和结束时间转换为`datetime`对象,并打印出来。 你可以根据实际需要修改目标日期和打卡数据,以适应你的情况。 希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白日与明月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值