CR:Capacity and Role(能力与角色)。你希望 AI 扮演怎样的角色 I:Insight(洞察),提供背景信息和上下文 S:Statement(陈述),你希望 AI 做什么。 P:Personality(个性),你希望 AI 以什么风格或方式回答你。 E:Experiment(实验),要求 AI 为你提供多个答案
defstartup( idea: str = typer.Argument(None, help="Your innovative idea, such as 'Create a 2048 game.'"), investment: float = typer.Option(default=3.0, help="Dollar amount to invest in the AI company."), n_round: int = typer.Option(default=5, help="Number of rounds for the simulation."), code_review: bool = typer.Option(default=True, help="Whether to use code review."), run_tests: bool = typer.Option(default=False, help="Whether to enable QA for adding & running tests."), implement: bool = typer.Option(default=True, help="Enable or disable code implementation."), project_name: str = typer.Option(default="", help="Unique project name, such as 'game_2048'."), inc: bool = typer.Option(default=False, help="Incremental mode. Use it to coop with existing repo."), project_path: str = typer.Option( default="", help="Specify the directory path of the old version project to fulfill the incremental requirements.", ), reqa_file: str = typer.Option( default="", help="Specify the source file name for rewriting the quality assurance code." ), max_auto_summarize_code: int = typer.Option( default=0, help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating " "unlimited. This parameter is used for debugging the workflow.", ), recover_path: str = typer.Option(default=None, help="recover the project from existing serialized storage"), init_config: bool = typer.Option(default=False, help="Initialize the configuration file for MetaGPT."), )
"""Set strategy of the Role reacting to observed Message. Variation lies in how this Role elects action to perform during the _think stage, especially if it is capable of multiple Actions. Args: react_mode (str): Mode for choosing action during the _think stage, can be one of: "react": standard think-act loop in the ReAct paper, alternating thinking and acting to solve the task, i.e. _think -> _act -> _think -> _act -> ... Use llm to select actions in _think dynamically; "by_order": switch action each time by order defined in _init_actions, i.e. _act (Action1) -> _act (Action2) -> ...; "plan_and_act": first plan, then execute an action sequence, i.e. _think (of a plan) -> _act -> _act -> ... Use llm to come up with the plan dynamically. Defaults to "react". max_react_loop (int): Maximum react cycles to execute, used to prevent the agent from reacting forever. Take effect only when react_mode is react, in which we use llm to choose actions, including termination. Defaults to 1, i.e. _think -> _act (-> return result and end) """
asyncdef_react(self) -> Message: """Think first, then act, until the Role _think it is time to stop and requires no more todo. This is the standard think-act loop in the ReAct paper, which alternates thinking and acting in task solving, i.e. _think -> _act -> _think -> _act -> ... Use llm to select actions in _think dynamically """ actions_taken = 0 rsp = Message(content="No actions taken yet", cause_by=Action) # will be overwritten after Role _act while actions_taken < self.rc.max_react_loop: # think todo = await self._think() ifnot todo: break # act logger.debug(f"{self._setting}: {self.rc.state=}, will do {self.rc.todo}") rsp = await self._act() actions_taken += 1 return rsp # return output from the last action
by_order模式源码如下
1 2 3 4 5 6 7 8
asyncdef_act_by_order(self) -> Message: """switch action each time by order defined in _init_actions, i.e. _act (Action1) -> _act (Action2) -> ...""" start_idx = self.rc.state if self.rc.state >= 0else0# action to run from recovered state rsp = Message(content="No actions taken yet") # return default message if actions=[] for i inrange(start_idx, len(self.states)): self._set_state(i) rsp = await self._act() return rsp # return output from the last action
asyncdef_plan_and_act(self) -> Message: """first plan, then execute an action sequence, i.e. _think (of a plan) -> _act -> _act -> ... Use llm to come up with the plan dynamically."""
# create initial plan and update it until confirmation goal = self.rc.memory.get()[-1].content # retreive latest user requirement await self.planner.update_plan(goal=goal)
# take on tasks until all finished while self.planner.current_task: task = self.planner.current_task logger.info(f"ready to take on task {task}")
# take on current task task_result = await self._act_on_task(task)
# process the result, such as reviewing, confirming, plan updating await self.planner.process_task_result(task_result)
rsp = self.planner.get_useful_memories()[0] # return the completed plan as a response
self.rc.memory.add(rsp) # add to persistent memory
PROMPT_TEMPLATE = """
NOTICE
Role: You are a professional engineer; the main goal is to write google-style, elegant, modular, easy to read and maintain code
Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese.
ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example".
# Context
## Design
{design}
## Task
{task}
## Legacy Code
1
{code}
## Debug logs
1 2 3
{logs}
{summary_log}
## Bug Feedback logs
1
{feedback}
# Format example
## Code: {filename}
1 2
## {filename} ...
# Instruction: Based on the context, follow "Format example", write code.
## Code: {filename}. Write code with triple quoto, based on the following attentions and context.
1. Only One file: do your best to implement THIS ONLY ONE FILE.
2. COMPLETE CODE: Your code will be part of the entire project, so please implement complete, reliable, reusable code snippets.
3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. AVOID circular import.
4. Follow design: YOU MUST FOLLOW "Data structures and interfaces". DONT CHANGE ANY DESIGN. Do not use public member functions that do not exist in your design.
5. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE.
6. Before using a external variable/module, make sure you import it first.
7. Write out EVERY CODE DETAIL, DON'T LEAVE TODO.
"""