From fd432fa1321b4c07d11f37f7dee5664f32121f5a Mon Sep 17 00:00:00 2001 From: didi <2020201387@ruc.edu.cn> Date: Sat, 29 Jun 2024 16:42:24 +0800 Subject: [PATCH] Update --- examples/ags/w_action_node/operator.py | 12 ++--- examples/ags/w_action_node/operator_an.py | 54 +++-------------------- 2 files changed, 12 insertions(+), 54 deletions(-) diff --git a/examples/ags/w_action_node/operator.py b/examples/ags/w_action_node/operator.py index eb91bbd69..031ef84b4 100644 --- a/examples/ags/w_action_node/operator.py +++ b/examples/ags/w_action_node/operator.py @@ -8,7 +8,7 @@ from typing import List from metagpt.actions.action_node import ActionNode from metagpt.llm import LLM -from examples.ags.w_action_node.operator_an import GENERATE_NODE, GENERATE_CODE_NODE, REVIEW_NODE, REVISE_NODE, ENSEMBLE_NODE +from examples.ags.w_action_node.operator_an import GenerateOp, GenerateCodeOp, ReviewOp, ReviseOp, EnsembleOp from examples.ags.w_action_node.prompt import GENERATE_PROMPT, GENERATE_CODE_PROMPT, REVIEW_PROMPT, REVISE_PROMPT, ENSEMBLE_PROMPT class Operator: @@ -25,7 +25,7 @@ class Generate(Operator): async def __call__(self, problem_description): prompt = GENERATE_PROMPT.format(problem_description=problem_description) - node = await GENERATE_NODE.fill(context=prompt, llm=self.llm) + node = await ActionNode.from_pydantic(GenerateOp).fill(context=prompt, llm=self.llm) response = node.instruct_content.model_dump() return response @@ -36,7 +36,7 @@ class GenerateCode(Operator): async def __call__(self, problem_description): prompt = GENERATE_CODE_PROMPT.format(problem_description=problem_description) - node = await GENERATE_CODE_NODE.fill(context=prompt, llm=self.llm) + node = await ActionNode.from_pydantic(GenerateCodeOp).fill(context=prompt, llm=self.llm) response = node.instruct_content.model_dump() return response @@ -48,7 +48,7 @@ class Review(Operator): async def __call__(self, problem_description, solution): prompt = REVIEW_PROMPT.format(problem_description=problem_description, solution=solution, criteria=self.criteria) - node = await REVIEW_NODE.fill(context=prompt, llm=self.llm) + node = await ActionNode.from_pydantic(ReviewOp).fill(context=prompt, llm=self.llm) response = node.instruct_content.model_dump() return response @@ -59,7 +59,7 @@ class Revise(Operator): async def __call__(self, problem_description, solution, feedback): prompt = REVISE_PROMPT.format(problem_description=problem_description, solution=solution, feedback=feedback) - node = await REVISE_NODE.fill(context=prompt, llm=self.llm) + node = await ActionNode.from_pydantic(ReviseOp).fill(context=prompt, llm=self.llm) response = node.instruct_content.model_dump() return response @@ -73,6 +73,6 @@ class Ensemble(Operator): for solution in solutions: solution_text += solution + "\n" prompt = ENSEMBLE_PROMPT.format(solutions=solution_text, problem_description=problem_description) - node = await ENSEMBLE_NODE.fill(context=prompt, llm=self.llm) + node = await ActionNode.from_pydantic(EnsembleOp).fill(context=prompt, llm=self.llm) response = node.instruct_content.model_dump() return response \ No newline at end of file diff --git a/examples/ags/w_action_node/operator_an.py b/examples/ags/w_action_node/operator_an.py index 1f989db35..aa4753113 100644 --- a/examples/ags/w_action_node/operator_an.py +++ b/examples/ags/w_action_node/operator_an.py @@ -6,60 +6,18 @@ from pydantic import BaseModel, Field from metagpt.actions.action_node import ActionNode -SOLUTION = ActionNode( - key="solution", - expected_type=str, - instruction="Your Solution for this problem", - example="" -) - -CODE_SOLUTION = ActionNode( - key="code_solution", - expected_type=str, - instruction="Your Code Solution for this problem", - example="" -) - -REVIEW_RESULT = ActionNode( - key="review_result", - expected_type=bool, - instruction="The Review Result (Bool). If you think this solution looks good for you, return 'true'; If not, return 'false'", - example="" -) - -FEEDBACK = ActionNode( - key="feedback", - expected_type=str, - instruction="Your FeedBack for this problem based on the criteria. If the review result is true, you can put it 'nothing here'.", - example="" -) - -GENERATE_NODE = ActionNode.from_children("Generate", [SOLUTION]) -GENERATE_CODE_NODE = ActionNode.from_children("GenerateCode", [CODE_SOLUTION]) -REVIEW_NODE = ActionNode.from_children("Review", [REVIEW_RESULT, FEEDBACK]) -REVISE_NODE = ActionNode.from_children("Revise", [SOLUTION]) -ENSEMBLE_NODE = ActionNode.from_children("Ensemble", [SOLUTION]) - -class Generate(BaseModel): +class GenerateOp(BaseModel): solution: str = Field(default="", description="Your Solution for this problem") -class GenerateCode(BaseModel): +class GenerateCodeOp(BaseModel): code_solution: str = Field(default="", description="Your Code Solution for this problem") -class Review(BaseModel): +class ReviewOp(BaseModel): review_result: bool = Field(default=False, description="The Review Result (Bool). If you think this solution looks good for you, return 'true'; If not, return 'false'") feedback: str = Field(default="", description="Your FeedBack for this problem based on the criteria. If the review result is true, you can put it 'nothing here'.") -class Revise(BaseModel): - revised_solution: str = Field(default="", description="Revised solution for this problem") +class ReviseOp(BaseModel): + revised_solution: str = Field(default="", description="Based on the feedback, revised solution for this problem") -class Ensemble(BaseModel): +class EnsembleOp(BaseModel): final_solution: str = Field(default="", description="Final ensemble solution for this problem") - - - - -# 接下来我将给予你两段代码,请你按照我的要求对其进行改写 -# 第一段代码是一个利用子ActionNode通过From_children方法实现的多个Node聚合 -# 第二段代码是一个利用Pydantic与From_pydantic方法实现的Node -# 现在,我希望你能够通过From Pydantic方法,通过第二段代码的风格,完成我第一段代码中的GENERATE -> ENSEMBLE 五个Node的实现,每一个实现都应该像是第二段代码中的一个Class一样 \ No newline at end of file