在role.py的_react函数中,rsp = Message(content=”No actions taken yet”, cause_by=Action) # will be overwritten after Role _act cause_by是一个str类型,为什么可以用Action这个类去传参?因为Message做了field_validator,会自动将cause_by转换为str,在项目中大量使用了Pydantic的validator功能以及一些装饰器,可以提前了解一下装饰器
import re from metagpt.actions import Action from metagpt.schema import Message from metagpt.logs import logger
classSimpleWriteCode(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: """
asyncdef_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 import asyncio
asyncdefmain(): msg = "write a function that calculates the sum of a list" role = SimpleCoder() logger.info(msg) result = await role.run(msg) logger.info(result)
""" Filename: MetaGPT/examples/build_customized_agent.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 """ 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
classSimpleWriteCode(Action): PROMPT_TEMPLATE: str = """ Write a python function that can {instruction} and provide two runnable test cases. Return ```python your_code_here ```with NO other texts, your code: """
asyncdef_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))
asyncdef_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)
defmain(msg="write a function that calculates the product of a list and run it"): # role = SimpleCoder() role = RunnableCoder() logger.info(msg) result = asyncio.run(role.run(msg)) logger.info(result)