From 1ee35c930e4875ad212a7541c62a96994dd00ce9 Mon Sep 17 00:00:00 2001 From: mannaandpoem <1580466765@qq.com> Date: Wed, 27 Dec 2023 15:08:01 +0800 Subject: [PATCH] Delete and modify some files --- metagpt/actions/__init__.py | 1 - metagpt/actions/refine_design_api.py | 7 +- metagpt/actions/refine_prd.py | 9 ++- metagpt/actions/refine_project_management.py | 4 +- metagpt/actions/write_code.py | 2 +- metagpt/actions/write_code_guide.py | 74 -------------------- metagpt/actions/write_code_refine.py | 67 ------------------ metagpt/roles/engineer.py | 12 ++-- tests/metagpt/provider/test_zhipuai_api.py | 5 +- 9 files changed, 20 insertions(+), 161 deletions(-) delete mode 100644 metagpt/actions/write_code_guide.py delete mode 100644 metagpt/actions/write_code_refine.py diff --git a/metagpt/actions/__init__.py b/metagpt/actions/__init__.py index 99a4175f6..c34c72ed2 100644 --- a/metagpt/actions/__init__.py +++ b/metagpt/actions/__init__.py @@ -14,7 +14,6 @@ from metagpt.actions.debug_error import DebugError from metagpt.actions.design_api import WriteDesign from metagpt.actions.design_api_review import DesignReview from metagpt.actions.project_management import AssignTasks, WriteTasks -from metagpt.actions.refine import Refine from metagpt.actions.research import CollectLinks, WebBrowseAndSummarize, ConductResearch from metagpt.actions.run_code import RunCode from metagpt.actions.search_and_summarize import SearchAndSummarize diff --git a/metagpt/actions/refine_design_api.py b/metagpt/actions/refine_design_api.py index d6a948b43..dba783323 100644 --- a/metagpt/actions/refine_design_api.py +++ b/metagpt/actions/refine_design_api.py @@ -7,7 +7,6 @@ from typing import List, Union from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG -from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template @@ -133,7 +132,7 @@ OUTPUT_MAPPING = { "Python package name": (str, ...), # "File list": (List[str], ...), "Data structures and interface definitions": (str, ...), - "Program call flow": (str, ...) + "Program call flow": (str, ...), } @@ -158,7 +157,7 @@ class RefineDesign(Action): original_workspace = workspace index = 1 while workspace.exists(): - ws_name_match = re.match(r'^(.*)_([\d]+)$', original_workspace.name) + ws_name_match = re.match(r"^(.*)_([\d]+)$", original_workspace.name) if ws_name_match: base_name, existing_index = ws_name_match.groups() index = int(existing_index) @@ -192,7 +191,7 @@ class RefineDesign(Action): logger.info(f"Saving System Designs to {system_design_file}") system_design_file.write_text((json_to_markdown(system_design.instruct_content.dict()))) - async def _save(self, context, system_design): + async def _save(self, context, system_design, WORKSPACE_ROOT=None): if isinstance(system_design, ActionOutput): ws_name = system_design.instruct_content.dict()["Python package name"] else: diff --git a/metagpt/actions/refine_prd.py b/metagpt/actions/refine_prd.py index 1d8bab5f8..e7fb080f8 100644 --- a/metagpt/actions/refine_prd.py +++ b/metagpt/actions/refine_prd.py @@ -1,6 +1,7 @@ -from typing import List, Union +from typing import List -from metagpt.actions import Refine, ActionOutput, SearchAndSummarize +from metagpt.actions import SearchAndSummarize +from metagpt.actions.refine import Refine from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.utils.get_template import get_template @@ -73,7 +74,6 @@ INCREMENT_OUTPUT_MAPPING = { class RefinePRD(Refine): - def __init__(self, name="RefinePRD", context=None, llm=None): super().__init__(name, context, llm) @@ -87,8 +87,7 @@ class RefinePRD(Refine): prompt_template, format_example = get_template(increment_template, format) prompt = prompt_template.format( - context=context, legacy=legacy, search_information=info, - format_example=format_example + context=context, legacy=legacy, search_information=info, format_example=format_example ) logger.debug(prompt) prd = await self._aask_v1(prompt, "prd", INCREMENT_OUTPUT_MAPPING, format=format) diff --git a/metagpt/actions/refine_project_management.py b/metagpt/actions/refine_project_management.py index 2775b1cb6..1cd1d1e0f 100644 --- a/metagpt/actions/refine_project_management.py +++ b/metagpt/actions/refine_project_management.py @@ -132,9 +132,7 @@ class RefineTasks(Action): async def run(self, context, legacy, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) - prompt = prompt_template.format(context=context, - legacy=legacy, - format_example=format_example) + prompt = prompt_template.format(context=context, legacy=legacy, format_example=format_example) rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) self._save(context, rsp) return rsp diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index fd6ad3eb1..9f4cba300 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -123,7 +123,7 @@ class WriteCode(Action): else: code_context = await self.get_codes(coding_context.task_doc, exclude=self.context.filename) - if guideline: # guide write code 也有两种方式,进行尝试 + if guideline: # guide write code 也有两种方式,进行尝试 prompt = WRITE_CODE_INCREMENT_TEMPLATE.format( guideline=guideline, design=coding_context.design_doc.content if coding_context.design_doc else "", diff --git a/metagpt/actions/write_code_guide.py b/metagpt/actions/write_code_guide.py deleted file mode 100644 index 3e51f0d2d..000000000 --- a/metagpt/actions/write_code_guide.py +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -from metagpt.actions.action import Action -from metagpt.logs import logger -from metagpt.schema import Message -from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed - - -PROMPT_TEMPLATE = """ -NOTICE -Role: You are a professional software engineer, and your main task is to conduct incremental development, proposing incremental development plans and code guideance based on context and legacy code. Existing code and logic that need to be retained must also appear in the code after incremental development, do not omit it. Ensure that the code conforms to the PEP8 standards, is elegantly designed and modularized, easy to read and maintain, and is written in Python 3.9 (or in another programming language). Output format carefully referenced "Format example". - -## Regulations Review: To make the software directly operable without further coding, follow the regulations below during incremental development: -0) Determine the scope of responsibilities of each file and what classes and methods need to be implemented. -1) Import all referenced classes. -2) Implement all methods. -3) Add necessary explanation to all methods. -4) Ensure there are no potential bugs. -5) Confirm that the entire project conforms to the tasks proposed by the user. -6) Review the code thoroughly, checking for errors and validating the logic to ensure seamless user interaction without compromising any specified requirements. - -## Incremental Development Plan: Provided as a Python list containing `filename.py`. Proposed the detail and essential incremental development plan, based on the following context and legacy code by thinking and analyzing step by step. All incremental modules/functions need to be added to the corresponding code files. - -## Code Guidance: Propose the foremost guidelines that how to implement code of modification part for incremental development based on the above context, legacy code and incremental development plan. - ------ -# Context -{context} - -## Legacy Code -You are tasked with conducting incremental development in the existing code based on the provided legacy code and above information. -``` -{legacy} -``` ------ - -## Format example ------ -## Incremental Development Guide -[ - "`game.py` Contains `Game` and ...", -] - - -## Code Guidance -### Implementation `xx` in `xxx.py` ..., else retain the original xxx.py code. -```python -## xxx.py -... -``` ---- -### Implementation of the `Game` in `game.py` ..., else retain the original game.py code. -```python -## game.py -class Game: - ... -``` ------ -""" - - -class WriteCodeGuide(Action): - def __init__(self, name="WriteCodeGuide", context: list[Message] = None, llm=None): - super().__init__(name, context, llm) - - async def run(self, context, legacy): - prompt = PROMPT_TEMPLATE.format(context=context, legacy=legacy) - logger.info(f'Write Code Guide ..') - code_guide = await self._aask(prompt) - # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) - return code_guide - \ No newline at end of file diff --git a/metagpt/actions/write_code_refine.py b/metagpt/actions/write_code_refine.py deleted file mode 100644 index 5a70b2c68..000000000 --- a/metagpt/actions/write_code_refine.py +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -from metagpt.actions.action import Action -from metagpt.logs import logger -from metagpt.schema import Message -from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed - -PROMPT_TEMPLATE = """ -NOTICE -Role: You are a professional engineer; your primary goal is to write PEP8 compliant, elegant, modular, easy-to-read, and maintainable Python 3.9 code (or any other programming language of your choice). -Requirements: Rewrite the complete code based on the Legacy Code so that it can be executed and avoid any potential bugs. You should modify the corresponding code based on the guidance. Output the complete code, fixing all errors according to the context. Ensure that you adhere to the specified guidelines for incremental development and modification of legacy code. -ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format should be carefully referenced using the "Format example". Only output the current modified code, nothing else. In the modified code, if unchanged, you should output it, the complete code. - -## Rewrite Complete Code: Only Write one file {filename}, Write code using triple quotes, based on the following list, context, guidelines and legacy code. -1. Important: Do your best to implement ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. -2. Implement one of the following code files based on the provided context. Return the code in the specified format. Your code will be part of the entire project, so ensure it is complete, reliable, and reusable. -3. Attention1: Implement the functions required by the current file scope of responsibility. For example, main only needs to focus on the basic functions of main.py in the legacy code and the incremental functions to be implemented. Reuse existing code as much as possible. You can import functions from other codes instead of reimplementing the function. If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. -4. Attention2: Make modifications and additions to the legacy code in accordance with the provided guidelines and API. Ensure that the complete code is implemented without any omissions, taking into account the guidelines, context, and existing legacy code. Retain the basic function methods from the legacy code, and make sure to preserve the existing code and logic that needs to be retained throughout the incremental development process. Avoid omitting any essential components. -5. Think before writing: What should be implemented and provided in this document? -6. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. -7. Do not use public member functions that do not exist in your design. -8. The Modified Code is implemented according to the requirements, and there are no issues with the code logic. All functions in the Modified Code are fully implemented; none are omitted or incomplete. - ------ -# Context -{context} - ------ -## Guidelines: The foremost guidelines of modification for incremental development. -{guide} - ------ -## Legacy Code: The Legacy Code that needs to be modified. '===' is the separator of each code file in the legacy code. Basic function methods need to be retained in {filename}. -{legacy} - ------ -## Format example ------ -## Rewrite Complete Code: {filename} -```python -# {filename} -... -``` ------ -""" - - -class WriteCodeRefine(Action): - def __init__(self, name="WriteCodeRefine", context: list[Message] = None, llm=None): - super().__init__(name, context, llm) - - @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) - async def write_code(self, prompt): - code_rsp = await self._aask(prompt) - code = CodeParser.parse_code(block="", text=code_rsp) - return code - - async def run(self, context, legacy, filename, guide): - prompt = PROMPT_TEMPLATE.format(context=context, legacy=legacy, filename=filename, guide=guide) - logger.info(f'Code refine {filename}..') - code = await self.write_code(prompt) - # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) - # self._save(context, filename, code) - return code - \ No newline at end of file diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index e2f4edba1..165ffadc2 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -28,13 +28,14 @@ from typing import Set from metagpt.actions import Action, WriteCode, WriteCodeReview, WriteTasks from metagpt.actions.fix_bug import FixBug from metagpt.actions.summarize_code import SummarizeCode -from metagpt.actions.write_code_guide_an import CODE_GUIDE_CONTEXT, WRITE_CODE_GUIDE_NODE, WriteCodeGuide +from metagpt.actions.write_code_guide_an import CODE_GUIDE_CONTEXT, WriteCodeGuide from metagpt.config import CONFIG from metagpt.const import ( CODE_SUMMARIES_FILE_REPO, CODE_SUMMARIES_PDF_FILE_REPO, + PRDS_FILE_REPO, SYSTEM_DESIGN_FILE_REPO, - TASK_FILE_REPO, PRDS_FILE_REPO, + TASK_FILE_REPO, ) from metagpt.logs import logger from metagpt.roles import Role @@ -220,7 +221,7 @@ class Engineer(Role): @staticmethod async def _new_coding_context( - filename, src_file_repo, task_file_repo, design_file_repo, dependency + filename, src_file_repo, task_file_repo, design_file_repo, dependency ) -> CodingContext: old_code_doc = await src_file_repo.get(filename) if not old_code_doc: @@ -324,8 +325,9 @@ class Engineer(Role): tasks = "\n".join([doc.content for doc in tasks]) old_codes = await self.get_old_codes() - context = CODE_GUIDE_CONTEXT.format(requirement=requirement, prd=prd, tasks=tasks, design=design, - code=old_codes) + context = CODE_GUIDE_CONTEXT.format( + requirement=requirement, prd=prd, tasks=tasks, design=design, code=old_codes + ) node = await WriteCodeGuide().run(context=context) guideline = node.instruct_content.json(ensure_ascii=False) return guideline diff --git a/tests/metagpt/provider/test_zhipuai_api.py b/tests/metagpt/provider/test_zhipuai_api.py index dc8b63cc3..8ce0f8f63 100644 --- a/tests/metagpt/provider/test_zhipuai_api.py +++ b/tests/metagpt/provider/test_zhipuai_api.py @@ -36,9 +36,12 @@ async def test_zhipuai_acompletion(mocker): assert resp["code"] == 200 assert "chatglm-turbo" in resp["data"]["choices"][0]["content"] + def test_zhipuai_proxy(mocker): import openai + from metagpt.config import CONFIG - CONFIG.openai_proxy = 'http://127.0.0.1:8080' + + CONFIG.openai_proxy = "http://127.0.0.1:8080" _ = ZhiPuAIGPTAPI() assert openai.proxy == CONFIG.openai_proxy