add more comments

This commit is contained in:
yzlin 2024-02-01 22:23:28 +08:00
parent 4cd09e703c
commit 1a1610a67e
4 changed files with 9 additions and 42 deletions

View file

@ -81,9 +81,9 @@ class DebugCode(BaseWriteAnalysisCode):
async def run_reflection(
self,
context: List[Message],
code,
runtime_result,
context: list[Message],
code: str,
runtime_result: str,
) -> dict:
info = []
reflection_prompt = REFLECTION_PROMPT.format(
@ -107,12 +107,11 @@ class DebugCode(BaseWriteAnalysisCode):
runtime_result: str = "",
) -> str:
"""
根据当前运行代码和报错信息进行reflection和纠错
use reflection to debug, based on current code and the execution errors
"""
reflection = await self.run_reflection(
code=code,
context=context,
runtime_result=runtime_result,
)
# 根据reflection结果重写代码
return {"code": reflection["improved_impl"]}

View file

@ -5,6 +5,7 @@
@File : code_executor.py
"""
import asyncio
import base64
import re
import traceback
from pathlib import Path
@ -117,8 +118,6 @@ class ExecuteNbCode(Action):
return parsed_output
def show_bytes_figure(self, image_base64: str, interaction_type: str = "ipython"):
import base64
image_bytes = base64.b64decode(image_base64)
if interaction_type == "ipython":
from IPython.display import Image, display
@ -145,8 +144,8 @@ class ExecuteNbCode(Action):
# 如果在Python脚本中运行__file__ 变量存在
return False
def _process_code(self, code: Union[str, Dict], language: str = None) -> Tuple:
language = language or "python"
def _process_code(self, code: Union[str, Dict], language: str = "python") -> Tuple:
"""handle different code response formats, support str or dict"""
if isinstance(code, str) and Path(code).suffix in (".py", ".txt"):
code = Path(code).read_text(encoding="utf-8")
return code, language

View file

@ -1,4 +1,3 @@
import json
from typing import List, Tuple
from metagpt.actions import Action
@ -11,7 +10,7 @@ from metagpt.prompts.ml_action import (
)
from metagpt.prompts.write_analysis_code import CODE_GENERATOR_WITH_TOOLS
from metagpt.schema import Message, Plan
from metagpt.utils.common import CodeParser, create_func_call_config, remove_comments
from metagpt.utils.common import create_func_call_config, remove_comments
class WriteCodeWithToolsML(WriteCodeWithTools):
@ -61,36 +60,6 @@ class WriteCodeWithToolsML(WriteCodeWithTools):
return context, rsp
class Reflect(Action):
PROMPT_TEMPLATE: str = """
# Context
__context__
# Latest User Requirement
__user_requirement__
# Summary
Above is all your attempts to tackle the user requirement. You plan, act, submit your output, and get the result and feedback.
Output a json following the format:
```json
{
"summary": str = "summarize each of your previous trial in a triple of (your methods, the corresponding result, potential improvement), list them out",
"takeaways": str = "carefully find key takeaways from your summarization",
"reflection": str = "give specific instruction to improve your next trial in a step-by-step thinking process",
}
```
"""
REWRITE_PLAN_INSTRUCTION: str = """Take this reflection for rewriting plan, modify the current plan in place, make reference to your specific instruction, think about you should
change which task, add or delete what tasks in the plan. Only make necessary changes, keep reusable tasks unchanged, output the COMPLETE new plan starting from the first task. Your plan should have no more than 5 tasks."""
async def run(self, context: str, user_requirement: str = "") -> str:
user_requirement = user_requirement or "Score as high as possible in a data modeling competition"
# prompt = self.PROMPT_TEMPLATE.format(context=context, user_requirement=user_requirement)
prompt = self.PROMPT_TEMPLATE.replace("__context__", context).replace("__user_requirement__", user_requirement)
rsp_json = await self._aask(prompt)
rsp = CodeParser.parse_code(block=None, text=rsp_json)
reflection = json.loads(rsp)["reflection"]
return reflection
class UpdateDataColumns(Action):
async def run(self, plan: Plan = None) -> dict:
finished_tasks = plan.get_finished_tasks()

View file

@ -67,7 +67,7 @@ class BaseWriteAnalysisCode(Action):
class WriteCodeByGenerate(BaseWriteAnalysisCode):
"""Write code fully by generation"""
"""Ask LLM to generate codes purely by itself without local user-defined tools"""
async def run(
self,