update ensemble example

This commit is contained in:
didi 2024-07-01 16:56:47 +08:00
parent f1ce1330d7
commit 4d376649cc
6 changed files with 53 additions and 230 deletions

View file

@ -5,7 +5,7 @@
from metagpt.llm import LLM
from examples.ags.w_action_node.operator import Generate, GenerateCode, Review, Revise, Ensemble
from examples.ags.w_action_node.operator import Generate, GenerateCode, Review, Revise, Ensemble, ScEnsemble
class Graph:
def __init__(self, name:str, llm:LLM) -> None:
@ -25,22 +25,41 @@ class HumanEvalGraph(Graph):
self.review = Review(llm=llm, criteria=criteria)
self.revise = Revise(llm=llm)
self.ensemble = Ensemble(llm=llm)
self.scensemble = ScEnsemble(llm=llm)
async def __call__(self, problem:str, ensemble_count:int = 2):
# TODO Ensemble Implamentation
# async def __call__(self, problem:str, ensemble_count:int = 2):
# solution_list = []
# for _ in range(ensemble_count):
# solution = await self.single_solve(problem, 3)
# solution_list.append(solution)
# solution = await self.ensemble(solution_list, problem)
# return solution
async def __call__(self, problem:str):
solution_list = []
for _ in range(ensemble_count):
solution = await self.single_solve(problem, 3)
for _ in range(3):
solution = await self.generate_code(problem)
solution = solution.get('code_solution')
solution_list.append(solution)
solution = await self.ensemble(solution_list, problem)
return solution
# async def __call__(self, problem:str):
# solution_list = []
# for _ in range(3):
# solution = await self.generate_code(problem)
# solution = solution.get('code_solution')
# solution_list.append(solution)
# solution = await self.scensemble(solution_list, problem)
# return solution
async def single_solve(self, problem:str, max_loop:int):
solution = await self.generate_code(problem)
solution = solution.get('code_solution')
for _ in range(max_loop):
review_feedback = await self.review(problem, solution)
if review_feedback['review_result']:
break
solution = await self.revise(problem, solution, review_feedback['feedback'])
solution = solution.get('revised_solution')
return solution

View file

@ -65,6 +65,21 @@ class Revise(Operator):
class Ensemble(Operator):
def __init__(self, name:str ="Ensembler", llm: LLM = LLM()):
super().__init__(name, llm)
async def __call__(self, solutions:List, problem_description):
solution_text = ""
for solution in solutions:
solution_text += str(solution) + "\n"
prompt = ENSEMBLE_PROMPT.format(solutions=solution_text, problem_description=problem_description)
node = await ActionNode.from_pydantic(EnsembleOp).fill(context=prompt, llm=self.llm)
response = node.instruct_content.model_dump()
return response
class ScEnsemble(Operator):
def __init__(self, name:str ="Ensembler", llm: LLM = LLM()):
super().__init__(name, llm)

View file

@ -4,7 +4,6 @@
# @Desc : action nodes for operator
from pydantic import BaseModel, Field
from metagpt.actions.action_node import ActionNode
class GenerateOp(BaseModel):
solution: str = Field(default="", description="Your Solution for this problem")
@ -21,3 +20,6 @@ class ReviseOp(BaseModel):
class EnsembleOp(BaseModel):
final_solution: str = Field(default="", description="Final ensemble solution for this problem")
class ScEnsembleOp(BaseModel):
solution_number: int = Field(default="", description="Choose The Best Solution Between These, and outp[ut the solution number")

View file

@ -25,6 +25,6 @@ Then output the revised solution.
"""
ENSEMBLE_PROMPT = """
For the question described as {problem_description},
please ensemble the following solutions: {solutions}, and provide an ensemble solution.
For the question described as {problem_description}, Solutions: {solutions}
Please select the solution that appears most frequently from these options and ensemble this to provide best solution.
"""