change task_guide to code_steps

This commit is contained in:
wubinhao 2023-12-06 11:24:24 +08:00
parent 7436150849
commit 2e7abe7d03
5 changed files with 31 additions and 36 deletions

View file

@ -85,7 +85,7 @@ class WriteCodeByGenerate(BaseWriteAnalysisCode):
self,
context: [List[Message]],
plan: Plan = None,
task_guide: str = "",
code_steps: str = "",
system_msg: str = None,
**kwargs,
) -> str:
@ -155,7 +155,7 @@ class WriteCodeWithTools(BaseWriteAnalysisCode):
self,
context: List[Message],
plan: Plan = None,
task_guide: str = "",
code_steps: str = "",
data_desc: str = "",
) -> str:
task_type = plan.current_task.task_type
@ -165,12 +165,12 @@ class WriteCodeWithTools(BaseWriteAnalysisCode):
{k: tool[k] for k in ["name", "description"] if k in tool}
for tool in available_tools
]
task_guide = "\n".join(
[f"Step {step.strip()}" for step in task_guide.split("\n")]
code_steps = "\n".join(
[f"Step {step.strip()}" for step in code_steps.split("\n")]
)
recommend_tools = await self._tool_recommendation(
task, task_guide, available_tools
task, code_steps, available_tools
)
recommend_tools, tool_catalog = self._parse_recommend_tools(task_type, recommend_tools)
logger.info(f"Recommended tools for every steps: {recommend_tools}")
@ -194,7 +194,7 @@ class WriteCodeWithTools(BaseWriteAnalysisCode):
completed_code=completed_code,
data_desc=data_desc,
special_prompt=special_prompt,
code_steps=task_guide,
code_steps=code_steps,
module_name=module_name,
output_desc=output_desc,
available_tools=recommend_tools,

View file

@ -6,7 +6,7 @@ from metagpt.actions import Action
from metagpt.schema import Message, Task, Plan
TASK_GUIDE_PROMPT_TEMPLATE = """
CODE_STEPS_PROMPT_TEMPLATE = """
# Context
{context}
@ -38,7 +38,7 @@ STRUCTURAL_CONTEXT = """
"""
class WriteTaskGuide(Action):
class WriteCodeSteps(Action):
async def run(self, plan: Plan) -> str:
"""Run of a task guide writing action, used in ml engineer
@ -51,24 +51,19 @@ class WriteTaskGuide(Action):
"""
context = self.get_context(plan)
task_guide_prompt = TASK_GUIDE_PROMPT_TEMPLATE.format(
code_steps_prompt = CODE_STEPS_PROMPT_TEMPLATE.format(
context=context,
)
task_guide = await self._aask(task_guide_prompt)
return task_guide
code_steps = await self._aask(code_steps_prompt)
return code_steps
def get_context(self, plan: Plan):
user_requirement = plan.goal
task_rename_map = {
'task_id': 'task_id',
'instruction': 'instruction',
'is_finished': 'is_finished',
'task_guide': 'code_plan'
}
select_task_keys = ['task_id', 'instruction', 'is_finished', 'code_steps']
def process_task(task):
task_dict = task.dict()
ptask = {task_rename_map[k]: task_dict[k] for k in task_dict if k in task_rename_map}
ptask = {k: task_dict[k] for k in task_dict if k in select_task_keys}
return ptask
tasks = json.dumps(
[process_task(task) for task in plan.tasks], indent=4, ensure_ascii=False
@ -77,6 +72,6 @@ class WriteTaskGuide(Action):
context = STRUCTURAL_CONTEXT.format(
user_requirement=user_requirement, tasks=tasks, current_task=current_task
)
# print(context)
print(context)
return context

View file

@ -11,7 +11,7 @@ from metagpt.config import CONFIG
from metagpt.provider.anthropic_api import Claude2 as Claude
from metagpt.provider.openai_api import OpenAIGPTAPI
from metagpt.provider.zhipuai_api import ZhiPuAIGPTAPI
from metagpt.provider.spark_api import SparkAPI
# from metagpt.provider.spark_api import SparkAPI
from metagpt.provider.human_provider import HumanProvider

View file

@ -12,7 +12,7 @@ from metagpt.logs import logger
from metagpt.actions.write_plan import WritePlan
from metagpt.actions.write_analysis_code import WriteCodeByGenerate, WriteCodeWithTools
from metagpt.actions.execute_code import ExecutePyCode
from metagpt.actions.write_task_guide import WriteTaskGuide
from metagpt.actions.write_code_steps import WriteCodeSteps
STRUCTURAL_CONTEXT = """
## User Requirement
@ -75,7 +75,7 @@ class MLEngineer(Role):
self._set_react_mode(react_mode="plan_and_act")
self.plan = Plan(goal=goal)
self.use_tools = False
self.use_task_guide = True
self.use_code_steps = True
self.execute_code = ExecutePyCode()
self.auto_run = auto_run
@ -88,7 +88,7 @@ class MLEngineer(Role):
logger.info(f"ready to take on task {task}")
# take on current task
code, result, success, task_guide = await self._write_and_exec_code()
code, result, success, code_steps = await self._write_and_exec_code()
# ask for acceptance, users can other refuse and change tasks in the plan
task_result_confirmed = await self._ask_review()
@ -97,7 +97,7 @@ class MLEngineer(Role):
# tick off this task and record progress
task.code = code
task.result = result
task.task_guide = task_guide
task.code_steps = code_steps
self.plan.finish_current_task()
self.working_memory.clear()
@ -106,9 +106,9 @@ class MLEngineer(Role):
await self._update_plan()
async def _write_and_exec_code(self, max_retry: int = 3):
task_guide = (
await WriteTaskGuide().run(self.plan)
if self.use_task_guide
code_steps = (
await WriteCodeSteps().run(self.plan)
if self.use_code_steps
else ""
)
@ -123,14 +123,14 @@ class MLEngineer(Role):
# breakpoint()
if not self.use_tools or self.plan.current_task.task_type == "other":
# code = "print('abc')"
code = await WriteCodeByGenerate().run(
context=context, plan=self.plan, task_guide=task_guide, temperature=0.0
)
code = "print('abc')"
# code = await WriteCodeByGenerate().run(
# context=context, plan=self.plan, code_steps=code_steps, temperature=0.0
# )
cause_by = WriteCodeByGenerate
else:
code = await WriteCodeWithTools().run(
context=context, plan=self.plan, task_guide=task_guide, data_desc=""
context=context, plan=self.plan, code_steps=code_steps, data_desc=""
)
cause_by = WriteCodeWithTools
@ -153,7 +153,7 @@ class MLEngineer(Role):
counter += 1
return code, result, success, task_guide
return code, result, success, code_steps
async def _ask_review(self):
if not self.auto_run:
@ -203,9 +203,9 @@ class MLEngineer(Role):
if __name__ == "__main__":
# requirement = "Run data analysis on sklearn Iris dataset, include a plot"
# requirement = "Run data analysis on sklearn Diabetes dataset, include a plot"
requirement = "Run data analysis on sklearn Wine recognition dataset, include a plot, and train a model to predict wine class (20% as validation), and show validation accuracy"
# requirement = "Run data analysis on sklearn Wine recognition dataset, include a plot, and train a model to predict wine class (20% as validation), and show validation accuracy"
# requirement = "Run data analysis on sklearn Wisconsin Breast Cancer dataset, include a plot, train a model to predict targets (20% as validation), and show validation accuracy"
# requirement = "Run EDA and visualization on this dataset, train a model to predict survival, report metrics on validation set (20%), dataset: workspace/titanic/train.csv"
requirement = "Run EDA and visualization on this dataset, train a model to predict survival, report metrics on validation set (20%), dataset: workspace/titanic/train.csv"
async def main(requirement: str = requirement, auto_run: bool = False):
role = MLEngineer(goal=requirement, auto_run=auto_run)

View file

@ -81,7 +81,7 @@ class Task(BaseModel):
code: str = ""
result: str = ""
is_finished: bool = False
task_guide: str = ""
code_steps: str = ""
class Plan(BaseModel):