网站打开的速度很慢应该怎么做,网站建设公司税率,培训中心,成都网站建设优化推广参考#xff1a;
metagpt环境配置参考模型智能体开发之metagpt-单智能体实践
需求分析
之前有过单智能体的测试case#xff0c;但是现实生活场景是很复杂的#xff0c;所以单智能体远远不能满足我们的诉求#xff0c;所以仍然还需要了解多智能体的实现。通过多个role对动…参考
metagpt环境配置参考模型智能体开发之metagpt-单智能体实践
需求分析
之前有过单智能体的测试case但是现实生活场景是很复杂的所以单智能体远远不能满足我们的诉求所以仍然还需要了解多智能体的实现。通过多个role对动作的关联、组合来构建一个工作流程从而使智能体能够完成更加复杂的任务基于单智能体测试case的扩展我们的诉求在简单的输出code的基础上新增一条就是生成code并且立刻运行code。那么这个时候我们就需要两个action一个负责生成code一个负责执行code
实现 定义一个负责生成code的action参照单智能体的测试case 模型智能体开发之metagpt-单智能体实践 定义一个负责运行code的action class SimpleRunCode(Action):name: str SimpleRunCodeasync def run(self, code_text: str):result subprocess.run([python3, -c, code_text], capture_outputTrue, textTrue)code_result result.stdoutlogger.info(f{code_result})return code_result运行code不需要调用llm所以不涉及到prompt模版的设计这里通过python的标准库 subprocess来fork一个子进程运行一个外部程序 subprocess包内定义了多个可以创建子进程的函数这些函数分别以不同的方法来创建子进程所以按需使用即可在本次的case里面通过subprocess.run在fork一个子进程执行传入的代码那么在fork之后存在两个进程一个是python程序本身的进程另一个就是subprocess.run创建的子进程两个进程是互不干预的在父进程中通过result.stdout来获取子进程的执行结果 定义 RunnableCoder 角色 完整的代码 class RunnableCoder(Role):name: str Aliceprofile: str RunnableCoderdef __init__(self, **kwargs):super().__init__(**kwargs)self.set_actions([SimpleWriteCode, SimpleRunCode])self._set_react_mode(react_modeRoleReactMode.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.todomsg self.get_memories(k1)[0] # find the most k recent messagesresult await todo.run(msg.content)msg Message(contentresult, roleself.profile, cause_bytype(todo))self.rc.memory.add(msg)return msg可以看到在重写init方法的时候这里关联了两个actionSimpleWriteCode, SimpleRunCode 将 react_mode 设置为 “by_order”这意味着 Role 将按照 self._init_actions 中指定的顺序执行其能够执行的 Action。在这种情况下当 Role 执行 _act 时self._rc.todo 将首先是 SimpleWriteCode然后是 SimpleRunCode。 def __init__(self, **kwargs):super().__init__(**kwargs)self.set_actions([SimpleWriteCode, SimpleRunCode])self._set_react_mode(react_modeRoleReactMode.BY_ORDER.value)重写act方法 覆盖 _act 函数。Role 从上一轮的人类输入或动作输出中检索消息用适当的 Message 内容提供当前的 Action (self._rc.todo)最后返回由当前 Action 输出组成的 Message 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.todomsg self.get_memories(k1)[0] # find the most k recent messagesresult await todo.run(msg.content)msg Message(contentresult, roleself.profile, cause_bytype(todo))self.rc.memory.add(msg)return msg测试 代码 async def main():msg write a function that calculates the sum of a listrole RunnableCoder()logger.info(msg)result await role.run(msg)logger.info(result)asyncio.run(main())运行
demo如果想正常运行的话需要调用llm的key环境配置可以参照 metagpt环境配置参考