From 29a1ea9cbd4524d2237c0285fad9f66cb2bf6094 Mon Sep 17 00:00:00 2001 From: better629 Date: Mon, 8 Jan 2024 16:09:14 +0800 Subject: [PATCH 01/50] add ActionNode review/revise --- metagpt/actions/action_node.py | 259 +++++++++++++++++- metagpt/utils/human_interaction.py | 107 ++++++++ tests/metagpt/actions/test_action_node.py | 87 +++++- tests/metagpt/utils/test_human_interaction.py | 74 +++++ 4 files changed, 520 insertions(+), 7 deletions(-) create mode 100644 metagpt/utils/human_interaction.py create mode 100644 tests/metagpt/utils/test_human_interaction.py diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 6c65b33ef..6dca00df0 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -9,7 +9,8 @@ NOTE: You should use typing.List instead of list to do type annotation. Because we can use typing to extract the type of the node, but we cannot use built-in list to extract. """ import json -from typing import Any, Dict, List, Optional, Tuple, Type +from enum import Enum +from typing import Any, Dict, List, Optional, Tuple, Type, Union from pydantic import BaseModel, create_model, model_validator from tenacity import retry, stop_after_attempt, wait_random_exponential @@ -19,6 +20,18 @@ from metagpt.llm import BaseLLM from metagpt.logs import logger from metagpt.provider.postprocess.llm_output_postprocess import llm_output_postprocess from metagpt.utils.common import OutputParser, general_after_log +from metagpt.utils.human_interaction import HumanInteraction + + +class ReviewMode(Enum): + HUMAN = "human" + AUTO = "auto" + + +class ReviseMode(Enum): + HUMAN = "human" + AUTO = "auto" + TAG = "CONTENT" @@ -45,6 +58,58 @@ SIMPLE_TEMPLATE = """ Follow instructions of nodes, generate output and make sure it follows the format example. """ +REVIEW_TEMPLATE = """ +## context +Compare the keys of nodes_output and the corresponding requirements one by one. If a key that does not match the requirement is found, provide the comment content on how to modify it. No output is required for matching keys. + +### nodes_output +{nodes_output} + +----- + +## format example +[{tag}] +{{ + "key1": "comment1", + "key2": "comment2", + "keyn": "commentn" +}} +[/{tag}] + +## nodes: ": # " +- key1: # the first key name of mismatch key +- key2: # the second key name of mismatch key +- keyn: # the last key name of mismatch key + +## constraint +{constraint} + +## action +generate output and make sure it follows the format example. +""" + +REVISE_TEMPLATE = """ +## context +change the nodes_output key's value to meet its comment and no need to add extra comment. + +### nodes_output +{nodes_output} + +----- + +## format example +{example} + +## nodes: ": # " +{instruction} + +## constraint +{constraint} + +## action +generate output and make sure it follows the format example. +""" + def dict_to_markdown(d, prefix="- ", kv_sep="\n", postfix="\n"): markdown_str = "" @@ -105,6 +170,9 @@ class ActionNode: """增加子ActionNode""" self.children[node.key] = node + def get_child(self, key: str) -> Union["ActionNode", None]: + return self.children.get(key, None) + def add_children(self, nodes: List["ActionNode"]): """批量增加子ActionNode""" for node in nodes: @@ -152,6 +220,11 @@ class ActionNode: new_class = create_model(class_name, __validators__=validators, **mapping) return new_class + def create_class(self, mode: str = "auto", class_name: str = None, exclude=None): + class_name = class_name if class_name else f"{self.key}_AN" + mapping = self.get_mapping(mode=mode, exclude=exclude) + return self.create_model_class(class_name, mapping) + def create_children_class(self, exclude=None): """使用object内有的字段直接生成model_class""" class_name = f"{self.key}_AN" @@ -186,6 +259,25 @@ class ActionNode: return node_dict + def update_instruct_content(self, incre_data: dict[str, Any]): + assert self.instruct_content + origin_sc_dict = self.instruct_content.model_dump() + origin_sc_dict.update(incre_data) + output_class = self.create_class() + self.instruct_content = output_class(**origin_sc_dict) + + def keys(self, mode: str = "auto") -> list: + if mode == "children" or (mode == "auto" and self.children): + keys = [] + else: + keys = [self.key] + if mode == "root": + return keys + + for _, child_node in self.children.items(): + keys.append(child_node.key) + return keys + def compile_to(self, i: Dict, schema, kv_sep) -> str: if schema == "json": return json.dumps(i, indent=4) @@ -343,7 +435,170 @@ class ActionNode: if exclude and i.key in exclude: continue child = await i.simple_fill(schema=schema, mode=mode, timeout=timeout, exclude=exclude) - tmp.update(child.instruct_content.dict()) + tmp.update(child.instruct_content.model_dump()) cls = self.create_children_class() self.instruct_content = cls(**tmp) return self + + async def human_review(self) -> dict[str, str]: + review_comments = HumanInteraction().interact_with_instruct_content( + instruct_content=self.instruct_content, interact_type="review" + ) + + return review_comments + + def _makeup_nodes_output_with_req(self) -> dict[str, str]: + instruct_content_dict = self.instruct_content.model_dump() + nodes_output = {} + for key, value in instruct_content_dict.items(): + child = self.get_child(key) + nodes_output[key] = {"value": value, "requirement": child.instruction if child else self.instruction} + return nodes_output + + async def auto_review(self, template: str = REVIEW_TEMPLATE) -> dict[str, str]: + """use key's output value and its instruction to review the modification comment""" + nodes_output = self._makeup_nodes_output_with_req() + """nodes_output format: + { + "key": {"value": "output value", "requirement": "key instruction"} + } + """ + if not nodes_output: + return dict() + + prompt = template.format( + nodes_output=json.dumps(nodes_output, ensure_ascii=False, indent=4), tag=TAG, constraint=FORMAT_CONSTRAINT + ) + + content = await self.llm.aask(prompt) + # Extract the dict of mismatch key and its comment. Due to the mismatch keys are unknown, here use the keys + # of ActionNode to judge if exist in `content` and then follow the `data_mapping` method to create model class. + keys = self.keys() + include_keys = [] + for key in keys: + if f'"{key}":' in content: + include_keys.append(key) + if not include_keys: + return dict() + + exclude_keys = list(set(keys).difference(include_keys)) + output_class_name = f"{self.key}_AN_REVIEW" + output_class = self.create_class(class_name=output_class_name, exclude=exclude_keys) + parsed_data = llm_output_postprocess( + output=content, schema=output_class.model_json_schema(), req_key=f"[/{TAG}]" + ) + instruct_content = output_class(**parsed_data) + return instruct_content.model_dump() + + async def simple_review(self, review_mode: ReviewMode = ReviewMode.AUTO): + # generate review comments + if review_mode == ReviewMode.HUMAN: + review_comments = await self.human_review() + else: + review_comments = await self.auto_review() + + if not review_comments: + logger.warning("There are no review comments") + return review_comments + + async def review(self, strgy: str = "simple", review_mode: ReviewMode = ReviewMode.AUTO): + """only give the review comment of each exist and mismatch key + + :param strgy: simple/complex + - simple: run only once + - complex: run each node + """ + if not hasattr(self, "llm"): + raise RuntimeError("use `review` after `fill`") + assert review_mode in ReviewMode + assert self.instruct_content, 'review only support with `schema != "raw"`' + + if strgy == "simple": + review_comments = await self.simple_review(review_mode) + elif strgy == "complex": + # review each child node one-by-one + review_comments = {} + for _, child in self.children.items(): + child_review_comment = await child.simple_review(review_mode) + review_comments.update(child_review_comment) + + return review_comments + + async def human_revise(self) -> dict[str, str]: + review_contents = HumanInteraction().interact_with_instruct_content( + instruct_content=self.instruct_content, mapping=self.get_mapping(mode="auto"), interact_type="revise" + ) + # re-fill the ActionNode + self.update_instruct_content(review_contents) + return review_contents + + def _makeup_nodes_output_with_comment(self, review_comments: dict[str, str]) -> dict[str, str]: + instruct_content_dict = self.instruct_content.model_dump() + nodes_output = {} + for key, value in instruct_content_dict.items(): + if key in review_comments: + nodes_output[key] = {"value": value, "comment": review_comments[key]} + return nodes_output + + async def auto_revise(self, template: str = REVISE_TEMPLATE) -> dict[str, str]: + """revise the value of incorrect keys""" + # generate review comments + review_comments: dict = await self.auto_review() + include_keys = list(review_comments.keys()) + + # generate revise content + nodes_output = self._makeup_nodes_output_with_comment(review_comments) + keys = self.keys() + exclude_keys = list(set(keys).difference(include_keys)) + example = self.compile_example(schema="json", mode="auto", tag=TAG, exclude=exclude_keys) + instruction = self.compile_instruction(schema="markdown", mode="auto", exclude=exclude_keys) + + prompt = template.format( + nodes_output=json.dumps(nodes_output, ensure_ascii=False, indent=4), + example=example, + instruction=instruction, + constraint=FORMAT_CONSTRAINT, + ) + + output_mapping = self.get_mapping(mode="auto", exclude=exclude_keys) + output_class_name = f"{self.key}_AN_REVISE" + content, scontent = await self._aask_v1( + prompt=prompt, output_class_name=output_class_name, output_data_mapping=output_mapping, schema="json" + ) + + # re-fill the ActionNode + sc_dict = scontent.model_dump() + self.update_instruct_content(sc_dict) + return sc_dict + + async def simple_revise(self, revise_mode: ReviseMode = ReviseMode.AUTO) -> dict[str, str]: + if revise_mode == ReviseMode.HUMAN: + revise_contents = await self.human_revise() + else: + revise_contents = await self.auto_revise() + + return revise_contents + + async def revise(self, strgy: str = "simple", revise_mode: ReviseMode = ReviseMode.AUTO) -> dict[str, str]: + """revise the content of ActionNode and update the instruct_content + + :param strgy: simple/complex + - simple: run only once + - complex: run each node + """ + if not hasattr(self, "llm"): + raise RuntimeError("use `revise` after `fill`") + assert revise_mode in ReviseMode + assert self.instruct_content, 'revise only support with `schema != "raw"`' + + if strgy == "simple": + revise_contents = await self.simple_revise(revise_mode) + elif strgy == "complex": + # revise each child node one-by-one + revise_contents = {} + for _, child in self.children.items(): + child_revise_content = await child.simple_revise(revise_mode) + revise_contents.update(child_revise_content) + self.update_instruct_content(revise_contents) + + return revise_contents diff --git a/metagpt/utils/human_interaction.py b/metagpt/utils/human_interaction.py new file mode 100644 index 000000000..3b245cac8 --- /dev/null +++ b/metagpt/utils/human_interaction.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : human interaction to get required type text + +import json +from typing import Any, Tuple, Type + +from pydantic import BaseModel + +from metagpt.logs import logger +from metagpt.utils.common import import_class + + +class HumanInteraction(object): + stop_list = ("q", "quit", "exit") + + def multilines_input(self, prompt: str = "Enter: ") -> str: + logger.warning("Enter your content, use Ctrl-D or Ctrl-Z ( windows ) to save it.") + logger.info(f"{prompt}\n") + lines = [] + while True: + try: + line = input() + lines.append(line) + except EOFError: + break + return "".join(lines) + + def check_input_type(self, input_str: str, req_type: Type) -> Tuple[bool, Any]: + check_ret = True + if req_type == str: + # required_type = str, just return True + return check_ret, input_str + try: + input_str = input_str.strip() + data = json.loads(input_str) + except Exception: + return False, None + + actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import + tmp_key = "tmp" + tmp_cls = actionnode_class.create_model_class(class_name=tmp_key.upper(), mapping={tmp_key: (req_type, ...)}) + try: + _ = tmp_cls(**{tmp_key: data}) + except Exception: + check_ret = False + return check_ret, data + + def input_until_valid(self, prompt: str, req_type: Type) -> Any: + # check the input with req_type until it's ok + while True: + input_content = self.multilines_input(prompt) + check_ret, structure_content = self.check_input_type(input_content, req_type) + if check_ret: + break + else: + logger.error(f"Input content can't meet required_type: {req_type}, please Re-Enter.") + return structure_content + + def input_num_until_valid(self, num_max: int) -> int: + while True: + input_num = input("Enter the num of the interaction key: ") + input_num = input_num.strip() + if input_num in self.stop_list: + return input_num + try: + input_num = int(input_num) + if 0 <= input_num < num_max: + return input_num + except Exception: + pass + + def interact_with_instruct_content( + self, instruct_content: BaseModel, mapping: dict = dict(), interact_type: str = "review" + ) -> dict[str, Any]: + assert interact_type in ["review", "revise"] + assert instruct_content + instruct_content_dict = instruct_content.model_dump() + num_fields_map = dict(zip(range(0, len(instruct_content_dict)), instruct_content_dict.keys())) + logger.info( + f"\n{interact_type.upper()} interaction\n" + f"Interaction data: {num_fields_map}\n" + f"Enter the num to interact with corresponding field or `q`/`quit`/`exit` to stop interaction.\n" + f"Enter the field content until it meet field required type.\n" + ) + + interact_contents = {} + while True: + input_num = self.input_num_until_valid(len(instruct_content_dict)) + if input_num in self.stop_list: + logger.warning("Stop human interaction") + break + + field = num_fields_map.get(input_num) + logger.info(f"You choose to interact with field: {field}, and do a `{interact_type}` operation.") + + if interact_type == "review": + prompt = "Enter your review comment: " + req_type = str + else: + prompt = "Enter your revise content: " + req_type = mapping.get(field)[0] # revise need input content match the required_type + + field_content = self.input_until_valid(prompt=prompt, req_type=req_type) + interact_contents[field] = field_content + + return interact_contents diff --git a/tests/metagpt/actions/test_action_node.py b/tests/metagpt/actions/test_action_node.py index 384c4507b..fd2c83ac9 100644 --- a/tests/metagpt/actions/test_action_node.py +++ b/tests/metagpt/actions/test_action_node.py @@ -11,7 +11,7 @@ import pytest from pydantic import ValidationError from metagpt.actions import Action -from metagpt.actions.action_node import ActionNode +from metagpt.actions.action_node import ActionNode, ReviewMode, ReviseMode from metagpt.environment import Environment from metagpt.llm import LLM from metagpt.roles import Role @@ -98,6 +98,83 @@ async def test_action_node_two_layer(): assert "579" in answer2.content +@pytest.mark.asyncio +async def test_action_node_review(): + key = "Project Name" + node_a = ActionNode( + key=key, + expected_type=str, + instruction='According to the content of "Original Requirements," name the project using snake case style ' + "with underline, like 'game_2048' or 'simple_crm.", + example="game_2048", + ) + + with pytest.raises(RuntimeError): + _ = await node_a.review() + + _ = await node_a.fill(context=None, llm=LLM()) + setattr(node_a.instruct_content, key, "game snake") # wrong content to review + + review_comments = await node_a.review(review_mode=ReviewMode.AUTO) + assert len(review_comments) == 1 + assert list(review_comments.keys())[0] == key + + review_comments = await node_a.review(strgy="complex", review_mode=ReviewMode.AUTO) + assert len(review_comments) == 0 + + node = ActionNode.from_children(key="WritePRD", nodes=[node_a]) + with pytest.raises(RuntimeError): + _ = await node.review() + + _ = await node.fill(context=None, llm=LLM()) + + review_comments = await node.review(review_mode=ReviewMode.AUTO) + assert len(review_comments) == 1 + assert list(review_comments.keys())[0] == key + + review_comments = await node.review(strgy="complex", review_mode=ReviewMode.AUTO) + assert len(review_comments) == 1 + assert list(review_comments.keys())[0] == key + + +@pytest.mark.asyncio +async def test_action_node_revise(): + key = "Project Name" + node_a = ActionNode( + key=key, + expected_type=str, + instruction='According to the content of "Original Requirements," name the project using snake case style ' + "with underline, like 'game_2048' or 'simple_crm.", + example="game_2048", + ) + + with pytest.raises(RuntimeError): + _ = await node_a.review() + + _ = await node_a.fill(context=None, llm=LLM()) + setattr(node_a.instruct_content, key, "game snake") # wrong content to revise + revise_contents = await node_a.revise(revise_mode=ReviseMode.AUTO) + assert len(revise_contents) == 1 + assert "game_snake" in getattr(node_a.instruct_content, key) + + revise_contents = await node_a.revise(strgy="complex", revise_mode=ReviseMode.AUTO) + assert len(revise_contents) == 0 + + node = ActionNode.from_children(key="WritePRD", nodes=[node_a]) + with pytest.raises(RuntimeError): + _ = await node.revise() + + _ = await node.fill(context=None, llm=LLM()) + setattr(node.instruct_content, key, "game snake") + revise_contents = await node.revise(revise_mode=ReviseMode.AUTO) + assert len(revise_contents) == 1 + assert "game_snake" in getattr(node.instruct_content, key) + + revise_contents = await node.revise(strgy="complex", revise_mode=ReviseMode.AUTO) + assert len(revise_contents) == 1 + assert "game_snake" in getattr(node.instruct_content, key) + + t_dict = { "Required Python third-party packages": '"""\nflask==1.1.2\npygame==2.0.1\n"""\n', "Required Other language third-party packages": '"""\nNo third-party packages required for other languages.\n"""\n', @@ -138,10 +215,10 @@ def test_create_model_class(): assert test_class.__name__ == "test_class" output = test_class(**t_dict) - print(output.schema()) - assert output.schema()["title"] == "test_class" - assert output.schema()["type"] == "object" - assert output.schema()["properties"]["Full API spec"] + print(output.model_json_schema()) + assert output.model_json_schema()["title"] == "test_class" + assert output.model_json_schema()["type"] == "object" + assert output.model_json_schema()["properties"]["Full API spec"] def test_create_model_class_with_fields_unrecognized(): diff --git a/tests/metagpt/utils/test_human_interaction.py b/tests/metagpt/utils/test_human_interaction.py new file mode 100644 index 000000000..038fc0d98 --- /dev/null +++ b/tests/metagpt/utils/test_human_interaction.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of human_interaction + +import pytest + +from pydantic import BaseModel + +from metagpt.utils.human_interaction import HumanInteraction + + +class InstructContent(BaseModel): + test_field1: str = "" + test_field2: list[str] = [] + + +data_mapping = { + "test_field1": (str, ...), + "test_field2": (list[str], ...) +} + +human_interaction = HumanInteraction() + + +def test_input_num(mocker): + mocker.patch("builtins.input", lambda _: "quit") + + interact_contents = human_interaction.interact_with_instruct_content(InstructContent(), data_mapping) + assert len(interact_contents) == 0 + + mocker.patch("builtins.input", lambda _: "1") + input_num = human_interaction.input_num_until_valid(2) + assert input_num == 1 + + +def test_check_input_type(): + ret, _ = human_interaction.check_input_type(input_str="test string", + req_type=str) + assert ret + + ret, _ = human_interaction.check_input_type(input_str='["test string"]', + req_type=list[str]) + assert ret + + ret, _ = human_interaction.check_input_type(input_str='{"key", "value"}', + req_type=list[str]) + assert not ret + + +global_index = 0 + + +def mock_input(*args, **kwargs): + """there are multi input call, return it by global_index""" + arr = ["1", '["test"]', "ignore", "quit"] + global global_index + global_index += 1 + if global_index == 3: + raise EOFError() + val = arr[global_index-1] + return val + + +def test_human_interact_valid_content(mocker): + mocker.patch("builtins.input", mock_input) + input_contents = HumanInteraction().interact_with_instruct_content(InstructContent(), data_mapping, "review") + assert len(input_contents) == 1 + assert input_contents["test_field2"] == '["test"]' + + global global_index + global_index = 0 + input_contents = HumanInteraction().interact_with_instruct_content(InstructContent(), data_mapping, "revise") + assert len(input_contents) == 1 + assert input_contents["test_field2"] == ["test"] From 57db3b775aa4f80cd4eb1f5420e86e0dda0a68ec Mon Sep 17 00:00:00 2001 From: better629 Date: Mon, 8 Jan 2024 16:23:46 +0800 Subject: [PATCH 02/50] fix format --- tests/metagpt/utils/test_human_interaction.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/metagpt/utils/test_human_interaction.py b/tests/metagpt/utils/test_human_interaction.py index 038fc0d98..24dbac61c 100644 --- a/tests/metagpt/utils/test_human_interaction.py +++ b/tests/metagpt/utils/test_human_interaction.py @@ -2,8 +2,6 @@ # -*- coding: utf-8 -*- # @Desc : unittest of human_interaction -import pytest - from pydantic import BaseModel from metagpt.utils.human_interaction import HumanInteraction @@ -14,10 +12,7 @@ class InstructContent(BaseModel): test_field2: list[str] = [] -data_mapping = { - "test_field1": (str, ...), - "test_field2": (list[str], ...) -} +data_mapping = {"test_field1": (str, ...), "test_field2": (list[str], ...)} human_interaction = HumanInteraction() @@ -34,16 +29,13 @@ def test_input_num(mocker): def test_check_input_type(): - ret, _ = human_interaction.check_input_type(input_str="test string", - req_type=str) + ret, _ = human_interaction.check_input_type(input_str="test string", req_type=str) assert ret - ret, _ = human_interaction.check_input_type(input_str='["test string"]', - req_type=list[str]) + ret, _ = human_interaction.check_input_type(input_str='["test string"]', req_type=list[str]) assert ret - ret, _ = human_interaction.check_input_type(input_str='{"key", "value"}', - req_type=list[str]) + ret, _ = human_interaction.check_input_type(input_str='{"key", "value"}', req_type=list[str]) assert not ret @@ -57,7 +49,7 @@ def mock_input(*args, **kwargs): global_index += 1 if global_index == 3: raise EOFError() - val = arr[global_index-1] + val = arr[global_index - 1] return val From 32ae369c00a7c141338eff4a636b88de2655adeb Mon Sep 17 00:00:00 2001 From: better629 Date: Mon, 8 Jan 2024 17:35:28 +0800 Subject: [PATCH 03/50] add revise_mode=HUMAN_REVIEW to support human_review and auto_revise --- metagpt/actions/action_node.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 6dca00df0..2d6782952 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -29,8 +29,9 @@ class ReviewMode(Enum): class ReviseMode(Enum): - HUMAN = "human" - AUTO = "auto" + HUMAN = "human" # human revise + HUMAN_REVIEW = "human_review" # human-review and auto-revise + AUTO = "auto" # auto-review and auto-revise TAG = "CONTENT" @@ -540,10 +541,16 @@ class ActionNode: nodes_output[key] = {"value": value, "comment": review_comments[key]} return nodes_output - async def auto_revise(self, template: str = REVISE_TEMPLATE) -> dict[str, str]: + async def auto_revise( + self, revise_mode: ReviseMode = ReviseMode.AUTO, template: str = REVISE_TEMPLATE + ) -> dict[str, str]: """revise the value of incorrect keys""" # generate review comments - review_comments: dict = await self.auto_review() + if revise_mode == ReviseMode.AUTO: + review_comments: dict = await self.auto_review() + elif revise_mode == ReviseMode.HUMAN_REVIEW: + review_comments: dict = await self.human_review() + include_keys = list(review_comments.keys()) # generate revise content @@ -575,7 +582,7 @@ class ActionNode: if revise_mode == ReviseMode.HUMAN: revise_contents = await self.human_revise() else: - revise_contents = await self.auto_revise() + revise_contents = await self.auto_revise(revise_mode) return revise_contents From 98ee696cf0fb28874c9b06e697be2b4f824ba61d Mon Sep 17 00:00:00 2001 From: better629 Date: Mon, 8 Jan 2024 22:15:56 +0800 Subject: [PATCH 04/50] rm expicit serialize&deserialize interface and update unittests --- metagpt/actions/action.py | 2 +- metagpt/environment.py | 41 +--------- metagpt/memory/memory.py | 24 +----- metagpt/roles/role.py | 53 ++----------- metagpt/schema.py | 74 +++++++++---------- metagpt/team.py | 15 +--- metagpt/utils/make_sk_kernel.py | 4 +- .../serialize_deserialize/test_action.py | 15 ++-- ...itect_deserialize.py => test_architect.py} | 9 +-- .../serialize_deserialize/test_environment.py | 21 +++--- .../serialize_deserialize/test_memory.py | 12 +-- .../serialize_deserialize/test_polymorphic.py | 9 ++- .../test_prepare_interview.py | 2 +- .../test_product_manager.py | 2 +- .../test_project_manager.py | 9 +-- .../serialize_deserialize/test_reasearcher.py | 2 +- .../serialize_deserialize/test_role.py | 41 +++++----- .../serialize_deserialize/test_sk_agent.py | 9 +-- .../serialize_deserialize/test_team.py | 42 +++++++---- .../test_tutorial_assistant.py | 2 +- .../serialize_deserialize/test_write_code.py | 4 +- .../test_write_code_review.py | 2 +- .../test_write_design.py | 32 +++----- .../test_write_docstring.py | 2 +- .../serialize_deserialize/test_write_prd.py | 10 +-- .../test_write_review.py | 2 +- .../test_write_tutorial.py | 4 +- 27 files changed, 154 insertions(+), 290 deletions(-) rename tests/metagpt/serialize_deserialize/{test_architect_deserialize.py => test_architect.py} (76%) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 24357a700..9f045bbaa 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -27,7 +27,7 @@ from metagpt.schema import ( from metagpt.utils.file_repository import FileRepository -class Action(SerializationMixin, is_polymorphic_base=True): +class Action(SerializationMixin): model_config = ConfigDict(arbitrary_types_allowed=True, exclude=["llm"]) name: str = "" diff --git a/metagpt/environment.py b/metagpt/environment.py index 6511647ef..5a2dd339b 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -12,7 +12,6 @@ functionality is to be consolidated into the `Environment` class. """ import asyncio -from pathlib import Path from typing import Iterable, Set from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validator @@ -21,7 +20,7 @@ from metagpt.context import Context from metagpt.logs import logger from metagpt.roles.role import Role from metagpt.schema import Message -from metagpt.utils.common import is_send_to, read_json_file, write_json_file +from metagpt.utils.common import is_send_to class Environment(BaseModel): @@ -42,44 +41,6 @@ class Environment(BaseModel): self.add_roles(self.roles.values()) return self - def serialize(self, stg_path: Path): - roles_path = stg_path.joinpath("roles.json") - roles_info = [] - for role_key, role in self.roles.items(): - roles_info.append( - { - "role_class": role.__class__.__name__, - "module_name": role.__module__, - "role_name": role.name, - "role_sub_tags": list(self.member_addrs.get(role)), - } - ) - role.serialize(stg_path=stg_path.joinpath(f"roles/{role.__class__.__name__}_{role.name}")) - write_json_file(roles_path, roles_info) - - history_path = stg_path.joinpath("history.json") - write_json_file(history_path, {"content": self.history}) - - @classmethod - def deserialize(cls, stg_path: Path) -> "Environment": - """stg_path: ./storage/team/environment/""" - roles_path = stg_path.joinpath("roles.json") - roles_info = read_json_file(roles_path) - roles = [] - for role_info in roles_info: - # role stored in ./environment/roles/{role_class}_{role_name} - role_path = stg_path.joinpath(f"roles/{role_info.get('role_class')}_{role_info.get('role_name')}") - role = Role.deserialize(role_path) - roles.append(role) - - history = read_json_file(stg_path.joinpath("history.json")) - history = history.get("content") - - environment = Environment(**{"history": history}) - environment.add_roles(roles) - - return environment - def add_role(self, role: Role): """增加一个在当前环境的角色 Add a role in the current environment diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 593409648..580361d33 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -7,19 +7,13 @@ @Modified By: mashenquan, 2023-11-1. According to RFC 116: Updated the type of index key. """ from collections import defaultdict -from pathlib import Path from typing import DefaultDict, Iterable, Set from pydantic import BaseModel, Field, SerializeAsAny from metagpt.const import IGNORED_MESSAGE_ID from metagpt.schema import Message -from metagpt.utils.common import ( - any_to_str, - any_to_str_set, - read_json_file, - write_json_file, -) +from metagpt.utils.common import any_to_str, any_to_str_set class Memory(BaseModel): @@ -29,22 +23,6 @@ class Memory(BaseModel): index: DefaultDict[str, list[SerializeAsAny[Message]]] = Field(default_factory=lambda: defaultdict(list)) ignore_id: bool = False - def serialize(self, stg_path: Path): - """stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/""" - memory_path = stg_path.joinpath("memory.json") - storage = self.model_dump() - write_json_file(memory_path, storage) - - @classmethod - def deserialize(cls, stg_path: Path) -> "Memory": - """stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/""" - memory_path = stg_path.joinpath("memory.json") - - memory_dict = read_json_file(memory_path) - memory = Memory(**memory_dict) - - return memory - def add(self, message: Message): """Add a new message to storage, while updating the index""" if self.ignore_id: diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index cdb2da40a..73d82e369 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -23,7 +23,6 @@ from __future__ import annotations from enum import Enum -from pathlib import Path from typing import Any, Iterable, Optional, Set, Type from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validator @@ -31,7 +30,6 @@ from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validat from metagpt.actions import Action, ActionOutput from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement -from metagpt.const import SERDESER_PATH from metagpt.context import Context, context from metagpt.llm import LLM from metagpt.logs import logger @@ -39,14 +37,7 @@ from metagpt.memory import Memory from metagpt.provider import HumanProvider from metagpt.provider.base_llm import BaseLLM from metagpt.schema import Message, MessageQueue, SerializationMixin -from metagpt.utils.common import ( - any_to_name, - any_to_str, - import_class, - read_json_file, - role_raise_decorator, - write_json_file, -) +from metagpt.utils.common import any_to_name, any_to_str, role_raise_decorator from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}. """ @@ -128,7 +119,7 @@ class RoleContext(BaseModel): return self.memory.get() -class Role(SerializationMixin, is_polymorphic_base=True): +class Role(SerializationMixin): """Role/Agent""" model_config = ConfigDict(arbitrary_types_allowed=True, exclude=["llm"]) @@ -217,6 +208,9 @@ class Role(SerializationMixin, is_polymorphic_base=True): self.llm.system_prompt = self._get_prefix() self._watch(data.get("watch") or [UserRequirement]) + if self.latest_observed_msg: + self.recovered = True + def _reset(self): self.states = [] self.actions = [] @@ -225,47 +219,12 @@ class Role(SerializationMixin, is_polymorphic_base=True): def _setting(self): return f"{self.name}({self.profile})" - def serialize(self, stg_path: Path = None): - stg_path = ( - SERDESER_PATH.joinpath(f"team/environment/roles/{self.__class__.__name__}_{self.name}") - if stg_path is None - else stg_path - ) - - role_info = self.model_dump(exclude={"rc": {"memory": True, "msg_buffer": True}, "llm": True}) - role_info.update({"role_class": self.__class__.__name__, "module_name": self.__module__}) - role_info_path = stg_path.joinpath("role_info.json") - write_json_file(role_info_path, role_info) - - self.rc.memory.serialize(stg_path) # serialize role's memory alone - - @classmethod - def deserialize(cls, stg_path: Path) -> "Role": - """stg_path = ./storage/team/environment/roles/{role_class}_{role_name}""" - role_info_path = stg_path.joinpath("role_info.json") - role_info = read_json_file(role_info_path) - - role_class_str = role_info.pop("role_class") - module_name = role_info.pop("module_name") - role_class = import_class(class_name=role_class_str, module_name=module_name) - - role = role_class(**role_info) # initiate particular Role - role.set_recovered(True) # set True to make a tag - - role_memory = Memory.deserialize(stg_path) - role.set_memory(role_memory) - - return role - def _init_action_system_message(self, action: Action): action.set_prefix(self._get_prefix()) def refresh_system_message(self): self.llm.system_prompt = self._get_prefix() - def set_recovered(self, recovered: bool = False): - self.recovered = recovered - def set_memory(self, memory: Memory): self.rc.memory = memory @@ -376,7 +335,7 @@ class Role(SerializationMixin, is_polymorphic_base=True): if self.recovered and self.rc.state >= 0: self._set_state(self.rc.state) # action to run from recovered state - self.set_recovered(False) # avoid max_react_loop out of work + self.recovered = False # avoid max_react_loop out of work return True prompt = self._get_prefix() diff --git a/metagpt/schema.py b/metagpt/schema.py index cf24fbc6f..a557951c7 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -23,7 +23,7 @@ from abc import ABC from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError from pathlib import Path -from typing import Any, Callable, Dict, List, Optional, Type, TypeVar, Union +from typing import Any, Dict, List, Optional, Type, TypeVar, Union from pydantic import ( BaseModel, @@ -32,8 +32,9 @@ from pydantic import ( PrivateAttr, field_serializer, field_validator, + model_serializer, + model_validator, ) -from pydantic_core import core_schema from metagpt.const import ( MESSAGE_ROUTE_CAUSE_BY, @@ -53,7 +54,7 @@ from metagpt.utils.serialize import ( ) -class SerializationMixin(BaseModel): +class SerializationMixin(BaseModel, extra="forbid"): """ PolyMorphic subclasses Serialization / Deserialization Mixin - First of all, we need to know that pydantic is not designed for polymorphism. @@ -68,49 +69,44 @@ class SerializationMixin(BaseModel): __is_polymorphic_base = False __subclasses_map__ = {} - @classmethod - def __get_pydantic_core_schema__( - cls, source: type["SerializationMixin"], handler: Callable[[Any], core_schema.CoreSchema] - ) -> core_schema.CoreSchema: - schema = handler(source) - og_schema_ref = schema["ref"] - schema["ref"] += ":mixin" - - return core_schema.no_info_before_validator_function( - cls.__deserialize_with_real_type__, - schema=schema, - ref=og_schema_ref, - serialization=core_schema.wrap_serializer_function_ser_schema(cls.__serialize_add_class_type__), - ) - - @classmethod - def __serialize_add_class_type__( - cls, - value, - handler: core_schema.SerializerFunctionWrapHandler, - ) -> Any: - ret = handler(value) - if not len(cls.__subclasses__()): - # only subclass add `__module_class_name` - ret["__module_class_name"] = f"{cls.__module__}.{cls.__qualname__}" + @model_serializer(mode="wrap") + def __serialize_with_class_type__(self, default_serializer) -> Any: + # default serializer, then append the `__module_class_name` field and return + ret = default_serializer(self) + ret["__module_class_name"] = f"{self.__class__.__module__}.{self.__class__.__qualname__}" return ret + @model_validator(mode="wrap") @classmethod - def __deserialize_with_real_type__(cls, value: Any): - if not isinstance(value, dict): - return value + def __convert_to_real_type__(cls, value: Any, handler): + if isinstance(value, dict) is False: + return handler(value) - if not cls.__is_polymorphic_base or (len(cls.__subclasses__()) and "__module_class_name" not in value): - # add right condition to init BaseClass like Action() - return value - module_class_name = value.get("__module_class_name", None) - if module_class_name is None: - raise ValueError("Missing field: __module_class_name") + # it is a dict so make sure to remove the __module_class_name + # because we don't allow extra keywords but want to ensure + # e.g Cat.model_validate(cat.model_dump()) works + class_full_name = value.pop("__module_class_name", None) - class_type = cls.__subclasses_map__.get(module_class_name, None) + # if it's not the polymorphic base we construct via default handler + if not cls.__is_polymorphic_base: + if class_full_name is None: + return handler(value) + elif str(cls) == f"": + return handler(value) + else: + # f"Trying to instantiate {class_full_name} but this is not the polymorphic base class") + pass + + # otherwise we lookup the correct polymorphic type and construct that + # instead + if class_full_name is None: + raise ValueError("Missing __module_class_name field") + + class_type = cls.__subclasses_map__.get(class_full_name, None) if class_type is None: - raise TypeError("Trying to instantiate {module_class_name} which not defined yet.") + # TODO could try dynamic import + raise TypeError("Trying to instantiate {class_full_name}, which has not yet been defined!") return class_type(**value) diff --git a/metagpt/team.py b/metagpt/team.py index 87fee8dc7..96a27d482 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -49,28 +49,21 @@ class Team(BaseModel): def serialize(self, stg_path: Path = None): stg_path = SERDESER_PATH.joinpath("team") if stg_path is None else stg_path + team_info_path = stg_path.joinpath("team.json") - team_info_path = stg_path.joinpath("team_info.json") - write_json_file(team_info_path, self.model_dump(exclude={"env": True})) - - self.env.serialize(stg_path.joinpath("environment")) # save environment alone + write_json_file(team_info_path, self.model_dump()) @classmethod def deserialize(cls, stg_path: Path) -> "Team": """stg_path = ./storage/team""" # recover team_info - team_info_path = stg_path.joinpath("team_info.json") + team_info_path = stg_path.joinpath("team.json") if not team_info_path.exists(): raise FileNotFoundError( - "recover storage meta file `team_info.json` not exist, " - "not to recover and please start a new project." + "recover storage meta file `team.json` not exist, " "not to recover and please start a new project." ) team_info: dict = read_json_file(team_info_path) - - # recover environment - environment = Environment.deserialize(stg_path=stg_path.joinpath("environment")) - team_info.update({"env": environment}) team = Team(**team_info) return team diff --git a/metagpt/utils/make_sk_kernel.py b/metagpt/utils/make_sk_kernel.py index 319ba3e34..283a682d6 100644 --- a/metagpt/utils/make_sk_kernel.py +++ b/metagpt/utils/make_sk_kernel.py @@ -18,12 +18,12 @@ from metagpt.config2 import config def make_sk_kernel(): kernel = sk.Kernel() - if llm := config.get_openai_llm(): + if llm := config.get_azure_llm(): kernel.add_chat_service( "chat_completion", AzureChatCompletion(llm.model, llm.base_url, llm.api_key), ) - else: + elif llm := config.get_openai_llm(): kernel.add_chat_service( "chat_completion", OpenAIChatCompletion(llm.model, llm.api_key), diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py index 81879e34e..f66900241 100644 --- a/tests/metagpt/serialize_deserialize/test_action.py +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -8,25 +8,20 @@ from metagpt.actions import Action from metagpt.llm import LLM -def test_action_serialize(): +@pytest.mark.asyncio +async def test_action_serdeser(): action = Action() ser_action_dict = action.model_dump() assert "name" in ser_action_dict assert "llm" not in ser_action_dict # not export - assert "__module_class_name" not in ser_action_dict + assert "__module_class_name" in ser_action_dict action = Action(name="test") ser_action_dict = action.model_dump() assert "test" in ser_action_dict["name"] + new_action = Action(**ser_action_dict) -@pytest.mark.asyncio -async def test_action_deserialize(): - action = Action() - serialized_data = action.model_dump() - - new_action = Action(**serialized_data) - - assert new_action.name == "Action" + assert new_action.name == "test" assert isinstance(new_action.llm, type(LLM())) assert len(await new_action._aask("who are you")) > 0 diff --git a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py b/tests/metagpt/serialize_deserialize/test_architect.py similarity index 76% rename from tests/metagpt/serialize_deserialize/test_architect_deserialize.py rename to tests/metagpt/serialize_deserialize/test_architect.py index b113912a7..343662494 100644 --- a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py +++ b/tests/metagpt/serialize_deserialize/test_architect.py @@ -8,20 +8,15 @@ from metagpt.actions.action import Action from metagpt.roles.architect import Architect -def test_architect_serialize(): +@pytest.mark.asyncio +async def test_architect_serdeser(): role = Architect() ser_role_dict = role.model_dump(by_alias=True) assert "name" in ser_role_dict assert "states" in ser_role_dict assert "actions" in ser_role_dict - -@pytest.mark.asyncio -async def test_architect_deserialize(): - role = Architect() - ser_role_dict = role.model_dump(by_alias=True) new_role = Architect(**ser_role_dict) - # new_role = Architect.deserialize(ser_role_dict) assert new_role.name == "Bob" assert len(new_role.actions) == 1 assert isinstance(new_role.actions[0], Action) diff --git a/tests/metagpt/serialize_deserialize/test_environment.py b/tests/metagpt/serialize_deserialize/test_environment.py index 5a68288a6..3e2a3abba 100644 --- a/tests/metagpt/serialize_deserialize/test_environment.py +++ b/tests/metagpt/serialize_deserialize/test_environment.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- # @Desc : -import shutil from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement @@ -10,7 +9,7 @@ from metagpt.actions.project_management import WriteTasks from metagpt.environment import Environment from metagpt.roles.project_manager import ProjectManager from metagpt.schema import Message -from metagpt.utils.common import any_to_str +from metagpt.utils.common import any_to_str, read_json_file, write_json_file from tests.metagpt.serialize_deserialize.test_serdeser_base import ( ActionOK, ActionRaise, @@ -19,17 +18,14 @@ from tests.metagpt.serialize_deserialize.test_serdeser_base import ( ) -def test_env_serialize(): +def test_env_serdeser(): env = Environment() + env.publish_message(message=Message(content="test env serialize")) + ser_env_dict = env.model_dump() assert "roles" in ser_env_dict assert len(ser_env_dict["roles"]) == 0 - -def test_env_deserialize(): - env = Environment() - env.publish_message(message=Message(content="test env serialize")) - ser_env_dict = env.model_dump() new_env = Environment(**ser_env_dict) assert len(new_env.roles) == 0 assert len(new_env.history) == 25 @@ -79,12 +75,13 @@ def test_environment_serdeser_save(): environment = Environment() role_c = RoleC() - shutil.rmtree(serdeser_path.joinpath("team"), ignore_errors=True) - stg_path = serdeser_path.joinpath("team", "environment") + env_path = stg_path.joinpath("env.json") environment.add_role(role_c) - environment.serialize(stg_path) - new_env: Environment = Environment.deserialize(stg_path) + write_json_file(env_path, environment.model_dump()) + + env_dict = read_json_file(env_path) + new_env: Environment = Environment(**env_dict) assert len(new_env.roles) == 1 assert type(list(new_env.roles.values())[0].actions[0]) == ActionOK diff --git a/tests/metagpt/serialize_deserialize/test_memory.py b/tests/metagpt/serialize_deserialize/test_memory.py index aa3e2a465..fdaea7861 100644 --- a/tests/metagpt/serialize_deserialize/test_memory.py +++ b/tests/metagpt/serialize_deserialize/test_memory.py @@ -9,7 +9,7 @@ from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.design_api import WriteDesign from metagpt.memory.memory import Memory from metagpt.schema import Message -from metagpt.utils.common import any_to_str +from metagpt.utils.common import any_to_str, read_json_file, write_json_file from tests.metagpt.serialize_deserialize.test_serdeser_base import serdeser_path @@ -53,14 +53,14 @@ def test_memory_serdeser_save(): memory.add_batch([msg1, msg2]) stg_path = serdeser_path.joinpath("team", "environment") - memory.serialize(stg_path) - assert stg_path.joinpath("memory.json").exists() + memory_path = stg_path.joinpath("memory.json") + write_json_file(memory_path, memory.model_dump()) + assert memory_path.exists() - new_memory = Memory.deserialize(stg_path) + memory_dict = read_json_file(memory_path) + new_memory = Memory(**memory_dict) assert new_memory.count() == 2 new_msg2 = new_memory.get(1)[0] assert new_msg2.instruct_content.field1 == ["field1 value1", "field1 value2"] assert new_msg2.cause_by == any_to_str(WriteDesign) assert len(new_memory.index) == 2 - - stg_path.joinpath("memory.json").unlink() diff --git a/tests/metagpt/serialize_deserialize/test_polymorphic.py b/tests/metagpt/serialize_deserialize/test_polymorphic.py index ed0482c34..e5f8ec8d6 100644 --- a/tests/metagpt/serialize_deserialize/test_polymorphic.py +++ b/tests/metagpt/serialize_deserialize/test_polymorphic.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # @Desc : unittest of polymorphic conditions +import copy from pydantic import BaseModel, ConfigDict, SerializeAsAny @@ -12,6 +13,8 @@ from tests.metagpt.serialize_deserialize.test_serdeser_base import ( class ActionSubClasses(BaseModel): + model_config = ConfigDict(arbitrary_types_allowed=True) + actions: list[SerializeAsAny[Action]] = [] @@ -40,19 +43,21 @@ def test_no_serialize_as_any(): def test_polymorphic(): - _ = ActionOKV2( + ok_v2 = ActionOKV2( **{"name": "ActionOKV2", "context": "", "prefix": "", "desc": "", "extra_field": "ActionOKV2 Extra Info"} ) action_subcls = ActionSubClasses(actions=[ActionOKV2(), ActionPass()]) action_subcls_dict = action_subcls.model_dump() + action_subcls_dict2 = copy.deepcopy(action_subcls_dict) assert "__module_class_name" in action_subcls_dict["actions"][0] new_action_subcls = ActionSubClasses(**action_subcls_dict) assert isinstance(new_action_subcls.actions[0], ActionOKV2) + assert new_action_subcls.actions[0].extra_field == ok_v2.extra_field assert isinstance(new_action_subcls.actions[1], ActionPass) - new_action_subcls = ActionSubClasses.model_validate(action_subcls_dict) + new_action_subcls = ActionSubClasses.model_validate(action_subcls_dict2) assert isinstance(new_action_subcls.actions[0], ActionOKV2) assert isinstance(new_action_subcls.actions[1], ActionPass) diff --git a/tests/metagpt/serialize_deserialize/test_prepare_interview.py b/tests/metagpt/serialize_deserialize/test_prepare_interview.py index cd9912103..3b57aa27e 100644 --- a/tests/metagpt/serialize_deserialize/test_prepare_interview.py +++ b/tests/metagpt/serialize_deserialize/test_prepare_interview.py @@ -8,7 +8,7 @@ from metagpt.actions.prepare_interview import PrepareInterview @pytest.mark.asyncio -async def test_action_deserialize(): +async def test_action_serdeser(): action = PrepareInterview() serialized_data = action.model_dump() assert serialized_data["name"] == "PrepareInterview" diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py index 094943900..1a056f9d4 100644 --- a/tests/metagpt/serialize_deserialize/test_product_manager.py +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -10,7 +10,7 @@ from metagpt.schema import Message @pytest.mark.asyncio -async def test_product_manager_deserialize(new_filename): +async def test_product_manager_serdeser(new_filename): role = ProductManager() ser_role_dict = role.model_dump(by_alias=True) new_role = ProductManager(**ser_role_dict) diff --git a/tests/metagpt/serialize_deserialize/test_project_manager.py b/tests/metagpt/serialize_deserialize/test_project_manager.py index 1088a4461..f2c5af853 100644 --- a/tests/metagpt/serialize_deserialize/test_project_manager.py +++ b/tests/metagpt/serialize_deserialize/test_project_manager.py @@ -9,19 +9,14 @@ from metagpt.actions.project_management import WriteTasks from metagpt.roles.project_manager import ProjectManager -def test_project_manager_serialize(): +@pytest.mark.asyncio +async def test_project_manager_serdeser(): role = ProjectManager() ser_role_dict = role.model_dump(by_alias=True) assert "name" in ser_role_dict assert "states" in ser_role_dict assert "actions" in ser_role_dict - -@pytest.mark.asyncio -async def test_project_manager_deserialize(): - role = ProjectManager() - ser_role_dict = role.model_dump(by_alias=True) - new_role = ProjectManager(**ser_role_dict) assert new_role.name == "Eve" assert len(new_role.actions) == 1 diff --git a/tests/metagpt/serialize_deserialize/test_reasearcher.py b/tests/metagpt/serialize_deserialize/test_reasearcher.py index 1b8dbf2c7..a2d1fa513 100644 --- a/tests/metagpt/serialize_deserialize/test_reasearcher.py +++ b/tests/metagpt/serialize_deserialize/test_reasearcher.py @@ -8,7 +8,7 @@ from metagpt.roles.researcher import Researcher @pytest.mark.asyncio -async def test_tutorial_assistant_deserialize(): +async def test_tutorial_assistant_serdeser(): role = Researcher() ser_role_dict = role.model_dump() assert "name" in ser_role_dict diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index d38797baf..bbfe350b7 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -10,13 +10,12 @@ from pydantic import BaseModel, SerializeAsAny from metagpt.actions import WriteCode from metagpt.actions.add_requirement import UserRequirement -from metagpt.const import SERDESER_PATH from metagpt.logs import logger from metagpt.roles.engineer import Engineer from metagpt.roles.product_manager import ProductManager from metagpt.roles.role import Role from metagpt.schema import Message -from metagpt.utils.common import format_trackback_info +from metagpt.utils.common import format_trackback_info, read_json_file, write_json_file from tests.metagpt.serialize_deserialize.test_serdeser_base import ( ActionOK, RoleA, @@ -60,37 +59,31 @@ def test_role_serialize(): assert "actions" in ser_role_dict -def test_engineer_serialize(): +def test_engineer_serdeser(): role = Engineer() ser_role_dict = role.model_dump() assert "name" in ser_role_dict assert "states" in ser_role_dict assert "actions" in ser_role_dict - -@pytest.mark.asyncio -async def test_engineer_deserialize(): - role = Engineer(use_code_review=True) - ser_role_dict = role.model_dump() - new_role = Engineer(**ser_role_dict) assert new_role.name == "Alex" - assert new_role.use_code_review is True + assert new_role.use_code_review is False assert len(new_role.actions) == 1 assert isinstance(new_role.actions[0], WriteCode) - # await new_role.actions[0].run(context="write a cli snake game", filename="test_code") def test_role_serdeser_save(): - stg_path_prefix = serdeser_path.joinpath("team", "environment", "roles") shutil.rmtree(serdeser_path.joinpath("team"), ignore_errors=True) pm = ProductManager() - role_tag = f"{pm.__class__.__name__}_{pm.name}" - stg_path = stg_path_prefix.joinpath(role_tag) - pm.serialize(stg_path) - new_pm = Role.deserialize(stg_path) + stg_path = serdeser_path.joinpath("team", "environment", "roles", f"{pm.__class__.__name__}_{pm.name}") + role_path = stg_path.joinpath("role.json") + write_json_file(role_path, pm.model_dump()) + + role_dict = read_json_file(role_path) + new_pm = ProductManager(**role_dict) assert new_pm.name == pm.name assert len(new_pm.get_memories(1)) == 0 @@ -98,22 +91,24 @@ def test_role_serdeser_save(): @pytest.mark.asyncio async def test_role_serdeser_interrupt(): role_c = RoleC() - shutil.rmtree(SERDESER_PATH.joinpath("team"), ignore_errors=True) + shutil.rmtree(serdeser_path.joinpath("team"), ignore_errors=True) - stg_path = SERDESER_PATH.joinpath("team", "environment", "roles", f"{role_c.__class__.__name__}_{role_c.name}") + stg_path = serdeser_path.joinpath("team", "environment", "roles", f"{role_c.__class__.__name__}_{role_c.name}") + role_path = stg_path.joinpath("role.json") try: await role_c.run(with_message=Message(content="demo", cause_by=UserRequirement)) except Exception: - logger.error(f"Exception in `role_a.run`, detail: {format_trackback_info()}") - role_c.serialize(stg_path) + logger.error(f"Exception in `role_c.run`, detail: {format_trackback_info()}") + write_json_file(role_path, role_c.model_dump()) assert role_c.rc.memory.count() == 1 - new_role_a: Role = Role.deserialize(stg_path) - assert new_role_a.rc.state == 1 + role_dict = read_json_file(role_path) + new_role_c: Role = RoleC(**role_dict) + assert new_role_c.rc.state == 1 with pytest.raises(Exception): - await new_role_a.run(with_message=Message(content="demo", cause_by=UserRequirement)) + await new_role_c.run(with_message=Message(content="demo", cause_by=UserRequirement)) if __name__ == "__main__": diff --git a/tests/metagpt/serialize_deserialize/test_sk_agent.py b/tests/metagpt/serialize_deserialize/test_sk_agent.py index 7f287b8f9..97c0ade99 100644 --- a/tests/metagpt/serialize_deserialize/test_sk_agent.py +++ b/tests/metagpt/serialize_deserialize/test_sk_agent.py @@ -5,15 +5,8 @@ import pytest from metagpt.roles.sk_agent import SkAgent -def test_sk_agent_serialize(): - role = SkAgent() - ser_role_dict = role.model_dump(exclude={"import_semantic_skill_from_directory", "import_skill"}) - assert "name" in ser_role_dict - assert "planner" in ser_role_dict - - @pytest.mark.asyncio -async def test_sk_agent_deserialize(): +async def test_sk_agent_serdeser(): role = SkAgent() ser_role_dict = role.model_dump(exclude={"import_semantic_skill_from_directory", "import_skill"}) assert "name" in ser_role_dict diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index 566f63c3d..57c8a8508 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -4,13 +4,14 @@ # @Desc : import shutil +from pathlib import Path import pytest -from metagpt.const import SERDESER_PATH from metagpt.logs import logger from metagpt.roles import Architect, ProductManager, ProjectManager from metagpt.team import Team +from metagpt.utils.common import write_json_file from tests.metagpt.serialize_deserialize.test_serdeser_base import ( ActionOK, RoleA, @@ -45,9 +46,16 @@ def test_team_deserialize(): assert new_company.env.get_role(arch.profile) is not None -def test_team_serdeser_save(): - company = Team() +def mock_team_serialize(self, stg_path: Path = serdeser_path.joinpath("team")): + team_info_path = stg_path.joinpath("team.json") + write_json_file(team_info_path, self.model_dump()) + + +def test_team_serdeser_save(mocker): + mocker.patch("metagpt.team.Team.serialize", mock_team_serialize) + + company = Team() company.hire([RoleC()]) stg_path = serdeser_path.joinpath("team") @@ -61,9 +69,11 @@ def test_team_serdeser_save(): @pytest.mark.asyncio -async def test_team_recover(): +async def test_team_recover(mocker): + mocker.patch("metagpt.team.Team.serialize", mock_team_serialize) + idea = "write a snake game" - stg_path = SERDESER_PATH.joinpath("team") + stg_path = serdeser_path.joinpath("team") shutil.rmtree(stg_path, ignore_errors=True) company = Team() @@ -75,9 +85,9 @@ async def test_team_recover(): ser_data = company.model_dump() new_company = Team(**ser_data) - new_company.env.get_role(role_c.profile) - # assert new_role_c.rc.memory == role_c.rc.memory # TODO - # assert new_role_c.rc.env != role_c.rc.env # TODO + new_role_c = new_company.env.get_role(role_c.profile) + assert new_role_c.rc.memory == role_c.rc.memory + assert new_role_c.rc.env != role_c.rc.env assert type(list(new_company.env.roles.values())[0].actions[0]) == ActionOK new_company.run_project(idea) @@ -85,9 +95,11 @@ async def test_team_recover(): @pytest.mark.asyncio -async def test_team_recover_save(): +async def test_team_recover_save(mocker): + mocker.patch("metagpt.team.Team.serialize", mock_team_serialize) + idea = "write a 2048 web game" - stg_path = SERDESER_PATH.joinpath("team") + stg_path = serdeser_path.joinpath("team") shutil.rmtree(stg_path, ignore_errors=True) company = Team() @@ -98,8 +110,8 @@ async def test_team_recover_save(): new_company = Team.deserialize(stg_path) new_role_c = new_company.env.get_role(role_c.profile) - # assert new_role_c.rc.memory == role_c.rc.memory - # assert new_role_c.rc.env != role_c.rc.env + assert new_role_c.rc.memory == role_c.rc.memory + assert new_role_c.rc.env != role_c.rc.env assert new_role_c.recovered != role_c.recovered # here cause previous ut is `!=` assert new_role_c.rc.todo != role_c.rc.todo # serialize exclude `rc.todo` assert new_role_c.rc.news != role_c.rc.news # serialize exclude `rc.news` @@ -109,9 +121,11 @@ async def test_team_recover_save(): @pytest.mark.asyncio -async def test_team_recover_multi_roles_save(): +async def test_team_recover_multi_roles_save(mocker): + mocker.patch("metagpt.team.Team.serialize", mock_team_serialize) + idea = "write a snake game" - stg_path = SERDESER_PATH.joinpath("team") + stg_path = serdeser_path.joinpath("team") shutil.rmtree(stg_path, ignore_errors=True) role_a = RoleA() diff --git a/tests/metagpt/serialize_deserialize/test_tutorial_assistant.py b/tests/metagpt/serialize_deserialize/test_tutorial_assistant.py index e642dae54..cb8feec19 100644 --- a/tests/metagpt/serialize_deserialize/test_tutorial_assistant.py +++ b/tests/metagpt/serialize_deserialize/test_tutorial_assistant.py @@ -7,7 +7,7 @@ from metagpt.roles.tutorial_assistant import TutorialAssistant @pytest.mark.asyncio -async def test_tutorial_assistant_deserialize(): +async def test_tutorial_assistant_serdeser(): role = TutorialAssistant() ser_role_dict = role.model_dump() assert "name" in ser_role_dict diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index cb262bb45..12dc49c3b 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -9,7 +9,7 @@ from metagpt.actions import WriteCode from metagpt.schema import CodingContext, Document -def test_write_design_serialize(): +def test_write_design_serdeser(): action = WriteCode() ser_action_dict = action.model_dump() assert ser_action_dict["name"] == "WriteCode" @@ -17,7 +17,7 @@ def test_write_design_serialize(): @pytest.mark.asyncio -async def test_write_code_deserialize(): +async def test_write_code_serdeser(): context = CodingContext( filename="test_code.py", design_doc=Document(content="write add function to calculate two numbers") ) diff --git a/tests/metagpt/serialize_deserialize/test_write_code_review.py b/tests/metagpt/serialize_deserialize/test_write_code_review.py index 991b3c13b..d1a9bff24 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code_review.py +++ b/tests/metagpt/serialize_deserialize/test_write_code_review.py @@ -9,7 +9,7 @@ from metagpt.schema import CodingContext, Document @pytest.mark.asyncio -async def test_write_code_review_deserialize(): +async def test_write_code_review_serdeser(): code_content = """ def div(a: int, b: int = 0): return a / b diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py index 7bcba3fc8..37d505914 100644 --- a/tests/metagpt/serialize_deserialize/test_write_design.py +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -7,33 +7,25 @@ import pytest from metagpt.actions import WriteDesign, WriteTasks -def test_write_design_serialize(): - action = WriteDesign() - ser_action_dict = action.model_dump() - assert "name" in ser_action_dict - assert "llm" not in ser_action_dict # not export - - -def test_write_task_serialize(): - action = WriteTasks() - ser_action_dict = action.model_dump() - assert "name" in ser_action_dict - assert "llm" not in ser_action_dict # not export - - @pytest.mark.asyncio -async def test_write_design_deserialize(): +async def test_write_design_serialize(): action = WriteDesign() - serialized_data = action.model_dump() - new_action = WriteDesign(**serialized_data) + ser_action_dict = action.model_dump() + assert "name" in ser_action_dict + assert "llm" not in ser_action_dict # not export + + new_action = WriteDesign(**ser_action_dict) assert new_action.name == "WriteDesign" await new_action.run(with_messages="write a cli snake game") @pytest.mark.asyncio -async def test_write_task_deserialize(): +async def test_write_task_serialize(): action = WriteTasks() - serialized_data = action.model_dump() - new_action = WriteTasks(**serialized_data) + ser_action_dict = action.model_dump() + assert "name" in ser_action_dict + assert "llm" not in ser_action_dict # not export + + new_action = WriteTasks(**ser_action_dict) assert new_action.name == "WriteTasks" await new_action.run(with_messages="write a cli snake game") diff --git a/tests/metagpt/serialize_deserialize/test_write_docstring.py b/tests/metagpt/serialize_deserialize/test_write_docstring.py index e4116ab30..fb927f089 100644 --- a/tests/metagpt/serialize_deserialize/test_write_docstring.py +++ b/tests/metagpt/serialize_deserialize/test_write_docstring.py @@ -29,7 +29,7 @@ class Person: ], ids=["google", "numpy", "sphinx"], ) -async def test_action_deserialize(style: str, part: str): +async def test_action_serdeser(style: str, part: str): action = WriteDocstring() serialized_data = action.model_dump() diff --git a/tests/metagpt/serialize_deserialize/test_write_prd.py b/tests/metagpt/serialize_deserialize/test_write_prd.py index b9eff5a19..820ee237c 100644 --- a/tests/metagpt/serialize_deserialize/test_write_prd.py +++ b/tests/metagpt/serialize_deserialize/test_write_prd.py @@ -9,18 +9,14 @@ from metagpt.actions import WritePRD from metagpt.schema import Message -def test_action_serialize(new_filename): +@pytest.mark.asyncio +async def test_action_serdeser(new_filename): action = WritePRD() ser_action_dict = action.model_dump() assert "name" in ser_action_dict assert "llm" not in ser_action_dict # not export - -@pytest.mark.asyncio -async def test_action_deserialize(new_filename): - action = WritePRD() - serialized_data = action.model_dump() - new_action = WritePRD(**serialized_data) + new_action = WritePRD(**ser_action_dict) assert new_action.name == "WritePRD" action_output = await new_action.run(with_messages=Message(content="write a cli snake game")) assert len(action_output.content) > 0 diff --git a/tests/metagpt/serialize_deserialize/test_write_review.py b/tests/metagpt/serialize_deserialize/test_write_review.py index f02a01910..17e212276 100644 --- a/tests/metagpt/serialize_deserialize/test_write_review.py +++ b/tests/metagpt/serialize_deserialize/test_write_review.py @@ -42,7 +42,7 @@ CONTEXT = """ @pytest.mark.asyncio -async def test_action_deserialize(): +async def test_action_serdeser(): action = WriteReview() serialized_data = action.model_dump() assert serialized_data["name"] == "WriteReview" diff --git a/tests/metagpt/serialize_deserialize/test_write_tutorial.py b/tests/metagpt/serialize_deserialize/test_write_tutorial.py index 606a90f8c..4eeef7e0d 100644 --- a/tests/metagpt/serialize_deserialize/test_write_tutorial.py +++ b/tests/metagpt/serialize_deserialize/test_write_tutorial.py @@ -9,7 +9,7 @@ from metagpt.actions.write_tutorial import WriteContent, WriteDirectory @pytest.mark.asyncio @pytest.mark.parametrize(("language", "topic"), [("English", "Write a tutorial about Python")]) -async def test_write_directory_deserialize(language: str, topic: str): +async def test_write_directory_serdeser(language: str, topic: str): action = WriteDirectory() serialized_data = action.model_dump() assert serialized_data["name"] == "WriteDirectory" @@ -30,7 +30,7 @@ async def test_write_directory_deserialize(language: str, topic: str): ("language", "topic", "directory"), [("English", "Write a tutorial about Python", {"Introduction": ["What is Python?", "Why learn Python?"]})], ) -async def test_write_content_deserialize(language: str, topic: str, directory: Dict): +async def test_write_content_serdeser(language: str, topic: str, directory: Dict): action = WriteContent(language=language, directory=directory) serialized_data = action.model_dump() assert serialized_data["name"] == "WriteContent" From 9ee5883c59d4112e3128417151beea51e59def33 Mon Sep 17 00:00:00 2001 From: better629 Date: Mon, 8 Jan 2024 22:21:21 +0800 Subject: [PATCH 05/50] add detail revise comments --- metagpt/actions/action_node.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 2d6782952..8bbf9472c 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -553,7 +553,8 @@ class ActionNode: include_keys = list(review_comments.keys()) - # generate revise content + # generate revise content, two-steps + # step1, find the needed revise keys from review comments to makeup prompt template nodes_output = self._makeup_nodes_output_with_comment(review_comments) keys = self.keys() exclude_keys = list(set(keys).difference(include_keys)) @@ -567,6 +568,7 @@ class ActionNode: constraint=FORMAT_CONSTRAINT, ) + # step2, use `_aask_v1` to get revise structure result output_mapping = self.get_mapping(mode="auto", exclude=exclude_keys) output_class_name = f"{self.key}_AN_REVISE" content, scontent = await self._aask_v1( From b4f049294b93cb5509d34f5ec1774a7715747b96 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 14:16:32 +0800 Subject: [PATCH 06/50] add context tests --- metagpt/config2.py | 24 ++++++++++++- metagpt/context.py | 40 +++++++++++----------- tests/metagpt/test_context.py | 63 +++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 22 deletions(-) create mode 100644 tests/metagpt/test_context.py diff --git a/metagpt/config2.py b/metagpt/config2.py index a6aa62f6b..9c809e559 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -3,7 +3,7 @@ """ @Time : 2024/1/4 01:25 @Author : alexanderwu -@File : llm_factory.py +@File : config2.py """ import os from pathlib import Path @@ -23,6 +23,8 @@ from metagpt.utils.yaml_model import YamlModel class CLIParams(BaseModel): + """CLI parameters""" + project_path: str = "" project_name: str = "" inc: bool = False @@ -32,12 +34,15 @@ class CLIParams(BaseModel): @model_validator(mode="after") def check_project_path(self): + """Check project_path and project_name""" if self.project_path: self.inc = True self.project_name = self.project_name or Path(self.project_path).name class Config(CLIParams, YamlModel): + """Configurations for MetaGPT""" + # Key Parameters llm: Dict[str, LLMConfig] = Field(default_factory=Dict) @@ -133,4 +138,21 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict: return result +class ConfigurableMixin: + """Mixin class for configurable objects""" + + def __init__(self, config=None): + self._config = config + + def try_set_parent_config(self, parent_config): + """Try to set parent config if not set""" + if self._config is None: + self._config = parent_config + + @property + def config(self): + """Get config""" + return self._config + + config = Config.default() diff --git a/metagpt/context.py b/metagpt/context.py index 0ea5d6046..e396de7e1 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -9,6 +9,8 @@ import os from pathlib import Path from typing import Optional +from pydantic import BaseModel, ConfigDict + from metagpt.config2 import Config from metagpt.configs.llm_config import LLMType from metagpt.const import OPTIONS @@ -18,28 +20,33 @@ from metagpt.utils.cost_manager import CostManager from metagpt.utils.git_repository import GitRepository -class AttrDict: - """A dict-like object that allows access to keys as attributes.""" +class AttrDict(BaseModel): + """A dict-like object that allows access to keys as attributes, compatible with Pydantic.""" - def __init__(self, d=None): - if d is None: - d = {} - self.__dict__["_dict"] = d + model_config = ConfigDict(extra="allow") + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.__dict__.update(kwargs) def __getattr__(self, key): - return self._dict.get(key, None) + return self.__dict__.get(key, None) def __setattr__(self, key, value): - self._dict[key] = value + self.__dict__[key] = value def __delattr__(self, key): - if key in self._dict: - del self._dict[key] + if key in self.__dict__: + del self.__dict__[key] else: raise AttributeError(f"No such attribute: {key}") -class Context: +class Context(BaseModel): + """Env context for MetaGPT""" + + model_config = ConfigDict(arbitrary_types_allowed=True) + kwargs: AttrDict = AttrDict() config: Config = Config.default() git_repo: Optional[GitRepository] = None @@ -82,14 +89,5 @@ class Context: return llm -# Global context +# Global context, not in Env context = Context() - - -if __name__ == "__main__": - # print(context.model_dump_json(indent=4)) - # print(context.config.get_openai_llm()) - ad = AttrDict({"name": "John", "age": 30}) - - print(ad.name) # Output: John - print(ad.height) # Output: None (因为height不存在) diff --git a/tests/metagpt/test_context.py b/tests/metagpt/test_context.py new file mode 100644 index 000000000..d4f29e352 --- /dev/null +++ b/tests/metagpt/test_context.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2024/1/9 13:52 +@Author : alexanderwu +@File : test_context.py +""" +from metagpt.configs.llm_config import LLMType +from metagpt.context import AttrDict, Context, context + + +def test_attr_dict_1(): + ad = AttrDict(name="John", age=30) + assert ad.name == "John" + assert ad.age == 30 + assert ad.height is None + + +def test_attr_dict_2(): + ad = AttrDict(name="John", age=30) + ad.height = 180 + assert ad.height == 180 + + +def test_attr_dict_3(): + ad = AttrDict(name="John", age=30) + del ad.age + assert ad.age is None + + +def test_attr_dict_4(): + ad = AttrDict(name="John", age=30) + try: + del ad.weight + except AttributeError as e: + assert str(e) == "No such attribute: weight" + + +def test_attr_dict_5(): + ad = AttrDict.model_validate({"name": "John", "age": 30}) + assert ad.name == "John" + assert ad.age == 30 + + +def test_context_1(): + ctx = Context() + assert ctx.config is not None + assert ctx.git_repo is None + assert ctx.src_workspace is None + assert ctx.cost_manager is not None + assert ctx.options is not None + + +def test_context_2(): + llm = context.config.get_openai_llm() + assert llm is not None + assert llm.api_type == LLMType.OPENAI + + kwargs = context.kwargs + assert kwargs is not None + + kwargs.test_key = "test_value" + assert kwargs.test_key == "test_value" From f9a150bab0a24b212b78469958aa2ee61813c844 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 9 Jan 2024 15:40:42 +0800 Subject: [PATCH 07/50] make instruct_content support any inherited basemodel ser&deser --- metagpt/schema.py | 25 ++++--- .../serialize_deserialize/test_schema.py | 68 +++++++++++++++---- .../test_serdeser_base.py | 10 +-- 3 files changed, 77 insertions(+), 26 deletions(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index a557951c7..7d1c2b539 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -182,12 +182,16 @@ class Message(BaseModel): @field_validator("instruct_content", mode="before") @classmethod def check_instruct_content(cls, ic: Any) -> BaseModel: - if ic and not isinstance(ic, BaseModel) and "class" in ic: - # compatible with custom-defined ActionOutput - mapping = actionoutput_str_to_mapping(ic["mapping"]) - - actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import - ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=mapping) + if ic and isinstance(ic, dict) and "class" in ic: + if "mapping" in ic: + # compatible with custom-defined ActionOutput + mapping = actionoutput_str_to_mapping(ic["mapping"]) + actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import + ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=mapping) + elif "module" in ic: + ic_obj = import_class(ic["class"], ic["module"]) + else: + raise KeyError("missing required key to init Message.instruct_content from dict") ic = ic_obj(**ic["value"]) return ic @@ -212,13 +216,16 @@ class Message(BaseModel): if ic: # compatible with custom-defined ActionOutput schema = ic.model_json_schema() - # `Documents` contain definitions - if "definitions" not in schema: - # TODO refine with nested BaseModel + ic_type = str(type(ic)) + if " Date: Tue, 9 Jan 2024 15:56:40 +0800 Subject: [PATCH 08/50] llm config mixin update --- metagpt/config2.py | 23 ++++++++-- metagpt/context.py | 51 +++++++++++++---------- metagpt/provider/base_llm.py | 1 + metagpt/provider/llm_provider_registry.py | 2 +- tests/metagpt/test_context.py | 9 ++++ 5 files changed, 61 insertions(+), 25 deletions(-) diff --git a/metagpt/config2.py b/metagpt/config2.py index 9c809e559..230e090af 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -101,7 +101,7 @@ class Config(CLIParams, YamlModel): self.reqa_file = reqa_file self.max_auto_summarize_code = max_auto_summarize_code - def get_llm_config(self, name: Optional[str] = None) -> LLMConfig: + def _get_llm_config(self, name: Optional[str] = None) -> LLMConfig: """Get LLM instance by name""" if name is None: # Use the first LLM as default @@ -121,6 +121,21 @@ class Config(CLIParams, YamlModel): return llm[0] return None + def get_llm_config(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> LLMConfig: + """Return a LLMConfig instance""" + if provider: + llm_configs = self.get_llm_configs_by_type(provider) + if name: + llm_configs = [c for c in llm_configs if c.name == name] + + if len(llm_configs) == 0: + raise ValueError(f"Cannot find llm config with name {name} and provider {provider}") + # return the first one if name is None, or return the only one + llm_config = llm_configs[0] + else: + llm_config = self._get_llm_config(name) + return llm_config + def get_openai_llm(self) -> Optional[LLMConfig]: """Get OpenAI LLMConfig by name. If no OpenAI, raise Exception""" return self.get_llm_config_by_type(LLMType.OPENAI) @@ -138,10 +153,12 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict: return result -class ConfigurableMixin: +class ConfigMixin: """Mixin class for configurable objects""" - def __init__(self, config=None): + _config: Optional[Config] = None + + def __init__(self, config: Optional[Config] = None): self._config = config def try_set_parent_config(self, parent_config): diff --git a/metagpt/context.py b/metagpt/context.py index e396de7e1..3505614bb 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -12,10 +12,10 @@ from typing import Optional from pydantic import BaseModel, ConfigDict from metagpt.config2 import Config -from metagpt.configs.llm_config import LLMType +from metagpt.configs.llm_config import LLMConfig, LLMType from metagpt.const import OPTIONS from metagpt.provider.base_llm import BaseLLM -from metagpt.provider.llm_provider_registry import get_llm +from metagpt.provider.llm_provider_registry import create_llm_instance from metagpt.utils.cost_manager import CostManager from metagpt.utils.git_repository import GitRepository @@ -42,7 +42,26 @@ class AttrDict(BaseModel): raise AttributeError(f"No such attribute: {key}") -class Context(BaseModel): +class LLMMixin: + config: Optional[Config] = None + llm_config: Optional[LLMConfig] = None + _llm_instance: Optional[BaseLLM] = None + + def use_llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI): + # 更新LLM配置 + self.llm_config = self.config.get_llm_config(name, provider) + # 重置LLM实例 + self._llm_instance = None + + @property + def llm(self) -> BaseLLM: + # 实例化LLM,如果尚未实例化 + if not self._llm_instance and self.llm_config: + self._llm_instance = create_llm_instance(self.llm_config) + return self._llm_instance + + +class Context(LLMMixin, BaseModel): """Env context for MetaGPT""" model_config = ConfigDict(arbitrary_types_allowed=True) @@ -69,24 +88,14 @@ class Context(BaseModel): env.update({k: v for k, v in i.items() if isinstance(v, str)}) return env - def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: - """Return a LLM instance""" - if provider: - llm_configs = self.config.get_llm_configs_by_type(provider) - if name: - llm_configs = [c for c in llm_configs if c.name == name] - - if len(llm_configs) == 0: - raise ValueError(f"Cannot find llm config with name {name} and provider {provider}") - # return the first one if name is None, or return the only one - llm_config = llm_configs[0] - else: - llm_config = self.config.get_llm_config(name) - - llm = get_llm(llm_config) - if llm.cost_manager is None: - llm.cost_manager = self.cost_manager - return llm + # def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: + # """Return a LLM instance""" + # llm_config = self.config.get_llm_config(name, provider) + # + # llm = create_llm_instance(llm_config) + # if llm.cost_manager is None: + # llm.cost_manager = self.cost_manager + # return llm # Global context, not in Env diff --git a/metagpt/provider/base_llm.py b/metagpt/provider/base_llm.py index 3c6c464dc..b9847850e 100644 --- a/metagpt/provider/base_llm.py +++ b/metagpt/provider/base_llm.py @@ -27,6 +27,7 @@ class BaseLLM(ABC): # OpenAI / Azure / Others aclient: Optional[Union[AsyncOpenAI]] = None cost_manager: Optional[CostManager] = None + model: Optional[str] = None @abstractmethod def __init__(self, config: LLMConfig): diff --git a/metagpt/provider/llm_provider_registry.py b/metagpt/provider/llm_provider_registry.py index 2f68f27c8..df89d36aa 100644 --- a/metagpt/provider/llm_provider_registry.py +++ b/metagpt/provider/llm_provider_registry.py @@ -31,7 +31,7 @@ def register_provider(key): return decorator -def get_llm(config: LLMConfig) -> BaseLLM: +def create_llm_instance(config: LLMConfig) -> BaseLLM: """get the default llm provider""" return LLM_REGISTRY.get_provider(config.api_type)(config) diff --git a/tests/metagpt/test_context.py b/tests/metagpt/test_context.py index d4f29e352..2d52325bc 100644 --- a/tests/metagpt/test_context.py +++ b/tests/metagpt/test_context.py @@ -61,3 +61,12 @@ def test_context_2(): kwargs.test_key = "test_value" assert kwargs.test_key == "test_value" + + +def test_context_3(): + ctx = Context() + ctx.use_llm(provider=LLMType.OPENAI) + assert ctx.llm_config is not None + assert ctx.llm_config.api_type == LLMType.OPENAI + assert ctx.llm is not None + assert "gpt" in ctx.llm.model From 8ddd17da28804240757ec004772679d1b433044b Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 16:01:05 +0800 Subject: [PATCH 09/50] add test config --- tests/metagpt/test_config.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/metagpt/test_config.py diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py new file mode 100644 index 000000000..d793b2615 --- /dev/null +++ b/tests/metagpt/test_config.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2024/1/9 15:57 +@Author : alexanderwu +@File : test_config.py +""" + +from metagpt.config2 import Config, config +from metagpt.configs.llm_config import LLMType + + +def test_config_1(): + cfg = Config.default() + llm = cfg.get_openai_llm() + assert llm is not None + assert llm.api_type == LLMType.OPENAI + + +def test_config_2(): + assert config == Config.default() From 0c3bae4de408838854f22e8890cd7b99d0e15f72 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 9 Jan 2024 16:07:33 +0800 Subject: [PATCH 10/50] update --- metagpt/schema.py | 1 + 1 file changed, 1 insertion(+) diff --git a/metagpt/schema.py b/metagpt/schema.py index 7d1c2b539..853a9c6bb 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -189,6 +189,7 @@ class Message(BaseModel): actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=mapping) elif "module" in ic: + # subclasses of BaseModel ic_obj = import_class(ic["class"], ic["module"]) else: raise KeyError("missing required key to init Message.instruct_content from dict") From 0f0ef86b262006d46c85860b7a3ba1ec98d06e3e Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 16:12:31 +0800 Subject: [PATCH 11/50] add test config --- metagpt/context.py | 7 ++++++- tests/metagpt/test_config.py | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/metagpt/context.py b/metagpt/context.py index 3505614bb..eb46ab19b 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -43,11 +43,14 @@ class AttrDict(BaseModel): class LLMMixin: + """Mixin class for LLM""" + config: Optional[Config] = None llm_config: Optional[LLMConfig] = None _llm_instance: Optional[BaseLLM] = None def use_llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI): + """Use a LLM provider""" # 更新LLM配置 self.llm_config = self.config.get_llm_config(name, provider) # 重置LLM实例 @@ -55,7 +58,9 @@ class LLMMixin: @property def llm(self) -> BaseLLM: - # 实例化LLM,如果尚未实例化 + """Return the LLM instance""" + if not self.llm_config: + self.use_llm() if not self._llm_instance and self.llm_config: self._llm_instance = create_llm_instance(self.llm_config) return self._llm_instance diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index d793b2615..eecabb546 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -8,6 +8,7 @@ from metagpt.config2 import Config, config from metagpt.configs.llm_config import LLMType +from tests.metagpt.provider.mock_llm_config import mock_llm_config def test_config_1(): @@ -19,3 +20,9 @@ def test_config_1(): def test_config_2(): assert config == Config.default() + + +def test_config_from_dict(): + cfg = Config(llm={"default": mock_llm_config}) + assert cfg + assert cfg.llm["default"].api_key == "mock_api_key" From 33db023e9448e06d7e3a7cbf8a7492990db44250 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 16:32:38 +0800 Subject: [PATCH 12/50] add context mixin --- metagpt/context.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/metagpt/context.py b/metagpt/context.py index eb46ab19b..293beb9b5 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -103,5 +103,23 @@ class Context(LLMMixin, BaseModel): # return llm +class ContextMixin: + """Mixin class for configurable objects""" + + _context: Optional[Context] = None + + def __init__(self, context: Optional[Context] = None): + self._context = context + + def set_context(self, context: Optional[Context] = None): + """Set parent context""" + self._context = context + + @property + def context(self): + """Get config""" + return self._context + + # Global context, not in Env context = Context() From 7e20e5471758abcf3e44c04e0d746c228835431e Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 17:01:21 +0800 Subject: [PATCH 13/50] refine code --- examples/agent_creator.py | 2 +- examples/build_customized_agent.py | 4 +-- examples/build_customized_multi_agents.py | 6 ++--- examples/debate.py | 2 +- metagpt/context.py | 2 +- metagpt/roles/architect.py | 2 +- metagpt/roles/engineer.py | 2 +- metagpt/roles/invoice_ocr_assistant.py | 6 ++--- metagpt/roles/product_manager.py | 2 +- metagpt/roles/project_manager.py | 2 +- metagpt/roles/qa_engineer.py | 2 +- metagpt/roles/researcher.py | 2 +- metagpt/roles/role.py | 26 +++++++++---------- metagpt/roles/sales.py | 2 +- metagpt/roles/searcher.py | 4 +-- metagpt/roles/sk_agent.py | 2 +- metagpt/roles/teacher.py | 2 +- metagpt/roles/tutorial_assistant.py | 4 +-- .../test_serdeser_base.py | 6 ++--- tests/metagpt/test_role.py | 8 +++--- 20 files changed, 43 insertions(+), 45 deletions(-) diff --git a/examples/agent_creator.py b/examples/agent_creator.py index e908fe6ee..fe883bdf4 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -61,7 +61,7 @@ class AgentCreator(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self._init_actions([CreateAgent]) + self.add_actions([CreateAgent]) async def _act(self) -> Message: logger.info(f"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})") diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py index 6c3219efc..a0c8ddfb3 100644 --- a/examples/build_customized_agent.py +++ b/examples/build_customized_agent.py @@ -57,7 +57,7 @@ class SimpleCoder(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self._init_actions([SimpleWriteCode]) + self.add_actions([SimpleWriteCode]) async def _act(self) -> Message: logger.info(f"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})") @@ -76,7 +76,7 @@ class RunnableCoder(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self._init_actions([SimpleWriteCode, SimpleRunCode]) + self.add_actions([SimpleWriteCode, SimpleRunCode]) self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value) async def _act(self) -> Message: diff --git a/examples/build_customized_multi_agents.py b/examples/build_customized_multi_agents.py index 73278c08c..aceb3f2ab 100644 --- a/examples/build_customized_multi_agents.py +++ b/examples/build_customized_multi_agents.py @@ -46,7 +46,7 @@ class SimpleCoder(Role): def __init__(self, **kwargs): super().__init__(**kwargs) self._watch([UserRequirement]) - self._init_actions([SimpleWriteCode]) + self.add_actions([SimpleWriteCode]) class SimpleWriteTest(Action): @@ -75,7 +75,7 @@ class SimpleTester(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self._init_actions([SimpleWriteTest]) + self.add_actions([SimpleWriteTest]) # self._watch([SimpleWriteCode]) self._watch([SimpleWriteCode, SimpleWriteReview]) # feel free to try this too @@ -114,7 +114,7 @@ class SimpleReviewer(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self._init_actions([SimpleWriteReview]) + self.add_actions([SimpleWriteReview]) self._watch([SimpleWriteTest]) diff --git a/examples/debate.py b/examples/debate.py index eb0a09839..b47eba3cd 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -49,7 +49,7 @@ class Debator(Role): def __init__(self, **data: Any): super().__init__(**data) - self._init_actions([SpeakAloud]) + self.add_actions([SpeakAloud]) self._watch([UserRequirement, SpeakAloud]) async def _observe(self) -> int: diff --git a/metagpt/context.py b/metagpt/context.py index 293beb9b5..495fe9e2f 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -104,7 +104,7 @@ class Context(LLMMixin, BaseModel): class ContextMixin: - """Mixin class for configurable objects""" + """Mixin class for configurable objects: Priority: more specific < parent""" _context: Optional[Context] = None diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index c6ceaccb7..a22a1c926 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -33,7 +33,7 @@ class Architect(Role): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) # Initialize actions specific to the Architect role - self._init_actions([WriteDesign]) + self.add_actions([WriteDesign]) # Set events or actions the Architect should watch or be aware of self._watch({WritePRD}) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 98744383c..ad0c1ac92 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -84,7 +84,7 @@ class Engineer(Role): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self._init_actions([WriteCode]) + self.add_actions([WriteCode]) self._watch([WriteTasks, SummarizeCode, WriteCode, WriteCodeReview, FixBug]) self.code_todos = [] self.summarize_todos = [] diff --git a/metagpt/roles/invoice_ocr_assistant.py b/metagpt/roles/invoice_ocr_assistant.py index 8635f4307..de7d3f8a3 100644 --- a/metagpt/roles/invoice_ocr_assistant.py +++ b/metagpt/roles/invoice_ocr_assistant.py @@ -60,7 +60,7 @@ class InvoiceOCRAssistant(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self._init_actions([InvoiceOCR]) + self.add_actions([InvoiceOCR]) self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value) async def _act(self) -> Message: @@ -82,10 +82,10 @@ class InvoiceOCRAssistant(Role): resp = await todo.run(file_path) if len(resp) == 1: # Single file support for questioning based on OCR recognition results - self._init_actions([GenerateTable, ReplyQuestion]) + self.add_actions([GenerateTable, ReplyQuestion]) self.orc_data = resp[0] else: - self._init_actions([GenerateTable]) + self.add_actions([GenerateTable]) self.set_todo(None) content = INVOICE_OCR_SUCCESS diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index 7f1a49231..a35dcb3a0 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -33,7 +33,7 @@ class ProductManager(Role): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self._init_actions([PrepareDocuments, WritePRD]) + self.add_actions([PrepareDocuments, WritePRD]) self._watch([UserRequirement, PrepareDocuments]) self.todo_action = any_to_name(PrepareDocuments) diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 1fad4afc2..7fa16b1e5 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -33,5 +33,5 @@ class ProjectManager(Role): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self._init_actions([WriteTasks]) + self.add_actions([WriteTasks]) self._watch([WriteDesign]) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 7da0af072..80b0fd39a 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -44,7 +44,7 @@ class QaEngineer(Role): # FIXME: a bit hack here, only init one action to circumvent _think() logic, # will overwrite _think() in future updates - self._init_actions([WriteTest]) + self.add_actions([WriteTest]) self._watch([SummarizeCode, WriteTest, RunCode, DebugError]) self.test_round = 0 diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index 5110c6485..e877778f6 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -34,7 +34,7 @@ class Researcher(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self._init_actions( + self.add_actions( [CollectLinks(name=self.name), WebBrowseAndSummarize(name=self.name), ConductResearch(name=self.name)] ) self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 73d82e369..42996bea8 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -23,7 +23,7 @@ from __future__ import annotations from enum import Enum -from typing import Any, Iterable, Optional, Set, Type +from typing import Any, Iterable, Optional, Set, Type, Union from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validator @@ -222,20 +222,18 @@ class Role(SerializationMixin): def _init_action_system_message(self, action: Action): action.set_prefix(self._get_prefix()) - def refresh_system_message(self): - self.llm.system_prompt = self._get_prefix() + def add_action(self, action: Action): + """Add action to the role.""" + self.add_actions([action]) - def set_memory(self, memory: Memory): - self.rc.memory = memory + def add_actions(self, actions: list[Union[Action, Type[Action]]]): + """Add actions to the role. - def init_actions(self, actions): - self._init_actions(actions) - - def _init_actions(self, actions): - self._reset() - for idx, action in enumerate(actions): + Args: + actions: list of Action classes or instances + """ + for action in actions: if not isinstance(action, Action): - ## 默认初始化 i = action(name="", llm=self.llm) else: if self.is_human and not isinstance(action.llm, HumanProvider): @@ -247,7 +245,7 @@ class Role(SerializationMixin): i = action self._init_action_system_message(i) self.actions.append(i) - self.states.append(f"{idx}. {action}") + self.states.append(f"{len(self.actions)}. {action}") def _set_react_mode(self, react_mode: str, max_react_loop: int = 1): """Set strategy of the Role reacting to observed Message. Variation lies in how @@ -302,7 +300,7 @@ class Role(SerializationMixin): self.rc.env = env if env: env.set_addresses(self, self.addresses) - self.refresh_system_message() # add env message to system message + self.llm.system_prompt = self._get_prefix() @property def action_count(self): diff --git a/metagpt/roles/sales.py b/metagpt/roles/sales.py index ca1cfee85..8da930888 100644 --- a/metagpt/roles/sales.py +++ b/metagpt/roles/sales.py @@ -38,5 +38,5 @@ class Sales(Role): action = SearchAndSummarize(name="", engine=SearchEngineType.CUSTOM_ENGINE, search_func=store.asearch) else: action = SearchAndSummarize() - self._init_actions([action]) + self.add_actions([action]) self._watch([UserRequirement]) diff --git a/metagpt/roles/searcher.py b/metagpt/roles/searcher.py index e713f7697..f37bd4704 100644 --- a/metagpt/roles/searcher.py +++ b/metagpt/roles/searcher.py @@ -48,12 +48,12 @@ class Searcher(Role): engine (SearchEngineType): The type of search engine to use. """ super().__init__(**kwargs) - self._init_actions([SearchAndSummarize(engine=self.engine)]) + self.add_actions([SearchAndSummarize(engine=self.engine)]) def set_search_func(self, search_func): """Sets a custom search function for the searcher.""" action = SearchAndSummarize(name="", engine=SearchEngineType.CUSTOM_ENGINE, search_func=search_func) - self._init_actions([action]) + self.add_actions([action]) async def _act_sp(self) -> Message: """Performs the search action in a single process.""" diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index 8921774f0..468905fce 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -52,7 +52,7 @@ class SkAgent(Role): def __init__(self, **data: Any) -> None: """Initializes the Engineer role with given attributes.""" super().__init__(**data) - self._init_actions([ExecuteTask()]) + self.add_actions([ExecuteTask()]) self._watch([UserRequirement]) self.kernel = make_sk_kernel() diff --git a/metagpt/roles/teacher.py b/metagpt/roles/teacher.py index fb547f56b..b4ffd01d3 100644 --- a/metagpt/roles/teacher.py +++ b/metagpt/roles/teacher.py @@ -47,7 +47,7 @@ class Teacher(Role): for topic in TeachingPlanBlock.TOPICS: act = WriteTeachingPlanPart(context=self.rc.news[0].content, topic=topic, llm=self.llm) actions.append(act) - self._init_actions(actions) + self.add_actions(actions) if self.rc.todo is None: self._set_state(0) diff --git a/metagpt/roles/tutorial_assistant.py b/metagpt/roles/tutorial_assistant.py index 10bd82c60..d296c7b3f 100644 --- a/metagpt/roles/tutorial_assistant.py +++ b/metagpt/roles/tutorial_assistant.py @@ -40,7 +40,7 @@ class TutorialAssistant(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self._init_actions([WriteDirectory(language=self.language)]) + self.add_actions([WriteDirectory(language=self.language)]) self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value) async def _handle_directory(self, titles: Dict) -> Message: @@ -63,7 +63,7 @@ class TutorialAssistant(Role): directory += f"- {key}\n" for second_dir in first_dir[key]: directory += f" - {second_dir}\n" - self._init_actions(actions) + self.add_actions(actions) async def _act(self) -> Message: """Perform an action as determined by the role. diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index ddb47a3e2..c97cea597 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -67,7 +67,7 @@ class RoleA(Role): def __init__(self, **kwargs): super(RoleA, self).__init__(**kwargs) - self._init_actions([ActionPass]) + self.add_actions([ActionPass]) self._watch([UserRequirement]) @@ -79,7 +79,7 @@ class RoleB(Role): def __init__(self, **kwargs): super(RoleB, self).__init__(**kwargs) - self._init_actions([ActionOK, ActionRaise]) + self.add_actions([ActionOK, ActionRaise]) self._watch([ActionPass]) self.rc.react_mode = RoleReactMode.BY_ORDER @@ -92,7 +92,7 @@ class RoleC(Role): def __init__(self, **kwargs): super(RoleC, self).__init__(**kwargs) - self._init_actions([ActionOK, ActionRaise]) + self.add_actions([ActionOK, ActionRaise]) self._watch([UserRequirement]) self.rc.react_mode = RoleReactMode.BY_ORDER self.rc.memory.ignore_id = True diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 52d08e92e..20c8dba6d 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -33,7 +33,7 @@ class MockAction(Action): class MockRole(Role): def __init__(self, name="", profile="", goal="", constraints="", desc=""): super().__init__(name=name, profile=profile, goal=goal, constraints=constraints, desc=desc) - self._init_actions([MockAction()]) + self.add_actions([MockAction()]) def test_basic(): @@ -111,7 +111,7 @@ async def test_send_to(): def test_init_action(): role = Role() - role.init_actions([MockAction, MockAction]) + role.add_actions([MockAction, MockAction]) assert role.action_count == 2 @@ -127,7 +127,7 @@ async def test_recover(): role.publish_message(None) role.llm = mock_llm - role.init_actions([MockAction, MockAction]) + role.add_actions([MockAction, MockAction]) role.recovered = True role.latest_observed_msg = Message(content="recover_test") role.rc.state = 0 @@ -144,7 +144,7 @@ async def test_think_act(): mock_llm.aask.side_effect = ["ok"] role = Role() - role.init_actions([MockAction]) + role.add_actions([MockAction]) await role.think() role.rc.memory.add(Message("run")) assert len(role.get_memories()) == 1 From b338dfca648a97d23e1c780458b9e9fca4728215 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 17:04:45 +0800 Subject: [PATCH 14/50] refine code --- metagpt/context.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/metagpt/context.py b/metagpt/context.py index 495fe9e2f..ba859ed5c 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -45,24 +45,24 @@ class AttrDict(BaseModel): class LLMMixin: """Mixin class for LLM""" - config: Optional[Config] = None - llm_config: Optional[LLMConfig] = None + # _config: Optional[Config] = None + _llm_config: Optional[LLMConfig] = None _llm_instance: Optional[BaseLLM] = None def use_llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI): """Use a LLM provider""" # 更新LLM配置 - self.llm_config = self.config.get_llm_config(name, provider) + self._llm_config = self._config.get_llm_config(name, provider) # 重置LLM实例 self._llm_instance = None @property def llm(self) -> BaseLLM: """Return the LLM instance""" - if not self.llm_config: + if not self._llm_config: self.use_llm() - if not self._llm_instance and self.llm_config: - self._llm_instance = create_llm_instance(self.llm_config) + if not self._llm_instance and self._llm_config: + self._llm_instance = create_llm_instance(self._llm_config) return self._llm_instance From ad525acd33a24d5a00187cb1a9f7829e10dbead8 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 17:13:22 +0800 Subject: [PATCH 15/50] refine code --- metagpt/context.py | 40 +++++++++++++++++++++------------------- metagpt/llm.py | 1 + 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/metagpt/context.py b/metagpt/context.py index ba859ed5c..71570bac6 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -9,7 +9,7 @@ import os from pathlib import Path from typing import Optional -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel, ConfigDict, Field from metagpt.config2 import Config from metagpt.configs.llm_config import LLMConfig, LLMType @@ -42,31 +42,33 @@ class AttrDict(BaseModel): raise AttributeError(f"No such attribute: {key}") -class LLMMixin: +class LLMMixin(BaseModel): """Mixin class for LLM""" + model_config = ConfigDict(arbitrary_types_allowed=True) + # _config: Optional[Config] = None - _llm_config: Optional[LLMConfig] = None - _llm_instance: Optional[BaseLLM] = None + llm_config: Optional[LLMConfig] = Field(default=None, exclude=True) + llm_instance: Optional[BaseLLM] = Field(default=None, exclude=True) def use_llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI): """Use a LLM provider""" # 更新LLM配置 - self._llm_config = self._config.get_llm_config(name, provider) + self.llm_config = self.config.get_llm_config(name, provider) # 重置LLM实例 - self._llm_instance = None + self.llm_instance = None @property def llm(self) -> BaseLLM: """Return the LLM instance""" - if not self._llm_config: + if not self.llm_config: self.use_llm() - if not self._llm_instance and self._llm_config: - self._llm_instance = create_llm_instance(self._llm_config) - return self._llm_instance + if not self.llm_instance and self.llm_config: + self.llm_instance = create_llm_instance(self.llm_config) + return self.llm_instance -class Context(LLMMixin, BaseModel): +class Context(BaseModel): """Env context for MetaGPT""" model_config = ConfigDict(arbitrary_types_allowed=True) @@ -93,14 +95,14 @@ class Context(LLMMixin, BaseModel): env.update({k: v for k, v in i.items() if isinstance(v, str)}) return env - # def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: - # """Return a LLM instance""" - # llm_config = self.config.get_llm_config(name, provider) - # - # llm = create_llm_instance(llm_config) - # if llm.cost_manager is None: - # llm.cost_manager = self.cost_manager - # return llm + def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: + """Return a LLM instance""" + llm_config = self.config.get_llm_config(name, provider) + + llm = create_llm_instance(llm_config) + if llm.cost_manager is None: + llm.cost_manager = self.cost_manager + return llm class ContextMixin: diff --git a/metagpt/llm.py b/metagpt/llm.py index f9a5aaedb..aff72d3c5 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -15,4 +15,5 @@ from metagpt.provider.base_llm import BaseLLM def LLM(name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: """get the default llm provider if name is None""" + # context.use_llm(name=name, provider=provider) return context.llm(name=name, provider=provider) From 8cb270f3f4444cf5523331be6a50b8d80a2ef8b1 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 17:39:09 +0800 Subject: [PATCH 16/50] refine code --- metagpt/context.py | 49 ++++++++++++---------------------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/metagpt/context.py b/metagpt/context.py index 71570bac6..4016e8d7c 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -9,7 +9,7 @@ import os from pathlib import Path from typing import Optional -from pydantic import BaseModel, ConfigDict, Field +from pydantic import BaseModel, ConfigDict from metagpt.config2 import Config from metagpt.configs.llm_config import LLMConfig, LLMType @@ -42,30 +42,26 @@ class AttrDict(BaseModel): raise AttributeError(f"No such attribute: {key}") -class LLMMixin(BaseModel): +class LLMInstance: """Mixin class for LLM""" - model_config = ConfigDict(arbitrary_types_allowed=True) - # _config: Optional[Config] = None - llm_config: Optional[LLMConfig] = Field(default=None, exclude=True) - llm_instance: Optional[BaseLLM] = Field(default=None, exclude=True) + _llm_config: Optional[LLMConfig] = None + _llm_instance: Optional[BaseLLM] = None - def use_llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI): + def __init__(self, config: Config, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI): """Use a LLM provider""" # 更新LLM配置 - self.llm_config = self.config.get_llm_config(name, provider) + self._llm_config = config.get_llm_config(name, provider) # 重置LLM实例 - self.llm_instance = None + self._llm_instance = None @property - def llm(self) -> BaseLLM: + def instance(self) -> BaseLLM: """Return the LLM instance""" - if not self.llm_config: - self.use_llm() - if not self.llm_instance and self.llm_config: - self.llm_instance = create_llm_instance(self.llm_config) - return self.llm_instance + if not self._llm_instance and self._llm_config: + self._llm_instance = create_llm_instance(self._llm_config) + return self._llm_instance class Context(BaseModel): @@ -78,6 +74,7 @@ class Context(BaseModel): git_repo: Optional[GitRepository] = None src_workspace: Optional[Path] = None cost_manager: CostManager = CostManager() + _llm: Optional[LLMInstance] = None @property def file_repo(self): @@ -97,31 +94,11 @@ class Context(BaseModel): def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: """Return a LLM instance""" - llm_config = self.config.get_llm_config(name, provider) - - llm = create_llm_instance(llm_config) + llm = LLMInstance(self.config, name, provider).instance if llm.cost_manager is None: llm.cost_manager = self.cost_manager return llm -class ContextMixin: - """Mixin class for configurable objects: Priority: more specific < parent""" - - _context: Optional[Context] = None - - def __init__(self, context: Optional[Context] = None): - self._context = context - - def set_context(self, context: Optional[Context] = None): - """Set parent context""" - self._context = context - - @property - def context(self): - """Get config""" - return self._context - - # Global context, not in Env context = Context() From 83d884f475b7d0d4f33343975454db3672a818b1 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 17:52:34 +0800 Subject: [PATCH 17/50] refine code --- metagpt/config2.py | 3 ++- tests/metagpt/test_config.py | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/metagpt/config2.py b/metagpt/config2.py index 230e090af..6b6f4935b 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -153,12 +153,13 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict: return result -class ConfigMixin: +class ConfigMixin(BaseModel): """Mixin class for configurable objects""" _config: Optional[Config] = None def __init__(self, config: Optional[Config] = None): + super().__init__() self._config = config def try_set_parent_config(self, parent_config): diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index eecabb546..85e32818d 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -5,8 +5,9 @@ @Author : alexanderwu @File : test_config.py """ +from pydantic import BaseModel -from metagpt.config2 import Config, config +from metagpt.config2 import Config, ConfigMixin, config from metagpt.configs.llm_config import LLMType from tests.metagpt.provider.mock_llm_config import mock_llm_config @@ -26,3 +27,24 @@ def test_config_from_dict(): cfg = Config(llm={"default": mock_llm_config}) assert cfg assert cfg.llm["default"].api_key == "mock_api_key" + + +class NewModel(ConfigMixin, BaseModel): + a: str = "a" + b: str = "b" + + +def test_config_mixin(): + new_model = NewModel() + assert new_model.a == "a" + assert new_model.b == "b" + assert new_model._config == new_model.config + assert new_model._config is None + + +def test_config_mixin_2(): + i = Config(llm={"default": mock_llm_config}) + new_model = NewModel(config=i) + assert new_model.config == i + assert new_model._config == i + assert new_model.config.llm["default"] == mock_llm_config From 4df1213c86e2cda06938368072fffb8a9219c7ee Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 17:56:58 +0800 Subject: [PATCH 18/50] refine code --- tests/metagpt/test_config.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index 85e32818d..5492d1726 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -34,7 +34,7 @@ class NewModel(ConfigMixin, BaseModel): b: str = "b" -def test_config_mixin(): +def test_config_mixin_1(): new_model = NewModel() assert new_model.a == "a" assert new_model.b == "b" @@ -44,7 +44,12 @@ def test_config_mixin(): def test_config_mixin_2(): i = Config(llm={"default": mock_llm_config}) - new_model = NewModel(config=i) - assert new_model.config == i - assert new_model._config == i - assert new_model.config.llm["default"] == mock_llm_config + j = Config(llm={"new": mock_llm_config}) + obj = NewModel(config=i) + assert obj.config == i + assert obj._config == i + assert obj.config.llm["default"] == mock_llm_config + + obj.try_set_parent_config(j) + # obj already has a config, so it will not be set + assert obj.config == i From ccdb9a4bb205ca2277b378c1d72d071b104ecf85 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 19:43:46 +0800 Subject: [PATCH 19/50] add tests.. --- metagpt/config2.py | 22 +++++++++--------- tests/metagpt/test_config.py | 43 ++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/metagpt/config2.py b/metagpt/config2.py index 6b6f4935b..243a98078 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -156,21 +156,21 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict: class ConfigMixin(BaseModel): """Mixin class for configurable objects""" - _config: Optional[Config] = None + config: Optional[Config] = None - def __init__(self, config: Optional[Config] = None): - super().__init__() - self._config = config + def __init__(self, config: Optional[Config] = None, **kwargs): + """Initialize with config""" + super().__init__(**kwargs) + self.set_config(config) - def try_set_parent_config(self, parent_config): + def set(self, k, v, override=False): """Try to set parent config if not set""" - if self._config is None: - self._config = parent_config + if override or not self.__dict__.get(k): + self.__dict__[k] = v - @property - def config(self): - """Get config""" - return self._config + def set_config(self, config: Config, override=False): + """Set config""" + self.set("config", config, override) config = Config.default() diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index 5492d1726..81673fc65 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -29,27 +29,56 @@ def test_config_from_dict(): assert cfg.llm["default"].api_key == "mock_api_key" -class NewModel(ConfigMixin, BaseModel): +class ModelX(ConfigMixin, BaseModel): a: str = "a" b: str = "b" +class WTFMixin(BaseModel): + c: str = "c" + d: str = "d" + + def __init__(self, **data): + super().__init__(**data) + + +class ModelY(WTFMixin, ModelX): + def __init__(self, **data): + super().__init__(**data) + + def test_config_mixin_1(): - new_model = NewModel() + new_model = ModelX() assert new_model.a == "a" assert new_model.b == "b" - assert new_model._config == new_model.config - assert new_model._config is None def test_config_mixin_2(): i = Config(llm={"default": mock_llm_config}) j = Config(llm={"new": mock_llm_config}) - obj = NewModel(config=i) + obj = ModelX(config=i) assert obj.config == i - assert obj._config == i assert obj.config.llm["default"] == mock_llm_config - obj.try_set_parent_config(j) + obj.set_config(j) # obj already has a config, so it will not be set assert obj.config == i + + +def test_config_mixin_3(): + """Test config mixin with multiple inheritance""" + i = Config(llm={"default": mock_llm_config}) + j = Config(llm={"new": mock_llm_config}) + obj = ModelY(config=i) + assert obj.config == i + assert obj.config.llm["default"] == mock_llm_config + + obj.set_config(j) + # obj already has a config, so it will not be set + assert obj.config == i + assert obj.config.llm["default"] == mock_llm_config + + assert obj.a == "a" + assert obj.b == "b" + assert obj.c == "c" + assert obj.d == "d" From 8bea366f2894a8c7f722f208849d78093d4b7b75 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 19:45:13 +0800 Subject: [PATCH 20/50] add tests.. --- tests/metagpt/test_config.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index 81673fc65..bd22bf88b 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -38,13 +38,9 @@ class WTFMixin(BaseModel): c: str = "c" d: str = "d" - def __init__(self, **data): - super().__init__(**data) - class ModelY(WTFMixin, ModelX): - def __init__(self, **data): - super().__init__(**data) + pass def test_config_mixin_1(): From b0efa4b6a54de0c72fd4142289e01eff73e69aab Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 21:16:11 +0800 Subject: [PATCH 21/50] refine config mixin --- metagpt/config2.py | 7 ++++--- metagpt/roles/role.py | 3 ++- tests/metagpt/test_config.py | 14 +++++++------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/metagpt/config2.py b/metagpt/config2.py index 243a98078..393c46200 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -156,7 +156,8 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict: class ConfigMixin(BaseModel): """Mixin class for configurable objects""" - config: Optional[Config] = None + # Env/Role/Action will use this config as private config, or use self.context.config as public config + _config: Optional[Config] = None def __init__(self, config: Optional[Config] = None, **kwargs): """Initialize with config""" @@ -164,13 +165,13 @@ class ConfigMixin(BaseModel): self.set_config(config) def set(self, k, v, override=False): - """Try to set parent config if not set""" + """Set attribute""" if override or not self.__dict__.get(k): self.__dict__[k] = v def set_config(self, config: Config, override=False): """Set config""" - self.set("config", config, override) + self.set("_config", config, override) config = Config.default() diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 42996bea8..88bab72cb 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -30,6 +30,7 @@ from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validat from metagpt.actions import Action, ActionOutput from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement +from metagpt.config2 import ConfigMixin from metagpt.context import Context, context from metagpt.llm import LLM from metagpt.logs import logger @@ -119,7 +120,7 @@ class RoleContext(BaseModel): return self.memory.get() -class Role(SerializationMixin): +class Role(SerializationMixin, ConfigMixin, BaseModel): """Role/Agent""" model_config = ConfigDict(arbitrary_types_allowed=True, exclude=["llm"]) diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index bd22bf88b..0a2c0d462 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -53,12 +53,12 @@ def test_config_mixin_2(): i = Config(llm={"default": mock_llm_config}) j = Config(llm={"new": mock_llm_config}) obj = ModelX(config=i) - assert obj.config == i - assert obj.config.llm["default"] == mock_llm_config + assert obj._config == i + assert obj._config.llm["default"] == mock_llm_config obj.set_config(j) # obj already has a config, so it will not be set - assert obj.config == i + assert obj._config == i def test_config_mixin_3(): @@ -66,13 +66,13 @@ def test_config_mixin_3(): i = Config(llm={"default": mock_llm_config}) j = Config(llm={"new": mock_llm_config}) obj = ModelY(config=i) - assert obj.config == i - assert obj.config.llm["default"] == mock_llm_config + assert obj._config == i + assert obj._config.llm["default"] == mock_llm_config obj.set_config(j) # obj already has a config, so it will not be set - assert obj.config == i - assert obj.config.llm["default"] == mock_llm_config + assert obj._config == i + assert obj._config.llm["default"] == mock_llm_config assert obj.a == "a" assert obj.b == "b" From c9e05a2186cd8d95e73f0ed41937b38e4f7721d5 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 21:31:38 +0800 Subject: [PATCH 22/50] refine code --- metagpt/actions/action.py | 5 +++-- metagpt/roles/role.py | 43 ++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 9f045bbaa..cdedfcd64 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -10,10 +10,11 @@ from __future__ import annotations from typing import Optional, Union -from pydantic import ConfigDict, Field, model_validator +from pydantic import BaseModel, ConfigDict, Field, model_validator import metagpt from metagpt.actions.action_node import ActionNode +from metagpt.config2 import ConfigMixin from metagpt.context import Context from metagpt.llm import LLM from metagpt.provider.base_llm import BaseLLM @@ -27,7 +28,7 @@ from metagpt.schema import ( from metagpt.utils.file_repository import FileRepository -class Action(SerializationMixin): +class Action(SerializationMixin, ConfigMixin, BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True, exclude=["llm"]) name: str = "" diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 88bab72cb..75dff94f2 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -146,6 +146,23 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): __hash__ = object.__hash__ # support Role as hashable type in `Environment.members` + def __init__(self, **data: Any): + self.pydantic_rebuild_model() + super().__init__(**data) + + self.llm.system_prompt = self._get_prefix() + self._watch(data.get("watch") or [UserRequirement]) + + if self.latest_observed_msg: + self.recovered = True + + @staticmethod + def pydantic_rebuild_model(): + from metagpt.environment import Environment + + Environment + Role.model_rebuild() + @property def todo(self) -> Action: return self.rc.todo @@ -157,6 +174,9 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): @property def config(self): + """Role config: role config > context config""" + if self._config: + return self._config return self.context.config @property @@ -177,19 +197,19 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): @property def prompt_schema(self): - return self.context.config.prompt_schema + return self.config.prompt_schema @property def project_name(self): - return self.context.config.project_name + return self.config.project_name @project_name.setter def project_name(self, value): - self.context.config.project_name = value + self.config.project_name = value @property def project_path(self): - return self.context.config.project_path + return self.config.project_path @model_validator(mode="after") def check_addresses(self): @@ -197,21 +217,6 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): self.addresses = {any_to_str(self), self.name} if self.name else {any_to_str(self)} return self - def __init__(self, **data: Any): - # --- avoid PydanticUndefinedAnnotation name 'Environment' is not defined # - from metagpt.environment import Environment - - Environment - # ------ - Role.model_rebuild() - super().__init__(**data) - - self.llm.system_prompt = self._get_prefix() - self._watch(data.get("watch") or [UserRequirement]) - - if self.latest_observed_msg: - self.recovered = True - def _reset(self): self.states = [] self.actions = [] From df9d5158ecda75a8fca6c4c1dd25326d5b85b126 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 21:38:09 +0800 Subject: [PATCH 23/50] refine code --- metagpt/roles/role.py | 11 ++++++----- tests/metagpt/test_role.py | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 75dff94f2..959b5d00d 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -158,6 +158,7 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): @staticmethod def pydantic_rebuild_model(): + """Rebuild model to avoid `RecursionError: maximum recursion depth exceeded in comparison`""" from metagpt.environment import Environment Environment @@ -165,9 +166,11 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): @property def todo(self) -> Action: + """Get action to do""" return self.rc.todo def set_todo(self, value: Optional[Action]): + """Set action to do and update context""" if value: value.g_context = self.context self.rc.todo = value @@ -181,6 +184,7 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): @property def git_repo(self): + """Git repo""" return self.context.git_repo @git_repo.setter @@ -189,6 +193,7 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): @property def src_workspace(self): + """Source workspace under git repo""" return self.context.src_workspace @src_workspace.setter @@ -197,6 +202,7 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): @property def prompt_schema(self): + """Prompt schema: json/markdown""" return self.config.prompt_schema @property @@ -308,11 +314,6 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): env.set_addresses(self, self.addresses) self.llm.system_prompt = self._get_prefix() - @property - def action_count(self): - """Return number of action""" - return len(self.actions) - def _get_prefix(self): """Get the role prefix""" if self.desc: diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 20c8dba6d..c67a8ad8a 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -112,7 +112,7 @@ async def test_send_to(): def test_init_action(): role = Role() role.add_actions([MockAction, MockAction]) - assert role.action_count == 2 + assert len(role.actions) == 2 @pytest.mark.asyncio From 21ee9662251f58ac05e1d9156a4376ef1c154942 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 9 Jan 2024 22:04:49 +0800 Subject: [PATCH 24/50] refine code --- metagpt/actions/action.py | 23 +++++------ metagpt/actions/debug_error.py | 4 +- metagpt/actions/design_api.py | 2 +- metagpt/actions/design_api_review.py | 2 +- metagpt/actions/execute_task.py | 2 +- metagpt/actions/invoice_ocr.py | 6 +-- metagpt/actions/prepare_documents.py | 6 +-- metagpt/actions/project_management.py | 2 +- metagpt/actions/rebuild_class_view.py | 6 +-- metagpt/actions/rebuild_sequence_view.py | 2 +- metagpt/actions/research.py | 6 +-- metagpt/actions/run_code.py | 4 +- metagpt/actions/search_and_summarize.py | 23 ++++------- metagpt/actions/summarize_code.py | 4 +- metagpt/actions/talk_action.py | 6 +-- metagpt/actions/write_code.py | 6 +-- metagpt/actions/write_code_review.py | 6 +-- metagpt/actions/write_docstring.py | 2 +- metagpt/actions/write_prd_review.py | 2 +- metagpt/actions/write_teaching_plan.py | 2 +- metagpt/actions/write_test.py | 2 +- metagpt/config.py | 4 +- metagpt/config2.py | 21 ---------- metagpt/context.py | 52 ++++++++++++++++++++++++ metagpt/roles/engineer.py | 16 ++++---- metagpt/roles/role.py | 16 ++------ tests/metagpt/test_config.py | 5 ++- 27 files changed, 123 insertions(+), 109 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index cdedfcd64..cabab784f 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -12,10 +12,8 @@ from typing import Optional, Union from pydantic import BaseModel, ConfigDict, Field, model_validator -import metagpt from metagpt.actions.action_node import ActionNode -from metagpt.config2 import ConfigMixin -from metagpt.context import Context +from metagpt.context import ContextMixin from metagpt.llm import LLM from metagpt.provider.base_llm import BaseLLM from metagpt.schema import ( @@ -28,44 +26,43 @@ from metagpt.schema import ( from metagpt.utils.file_repository import FileRepository -class Action(SerializationMixin, ConfigMixin, BaseModel): +class Action(SerializationMixin, ContextMixin, BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True, exclude=["llm"]) name: str = "" llm: BaseLLM = Field(default_factory=LLM, exclude=True) - context: Union[dict, CodingContext, CodeSummarizeContext, TestingContext, RunCodeContext, str, None] = "" + i_context: Union[dict, CodingContext, CodeSummarizeContext, TestingContext, RunCodeContext, str, None] = "" prefix: str = "" # aask*时会加上prefix,作为system_message desc: str = "" # for skill manager node: ActionNode = Field(default=None, exclude=True) - g_context: Optional[Context] = Field(default=metagpt.context.context, exclude=True) @property def git_repo(self): - return self.g_context.git_repo + return self.context.git_repo @property def file_repo(self): - return FileRepository(self.g_context.git_repo) + return FileRepository(self.context.git_repo) @property def src_workspace(self): - return self.g_context.src_workspace + return self.context.src_workspace @property def prompt_schema(self): - return self.g_context.config.prompt_schema + return self.config.prompt_schema @property def project_name(self): - return self.g_context.config.project_name + return self.config.project_name @project_name.setter def project_name(self, value): - self.g_context.config.project_name = value + self.config.project_name = value @property def project_path(self): - return self.g_context.config.project_path + return self.config.project_path @model_validator(mode="before") @classmethod diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index aa84d1f11..3647640c0 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -47,7 +47,7 @@ Now you should start rewriting the code: class DebugError(Action): - context: RunCodeContext = Field(default_factory=RunCodeContext) + i_context: RunCodeContext = Field(default_factory=RunCodeContext) async def run(self, *args, **kwargs) -> str: output_doc = await self.file_repo.get_file( @@ -63,7 +63,7 @@ class DebugError(Action): logger.info(f"Debug and rewrite {self.context.test_filename}") code_doc = await self.file_repo.get_file( - filename=self.context.code_filename, relative_path=self.g_context.src_workspace + filename=self.context.code_filename, relative_path=self.context.src_workspace ) if not code_doc: return "" diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index b89ec7877..3e978f823 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -37,7 +37,7 @@ NEW_REQ_TEMPLATE = """ class WriteDesign(Action): name: str = "" - context: Optional[str] = None + i_context: Optional[str] = None desc: str = ( "Based on the PRD, think about the system design, and design the corresponding APIs, " "data structures, library tables, processes, and paths. Please provide your design, feedback " diff --git a/metagpt/actions/design_api_review.py b/metagpt/actions/design_api_review.py index fb1b92d85..ccd01a4c3 100644 --- a/metagpt/actions/design_api_review.py +++ b/metagpt/actions/design_api_review.py @@ -13,7 +13,7 @@ from metagpt.actions.action import Action class DesignReview(Action): name: str = "DesignReview" - context: Optional[str] = None + i_context: Optional[str] = None async def run(self, prd, api_design): prompt = ( diff --git a/metagpt/actions/execute_task.py b/metagpt/actions/execute_task.py index 4ae4ee17b..1cc3bd699 100644 --- a/metagpt/actions/execute_task.py +++ b/metagpt/actions/execute_task.py @@ -13,7 +13,7 @@ from metagpt.schema import Message class ExecuteTask(Action): name: str = "ExecuteTask" - context: list[Message] = [] + i_context: list[Message] = [] async def run(self, *args, **kwargs): pass diff --git a/metagpt/actions/invoice_ocr.py b/metagpt/actions/invoice_ocr.py index 36570097a..a3406ff65 100644 --- a/metagpt/actions/invoice_ocr.py +++ b/metagpt/actions/invoice_ocr.py @@ -41,7 +41,7 @@ class InvoiceOCR(Action): """ name: str = "InvoiceOCR" - context: Optional[str] = None + i_context: Optional[str] = None @staticmethod async def _check_file_type(file_path: Path) -> str: @@ -132,7 +132,7 @@ class GenerateTable(Action): """ name: str = "GenerateTable" - context: Optional[str] = None + i_context: Optional[str] = None llm: BaseLLM = Field(default_factory=LLM) language: str = "ch" @@ -177,7 +177,7 @@ class ReplyQuestion(Action): """ name: str = "ReplyQuestion" - context: Optional[str] = None + i_context: Optional[str] = None llm: BaseLLM = Field(default_factory=LLM) language: str = "ch" diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index ae5aaf2b5..8a9e78b2a 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -22,11 +22,11 @@ class PrepareDocuments(Action): """PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt.""" name: str = "PrepareDocuments" - context: Optional[str] = None + i_context: Optional[str] = None @property def config(self): - return self.g_context.config + return self.context.config def _init_repo(self): """Initialize the Git environment.""" @@ -39,7 +39,7 @@ class PrepareDocuments(Action): shutil.rmtree(path) self.config.project_path = path self.config.project_name = path.name - self.g_context.git_repo = GitRepository(local_path=path, auto_init=True) + self.context.git_repo = GitRepository(local_path=path, auto_init=True) async def run(self, with_messages, **kwargs): """Create and initialize the workspace folder, initialize the Git environment.""" diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index b40da824f..bb8141a74 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -36,7 +36,7 @@ NEW_REQ_TEMPLATE = """ class WriteTasks(Action): name: str = "CreateTasks" - context: Optional[str] = None + i_context: Optional[str] = None async def run(self, with_messages): system_design_file_repo = self.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) diff --git a/metagpt/actions/rebuild_class_view.py b/metagpt/actions/rebuild_class_view.py index 5128b9fee..876beccec 100644 --- a/metagpt/actions/rebuild_class_view.py +++ b/metagpt/actions/rebuild_class_view.py @@ -32,13 +32,13 @@ class RebuildClassView(Action): async def run(self, with_messages=None, format=CONFIG.prompt_schema): graph_repo_pathname = CONFIG.git_repo.workdir / GRAPH_REPO_FILE_REPO / CONFIG.git_repo.workdir.name graph_db = await DiGraphRepository.load_from(str(graph_repo_pathname.with_suffix(".json"))) - repo_parser = RepoParser(base_directory=Path(self.context)) + repo_parser = RepoParser(base_directory=Path(self.i_context)) # use pylint - class_views, relationship_views, package_root = await repo_parser.rebuild_class_views(path=Path(self.context)) + class_views, relationship_views, package_root = await repo_parser.rebuild_class_views(path=Path(self.i_context)) await GraphRepository.update_graph_db_with_class_views(graph_db, class_views) await GraphRepository.update_graph_db_with_class_relationship_views(graph_db, relationship_views) # use ast - direction, diff_path = self._diff_path(path_root=Path(self.context).resolve(), package_root=package_root) + direction, diff_path = self._diff_path(path_root=Path(self.i_context).resolve(), package_root=package_root) symbols = repo_parser.generate_symbols() for file_info in symbols: # Align to the same root directory in accordance with `class_views`. diff --git a/metagpt/actions/rebuild_sequence_view.py b/metagpt/actions/rebuild_sequence_view.py index 865050c93..bc128d8b0 100644 --- a/metagpt/actions/rebuild_sequence_view.py +++ b/metagpt/actions/rebuild_sequence_view.py @@ -41,7 +41,7 @@ class RebuildSequenceView(Action): async def _rebuild_sequence_view(self, entry, graph_db): filename = entry.subject.split(":", 1)[0] - src_filename = RebuildSequenceView._get_full_filename(root=self.context, pathname=filename) + src_filename = RebuildSequenceView._get_full_filename(root=self.i_context, pathname=filename) content = await aread(filename=src_filename, encoding="utf-8") content = f"```python\n{content}\n```\n\n---\nTranslate the code above into Mermaid Sequence Diagram." data = await self.llm.aask( diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index 90b08cb6a..84067ad92 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -81,7 +81,7 @@ class CollectLinks(Action): """Action class to collect links from a search engine.""" name: str = "CollectLinks" - context: Optional[str] = None + i_context: Optional[str] = None desc: str = "Collect links from a search engine." search_engine: SearchEngine = Field(default_factory=SearchEngine) @@ -177,7 +177,7 @@ class WebBrowseAndSummarize(Action): """Action class to explore the web and provide summaries of articles and webpages.""" name: str = "WebBrowseAndSummarize" - context: Optional[str] = None + i_context: Optional[str] = None llm: BaseLLM = Field(default_factory=LLM) desc: str = "Explore the web and provide summaries of articles and webpages." browse_func: Union[Callable[[list[str]], None], None] = None @@ -248,7 +248,7 @@ class ConductResearch(Action): """Action class to conduct research and generate a research report.""" name: str = "ConductResearch" - context: Optional[str] = None + i_context: Optional[str] = None llm: BaseLLM = Field(default_factory=LLM) def __init__(self, **kwargs): diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index 0d42308c1..8fdda0a0d 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -76,7 +76,7 @@ standard errors: class RunCode(Action): name: str = "RunCode" - context: RunCodeContext = Field(default_factory=RunCodeContext) + i_context: RunCodeContext = Field(default_factory=RunCodeContext) @classmethod async def run_text(cls, code) -> Tuple[str, str]: @@ -93,7 +93,7 @@ class RunCode(Action): additional_python_paths = [str(path) for path in additional_python_paths] # Copy the current environment variables - env = self.g_context.new_environ() + env = self.context.new_environ() # Modify the PYTHONPATH environment variable additional_python_paths = [working_directory] + additional_python_paths diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 39ca23df5..59b35cd58 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -8,10 +8,9 @@ from typing import Any, Optional import pydantic -from pydantic import Field, model_validator +from pydantic import model_validator from metagpt.actions import Action -from metagpt.config import Config from metagpt.logs import logger from metagpt.schema import Message from metagpt.tools import SearchEngineType @@ -106,28 +105,22 @@ You are a member of a professional butler team and will provide helpful suggesti class SearchAndSummarize(Action): name: str = "" content: Optional[str] = None - config: None = Field(default_factory=Config) engine: Optional[SearchEngineType] = None search_func: Optional[Any] = None search_engine: SearchEngine = None result: str = "" - @model_validator(mode="before") - @classmethod - def validate_engine_and_run_func(cls, values): - engine = values.get("engine") - search_func = values.get("search_func") - config = Config() - - if engine is None: - engine = config.search_engine + @model_validator(mode="after") + def validate_engine_and_run_func(self): + if self.engine is None: + self.engine = self.config.search_engine try: - search_engine = SearchEngine(engine=engine, run_func=search_func) + search_engine = SearchEngine(engine=self.engine, run_func=self.search_func) except pydantic.ValidationError: search_engine = None - values["search_engine"] = search_engine - return values + self.search_engine = search_engine + return self async def run(self, context: list[Message], system_text=SEARCH_AND_SUMMARIZE_SYSTEM) -> str: if self.search_engine is None: diff --git a/metagpt/actions/summarize_code.py b/metagpt/actions/summarize_code.py index 948eceab2..690d5c77b 100644 --- a/metagpt/actions/summarize_code.py +++ b/metagpt/actions/summarize_code.py @@ -90,7 +90,7 @@ flowchart TB class SummarizeCode(Action): name: str = "SummarizeCode" - context: CodeSummarizeContext = Field(default_factory=CodeSummarizeContext) + i_context: CodeSummarizeContext = Field(default_factory=CodeSummarizeContext) @retry(stop=stop_after_attempt(2), wait=wait_random_exponential(min=1, max=60)) async def summarize_code(self, prompt): @@ -103,7 +103,7 @@ class SummarizeCode(Action): design_doc = await repo.get_file(filename=design_pathname.name, relative_path=SYSTEM_DESIGN_FILE_REPO) task_pathname = Path(self.context.task_filename) task_doc = await repo.get_file(filename=task_pathname.name, relative_path=TASK_FILE_REPO) - src_file_repo = self.git_repo.new_file_repository(relative_path=self.g_context.src_workspace) + src_file_repo = self.git_repo.new_file_repository(relative_path=self.context.src_workspace) code_blocks = [] for filename in self.context.codes_filenames: code_doc = await src_file_repo.get(filename) diff --git a/metagpt/actions/talk_action.py b/metagpt/actions/talk_action.py index eab1740fc..253b829ed 100644 --- a/metagpt/actions/talk_action.py +++ b/metagpt/actions/talk_action.py @@ -15,18 +15,18 @@ from metagpt.schema import Message class TalkAction(Action): - context: str + i_context: str history_summary: str = "" knowledge: str = "" rsp: Optional[Message] = None @property def agent_description(self): - return self.g_context.kwargs.agent_description + return self.context.kwargs.agent_description @property def language(self): - return self.g_context.kwargs.language or config.language + return self.context.kwargs.language or config.language @property def prompt(self): diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 2b8f91a1d..779fe52a6 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -85,7 +85,7 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc class WriteCode(Action): name: str = "WriteCode" - context: Document = Field(default_factory=Document) + i_context: Document = Field(default_factory=Document) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def write_code(self, prompt) -> str: @@ -116,7 +116,7 @@ class WriteCode(Action): coding_context.task_doc, exclude=self.context.filename, git_repo=self.git_repo, - src_workspace=self.g_context.src_workspace, + src_workspace=self.context.src_workspace, ) prompt = PROMPT_TEMPLATE.format( @@ -132,7 +132,7 @@ class WriteCode(Action): code = await self.write_code(prompt) if not coding_context.code_doc: # avoid root_path pydantic ValidationError if use WriteCode alone - root_path = self.g_context.src_workspace if self.g_context.src_workspace else "" + root_path = self.context.src_workspace if self.context.src_workspace else "" coding_context.code_doc = Document(filename=coding_context.filename, root_path=str(root_path)) coding_context.code_doc.content = code return coding_context diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 4433a7ab9..6ff9d5aa4 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -119,7 +119,7 @@ REWRITE_CODE_TEMPLATE = """ class WriteCodeReview(Action): name: str = "WriteCodeReview" - context: CodingContext = Field(default_factory=CodingContext) + i_context: CodingContext = Field(default_factory=CodingContext) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def write_code_review_and_rewrite(self, context_prompt, cr_prompt, filename): @@ -136,14 +136,14 @@ class WriteCodeReview(Action): async def run(self, *args, **kwargs) -> CodingContext: iterative_code = self.context.code_doc.content - k = self.g_context.config.code_review_k_times or 1 + k = self.context.config.code_review_k_times or 1 for i in range(k): format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) task_content = self.context.task_doc.content if self.context.task_doc else "" code_context = await WriteCode.get_codes( self.context.task_doc, exclude=self.context.filename, - git_repo=self.g_context.git_repo, + git_repo=self.context.git_repo, src_workspace=self.src_workspace, ) context = "\n".join( diff --git a/metagpt/actions/write_docstring.py b/metagpt/actions/write_docstring.py index 8b8335517..79204e6a4 100644 --- a/metagpt/actions/write_docstring.py +++ b/metagpt/actions/write_docstring.py @@ -161,7 +161,7 @@ class WriteDocstring(Action): """ desc: str = "Write docstring for code." - context: Optional[str] = None + i_context: Optional[str] = None async def run( self, diff --git a/metagpt/actions/write_prd_review.py b/metagpt/actions/write_prd_review.py index 2babe38db..68fb5d9e8 100644 --- a/metagpt/actions/write_prd_review.py +++ b/metagpt/actions/write_prd_review.py @@ -13,7 +13,7 @@ from metagpt.actions.action import Action class WritePRDReview(Action): name: str = "" - context: Optional[str] = None + i_context: Optional[str] = None prd: Optional[str] = None desc: str = "Based on the PRD, conduct a PRD Review, providing clear and detailed feedback" diff --git a/metagpt/actions/write_teaching_plan.py b/metagpt/actions/write_teaching_plan.py index 76923a663..04507fda3 100644 --- a/metagpt/actions/write_teaching_plan.py +++ b/metagpt/actions/write_teaching_plan.py @@ -15,7 +15,7 @@ from metagpt.logs import logger class WriteTeachingPlanPart(Action): """Write Teaching Plan Part""" - context: Optional[str] = None + i_context: Optional[str] = None topic: str = "" language: str = "Chinese" rsp: Optional[str] = None diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 96486311f..38b1cf03c 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -39,7 +39,7 @@ you should correctly import the necessary classes based on these file locations! class WriteTest(Action): name: str = "WriteTest" - context: Optional[TestingContext] = None + i_context: Optional[TestingContext] = None async def write_code(self, prompt): code_rsp = await self._aask(prompt) diff --git a/metagpt/config.py b/metagpt/config.py index 0c7b54f83..952ccc962 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -133,8 +133,8 @@ class Config(metaclass=Singleton): self.ollama_api_base = self._get("OLLAMA_API_BASE") self.ollama_api_model = self._get("OLLAMA_API_MODEL") - if not self._get("DISABLE_LLM_PROVIDER_CHECK"): - _ = self.get_default_llm_provider_enum() + # if not self._get("DISABLE_LLM_PROVIDER_CHECK"): + # _ = self.get_default_llm_provider_enum() self.openai_base_url = self._get("OPENAI_BASE_URL") self.openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy diff --git a/metagpt/config2.py b/metagpt/config2.py index 393c46200..cb5c22ac2 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -153,25 +153,4 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict: return result -class ConfigMixin(BaseModel): - """Mixin class for configurable objects""" - - # Env/Role/Action will use this config as private config, or use self.context.config as public config - _config: Optional[Config] = None - - def __init__(self, config: Optional[Config] = None, **kwargs): - """Initialize with config""" - super().__init__(**kwargs) - self.set_config(config) - - def set(self, k, v, override=False): - """Set attribute""" - if override or not self.__dict__.get(k): - self.__dict__[k] = v - - def set_config(self, config: Config, override=False): - """Set config""" - self.set("_config", config, override) - - config = Config.default() diff --git a/metagpt/context.py b/metagpt/context.py index 4016e8d7c..74f7b133d 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -100,5 +100,57 @@ class Context(BaseModel): return llm +class ContextMixin(BaseModel): + """Mixin class for context and config""" + + # Env/Role/Action will use this context as private context, or use self.context as public context + _context: Optional[Context] = None + # Env/Role/Action will use this config as private config, or use self.context.config as public config + _config: Optional[Config] = None + + def __init__(self, context: Optional[Context] = None, config: Optional[Config] = None, **kwargs): + """Initialize with config""" + super().__init__(**kwargs) + self.set_context(context) + self.set_config(config) + + def set(self, k, v, override=False): + """Set attribute""" + if override or not self.__dict__.get(k): + self.__dict__[k] = v + + def set_context(self, context: Context, override=True): + """Set context""" + self.set("_context", context, override) + + def set_config(self, config: Config, override=False): + """Set config""" + self.set("_config", config, override) + + @property + def config(self): + """Role config: role config > context config""" + if self._config: + return self._config + return self.context.config + + @config.setter + def config(self, config: Config): + """Set config""" + self.set_config(config) + + @property + def context(self): + """Role context: role context > context""" + if self._context: + return self._context + return context + + @context.setter + def context(self, context: Context): + """Set context""" + self.set_context(context) + + # Global context, not in Env context = Context() diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index ad0c1ac92..dc9f31686 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -159,9 +159,9 @@ class Engineer(Role): src_relative_path = self.src_workspace.relative_to(self.git_repo.workdir) for todo in self.summarize_todos: summary = await todo.run() - summary_filename = Path(todo.context.design_filename).with_suffix(".md").name - dependencies = {todo.context.design_filename, todo.context.task_filename} - for filename in todo.context.codes_filenames: + summary_filename = Path(todo.i_context.design_filename).with_suffix(".md").name + dependencies = {todo.i_context.design_filename, todo.i_context.task_filename} + for filename in todo.i_context.codes_filenames: rpath = src_relative_path / filename dependencies.add(str(rpath)) await code_summaries_pdf_file_repo.save( @@ -169,15 +169,15 @@ class Engineer(Role): ) is_pass, reason = await self._is_pass(summary) if not is_pass: - todo.context.reason = reason - tasks.append(todo.context.dict()) + todo.i_context.reason = reason + tasks.append(todo.i_context.dict()) await code_summaries_file_repo.save( - filename=Path(todo.context.design_filename).name, - content=todo.context.model_dump_json(), + filename=Path(todo.i_context.design_filename).name, + content=todo.i_context.model_dump_json(), dependencies=dependencies, ) else: - await code_summaries_file_repo.delete(filename=Path(todo.context.design_filename).name) + await code_summaries_file_repo.delete(filename=Path(todo.i_context.design_filename).name) logger.info(f"--max-auto-summarize-code={self.config.max_auto_summarize_code}") if not tasks or self.config.max_auto_summarize_code == 0: diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 959b5d00d..e31eabd23 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -30,8 +30,7 @@ from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validat from metagpt.actions import Action, ActionOutput from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement -from metagpt.config2 import ConfigMixin -from metagpt.context import Context, context +from metagpt.context import ContextMixin from metagpt.llm import LLM from metagpt.logs import logger from metagpt.memory import Memory @@ -120,7 +119,7 @@ class RoleContext(BaseModel): return self.memory.get() -class Role(SerializationMixin, ConfigMixin, BaseModel): +class Role(SerializationMixin, ContextMixin, BaseModel): """Role/Agent""" model_config = ConfigDict(arbitrary_types_allowed=True, exclude=["llm"]) @@ -142,7 +141,7 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): # builtin variables recovered: bool = False # to tag if a recovered role latest_observed_msg: Optional[Message] = None # record the latest observed message when interrupted - context: Optional[Context] = Field(default=context, exclude=True) + # context: Optional[Context] = Field(default=context, exclude=True) __hash__ = object.__hash__ # support Role as hashable type in `Environment.members` @@ -172,16 +171,9 @@ class Role(SerializationMixin, ConfigMixin, BaseModel): def set_todo(self, value: Optional[Action]): """Set action to do and update context""" if value: - value.g_context = self.context + value.context = self.context self.rc.todo = value - @property - def config(self): - """Role config: role config > context config""" - if self._config: - return self._config - return self.context.config - @property def git_repo(self): """Git repo""" diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index 0a2c0d462..c74b16930 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -7,8 +7,9 @@ """ from pydantic import BaseModel -from metagpt.config2 import Config, ConfigMixin, config +from metagpt.config2 import Config, config from metagpt.configs.llm_config import LLMType +from metagpt.context import ContextMixin from tests.metagpt.provider.mock_llm_config import mock_llm_config @@ -29,7 +30,7 @@ def test_config_from_dict(): assert cfg.llm["default"].api_key == "mock_api_key" -class ModelX(ConfigMixin, BaseModel): +class ModelX(ContextMixin, BaseModel): a: str = "a" b: str = "b" From 8dc98b27695b349f0fab91e748353342402042ac Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 11:10:51 +0800 Subject: [PATCH 25/50] refine code --- metagpt/roles/role.py | 1 - 1 file changed, 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index e31eabd23..98cc05234 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -141,7 +141,6 @@ class Role(SerializationMixin, ContextMixin, BaseModel): # builtin variables recovered: bool = False # to tag if a recovered role latest_observed_msg: Optional[Message] = None # record the latest observed message when interrupted - # context: Optional[Context] = Field(default=context, exclude=True) __hash__ = object.__hash__ # support Role as hashable type in `Environment.members` From ba6793383ffb283d7878ee91f30b0a39c10a4d46 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 13:40:55 +0800 Subject: [PATCH 26/50] disable pretty_exceptions_show_locals --- metagpt/startup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/startup.py b/metagpt/startup.py index cd5b4dac7..14092edd2 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -9,7 +9,7 @@ import typer from metagpt.config2 import config from metagpt.const import METAGPT_ROOT -app = typer.Typer(add_completion=False) +app = typer.Typer(add_completion=False, pretty_exceptions_show_locals=False) def generate_repo( From f5bb850f2500a1a9fc2de21d65e1741520a75054 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 13:56:02 +0800 Subject: [PATCH 27/50] refine code: gloabl context to CONTEXT --- metagpt/context.py | 4 +-- metagpt/llm.py | 4 +-- metagpt/roles/assistant.py | 6 ++-- tests/conftest.py | 8 ++--- tests/metagpt/actions/test_debug_error.py | 8 ++--- tests/metagpt/actions/test_design_api.py | 4 +-- .../metagpt/actions/test_prepare_documents.py | 14 ++++----- .../actions/test_project_management.py | 8 ++--- tests/metagpt/actions/test_summarize_code.py | 18 +++++------ tests/metagpt/actions/test_write_code.py | 20 ++++++------- tests/metagpt/actions/test_write_prd.py | 4 +-- tests/metagpt/roles/test_architect.py | 4 +-- tests/metagpt/roles/test_assistant.py | 10 +++---- tests/metagpt/roles/test_engineer.py | 30 +++++++++---------- tests/metagpt/roles/test_qa_engineer.py | 8 ++--- tests/metagpt/roles/test_teacher.py | 6 ++-- tests/metagpt/test_context.py | 6 ++-- tests/metagpt/test_environment.py | 8 ++--- 18 files changed, 85 insertions(+), 85 deletions(-) diff --git a/metagpt/context.py b/metagpt/context.py index 74f7b133d..4083a1696 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -144,7 +144,7 @@ class ContextMixin(BaseModel): """Role context: role context > context""" if self._context: return self._context - return context + return CONTEXT @context.setter def context(self, context: Context): @@ -153,4 +153,4 @@ class ContextMixin(BaseModel): # Global context, not in Env -context = Context() +CONTEXT = Context() diff --git a/metagpt/llm.py b/metagpt/llm.py index aff72d3c5..d393738bb 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -9,11 +9,11 @@ from typing import Optional from metagpt.configs.llm_config import LLMType -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.provider.base_llm import BaseLLM def LLM(name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: """get the default llm provider if name is None""" # context.use_llm(name=name, provider=provider) - return context.llm(name=name, provider=provider) + return CONTEXT.llm(name=name, provider=provider) diff --git a/metagpt/roles/assistant.py b/metagpt/roles/assistant.py index 90a33ad6a..8939094ed 100644 --- a/metagpt/roles/assistant.py +++ b/metagpt/roles/assistant.py @@ -22,7 +22,7 @@ from pydantic import Field from metagpt.actions.skill_action import ArgumentsParingAction, SkillAction from metagpt.actions.talk_action import TalkAction -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.learn.skill_loader import SkillsDeclaration from metagpt.logs import logger from metagpt.memory.brain_memory import BrainMemory @@ -48,7 +48,7 @@ class Assistant(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self.constraints = self.constraints.format(language=kwargs.get("language") or context.kwargs.language) + self.constraints = self.constraints.format(language=kwargs.get("language") or CONTEXT.kwargs.language) async def think(self) -> bool: """Everything will be done part by part.""" @@ -56,7 +56,7 @@ class Assistant(Role): if not last_talk: return False if not self.skills: - skill_path = Path(context.kwargs.SKILL_PATH) if context.kwargs.SKILL_PATH else None + skill_path = Path(CONTEXT.kwargs.SKILL_PATH) if CONTEXT.kwargs.SKILL_PATH else None self.skills = await SkillsDeclaration.load(skill_yaml_file_name=skill_path) prompt = "" diff --git a/tests/conftest.py b/tests/conftest.py index fab1fa198..faa2d92e9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,7 +16,7 @@ import uuid import pytest from metagpt.const import DEFAULT_WORKSPACE_ROOT, TEST_DATA_PATH -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.llm import LLM from metagpt.logs import logger from metagpt.utils.git_repository import GitRepository @@ -141,12 +141,12 @@ def loguru_caplog(caplog): # init & dispose git repo @pytest.fixture(scope="function", autouse=True) def setup_and_teardown_git_repo(request): - context.git_repo = GitRepository(local_path=DEFAULT_WORKSPACE_ROOT / f"unittest/{uuid.uuid4().hex}") - context.config.git_reinit = True + CONTEXT.git_repo = GitRepository(local_path=DEFAULT_WORKSPACE_ROOT / f"unittest/{uuid.uuid4().hex}") + CONTEXT.config.git_reinit = True # Destroy git repo at the end of the test session. def fin(): - context.git_repo.delete_repository() + CONTEXT.git_repo.delete_repository() # Register the function for destroying the environment. request.addfinalizer(fin) diff --git a/tests/metagpt/actions/test_debug_error.py b/tests/metagpt/actions/test_debug_error.py index ff9e9cd81..922aa8613 100644 --- a/tests/metagpt/actions/test_debug_error.py +++ b/tests/metagpt/actions/test_debug_error.py @@ -12,7 +12,7 @@ import pytest from metagpt.actions.debug_error import DebugError from metagpt.const import TEST_CODES_FILE_REPO, TEST_OUTPUTS_FILE_REPO -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.schema import RunCodeContext, RunCodeResult CODE_CONTENT = ''' @@ -117,7 +117,7 @@ if __name__ == '__main__': @pytest.mark.asyncio async def test_debug_error(): - context.src_workspace = context.git_repo.workdir / uuid.uuid4().hex + CONTEXT.src_workspace = CONTEXT.git_repo.workdir / uuid.uuid4().hex ctx = RunCodeContext( code_filename="player.py", test_filename="test_player.py", @@ -125,8 +125,8 @@ async def test_debug_error(): output_filename="output.log", ) - repo = context.file_repo - await repo.save_file(filename=ctx.code_filename, content=CODE_CONTENT, relative_path=context.src_workspace) + repo = CONTEXT.file_repo + await repo.save_file(filename=ctx.code_filename, content=CODE_CONTENT, relative_path=CONTEXT.src_workspace) await repo.save_file(filename=ctx.test_filename, content=TEST_CONTENT, relative_path=TEST_CODES_FILE_REPO) output_data = RunCodeResult( stdout=";", diff --git a/tests/metagpt/actions/test_design_api.py b/tests/metagpt/actions/test_design_api.py index 88cb612fc..027f7ca20 100644 --- a/tests/metagpt/actions/test_design_api.py +++ b/tests/metagpt/actions/test_design_api.py @@ -10,7 +10,7 @@ import pytest from metagpt.actions.design_api import WriteDesign from metagpt.const import PRDS_FILE_REPO -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.logs import logger from metagpt.schema import Message @@ -18,7 +18,7 @@ from metagpt.schema import Message @pytest.mark.asyncio async def test_design_api(): inputs = ["我们需要一个音乐播放器,它应该有播放、暂停、上一曲、下一曲等功能。"] # PRD_SAMPLE - repo = context.file_repo + repo = CONTEXT.file_repo for prd in inputs: await repo.save_file("new_prd.txt", content=prd, relative_path=PRDS_FILE_REPO) diff --git a/tests/metagpt/actions/test_prepare_documents.py b/tests/metagpt/actions/test_prepare_documents.py index a67f89874..fde971f3c 100644 --- a/tests/metagpt/actions/test_prepare_documents.py +++ b/tests/metagpt/actions/test_prepare_documents.py @@ -10,7 +10,7 @@ import pytest from metagpt.actions.prepare_documents import PrepareDocuments from metagpt.const import DOCS_FILE_REPO, REQUIREMENT_FILENAME -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.schema import Message @@ -18,12 +18,12 @@ from metagpt.schema import Message async def test_prepare_documents(): msg = Message(content="New user requirements balabala...") - if context.git_repo: - context.git_repo.delete_repository() - context.git_repo = None + if CONTEXT.git_repo: + CONTEXT.git_repo.delete_repository() + CONTEXT.git_repo = None - await PrepareDocuments(g_context=context).run(with_messages=[msg]) - assert context.git_repo - doc = await context.file_repo.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO) + await PrepareDocuments(g_context=CONTEXT).run(with_messages=[msg]) + assert CONTEXT.git_repo + doc = await CONTEXT.file_repo.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO) assert doc assert doc.content == msg.content diff --git a/tests/metagpt/actions/test_project_management.py b/tests/metagpt/actions/test_project_management.py index a462319b8..1eadb49fb 100644 --- a/tests/metagpt/actions/test_project_management.py +++ b/tests/metagpt/actions/test_project_management.py @@ -10,7 +10,7 @@ import pytest from metagpt.actions.project_management import WriteTasks from metagpt.const import PRDS_FILE_REPO, SYSTEM_DESIGN_FILE_REPO -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.logs import logger from metagpt.schema import Message from tests.metagpt.actions.mock_json import DESIGN, PRD @@ -18,9 +18,9 @@ from tests.metagpt.actions.mock_json import DESIGN, PRD @pytest.mark.asyncio async def test_design_api(): - await context.file_repo.save_file("1.txt", content=str(PRD), relative_path=PRDS_FILE_REPO) - await context.file_repo.save_file("1.txt", content=str(DESIGN), relative_path=SYSTEM_DESIGN_FILE_REPO) - logger.info(context.git_repo) + await CONTEXT.file_repo.save_file("1.txt", content=str(PRD), relative_path=PRDS_FILE_REPO) + await CONTEXT.file_repo.save_file("1.txt", content=str(DESIGN), relative_path=SYSTEM_DESIGN_FILE_REPO) + logger.info(CONTEXT.git_repo) action = WriteTasks() diff --git a/tests/metagpt/actions/test_summarize_code.py b/tests/metagpt/actions/test_summarize_code.py index 1c14d256d..2f7b5c61d 100644 --- a/tests/metagpt/actions/test_summarize_code.py +++ b/tests/metagpt/actions/test_summarize_code.py @@ -11,7 +11,7 @@ import pytest from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.logs import logger from metagpt.schema import CodeSummarizeContext @@ -178,15 +178,15 @@ class Snake: @pytest.mark.asyncio async def test_summarize_code(): - context.src_workspace = context.git_repo.workdir / "src" - await context.file_repo.save_file(filename="1.json", relative_path=SYSTEM_DESIGN_FILE_REPO, content=DESIGN_CONTENT) - await context.file_repo.save_file(filename="1.json", relative_path=TASK_FILE_REPO, content=TASK_CONTENT) - await context.file_repo.save_file(filename="food.py", relative_path=CONFIG.src_workspace, content=FOOD_PY) - await context.file_repo.save_file(filename="game.py", relative_path=CONFIG.src_workspace, content=GAME_PY) - await context.file_repo.save_file(filename="main.py", relative_path=CONFIG.src_workspace, content=MAIN_PY) - await context.file_repo.save_file(filename="snake.py", relative_path=CONFIG.src_workspace, content=SNAKE_PY) + CONTEXT.src_workspace = CONTEXT.git_repo.workdir / "src" + await CONTEXT.file_repo.save_file(filename="1.json", relative_path=SYSTEM_DESIGN_FILE_REPO, content=DESIGN_CONTENT) + await CONTEXT.file_repo.save_file(filename="1.json", relative_path=TASK_FILE_REPO, content=TASK_CONTENT) + await CONTEXT.file_repo.save_file(filename="food.py", relative_path=CONFIG.src_workspace, content=FOOD_PY) + await CONTEXT.file_repo.save_file(filename="game.py", relative_path=CONFIG.src_workspace, content=GAME_PY) + await CONTEXT.file_repo.save_file(filename="main.py", relative_path=CONFIG.src_workspace, content=MAIN_PY) + await CONTEXT.file_repo.save_file(filename="snake.py", relative_path=CONFIG.src_workspace, content=SNAKE_PY) - src_file_repo = context.git_repo.new_file_repository(relative_path=CONFIG.src_workspace) + src_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=CONFIG.src_workspace) all_files = src_file_repo.all_files ctx = CodeSummarizeContext(design_filename="1.json", task_filename="1.json", codes_filenames=all_files) action = SummarizeCode(context=ctx) diff --git a/tests/metagpt/actions/test_write_code.py b/tests/metagpt/actions/test_write_code.py index 2a7b8e696..cfc5863f4 100644 --- a/tests/metagpt/actions/test_write_code.py +++ b/tests/metagpt/actions/test_write_code.py @@ -18,7 +18,7 @@ from metagpt.const import ( TASK_FILE_REPO, TEST_OUTPUTS_FILE_REPO, ) -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.logs import logger from metagpt.provider.openai_api import OpenAILLM as LLM from metagpt.schema import CodingContext, Document @@ -53,35 +53,35 @@ async def test_write_code_directly(): @pytest.mark.asyncio async def test_write_code_deps(): # Prerequisites - context.src_workspace = context.git_repo.workdir / "snake1/snake1" + CONTEXT.src_workspace = CONTEXT.git_repo.workdir / "snake1/snake1" demo_path = Path(__file__).parent / "../../data/demo_project" - await context.file_repo.save_file( + await CONTEXT.file_repo.save_file( filename="test_game.py.json", content=await aread(str(demo_path / "test_game.py.json")), relative_path=TEST_OUTPUTS_FILE_REPO, ) - await context.file_repo.save_file( + await CONTEXT.file_repo.save_file( filename="20231221155954.json", content=await aread(str(demo_path / "code_summaries.json")), relative_path=CODE_SUMMARIES_FILE_REPO, ) - await context.file_repo.save_file( + await CONTEXT.file_repo.save_file( filename="20231221155954.json", content=await aread(str(demo_path / "system_design.json")), relative_path=SYSTEM_DESIGN_FILE_REPO, ) - await context.file_repo.save_file( + await CONTEXT.file_repo.save_file( filename="20231221155954.json", content=await aread(str(demo_path / "tasks.json")), relative_path=TASK_FILE_REPO ) - await context.file_repo.save_file( - filename="main.py", content='if __name__ == "__main__":\nmain()', relative_path=context.src_workspace + await CONTEXT.file_repo.save_file( + filename="main.py", content='if __name__ == "__main__":\nmain()', relative_path=CONTEXT.src_workspace ) ccontext = CodingContext( filename="game.py", - design_doc=await context.file_repo.get_file( + design_doc=await CONTEXT.file_repo.get_file( filename="20231221155954.json", relative_path=SYSTEM_DESIGN_FILE_REPO ), - task_doc=await context.file_repo.get_file(filename="20231221155954.json", relative_path=TASK_FILE_REPO), + task_doc=await CONTEXT.file_repo.get_file(filename="20231221155954.json", relative_path=TASK_FILE_REPO), code_doc=Document(filename="game.py", content="", root_path="snake1"), ) coding_doc = Document(root_path="snake1", filename="game.py", content=ccontext.json()) diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index 1f92c079b..faa5b77a4 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -10,7 +10,7 @@ import pytest from metagpt.actions import UserRequirement, WritePRD from metagpt.const import DOCS_FILE_REPO, PRDS_FILE_REPO, REQUIREMENT_FILENAME -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.logs import logger from metagpt.roles.product_manager import ProductManager from metagpt.roles.role import RoleReactMode @@ -33,7 +33,7 @@ async def test_write_prd(new_filename): # Assert the prd is not None or empty assert prd is not None assert prd.content != "" - assert context.git_repo.new_file_repository(relative_path=PRDS_FILE_REPO).changed_files + assert CONTEXT.git_repo.new_file_repository(relative_path=PRDS_FILE_REPO).changed_files if __name__ == "__main__": diff --git a/tests/metagpt/roles/test_architect.py b/tests/metagpt/roles/test_architect.py index 69afbcfe1..f9d6606ac 100644 --- a/tests/metagpt/roles/test_architect.py +++ b/tests/metagpt/roles/test_architect.py @@ -13,7 +13,7 @@ import pytest from metagpt.actions import WriteDesign, WritePRD from metagpt.const import PRDS_FILE_REPO -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.logs import logger from metagpt.roles import Architect from metagpt.schema import Message @@ -25,7 +25,7 @@ from tests.metagpt.roles.mock import MockMessages async def test_architect(): # Prerequisites filename = uuid.uuid4().hex + ".json" - await awrite(context.git_repo.workdir / PRDS_FILE_REPO / filename, data=MockMessages.prd.content) + await awrite(CONTEXT.git_repo.workdir / PRDS_FILE_REPO / filename, data=MockMessages.prd.content) role = Architect() rsp = await role.run(with_message=Message(content="", cause_by=WritePRD)) diff --git a/tests/metagpt/roles/test_assistant.py b/tests/metagpt/roles/test_assistant.py index 8797ba7f1..4ef44d77a 100644 --- a/tests/metagpt/roles/test_assistant.py +++ b/tests/metagpt/roles/test_assistant.py @@ -12,7 +12,7 @@ from pydantic import BaseModel from metagpt.actions.skill_action import SkillAction from metagpt.actions.talk_action import TalkAction -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.memory.brain_memory import BrainMemory from metagpt.roles.assistant import Assistant from metagpt.schema import Message @@ -21,7 +21,7 @@ from metagpt.utils.common import any_to_str @pytest.mark.asyncio async def test_run(): - context.kwargs.language = "Chinese" + CONTEXT.kwargs.language = "Chinese" class Input(BaseModel): memory: BrainMemory @@ -65,7 +65,7 @@ async def test_run(): "cause_by": any_to_str(SkillAction), }, ] - context.kwargs.agent_skills = [ + CONTEXT.kwargs.agent_skills = [ {"id": 1, "name": "text_to_speech", "type": "builtin", "config": {}, "enabled": True}, {"id": 2, "name": "text_to_image", "type": "builtin", "config": {}, "enabled": True}, {"id": 3, "name": "ai_call", "type": "builtin", "config": {}, "enabled": True}, @@ -77,8 +77,8 @@ async def test_run(): for i in inputs: seed = Input(**i) - context.kwargs.language = seed.language - context.kwargs.agent_description = seed.agent_description + CONTEXT.kwargs.language = seed.language + CONTEXT.kwargs.agent_description = seed.agent_description role = Assistant(language="Chinese") role.memory = seed.memory # Restore historical conversation content. while True: diff --git a/tests/metagpt/roles/test_engineer.py b/tests/metagpt/roles/test_engineer.py index b35321a1b..710e74b8f 100644 --- a/tests/metagpt/roles/test_engineer.py +++ b/tests/metagpt/roles/test_engineer.py @@ -19,7 +19,7 @@ from metagpt.const import ( SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO, ) -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.logs import logger from metagpt.roles.engineer import Engineer from metagpt.schema import CodingContext, Message @@ -32,19 +32,19 @@ from tests.metagpt.roles.mock import STRS_FOR_PARSING, TASKS, MockMessages async def test_engineer(): # Prerequisites rqno = "20231221155954.json" - await context.file_repo.save_file(REQUIREMENT_FILENAME, content=MockMessages.req.content) - await context.file_repo.save_file(rqno, relative_path=PRDS_FILE_REPO, content=MockMessages.prd.content) - await context.file_repo.save_file( + await CONTEXT.file_repo.save_file(REQUIREMENT_FILENAME, content=MockMessages.req.content) + await CONTEXT.file_repo.save_file(rqno, relative_path=PRDS_FILE_REPO, content=MockMessages.prd.content) + await CONTEXT.file_repo.save_file( rqno, relative_path=SYSTEM_DESIGN_FILE_REPO, content=MockMessages.system_design.content ) - await context.file_repo.save_file(rqno, relative_path=TASK_FILE_REPO, content=MockMessages.json_tasks.content) + await CONTEXT.file_repo.save_file(rqno, relative_path=TASK_FILE_REPO, content=MockMessages.json_tasks.content) engineer = Engineer() rsp = await engineer.run(Message(content="", cause_by=WriteTasks)) logger.info(rsp) assert rsp.cause_by == any_to_str(WriteCode) - src_file_repo = context.git_repo.new_file_repository(context.src_workspace) + src_file_repo = CONTEXT.git_repo.new_file_repository(CONTEXT.src_workspace) assert src_file_repo.changed_files @@ -116,19 +116,19 @@ async def test_new_coding_context(): # Prerequisites demo_path = Path(__file__).parent / "../../data/demo_project" deps = json.loads(await aread(demo_path / "dependencies.json")) - dependency = await context.git_repo.get_dependency() + dependency = await CONTEXT.git_repo.get_dependency() for k, v in deps.items(): await dependency.update(k, set(v)) data = await aread(demo_path / "system_design.json") rqno = "20231221155954.json" - await awrite(context.git_repo.workdir / SYSTEM_DESIGN_FILE_REPO / rqno, data) + await awrite(CONTEXT.git_repo.workdir / SYSTEM_DESIGN_FILE_REPO / rqno, data) data = await aread(demo_path / "tasks.json") - await awrite(context.git_repo.workdir / TASK_FILE_REPO / rqno, data) + await awrite(CONTEXT.git_repo.workdir / TASK_FILE_REPO / rqno, data) - context.src_workspace = Path(context.git_repo.workdir) / "game_2048" - src_file_repo = context.git_repo.new_file_repository(relative_path=context.src_workspace) - task_file_repo = context.git_repo.new_file_repository(relative_path=TASK_FILE_REPO) - design_file_repo = context.git_repo.new_file_repository(relative_path=SYSTEM_DESIGN_FILE_REPO) + CONTEXT.src_workspace = Path(CONTEXT.git_repo.workdir) / "game_2048" + src_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=CONTEXT.src_workspace) + task_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=TASK_FILE_REPO) + design_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=SYSTEM_DESIGN_FILE_REPO) filename = "game.py" ctx_doc = await Engineer._new_coding_doc( @@ -149,8 +149,8 @@ async def test_new_coding_context(): assert ctx.task_doc.content assert ctx.code_doc - context.git_repo.add_change({f"{TASK_FILE_REPO}/{rqno}": ChangeType.UNTRACTED}) - context.git_repo.commit("mock env") + CONTEXT.git_repo.add_change({f"{TASK_FILE_REPO}/{rqno}": ChangeType.UNTRACTED}) + CONTEXT.git_repo.commit("mock env") await src_file_repo.save(filename=filename, content="content") role = Engineer() assert not role.code_todos diff --git a/tests/metagpt/roles/test_qa_engineer.py b/tests/metagpt/roles/test_qa_engineer.py index 825fe58a3..c51642e6a 100644 --- a/tests/metagpt/roles/test_qa_engineer.py +++ b/tests/metagpt/roles/test_qa_engineer.py @@ -13,7 +13,7 @@ from pydantic import Field from metagpt.actions import DebugError, RunCode, WriteTest from metagpt.actions.summarize_code import SummarizeCode -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.environment import Environment from metagpt.roles import QaEngineer from metagpt.schema import Message @@ -23,10 +23,10 @@ from metagpt.utils.common import any_to_str, aread, awrite async def test_qa(): # Prerequisites demo_path = Path(__file__).parent / "../../data/demo_project" - context.src_workspace = Path(context.git_repo.workdir) / "qa/game_2048" + CONTEXT.src_workspace = Path(CONTEXT.git_repo.workdir) / "qa/game_2048" data = await aread(filename=demo_path / "game.py", encoding="utf-8") - await awrite(filename=context.src_workspace / "game.py", data=data, encoding="utf-8") - await awrite(filename=Path(context.git_repo.workdir) / "requirements.txt", data="") + await awrite(filename=CONTEXT.src_workspace / "game.py", data=data, encoding="utf-8") + await awrite(filename=Path(CONTEXT.git_repo.workdir) / "requirements.txt", data="") class MockEnv(Environment): msgs: List[Message] = Field(default_factory=list) diff --git a/tests/metagpt/roles/test_teacher.py b/tests/metagpt/roles/test_teacher.py index ff2139929..8bd37f482 100644 --- a/tests/metagpt/roles/test_teacher.py +++ b/tests/metagpt/roles/test_teacher.py @@ -10,7 +10,7 @@ from typing import Dict, Optional import pytest from pydantic import BaseModel -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.roles.teacher import Teacher from metagpt.schema import Message @@ -97,8 +97,8 @@ async def test_new_file_name(): @pytest.mark.asyncio async def test_run(): - context.kwargs.language = "Chinese" - context.kwargs.teaching_language = "English" + CONTEXT.kwargs.language = "Chinese" + CONTEXT.kwargs.teaching_language = "English" lesson = """ UNIT 1 Making New Friends TOPIC 1 Welcome to China! diff --git a/tests/metagpt/test_context.py b/tests/metagpt/test_context.py index 2d52325bc..f1c9da4e7 100644 --- a/tests/metagpt/test_context.py +++ b/tests/metagpt/test_context.py @@ -6,7 +6,7 @@ @File : test_context.py """ from metagpt.configs.llm_config import LLMType -from metagpt.context import AttrDict, Context, context +from metagpt.context import CONTEXT, AttrDict, Context def test_attr_dict_1(): @@ -52,11 +52,11 @@ def test_context_1(): def test_context_2(): - llm = context.config.get_openai_llm() + llm = CONTEXT.config.get_openai_llm() assert llm is not None assert llm.api_type == LLMType.OPENAI - kwargs = context.kwargs + kwargs = CONTEXT.kwargs assert kwargs is not None kwargs.test_key = "test_value" diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index d7d8d990a..49fd8a5fc 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -13,7 +13,7 @@ from pathlib import Path import pytest from metagpt.actions import UserRequirement -from metagpt.context import context +from metagpt.context import CONTEXT from metagpt.environment import Environment from metagpt.logs import logger from metagpt.roles import Architect, ProductManager, Role @@ -46,9 +46,9 @@ def test_get_roles(env: Environment): @pytest.mark.asyncio async def test_publish_and_process_message(env: Environment): - if context.git_repo: - context.git_repo.delete_repository() - context.git_repo = None + if CONTEXT.git_repo: + CONTEXT.git_repo.delete_repository() + CONTEXT.git_repo = None product_manager = ProductManager(name="Alice", profile="Product Manager", goal="做AI Native产品", constraints="资源有限") architect = Architect( From 5cd5eebc5b6ec2e874f0345e4983c08c817368f9 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 15:34:49 +0800 Subject: [PATCH 28/50] refine code --- metagpt/actions/action.py | 3 - metagpt/actions/invoice_ocr.py | 1 - metagpt/actions/research.py | 1 - metagpt/context.py | 89 ++++++++++++++++---------- metagpt/roles/engineer.py | 2 +- metagpt/roles/role.py | 3 - metagpt/roles/sk_agent.py | 3 - metagpt/tools/moderation.py | 6 +- metagpt/tools/openai_text_to_image.py | 3 - tests/metagpt/test_config.py | 3 + tests/metagpt/test_context.py | 6 +- tests/metagpt/tools/test_moderation.py | 3 +- 12 files changed, 67 insertions(+), 56 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index cabab784f..cad8112d2 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -14,8 +14,6 @@ from pydantic import BaseModel, ConfigDict, Field, model_validator from metagpt.actions.action_node import ActionNode from metagpt.context import ContextMixin -from metagpt.llm import LLM -from metagpt.provider.base_llm import BaseLLM from metagpt.schema import ( CodeSummarizeContext, CodingContext, @@ -30,7 +28,6 @@ class Action(SerializationMixin, ContextMixin, BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True, exclude=["llm"]) name: str = "" - llm: BaseLLM = Field(default_factory=LLM, exclude=True) i_context: Union[dict, CodingContext, CodeSummarizeContext, TestingContext, RunCodeContext, str, None] = "" prefix: str = "" # aask*时会加上prefix,作为system_message desc: str = "" # for skill manager diff --git a/metagpt/actions/invoice_ocr.py b/metagpt/actions/invoice_ocr.py index a3406ff65..60939d2eb 100644 --- a/metagpt/actions/invoice_ocr.py +++ b/metagpt/actions/invoice_ocr.py @@ -133,7 +133,6 @@ class GenerateTable(Action): name: str = "GenerateTable" i_context: Optional[str] = None - llm: BaseLLM = Field(default_factory=LLM) language: str = "ch" async def run(self, ocr_results: list, filename: str, *args, **kwargs) -> dict[str, str]: diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index 84067ad92..ce366e3d2 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -178,7 +178,6 @@ class WebBrowseAndSummarize(Action): name: str = "WebBrowseAndSummarize" i_context: Optional[str] = None - llm: BaseLLM = Field(default_factory=LLM) desc: str = "Explore the web and provide summaries of articles and webpages." browse_func: Union[Callable[[list[str]], None], None] = None web_browser_engine: Optional[WebBrowserEngine] = None diff --git a/metagpt/context.py b/metagpt/context.py index 4083a1696..bd86fb039 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -42,28 +42,6 @@ class AttrDict(BaseModel): raise AttributeError(f"No such attribute: {key}") -class LLMInstance: - """Mixin class for LLM""" - - # _config: Optional[Config] = None - _llm_config: Optional[LLMConfig] = None - _llm_instance: Optional[BaseLLM] = None - - def __init__(self, config: Config, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI): - """Use a LLM provider""" - # 更新LLM配置 - self._llm_config = config.get_llm_config(name, provider) - # 重置LLM实例 - self._llm_instance = None - - @property - def instance(self) -> BaseLLM: - """Return the LLM instance""" - if not self._llm_instance and self._llm_config: - self._llm_instance = create_llm_instance(self._llm_config) - return self._llm_instance - - class Context(BaseModel): """Env context for MetaGPT""" @@ -74,7 +52,8 @@ class Context(BaseModel): git_repo: Optional[GitRepository] = None src_workspace: Optional[Path] = None cost_manager: CostManager = CostManager() - _llm: Optional[LLMInstance] = None + + _llm: Optional[BaseLLM] = None @property def file_repo(self): @@ -92,12 +71,19 @@ class Context(BaseModel): env.update({k: v for k, v in i.items() if isinstance(v, str)}) return env + # def use_llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: + # """Use a LLM instance""" + # self._llm_config = self.config.get_llm_config(name, provider) + # self._llm = None + # return self._llm + def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: - """Return a LLM instance""" - llm = LLMInstance(self.config, name, provider).instance - if llm.cost_manager is None: - llm.cost_manager = self.cost_manager - return llm + """Return a LLM instance, fixme: support multiple llm instances""" + if self._llm is None: + self._llm = create_llm_instance(self.config.get_llm_config(name, provider)) + if self._llm.cost_manager is None: + self._llm.cost_manager = self.cost_manager + return self._llm class ContextMixin(BaseModel): @@ -108,11 +94,22 @@ class ContextMixin(BaseModel): # Env/Role/Action will use this config as private config, or use self.context.config as public config _config: Optional[Config] = None - def __init__(self, context: Optional[Context] = None, config: Optional[Config] = None, **kwargs): + # Env/Role/Action will use this llm as private llm, or use self.context._llm instance + _llm_config: Optional[LLMConfig] = None + _llm: Optional[BaseLLM] = None + + def __init__( + self, + context: Optional[Context] = None, + config: Optional[Config] = None, + llm: Optional[BaseLLM] = None, + **kwargs, + ): """Initialize with config""" super().__init__(**kwargs) self.set_context(context) self.set_config(config) + self.set_llm(llm) def set(self, k, v, override=False): """Set attribute""" @@ -127,30 +124,56 @@ class ContextMixin(BaseModel): """Set config""" self.set("_config", config, override) + def set_llm_config(self, llm_config: LLMConfig, override=False): + """Set llm config""" + self.set("_llm_config", llm_config, override) + + def set_llm(self, llm: BaseLLM, override=False): + """Set llm""" + self.set("_llm", llm, override) + + def use_llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: + """Use a LLM instance""" + self._llm_config = self.config.get_llm_config(name, provider) + self._llm = None + return self.llm + @property - def config(self): + def config(self) -> Config: """Role config: role config > context config""" if self._config: return self._config return self.context.config @config.setter - def config(self, config: Config): + def config(self, config: Config) -> None: """Set config""" self.set_config(config) @property - def context(self): + def context(self) -> Context: """Role context: role context > context""" if self._context: return self._context return CONTEXT @context.setter - def context(self, context: Context): + def context(self, context: Context) -> None: """Set context""" self.set_context(context) + @property + def llm(self) -> BaseLLM: + """Role llm: role llm > context llm""" + if self._llm_config and not self._llm: + self._llm = self.context.llm(self._llm_config.name, self._llm_config.provider) + return self._llm or self.context.llm() + + @llm.setter + def llm(self, llm: BaseLLM) -> None: + """Set llm""" + self._llm = llm + # Global context, not in Env CONTEXT = Context() diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index dc9f31686..364566b37 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -109,7 +109,7 @@ class Engineer(Role): coding_context = await todo.run() # Code review if review: - action = WriteCodeReview(context=coding_context, g_context=self.context, llm=self.llm) + action = WriteCodeReview(context=coding_context, _context=self.context, llm=self.llm) self._init_action_system_message(action) coding_context = await action.run() await src_file_repo.save( diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 98cc05234..9c6832d8f 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -31,11 +31,9 @@ from metagpt.actions import Action, ActionOutput from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement from metagpt.context import ContextMixin -from metagpt.llm import LLM from metagpt.logs import logger from metagpt.memory import Memory from metagpt.provider import HumanProvider -from metagpt.provider.base_llm import BaseLLM from metagpt.schema import Message, MessageQueue, SerializationMixin from metagpt.utils.common import any_to_name, any_to_str, role_raise_decorator from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output @@ -131,7 +129,6 @@ class Role(SerializationMixin, ContextMixin, BaseModel): desc: str = "" is_human: bool = False - llm: BaseLLM = Field(default_factory=LLM, exclude=True) # Each role has its own LLM, use different system message role_id: str = "" states: list[str] = [] actions: list[SerializeAsAny[Action]] = Field(default=[], validate_default=True) diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index 468905fce..200ed5051 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -17,9 +17,7 @@ from semantic_kernel.planning.basic_planner import BasicPlanner, Plan from metagpt.actions import UserRequirement from metagpt.actions.execute_task import ExecuteTask -from metagpt.llm import LLM from metagpt.logs import logger -from metagpt.provider.base_llm import BaseLLM from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.make_sk_kernel import make_sk_kernel @@ -44,7 +42,6 @@ class SkAgent(Role): plan: Plan = Field(default=None, exclude=True) planner_cls: Any = None planner: Union[BasicPlanner, SequentialPlanner, ActionPlanner] = None - llm: BaseLLM = Field(default_factory=LLM) kernel: Kernel = Field(default_factory=Kernel) import_semantic_skill_from_directory: Callable = Field(default=None, exclude=True) import_skill: Callable = Field(default=None, exclude=True) diff --git a/metagpt/tools/moderation.py b/metagpt/tools/moderation.py index cda164ec5..f00b0e1f2 100644 --- a/metagpt/tools/moderation.py +++ b/metagpt/tools/moderation.py @@ -7,12 +7,12 @@ """ from typing import Union -from metagpt.llm import LLM +from metagpt.provider.base_llm import BaseLLM class Moderation: - def __init__(self): - self.llm = LLM() + def __init__(self, llm: BaseLLM): + self.llm = llm def handle_moderation_results(self, results): resp = [] diff --git a/metagpt/tools/openai_text_to_image.py b/metagpt/tools/openai_text_to_image.py index fc31b95f7..bf7c5e799 100644 --- a/metagpt/tools/openai_text_to_image.py +++ b/metagpt/tools/openai_text_to_image.py @@ -16,9 +16,6 @@ from metagpt.provider.base_llm import BaseLLM class OpenAIText2Image: def __init__(self, llm: BaseLLM): - """ - :param openai_api_key: OpenAI API key, For more details, checkout: `https://platform.openai.com/account/api-keys` - """ self.llm = llm async def text_2_image(self, text, size_type="1024x1024"): diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index c74b16930..cfde7a04c 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -79,3 +79,6 @@ def test_config_mixin_3(): assert obj.b == "b" assert obj.c == "c" assert obj.d == "d" + + print(obj.__dict__.keys()) + assert "_config" in obj.__dict__.keys() diff --git a/tests/metagpt/test_context.py b/tests/metagpt/test_context.py index f1c9da4e7..255794c41 100644 --- a/tests/metagpt/test_context.py +++ b/tests/metagpt/test_context.py @@ -66,7 +66,5 @@ def test_context_2(): def test_context_3(): ctx = Context() ctx.use_llm(provider=LLMType.OPENAI) - assert ctx.llm_config is not None - assert ctx.llm_config.api_type == LLMType.OPENAI - assert ctx.llm is not None - assert "gpt" in ctx.llm.model + assert ctx.llm() is not None + assert "gpt" in ctx.llm().model diff --git a/tests/metagpt/tools/test_moderation.py b/tests/metagpt/tools/test_moderation.py index 534fe812a..d265c3f78 100644 --- a/tests/metagpt/tools/test_moderation.py +++ b/tests/metagpt/tools/test_moderation.py @@ -9,6 +9,7 @@ import pytest from metagpt.config import CONFIG +from metagpt.context import CONTEXT from metagpt.tools.moderation import Moderation @@ -27,7 +28,7 @@ async def test_amoderation(content): assert not CONFIG.OPENAI_API_TYPE assert CONFIG.OPENAI_API_MODEL - moderation = Moderation() + moderation = Moderation(CONTEXT.llm()) results = await moderation.amoderation(content=content) assert isinstance(results, list) assert len(results) == len(content) From 2881c5e9ebf79aaf1b51dde4049964c1ae2097ce Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 16:02:05 +0800 Subject: [PATCH 29/50] refine code --- metagpt/actions/invoice_ocr.py | 6 ------ metagpt/actions/research.py | 6 ------ metagpt/context.py | 10 +++++----- tests/metagpt/test_context.py | 11 +++++++---- tests/metagpt/tools/test_moderation.py | 4 ++-- 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/metagpt/actions/invoice_ocr.py b/metagpt/actions/invoice_ocr.py index 60939d2eb..7cf71a8ff 100644 --- a/metagpt/actions/invoice_ocr.py +++ b/metagpt/actions/invoice_ocr.py @@ -16,17 +16,14 @@ from typing import Optional import pandas as pd from paddleocr import PaddleOCR -from pydantic import Field from metagpt.actions import Action from metagpt.const import INVOICE_OCR_TABLE_PATH -from metagpt.llm import LLM from metagpt.logs import logger from metagpt.prompts.invoice_ocr import ( EXTRACT_OCR_MAIN_INFO_PROMPT, REPLY_OCR_QUESTION_PROMPT, ) -from metagpt.provider.base_llm import BaseLLM from metagpt.utils.common import OutputParser from metagpt.utils.file import File @@ -175,9 +172,6 @@ class ReplyQuestion(Action): """ - name: str = "ReplyQuestion" - i_context: Optional[str] = None - llm: BaseLLM = Field(default_factory=LLM) language: str = "ch" async def run(self, query: str, ocr_result: list, *args, **kwargs) -> str: diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index ce366e3d2..d2db228ae 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -9,9 +9,7 @@ from pydantic import Field, parse_obj_as from metagpt.actions import Action from metagpt.config import CONFIG -from metagpt.llm import LLM from metagpt.logs import logger -from metagpt.provider.base_llm import BaseLLM from metagpt.tools.search_engine import SearchEngine from metagpt.tools.web_browser_engine import WebBrowserEngine, WebBrowserEngineType from metagpt.utils.common import OutputParser @@ -246,10 +244,6 @@ class WebBrowseAndSummarize(Action): class ConductResearch(Action): """Action class to conduct research and generate a research report.""" - name: str = "ConductResearch" - i_context: Optional[str] = None - llm: BaseLLM = Field(default_factory=LLM) - def __init__(self, **kwargs): super().__init__(**kwargs) if CONFIG.model_for_researcher_report: diff --git a/metagpt/context.py b/metagpt/context.py index bd86fb039..0686aedc3 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -78,11 +78,11 @@ class Context(BaseModel): # return self._llm def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: - """Return a LLM instance, fixme: support multiple llm instances""" - if self._llm is None: - self._llm = create_llm_instance(self.config.get_llm_config(name, provider)) - if self._llm.cost_manager is None: - self._llm.cost_manager = self.cost_manager + """Return a LLM instance, fixme: support cache""" + # if self._llm is None: + self._llm = create_llm_instance(self.config.get_llm_config(name, provider)) + if self._llm.cost_manager is None: + self._llm.cost_manager = self.cost_manager return self._llm diff --git a/tests/metagpt/test_context.py b/tests/metagpt/test_context.py index 255794c41..d662a906a 100644 --- a/tests/metagpt/test_context.py +++ b/tests/metagpt/test_context.py @@ -64,7 +64,10 @@ def test_context_2(): def test_context_3(): - ctx = Context() - ctx.use_llm(provider=LLMType.OPENAI) - assert ctx.llm() is not None - assert "gpt" in ctx.llm().model + # ctx = Context() + # ctx.use_llm(provider=LLMType.OPENAI) + # assert ctx._llm_config is not None + # assert ctx._llm_config.api_type == LLMType.OPENAI + # assert ctx.llm() is not None + # assert "gpt" in ctx.llm().model + pass diff --git a/tests/metagpt/tools/test_moderation.py b/tests/metagpt/tools/test_moderation.py index d265c3f78..e1226484a 100644 --- a/tests/metagpt/tools/test_moderation.py +++ b/tests/metagpt/tools/test_moderation.py @@ -9,7 +9,7 @@ import pytest from metagpt.config import CONFIG -from metagpt.context import CONTEXT +from metagpt.llm import LLM from metagpt.tools.moderation import Moderation @@ -28,7 +28,7 @@ async def test_amoderation(content): assert not CONFIG.OPENAI_API_TYPE assert CONFIG.OPENAI_API_MODEL - moderation = Moderation(CONTEXT.llm()) + moderation = Moderation(LLM()) results = await moderation.amoderation(content=content) assert isinstance(results, list) assert len(results) == len(content) From d9caaea75361b5fcdaedd4f8af45322a3470ace8 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 16:17:48 +0800 Subject: [PATCH 30/50] refine code --- metagpt/context.py | 1 + metagpt/roles/engineer.py | 8 ++++---- metagpt/roles/qa_engineer.py | 6 +++--- metagpt/roles/teacher.py | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/metagpt/context.py b/metagpt/context.py index 0686aedc3..4badafcc4 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -165,6 +165,7 @@ class ContextMixin(BaseModel): @property def llm(self) -> BaseLLM: """Role llm: role llm > context llm""" + # logger.info(f"class:{self.__class__.__name__}, llm: {self._llm}, llm_config: {self._llm_config}") if self._llm_config and not self._llm: self._llm = self.context.llm(self._llm_config.name, self._llm_config.provider) return self._llm or self.context.llm() diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 364566b37..0d277813e 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -109,7 +109,7 @@ class Engineer(Role): coding_context = await todo.run() # Code review if review: - action = WriteCodeReview(context=coding_context, _context=self.context, llm=self.llm) + action = WriteCodeReview(i_context=coding_context, context=self.context, llm=self.llm) self._init_action_system_message(action) coding_context = await action.run() await src_file_repo.save( @@ -282,7 +282,7 @@ class Engineer(Role): ) changed_files.docs[task_filename] = coding_doc self.code_todos = [ - WriteCode(context=i, g_context=self.context, llm=self.llm) for i in changed_files.docs.values() + WriteCode(i_context=i, context=self.context, llm=self.llm) for i in changed_files.docs.values() ] # Code directly modified by the user. dependency = await self.git_repo.get_dependency() @@ -297,7 +297,7 @@ class Engineer(Role): dependency=dependency, ) changed_files.docs[filename] = coding_doc - self.code_todos.append(WriteCode(context=coding_doc, g_context=self.context, llm=self.llm)) + self.code_todos.append(WriteCode(i_context=coding_doc, context=self.context, llm=self.llm)) if self.code_todos: self.set_todo(self.code_todos[0]) @@ -313,7 +313,7 @@ class Engineer(Role): summarizations[ctx].append(filename) for ctx, filenames in summarizations.items(): ctx.codes_filenames = filenames - self.summarize_todos.append(SummarizeCode(context=ctx, llm=self.llm)) + self.summarize_todos.append(SummarizeCode(i_context=ctx, llm=self.llm)) if self.summarize_todos: self.set_todo(self.summarize_todos[0]) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 80b0fd39a..9483ea260 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -71,7 +71,7 @@ class QaEngineer(Role): ) logger.info(f"Writing {test_doc.filename}..") context = TestingContext(filename=test_doc.filename, test_doc=test_doc, code_doc=code_doc) - context = await WriteTest(context=context, g_context=self.context, llm=self.llm).run() + context = await WriteTest(i_context=context, context=self.context, llm=self.llm).run() await tests_file_repo.save( filename=context.test_doc.filename, content=context.test_doc.content, @@ -112,7 +112,7 @@ class QaEngineer(Role): return run_code_context.code = src_doc.content run_code_context.test_code = test_doc.content - result = await RunCode(context=run_code_context, g_context=self.context, llm=self.llm).run() + result = await RunCode(i_context=run_code_context, context=self.context, llm=self.llm).run() run_code_context.output_filename = run_code_context.test_filename + ".json" await self.context.git_repo.new_file_repository(TEST_OUTPUTS_FILE_REPO).save( filename=run_code_context.output_filename, @@ -136,7 +136,7 @@ class QaEngineer(Role): async def _debug_error(self, msg): run_code_context = RunCodeContext.loads(msg.content) - code = await DebugError(context=run_code_context, g_context=self.context, llm=self.llm).run() + code = await DebugError(i_context=run_code_context, context=self.context, llm=self.llm).run() await self.context.file_repo.save_file( filename=run_code_context.test_filename, content=code, relative_path=TEST_CODES_FILE_REPO ) diff --git a/metagpt/roles/teacher.py b/metagpt/roles/teacher.py index b4ffd01d3..9206d5f80 100644 --- a/metagpt/roles/teacher.py +++ b/metagpt/roles/teacher.py @@ -45,7 +45,7 @@ class Teacher(Role): actions = [] print(TeachingPlanBlock.TOPICS) for topic in TeachingPlanBlock.TOPICS: - act = WriteTeachingPlanPart(context=self.rc.news[0].content, topic=topic, llm=self.llm) + act = WriteTeachingPlanPart(i_context=self.rc.news[0].content, topic=topic, llm=self.llm) actions.append(act) self.add_actions(actions) From 4df716bc6c8fee82636105fd0555ea10ed6d7f24 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 16:18:55 +0800 Subject: [PATCH 31/50] refine code --- metagpt/actions/write_code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 779fe52a6..1aa76b67e 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -95,7 +95,7 @@ class WriteCode(Action): async def run(self, *args, **kwargs) -> CodingContext: bug_feedback = await self.file_repo.get_file(filename=BUGFIX_FILENAME, relative_path=DOCS_FILE_REPO) - coding_context = CodingContext.loads(self.context.content) + coding_context = CodingContext.loads(self.i_context.content) test_doc = await self.file_repo.get_file( filename="test_" + coding_context.filename + ".json", relative_path=TEST_OUTPUTS_FILE_REPO ) From 2b3a4e06cce987e94d9687c9f22ae650de1a1489 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 16:24:21 +0800 Subject: [PATCH 32/50] refine code --- metagpt/actions/debug_error.py | 8 ++++---- metagpt/actions/run_code.py | 24 ++++++++++++------------ metagpt/actions/summarize_code.py | 6 +++--- metagpt/actions/write_code.py | 6 +++--- metagpt/actions/write_code_review.py | 28 ++++++++++++++-------------- metagpt/actions/write_test.py | 16 ++++++++-------- 6 files changed, 44 insertions(+), 44 deletions(-) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index 3647640c0..bb57e1927 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -51,7 +51,7 @@ class DebugError(Action): async def run(self, *args, **kwargs) -> str: output_doc = await self.file_repo.get_file( - filename=self.context.output_filename, relative_path=TEST_OUTPUTS_FILE_REPO + filename=self.i_context.output_filename, relative_path=TEST_OUTPUTS_FILE_REPO ) if not output_doc: return "" @@ -61,14 +61,14 @@ class DebugError(Action): if matches: return "" - logger.info(f"Debug and rewrite {self.context.test_filename}") + logger.info(f"Debug and rewrite {self.i_context.test_filename}") code_doc = await self.file_repo.get_file( - filename=self.context.code_filename, relative_path=self.context.src_workspace + filename=self.i_context.code_filename, relative_path=self.i_context.src_workspace ) if not code_doc: return "" test_doc = await self.file_repo.get_file( - filename=self.context.test_filename, relative_path=TEST_CODES_FILE_REPO + filename=self.i_context.test_filename, relative_path=TEST_CODES_FILE_REPO ) if not test_doc: return "" diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index 8fdda0a0d..072ee8f22 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -117,25 +117,25 @@ class RunCode(Action): return stdout.decode("utf-8"), stderr.decode("utf-8") async def run(self, *args, **kwargs) -> RunCodeResult: - logger.info(f"Running {' '.join(self.context.command)}") - if self.context.mode == "script": + logger.info(f"Running {' '.join(self.i_context.command)}") + if self.i_context.mode == "script": outs, errs = await self.run_script( - command=self.context.command, - working_directory=self.context.working_directory, - additional_python_paths=self.context.additional_python_paths, + command=self.i_context.command, + working_directory=self.i_context.working_directory, + additional_python_paths=self.i_context.additional_python_paths, ) - elif self.context.mode == "text": - outs, errs = await self.run_text(code=self.context.code) + elif self.i_context.mode == "text": + outs, errs = await self.run_text(code=self.i_context.code) logger.info(f"{outs=}") logger.info(f"{errs=}") context = CONTEXT.format( - code=self.context.code, - code_file_name=self.context.code_filename, - test_code=self.context.test_code, - test_file_name=self.context.test_filename, - command=" ".join(self.context.command), + code=self.i_context.code, + code_file_name=self.i_context.code_filename, + test_code=self.i_context.test_code, + test_file_name=self.i_context.test_filename, + command=" ".join(self.i_context.command), outs=outs[:500], # outs might be long but they are not important, truncate them to avoid token overflow errs=errs[:10000], # truncate errors to avoid token overflow ) diff --git a/metagpt/actions/summarize_code.py b/metagpt/actions/summarize_code.py index 690d5c77b..dde41d3c6 100644 --- a/metagpt/actions/summarize_code.py +++ b/metagpt/actions/summarize_code.py @@ -98,14 +98,14 @@ class SummarizeCode(Action): return code_rsp async def run(self): - design_pathname = Path(self.context.design_filename) + design_pathname = Path(self.i_context.design_filename) repo = self.file_repo design_doc = await repo.get_file(filename=design_pathname.name, relative_path=SYSTEM_DESIGN_FILE_REPO) - task_pathname = Path(self.context.task_filename) + task_pathname = Path(self.i_context.task_filename) task_doc = await repo.get_file(filename=task_pathname.name, relative_path=TASK_FILE_REPO) src_file_repo = self.git_repo.new_file_repository(relative_path=self.context.src_workspace) code_blocks = [] - for filename in self.context.codes_filenames: + for filename in self.i_context.codes_filenames: code_doc = await src_file_repo.get(filename) code_block = f"```python\n{code_doc.content}\n```\n-----" code_blocks.append(code_block) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 1aa76b67e..62de34ef4 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -114,7 +114,7 @@ class WriteCode(Action): else: code_context = await self.get_codes( coding_context.task_doc, - exclude=self.context.filename, + exclude=self.i_context.filename, git_repo=self.git_repo, src_workspace=self.context.src_workspace, ) @@ -125,14 +125,14 @@ class WriteCode(Action): code=code_context, logs=logs, feedback=bug_feedback.content if bug_feedback else "", - filename=self.context.filename, + filename=self.i_context.filename, summary_log=summary_doc.content if summary_doc else "", ) logger.info(f"Writing {coding_context.filename}..") code = await self.write_code(prompt) if not coding_context.code_doc: # avoid root_path pydantic ValidationError if use WriteCode alone - root_path = self.context.src_workspace if self.context.src_workspace else "" + root_path = self.i_context.src_workspace if self.i_context.src_workspace else "" coding_context.code_doc = Document(filename=coding_context.filename, root_path=str(root_path)) coding_context.code_doc.content = code return coding_context diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 6ff9d5aa4..b25f1ab69 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -135,20 +135,20 @@ class WriteCodeReview(Action): return result, code async def run(self, *args, **kwargs) -> CodingContext: - iterative_code = self.context.code_doc.content + iterative_code = self.i_context.code_doc.content k = self.context.config.code_review_k_times or 1 for i in range(k): - format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) - task_content = self.context.task_doc.content if self.context.task_doc else "" + format_example = FORMAT_EXAMPLE.format(filename=self.i_context.code_doc.filename) + task_content = self.i_context.task_doc.content if self.i_context.task_doc else "" code_context = await WriteCode.get_codes( - self.context.task_doc, - exclude=self.context.filename, + self.i_context.task_doc, + exclude=self.i_context.filename, git_repo=self.context.git_repo, src_workspace=self.src_workspace, ) context = "\n".join( [ - "## System Design\n" + str(self.context.design_doc) + "\n", + "## System Design\n" + str(self.i_context.design_doc) + "\n", "## Tasks\n" + task_content + "\n", "## Code Files\n" + code_context + "\n", ] @@ -156,25 +156,25 @@ class WriteCodeReview(Action): context_prompt = PROMPT_TEMPLATE.format( context=context, code=iterative_code, - filename=self.context.code_doc.filename, + filename=self.i_context.code_doc.filename, ) cr_prompt = EXAMPLE_AND_INSTRUCTION.format( format_example=format_example, ) logger.info( - f"Code review and rewrite {self.context.code_doc.filename}: {i + 1}/{k} | {len(iterative_code)=}, " - f"{len(self.context.code_doc.content)=}" + f"Code review and rewrite {self.i_context.code_doc.filename}: {i + 1}/{k} | {len(iterative_code)=}, " + f"{len(self.i_context.code_doc.content)=}" ) result, rewrited_code = await self.write_code_review_and_rewrite( - context_prompt, cr_prompt, self.context.code_doc.filename + context_prompt, cr_prompt, self.i_context.code_doc.filename ) if "LBTM" in result: iterative_code = rewrited_code elif "LGTM" in result: - self.context.code_doc.content = iterative_code - return self.context + self.i_context.code_doc.content = iterative_code + return self.i_context # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) # self._save(context, filename, code) # 如果rewrited_code是None(原code perfect),那么直接返回code - self.context.code_doc.content = iterative_code - return self.context + self.i_context.code_doc.content = iterative_code + return self.i_context diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 38b1cf03c..978fa20a6 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -55,16 +55,16 @@ class WriteTest(Action): return code async def run(self, *args, **kwargs) -> TestingContext: - if not self.context.test_doc: - self.context.test_doc = Document( - filename="test_" + self.context.code_doc.filename, root_path=TEST_CODES_FILE_REPO + if not self.i_context.test_doc: + self.i_context.test_doc = Document( + filename="test_" + self.i_context.code_doc.filename, root_path=TEST_CODES_FILE_REPO ) fake_root = "/data" prompt = PROMPT_TEMPLATE.format( - code_to_test=self.context.code_doc.content, - test_file_name=self.context.test_doc.filename, - source_file_path=fake_root + "/" + self.context.code_doc.root_relative_path, + code_to_test=self.i_context.code_doc.content, + test_file_name=self.i_context.test_doc.filename, + source_file_path=fake_root + "/" + self.i_context.code_doc.root_relative_path, workspace=fake_root, ) - self.context.test_doc.content = await self.write_code(prompt) - return self.context + self.i_context.test_doc.content = await self.write_code(prompt) + return self.i_context From 4003f124bbf62536f064c82373901bd54449f223 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 16:28:01 +0800 Subject: [PATCH 33/50] refine code --- metagpt/actions/write_teaching_plan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/actions/write_teaching_plan.py b/metagpt/actions/write_teaching_plan.py index 04507fda3..6ea3c3099 100644 --- a/metagpt/actions/write_teaching_plan.py +++ b/metagpt/actions/write_teaching_plan.py @@ -35,7 +35,7 @@ class WriteTeachingPlanPart(Action): formation=TeachingPlanBlock.FORMATION, role=self.prefix, statements="\n".join(statements), - lesson=self.context, + lesson=self.i_context, topic=self.topic, language=self.language, ) From 9a95bcd6e9b757f825388ad1f25cf49c0c1ed7ea Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 17:17:27 +0800 Subject: [PATCH 34/50] extra='ignore' --- metagpt/actions/action.py | 2 +- metagpt/context.py | 2 +- metagpt/roles/role.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index cad8112d2..a3f7163c3 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -73,7 +73,7 @@ class Action(SerializationMixin, ContextMixin, BaseModel): def _init_with_instruction(cls, values): if "instruction" in values: name = values["name"] - i = values["instruction"] + i = values.pop("instruction") values["node"] = ActionNode(key=name, expected_type=str, instruction=i, example="", schema="raw") return values diff --git a/metagpt/context.py b/metagpt/context.py index 4badafcc4..406be1f53 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -165,7 +165,7 @@ class ContextMixin(BaseModel): @property def llm(self) -> BaseLLM: """Role llm: role llm > context llm""" - # logger.info(f"class:{self.__class__.__name__}, llm: {self._llm}, llm_config: {self._llm_config}") + print(f"class:{self.__class__.__name__}, llm: {self._llm}, llm_config: {self._llm_config}") if self._llm_config and not self._llm: self._llm = self.context.llm(self._llm_config.name, self._llm_config.provider) return self._llm or self.context.llm() diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 9c6832d8f..72ee1175b 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -120,7 +120,7 @@ class RoleContext(BaseModel): class Role(SerializationMixin, ContextMixin, BaseModel): """Role/Agent""" - model_config = ConfigDict(arbitrary_types_allowed=True, exclude=["llm"]) + model_config = ConfigDict(arbitrary_types_allowed=True, extra="ignore") name: str = "" profile: str = "" From 07d34bda7af705cd830b70bf911e9f851a966dcc Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 17:31:55 +0800 Subject: [PATCH 35/50] extra='ignore' --- metagpt/context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/context.py b/metagpt/context.py index 406be1f53..e2bead828 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -165,7 +165,7 @@ class ContextMixin(BaseModel): @property def llm(self) -> BaseLLM: """Role llm: role llm > context llm""" - print(f"class:{self.__class__.__name__}, llm: {self._llm}, llm_config: {self._llm_config}") + # print(f"class:{self.__class__.__name__}({self.name}), llm: {self._llm}, llm_config: {self._llm_config}") if self._llm_config and not self._llm: self._llm = self.context.llm(self._llm_config.name, self._llm_config.provider) return self._llm or self.context.llm() From 91e65645865bbe731e0aa012959cd30c6d24333b Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 17:54:13 +0800 Subject: [PATCH 36/50] modify add action to set action --- examples/agent_creator.py | 2 +- examples/build_customized_agent.py | 4 ++-- examples/build_customized_multi_agents.py | 6 +++--- examples/debate.py | 2 +- metagpt/roles/architect.py | 2 +- metagpt/roles/engineer.py | 2 +- metagpt/roles/invoice_ocr_assistant.py | 6 +++--- metagpt/roles/product_manager.py | 2 +- metagpt/roles/project_manager.py | 2 +- metagpt/roles/qa_engineer.py | 2 +- metagpt/roles/researcher.py | 2 +- metagpt/roles/role.py | 7 ++++--- metagpt/roles/sales.py | 2 +- metagpt/roles/searcher.py | 4 ++-- metagpt/roles/sk_agent.py | 2 +- metagpt/roles/teacher.py | 2 +- metagpt/roles/tutorial_assistant.py | 4 ++-- tests/metagpt/serialize_deserialize/test_serdeser_base.py | 6 +++--- tests/metagpt/test_role.py | 8 ++++---- 19 files changed, 34 insertions(+), 33 deletions(-) diff --git a/examples/agent_creator.py b/examples/agent_creator.py index fe883bdf4..bd58840ce 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -61,7 +61,7 @@ class AgentCreator(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self.add_actions([CreateAgent]) + self.set_actions([CreateAgent]) async def _act(self) -> Message: logger.info(f"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})") diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py index a0c8ddfb3..cfe264b47 100644 --- a/examples/build_customized_agent.py +++ b/examples/build_customized_agent.py @@ -57,7 +57,7 @@ class SimpleCoder(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self.add_actions([SimpleWriteCode]) + self.set_actions([SimpleWriteCode]) async def _act(self) -> Message: logger.info(f"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})") @@ -76,7 +76,7 @@ class RunnableCoder(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self.add_actions([SimpleWriteCode, SimpleRunCode]) + self.set_actions([SimpleWriteCode, SimpleRunCode]) self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value) async def _act(self) -> Message: diff --git a/examples/build_customized_multi_agents.py b/examples/build_customized_multi_agents.py index aceb3f2ab..296323cea 100644 --- a/examples/build_customized_multi_agents.py +++ b/examples/build_customized_multi_agents.py @@ -46,7 +46,7 @@ class SimpleCoder(Role): def __init__(self, **kwargs): super().__init__(**kwargs) self._watch([UserRequirement]) - self.add_actions([SimpleWriteCode]) + self.set_actions([SimpleWriteCode]) class SimpleWriteTest(Action): @@ -75,7 +75,7 @@ class SimpleTester(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self.add_actions([SimpleWriteTest]) + self.set_actions([SimpleWriteTest]) # self._watch([SimpleWriteCode]) self._watch([SimpleWriteCode, SimpleWriteReview]) # feel free to try this too @@ -114,7 +114,7 @@ class SimpleReviewer(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self.add_actions([SimpleWriteReview]) + self.set_actions([SimpleWriteReview]) self._watch([SimpleWriteTest]) diff --git a/examples/debate.py b/examples/debate.py index b47eba3cd..72ab8796d 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -49,7 +49,7 @@ class Debator(Role): def __init__(self, **data: Any): super().__init__(**data) - self.add_actions([SpeakAloud]) + self.set_actions([SpeakAloud]) self._watch([UserRequirement, SpeakAloud]) async def _observe(self) -> int: diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index a22a1c926..166f8cfd0 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -33,7 +33,7 @@ class Architect(Role): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) # Initialize actions specific to the Architect role - self.add_actions([WriteDesign]) + self.set_actions([WriteDesign]) # Set events or actions the Architect should watch or be aware of self._watch({WritePRD}) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 0d277813e..bc56ca813 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -84,7 +84,7 @@ class Engineer(Role): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self.add_actions([WriteCode]) + self.set_actions([WriteCode]) self._watch([WriteTasks, SummarizeCode, WriteCode, WriteCodeReview, FixBug]) self.code_todos = [] self.summarize_todos = [] diff --git a/metagpt/roles/invoice_ocr_assistant.py b/metagpt/roles/invoice_ocr_assistant.py index de7d3f8a3..a39a48b97 100644 --- a/metagpt/roles/invoice_ocr_assistant.py +++ b/metagpt/roles/invoice_ocr_assistant.py @@ -60,7 +60,7 @@ class InvoiceOCRAssistant(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self.add_actions([InvoiceOCR]) + self.set_actions([InvoiceOCR]) self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value) async def _act(self) -> Message: @@ -82,10 +82,10 @@ class InvoiceOCRAssistant(Role): resp = await todo.run(file_path) if len(resp) == 1: # Single file support for questioning based on OCR recognition results - self.add_actions([GenerateTable, ReplyQuestion]) + self.set_actions([GenerateTable, ReplyQuestion]) self.orc_data = resp[0] else: - self.add_actions([GenerateTable]) + self.set_actions([GenerateTable]) self.set_todo(None) content = INVOICE_OCR_SUCCESS diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index a35dcb3a0..ec80d7bb0 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -33,7 +33,7 @@ class ProductManager(Role): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self.add_actions([PrepareDocuments, WritePRD]) + self.set_actions([PrepareDocuments, WritePRD]) self._watch([UserRequirement, PrepareDocuments]) self.todo_action = any_to_name(PrepareDocuments) diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 7fa16b1e5..422d2889b 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -33,5 +33,5 @@ class ProjectManager(Role): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self.add_actions([WriteTasks]) + self.set_actions([WriteTasks]) self._watch([WriteDesign]) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 9483ea260..783fde9b6 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -44,7 +44,7 @@ class QaEngineer(Role): # FIXME: a bit hack here, only init one action to circumvent _think() logic, # will overwrite _think() in future updates - self.add_actions([WriteTest]) + self.set_actions([WriteTest]) self._watch([SummarizeCode, WriteTest, RunCode, DebugError]) self.test_round = 0 diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index e877778f6..137cfdb4c 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -34,7 +34,7 @@ class Researcher(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self.add_actions( + self.set_actions( [CollectLinks(name=self.name), WebBrowseAndSummarize(name=self.name), ConductResearch(name=self.name)] ) self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 72ee1175b..e467ef83e 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -222,16 +222,17 @@ class Role(SerializationMixin, ContextMixin, BaseModel): def _init_action_system_message(self, action: Action): action.set_prefix(self._get_prefix()) - def add_action(self, action: Action): + def set_action(self, action: Action): """Add action to the role.""" - self.add_actions([action]) + self.set_actions([action]) - def add_actions(self, actions: list[Union[Action, Type[Action]]]): + def set_actions(self, actions: list[Union[Action, Type[Action]]]): """Add actions to the role. Args: actions: list of Action classes or instances """ + self._reset() for action in actions: if not isinstance(action, Action): i = action(name="", llm=self.llm) diff --git a/metagpt/roles/sales.py b/metagpt/roles/sales.py index 8da930888..7929ce7fe 100644 --- a/metagpt/roles/sales.py +++ b/metagpt/roles/sales.py @@ -38,5 +38,5 @@ class Sales(Role): action = SearchAndSummarize(name="", engine=SearchEngineType.CUSTOM_ENGINE, search_func=store.asearch) else: action = SearchAndSummarize() - self.add_actions([action]) + self.set_actions([action]) self._watch([UserRequirement]) diff --git a/metagpt/roles/searcher.py b/metagpt/roles/searcher.py index f37bd4704..e0d2dbb65 100644 --- a/metagpt/roles/searcher.py +++ b/metagpt/roles/searcher.py @@ -48,12 +48,12 @@ class Searcher(Role): engine (SearchEngineType): The type of search engine to use. """ super().__init__(**kwargs) - self.add_actions([SearchAndSummarize(engine=self.engine)]) + self.set_actions([SearchAndSummarize(engine=self.engine)]) def set_search_func(self, search_func): """Sets a custom search function for the searcher.""" action = SearchAndSummarize(name="", engine=SearchEngineType.CUSTOM_ENGINE, search_func=search_func) - self.add_actions([action]) + self.set_actions([action]) async def _act_sp(self) -> Message: """Performs the search action in a single process.""" diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index 200ed5051..71df55fcc 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -49,7 +49,7 @@ class SkAgent(Role): def __init__(self, **data: Any) -> None: """Initializes the Engineer role with given attributes.""" super().__init__(**data) - self.add_actions([ExecuteTask()]) + self.set_actions([ExecuteTask()]) self._watch([UserRequirement]) self.kernel = make_sk_kernel() diff --git a/metagpt/roles/teacher.py b/metagpt/roles/teacher.py index 9206d5f80..d47f4af5b 100644 --- a/metagpt/roles/teacher.py +++ b/metagpt/roles/teacher.py @@ -47,7 +47,7 @@ class Teacher(Role): for topic in TeachingPlanBlock.TOPICS: act = WriteTeachingPlanPart(i_context=self.rc.news[0].content, topic=topic, llm=self.llm) actions.append(act) - self.add_actions(actions) + self.set_actions(actions) if self.rc.todo is None: self._set_state(0) diff --git a/metagpt/roles/tutorial_assistant.py b/metagpt/roles/tutorial_assistant.py index d296c7b3f..6cf3a6469 100644 --- a/metagpt/roles/tutorial_assistant.py +++ b/metagpt/roles/tutorial_assistant.py @@ -40,7 +40,7 @@ class TutorialAssistant(Role): def __init__(self, **kwargs): super().__init__(**kwargs) - self.add_actions([WriteDirectory(language=self.language)]) + self.set_actions([WriteDirectory(language=self.language)]) self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value) async def _handle_directory(self, titles: Dict) -> Message: @@ -63,7 +63,7 @@ class TutorialAssistant(Role): directory += f"- {key}\n" for second_dir in first_dir[key]: directory += f" - {second_dir}\n" - self.add_actions(actions) + self.set_actions(actions) async def _act(self) -> Message: """Perform an action as determined by the role. diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index c97cea597..62ab26d72 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -67,7 +67,7 @@ class RoleA(Role): def __init__(self, **kwargs): super(RoleA, self).__init__(**kwargs) - self.add_actions([ActionPass]) + self.set_actions([ActionPass]) self._watch([UserRequirement]) @@ -79,7 +79,7 @@ class RoleB(Role): def __init__(self, **kwargs): super(RoleB, self).__init__(**kwargs) - self.add_actions([ActionOK, ActionRaise]) + self.set_actions([ActionOK, ActionRaise]) self._watch([ActionPass]) self.rc.react_mode = RoleReactMode.BY_ORDER @@ -92,7 +92,7 @@ class RoleC(Role): def __init__(self, **kwargs): super(RoleC, self).__init__(**kwargs) - self.add_actions([ActionOK, ActionRaise]) + self.set_actions([ActionOK, ActionRaise]) self._watch([UserRequirement]) self.rc.react_mode = RoleReactMode.BY_ORDER self.rc.memory.ignore_id = True diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index c67a8ad8a..351ba9051 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -33,7 +33,7 @@ class MockAction(Action): class MockRole(Role): def __init__(self, name="", profile="", goal="", constraints="", desc=""): super().__init__(name=name, profile=profile, goal=goal, constraints=constraints, desc=desc) - self.add_actions([MockAction()]) + self.set_actions([MockAction()]) def test_basic(): @@ -111,7 +111,7 @@ async def test_send_to(): def test_init_action(): role = Role() - role.add_actions([MockAction, MockAction]) + role.set_actions([MockAction, MockAction]) assert len(role.actions) == 2 @@ -127,7 +127,7 @@ async def test_recover(): role.publish_message(None) role.llm = mock_llm - role.add_actions([MockAction, MockAction]) + role.set_actions([MockAction, MockAction]) role.recovered = True role.latest_observed_msg = Message(content="recover_test") role.rc.state = 0 @@ -144,7 +144,7 @@ async def test_think_act(): mock_llm.aask.side_effect = ["ok"] role = Role() - role.add_actions([MockAction]) + role.set_actions([MockAction]) await role.think() role.rc.memory.add(Message("run")) assert len(role.get_memories()) == 1 From a1b900aa3a73621b26f457bde867633657ec5b56 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 18:32:03 +0800 Subject: [PATCH 37/50] fix bug --- metagpt/actions/write_code.py | 2 +- metagpt/config2.py | 4 +--- metagpt/context.py | 16 ++++++++++++---- tests/metagpt/actions/test_write_code.py | 6 +++--- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 62de34ef4..1b3dcf5f0 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -132,7 +132,7 @@ class WriteCode(Action): code = await self.write_code(prompt) if not coding_context.code_doc: # avoid root_path pydantic ValidationError if use WriteCode alone - root_path = self.i_context.src_workspace if self.i_context.src_workspace else "" + root_path = self.context.src_workspace if self.context.src_workspace else "" coding_context.code_doc = Document(filename=coding_context.filename, root_path=str(root_path)) coding_context.code_doc.content = code return coding_context diff --git a/metagpt/config2.py b/metagpt/config2.py index cb5c22ac2..30d3818f6 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -121,12 +121,10 @@ class Config(CLIParams, YamlModel): return llm[0] return None - def get_llm_config(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> LLMConfig: + def get_llm_config(self, name: Optional[str] = None, provider: LLMType = None) -> LLMConfig: """Return a LLMConfig instance""" if provider: llm_configs = self.get_llm_configs_by_type(provider) - if name: - llm_configs = [c for c in llm_configs if c.name == name] if len(llm_configs) == 0: raise ValueError(f"Cannot find llm config with name {name} and provider {provider}") diff --git a/metagpt/context.py b/metagpt/context.py index e2bead828..35892f3f3 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -77,7 +77,7 @@ class Context(BaseModel): # self._llm = None # return self._llm - def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: + def llm(self, name: Optional[str] = None, provider: LLMType = None) -> BaseLLM: """Return a LLM instance, fixme: support cache""" # if self._llm is None: self._llm = create_llm_instance(self.config.get_llm_config(name, provider)) @@ -85,6 +85,14 @@ class Context(BaseModel): self._llm.cost_manager = self.cost_manager return self._llm + def llm_with_cost_manager_from_llm_config(self, llm_config: LLMConfig) -> BaseLLM: + """Return a LLM instance, fixme: support cache""" + # if self._llm is None: + llm = create_llm_instance(llm_config) + if llm.cost_manager is None: + llm.cost_manager = self.cost_manager + return llm + class ContextMixin(BaseModel): """Mixin class for context and config""" @@ -132,7 +140,7 @@ class ContextMixin(BaseModel): """Set llm""" self.set("_llm", llm, override) - def use_llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: + def use_llm(self, name: Optional[str] = None, provider: LLMType = None) -> BaseLLM: """Use a LLM instance""" self._llm_config = self.config.get_llm_config(name, provider) self._llm = None @@ -165,9 +173,9 @@ class ContextMixin(BaseModel): @property def llm(self) -> BaseLLM: """Role llm: role llm > context llm""" - # print(f"class:{self.__class__.__name__}({self.name}), llm: {self._llm}, llm_config: {self._llm_config}") + print(f"class:{self.__class__.__name__}({self.name}), llm: {self._llm}, llm_config: {self._llm_config}") if self._llm_config and not self._llm: - self._llm = self.context.llm(self._llm_config.name, self._llm_config.provider) + self._llm = self.context.llm_with_cost_manager_from_llm_config(self._llm_config) return self._llm or self.context.llm() @llm.setter diff --git a/tests/metagpt/actions/test_write_code.py b/tests/metagpt/actions/test_write_code.py index cfc5863f4..792b89d90 100644 --- a/tests/metagpt/actions/test_write_code.py +++ b/tests/metagpt/actions/test_write_code.py @@ -19,8 +19,8 @@ from metagpt.const import ( TEST_OUTPUTS_FILE_REPO, ) from metagpt.context import CONTEXT +from metagpt.llm import LLM from metagpt.logs import logger -from metagpt.provider.openai_api import OpenAILLM as LLM from metagpt.schema import CodingContext, Document from metagpt.utils.common import aread from tests.metagpt.actions.mock_markdown import TASKS_2, WRITE_CODE_PROMPT_SAMPLE @@ -32,7 +32,7 @@ async def test_write_code(): filename="task_filename.py", design_doc=Document(content="设计一个名为'add'的函数,该函数接受两个整数作为输入,并返回它们的和。") ) doc = Document(content=ccontext.model_dump_json()) - write_code = WriteCode(context=doc) + write_code = WriteCode(i_context=doc) code = await write_code.run() logger.info(code.model_dump_json()) @@ -86,7 +86,7 @@ async def test_write_code_deps(): ) coding_doc = Document(root_path="snake1", filename="game.py", content=ccontext.json()) - action = WriteCode(context=coding_doc) + action = WriteCode(i_context=coding_doc) rsp = await action.run() assert rsp assert rsp.code_doc.content From 58c2c55ee93c63a5600937104cf22b5d9edbbd59 Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 10 Jan 2024 19:13:19 +0800 Subject: [PATCH 38/50] add action_outcls decorator to support init same class with same class name and fields --- metagpt/actions/action_node.py | 2 + metagpt/actions/action_outcls_registry.py | 42 +++++++++++++++++ .../actions/test_action_outcls_registry.py | 46 +++++++++++++++++++ .../serialize_deserialize/test_architect.py | 1 + .../serialize_deserialize/test_schema.py | 9 +++- 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 metagpt/actions/action_outcls_registry.py create mode 100644 tests/metagpt/actions/test_action_outcls_registry.py diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 286cf534d..b4d8c32df 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -15,6 +15,7 @@ from typing import Any, Dict, List, Optional, Tuple, Type, Union from pydantic import BaseModel, create_model, model_validator from tenacity import retry, stop_after_attempt, wait_random_exponential +from metagpt.actions.action_outcls_registry import register_action_outcls from metagpt.llm import BaseLLM from metagpt.logs import logger from metagpt.provider.postprocess.llm_output_postprocess import llm_output_postprocess @@ -201,6 +202,7 @@ class ActionNode: return {} if exclude and self.key in exclude else self.get_self_mapping() @classmethod + @register_action_outcls def create_model_class(cls, class_name: str, mapping: Dict[str, Tuple[Type, Any]]): """基于pydantic v1的模型动态生成,用来检验结果类型正确性""" diff --git a/metagpt/actions/action_outcls_registry.py b/metagpt/actions/action_outcls_registry.py new file mode 100644 index 000000000..780a061b4 --- /dev/null +++ b/metagpt/actions/action_outcls_registry.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : registry to store Dynamic Model from ActionNode.create_model_class to keep it as same Class +# with same class name and mapping + +from functools import wraps + + +action_outcls_registry = dict() + + +def register_action_outcls(func): + """ + Due to `create_model` return different Class even they have same class name and mapping. + In order to do a comparison, use outcls_id to identify same Class with same class name and field definition + """ + @wraps(func) + def decorater(*args, **kwargs): + """ + arr example + [, 'test', {'field': (str, Ellipsis)}] + """ + arr = list(args) + list(kwargs.values()) + """ + outcls_id example + "_test_{'field': (str, Ellipsis)}" + """ + for idx, item in enumerate(arr): + if isinstance(item, dict): + arr[idx] = dict(sorted(item.items())) + outcls_id = "_".join([str(i) for i in arr]) + # eliminate typing influence + outcls_id = outcls_id.replace("typing.List", "list").replace("typing.Dict", "dict") + + if outcls_id in action_outcls_registry: + return action_outcls_registry[outcls_id] + + out_cls = func(*args, **kwargs) + action_outcls_registry[outcls_id] = out_cls + return out_cls + + return decorater diff --git a/tests/metagpt/actions/test_action_outcls_registry.py b/tests/metagpt/actions/test_action_outcls_registry.py new file mode 100644 index 000000000..e949ac16b --- /dev/null +++ b/tests/metagpt/actions/test_action_outcls_registry.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of action_outcls_registry + +from typing import List +from metagpt.actions.action_node import ActionNode + + +def test_action_outcls_registry(): + class_name = "test" + out_mapping = {"field": (list[str], ...), "field1": (str, ...)} + out_data = {"field": ["field value1", "field value2"], "field1": "field1 value1"} + + outcls = ActionNode.create_model_class(class_name, mapping=out_mapping) + outinst = outcls(**out_data) + + outcls1 = ActionNode.create_model_class(class_name=class_name, mapping=out_mapping) + outinst1 = outcls1(**out_data) + assert outinst1 == outinst + + outcls2 = ActionNode(key="", + expected_type=str, + instruction="", + example="").create_model_class(class_name, out_mapping) + outinst2 = outcls2(**out_data) + assert outinst2 == outinst + + out_mapping = {"field1": (str, ...), "field": (list[str], ...)} # different order + outcls3 = ActionNode.create_model_class(class_name=class_name, mapping=out_mapping) + outinst3 = outcls3(**out_data) + assert outinst3 == outinst + + out_mapping2 = {"field1": (str, ...), "field": (List[str], ...)} # typing case + outcls4 = ActionNode.create_model_class(class_name=class_name, mapping=out_mapping2) + outinst4 = outcls4(**out_data) + assert outinst4 == outinst + + out_data2 = {"field2": ["field2 value1", "field2 value2"], "field1": "field1 value1"} + out_mapping = {"field1": (str, ...), "field2": (List[str], ...)} # List first + outcls5 = ActionNode.create_model_class(class_name, out_mapping) + outinst5 = outcls5(**out_data2) + + out_mapping = {"field1": (str, ...), "field2": (list[str], ...)} + outcls6 = ActionNode.create_model_class(class_name, out_mapping) + outinst6 = outcls6(**out_data2) + assert outinst5 == outinst6 diff --git a/tests/metagpt/serialize_deserialize/test_architect.py b/tests/metagpt/serialize_deserialize/test_architect.py index 343662494..a6823197a 100644 --- a/tests/metagpt/serialize_deserialize/test_architect.py +++ b/tests/metagpt/serialize_deserialize/test_architect.py @@ -19,5 +19,6 @@ async def test_architect_serdeser(): new_role = Architect(**ser_role_dict) assert new_role.name == "Bob" assert len(new_role.actions) == 1 + assert len(new_role.rc.watch) == 1 assert isinstance(new_role.actions[0], Action) await new_role.actions[0].run(with_messages="write a cli snake game") diff --git a/tests/metagpt/serialize_deserialize/test_schema.py b/tests/metagpt/serialize_deserialize/test_schema.py index b55b82088..c5a457a1e 100644 --- a/tests/metagpt/serialize_deserialize/test_schema.py +++ b/tests/metagpt/serialize_deserialize/test_schema.py @@ -31,15 +31,17 @@ def test_message_serdeser_from_create_model(): assert new_message.cause_by == any_to_str(WriteCode) assert new_message.cause_by in [any_to_str(WriteCode)] - assert new_message.instruct_content != ic_obj(**out_data) # TODO find why `!=` - assert new_message.instruct_content != ic_inst + assert new_message.instruct_content == ic_obj(**out_data) + assert new_message.instruct_content == ic_inst assert new_message.instruct_content.model_dump() == ic_obj(**out_data).model_dump() + assert new_message == message mock_msg = MockMessage() message = Message(content="test_ic", instruct_content=mock_msg) ser_data = message.model_dump() new_message = Message(**ser_data) assert new_message.instruct_content == mock_msg + assert new_message == message def test_message_without_postprocess(): @@ -54,6 +56,7 @@ def test_message_without_postprocess(): ser_data["instruct_content"] = None new_message = MockICMessage(**ser_data) assert new_message.instruct_content != ic_obj(**out_data) + assert new_message != message def test_message_serdeser_from_basecontext(): @@ -83,6 +86,7 @@ def test_message_serdeser_from_basecontext(): new_code_ctxt_msg = Message(**ser_data) assert new_code_ctxt_msg.instruct_content == code_ctxt assert new_code_ctxt_msg.instruct_content.code_doc.filename == "game.py" + assert new_code_ctxt_msg == code_ctxt_msg testing_ctxt = TestingContext( filename="test.py", @@ -94,3 +98,4 @@ def test_message_serdeser_from_basecontext(): new_testing_ctxt_msg = Message(**ser_data) assert new_testing_ctxt_msg.instruct_content == testing_ctxt assert new_testing_ctxt_msg.instruct_content.test_doc.filename == "test.py" + assert new_testing_ctxt_msg == testing_ctxt_msg From 294b035fb47853969d101272a854a4f79bf5c3ea Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 10 Jan 2024 19:27:33 +0800 Subject: [PATCH 39/50] fix format --- metagpt/actions/action_outcls_registry.py | 2 +- tests/metagpt/actions/test_action_outcls_registry.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/metagpt/actions/action_outcls_registry.py b/metagpt/actions/action_outcls_registry.py index 780a061b4..6baa4cea9 100644 --- a/metagpt/actions/action_outcls_registry.py +++ b/metagpt/actions/action_outcls_registry.py @@ -5,7 +5,6 @@ from functools import wraps - action_outcls_registry = dict() @@ -14,6 +13,7 @@ def register_action_outcls(func): Due to `create_model` return different Class even they have same class name and mapping. In order to do a comparison, use outcls_id to identify same Class with same class name and field definition """ + @wraps(func) def decorater(*args, **kwargs): """ diff --git a/tests/metagpt/actions/test_action_outcls_registry.py b/tests/metagpt/actions/test_action_outcls_registry.py index e949ac16b..eac0ba4d9 100644 --- a/tests/metagpt/actions/test_action_outcls_registry.py +++ b/tests/metagpt/actions/test_action_outcls_registry.py @@ -3,6 +3,7 @@ # @Desc : unittest of action_outcls_registry from typing import List + from metagpt.actions.action_node import ActionNode @@ -18,10 +19,9 @@ def test_action_outcls_registry(): outinst1 = outcls1(**out_data) assert outinst1 == outinst - outcls2 = ActionNode(key="", - expected_type=str, - instruction="", - example="").create_model_class(class_name, out_mapping) + outcls2 = ActionNode(key="", expected_type=str, instruction="", example="").create_model_class( + class_name, out_mapping + ) outinst2 = outcls2(**out_data) assert outinst2 == outinst From 479bbc9b2d04b968ceb48908e8ac1ba8aae2a5b3 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 20:19:56 +0800 Subject: [PATCH 40/50] use config --- metagpt/actions/rebuild_class_view.py | 11 +- metagpt/actions/rebuild_sequence_view.py | 5 +- metagpt/actions/research.py | 9 +- metagpt/actions/write_teaching_plan.py | 4 +- metagpt/config2.py | 3 + metagpt/learn/skill_loader.py | 4 +- metagpt/learn/text_to_embedding.py | 5 +- metagpt/learn/text_to_speech.py | 3 +- metagpt/tools/openai_text_to_embedding.py | 9 +- metagpt/tools/sd_engine.py | 133 ------------------ metagpt/tools/search_engine_ddg.py | 8 +- metagpt/tools/search_engine_googleapi.py | 10 +- metagpt/tools/search_engine_serpapi.py | 4 +- metagpt/tools/search_engine_serper.py | 4 +- metagpt/tools/web_browser_engine.py | 2 - .../tools/web_browser_engine_playwright.py | 12 +- metagpt/tools/web_browser_engine_selenium.py | 12 +- metagpt/utils/mermaid.py | 14 +- metagpt/utils/mmdc_pyppeteer.py | 6 +- metagpt/utils/repair_llm_raw_output.py | 8 +- .../actions/test_rebuild_class_view.py | 3 +- .../actions/test_rebuild_sequence_view.py | 9 +- tests/metagpt/actions/test_summarize_code.py | 11 +- tests/metagpt/learn/test_skill_loader.py | 4 +- tests/metagpt/learn/test_text_to_embedding.py | 4 +- tests/metagpt/tools/test_azure_tts.py | 3 +- .../tools/test_metagpt_oas3_api_svc.py | 4 +- .../tools/test_metagpt_text_to_image.py | 4 +- tests/metagpt/tools/test_moderation.py | 6 +- .../tools/test_openai_text_to_embedding.py | 6 +- .../tools/test_openai_text_to_image.py | 6 +- tests/metagpt/tools/test_openapi_v3_hello.py | 4 +- tests/metagpt/tools/test_sd_tool.py | 26 ---- tests/metagpt/tools/test_search_engine.py | 9 +- tests/metagpt/tools/test_ut_writer.py | 6 +- tests/metagpt/utils/test_mermaid.py | 3 +- .../utils/test_repair_llm_raw_output.py | 4 +- 37 files changed, 102 insertions(+), 276 deletions(-) delete mode 100644 metagpt/tools/sd_engine.py delete mode 100644 tests/metagpt/tools/test_sd_tool.py diff --git a/metagpt/actions/rebuild_class_view.py b/metagpt/actions/rebuild_class_view.py index 876beccec..d25d9e49b 100644 --- a/metagpt/actions/rebuild_class_view.py +++ b/metagpt/actions/rebuild_class_view.py @@ -12,7 +12,7 @@ from pathlib import Path import aiofiles from metagpt.actions import Action -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.const import ( AGGREGATION, COMPOSITION, @@ -20,6 +20,7 @@ from metagpt.const import ( GENERALIZATION, GRAPH_REPO_FILE_REPO, ) +from metagpt.context import CONTEXT from metagpt.logs import logger from metagpt.repo_parser import RepoParser from metagpt.schema import ClassAttribute, ClassMethod, ClassView @@ -29,8 +30,8 @@ from metagpt.utils.graph_repository import GraphKeyword, GraphRepository class RebuildClassView(Action): - async def run(self, with_messages=None, format=CONFIG.prompt_schema): - graph_repo_pathname = CONFIG.git_repo.workdir / GRAPH_REPO_FILE_REPO / CONFIG.git_repo.workdir.name + async def run(self, with_messages=None, format=config.prompt_schema): + graph_repo_pathname = CONTEXT.git_repo.workdir / GRAPH_REPO_FILE_REPO / CONTEXT.git_repo.workdir.name graph_db = await DiGraphRepository.load_from(str(graph_repo_pathname.with_suffix(".json"))) repo_parser = RepoParser(base_directory=Path(self.i_context)) # use pylint @@ -48,9 +49,9 @@ class RebuildClassView(Action): await graph_db.save() async def _create_mermaid_class_views(self, graph_db): - path = Path(CONFIG.git_repo.workdir) / DATA_API_DESIGN_FILE_REPO + path = Path(CONTEXT.git_repo.workdir) / DATA_API_DESIGN_FILE_REPO path.mkdir(parents=True, exist_ok=True) - pathname = path / CONFIG.git_repo.workdir.name + pathname = path / CONTEXT.git_repo.workdir.name async with aiofiles.open(str(pathname.with_suffix(".mmd")), mode="w", encoding="utf-8") as writer: content = "classDiagram\n" logger.debug(content) diff --git a/metagpt/actions/rebuild_sequence_view.py b/metagpt/actions/rebuild_sequence_view.py index bc128d8b0..8785e6245 100644 --- a/metagpt/actions/rebuild_sequence_view.py +++ b/metagpt/actions/rebuild_sequence_view.py @@ -12,7 +12,6 @@ from pathlib import Path from typing import List from metagpt.actions import Action -from metagpt.config import CONFIG from metagpt.const import GRAPH_REPO_FILE_REPO from metagpt.logs import logger from metagpt.utils.common import aread, list_files @@ -21,8 +20,8 @@ from metagpt.utils.graph_repository import GraphKeyword class RebuildSequenceView(Action): - async def run(self, with_messages=None, format=CONFIG.prompt_schema): - graph_repo_pathname = CONFIG.git_repo.workdir / GRAPH_REPO_FILE_REPO / CONFIG.git_repo.workdir.name + async def run(self, with_messages=None, format=config.prompt_schema): + graph_repo_pathname = CONTEXT.git_repo.workdir / GRAPH_REPO_FILE_REPO / CONTEXT.git_repo.workdir.name graph_db = await DiGraphRepository.load_from(str(graph_repo_pathname.with_suffix(".json"))) entries = await RebuildSequenceView._search_main_entry(graph_db) for entry in entries: diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index d2db228ae..a635714ef 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -9,6 +9,7 @@ from pydantic import Field, parse_obj_as from metagpt.actions import Action from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.logs import logger from metagpt.tools.search_engine import SearchEngine from metagpt.tools.web_browser_engine import WebBrowserEngine, WebBrowserEngineType @@ -127,8 +128,8 @@ class CollectLinks(Action): if len(remove) == 0: break - model_name = CONFIG.get_model_name(CONFIG.get_default_llm_provider_enum()) - prompt = reduce_message_length(gen_msg(), model_name, system_text, CONFIG.max_tokens_rsp) + model_name = config.get_openai_llm().model + prompt = reduce_message_length(gen_msg(), model_name, system_text, 4096) logger.debug(prompt) queries = await self._aask(prompt, [system_text]) try: @@ -182,8 +183,6 @@ class WebBrowseAndSummarize(Action): def __init__(self, **kwargs): super().__init__(**kwargs) - if CONFIG.model_for_researcher_summary: - self.llm.model = CONFIG.model_for_researcher_summary self.web_browser_engine = WebBrowserEngine( engine=WebBrowserEngineType.CUSTOM if self.browse_func else None, @@ -246,8 +245,6 @@ class ConductResearch(Action): def __init__(self, **kwargs): super().__init__(**kwargs) - if CONFIG.model_for_researcher_report: - self.llm.model = CONFIG.model_for_researcher_report async def run( self, diff --git a/metagpt/actions/write_teaching_plan.py b/metagpt/actions/write_teaching_plan.py index 6ea3c3099..1678bc8dc 100644 --- a/metagpt/actions/write_teaching_plan.py +++ b/metagpt/actions/write_teaching_plan.py @@ -8,7 +8,7 @@ from typing import Optional from metagpt.actions import Action -from metagpt.config import CONFIG +from metagpt.context import CONTEXT from metagpt.logs import logger @@ -76,7 +76,7 @@ class WriteTeachingPlanPart(Action): return value # FIXME: 从Context中获取参数,而非从options - merged_opts = CONFIG.options or {} + merged_opts = CONTEXT.options or {} try: return value.format(**merged_opts) except KeyError as e: diff --git a/metagpt/config2.py b/metagpt/config2.py index 30d3818f6..2a9611627 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -71,6 +71,9 @@ class Config(CLIParams, YamlModel): METAGPT_TEXT_TO_IMAGE_MODEL_URL: str = "" language: str = "English" redis_key: str = "placeholder" + mmdc: str = "mmdc" + puppeteer_config: str = "" + pyppeteer_executable_path: str = "" @classmethod def default(cls): diff --git a/metagpt/learn/skill_loader.py b/metagpt/learn/skill_loader.py index 7383af66d..b60fa9093 100644 --- a/metagpt/learn/skill_loader.py +++ b/metagpt/learn/skill_loader.py @@ -13,7 +13,7 @@ import aiofiles import yaml from pydantic import BaseModel, Field -from metagpt.config import CONFIG +from metagpt.context import CONTEXT class Example(BaseModel): @@ -80,7 +80,7 @@ class SkillsDeclaration(BaseModel): return {} # List of skills that the agent chooses to activate. - agent_skills = CONFIG.agent_skills + agent_skills = CONTEXT.kwargs.agent_skills if not agent_skills: return {} diff --git a/metagpt/learn/text_to_embedding.py b/metagpt/learn/text_to_embedding.py index 26dab0419..6a4342b06 100644 --- a/metagpt/learn/text_to_embedding.py +++ b/metagpt/learn/text_to_embedding.py @@ -7,7 +7,6 @@ @Desc : Text-to-Embedding skill, which provides text-to-embedding functionality. """ -from metagpt.config import CONFIG from metagpt.tools.openai_text_to_embedding import oas3_openai_text_to_embedding @@ -19,6 +18,4 @@ async def text_to_embedding(text, model="text-embedding-ada-002", openai_api_key :param openai_api_key: OpenAI API key, For more details, checkout: `https://platform.openai.com/account/api-keys` :return: A json object of :class:`ResultEmbedding` class if successful, otherwise `{}`. """ - if CONFIG.OPENAI_API_KEY or openai_api_key: - return await oas3_openai_text_to_embedding(text, model=model, openai_api_key=openai_api_key) - raise EnvironmentError + return await oas3_openai_text_to_embedding(text, model=model, openai_api_key=openai_api_key) diff --git a/metagpt/learn/text_to_speech.py b/metagpt/learn/text_to_speech.py index 9ee3d64ee..f12e52b8e 100644 --- a/metagpt/learn/text_to_speech.py +++ b/metagpt/learn/text_to_speech.py @@ -8,6 +8,7 @@ """ from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.const import BASE64_FORMAT from metagpt.tools.azure_tts import oas3_azsure_tts from metagpt.tools.iflytek_tts import oas3_iflytek_tts @@ -47,7 +48,7 @@ async def text_to_speech( if (CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_REGION) or (subscription_key and region): audio_declaration = "data:audio/wav;base64," base64_data = await oas3_azsure_tts(text, lang, voice, style, role, subscription_key, region) - s3 = S3() + s3 = S3(config.s3) url = await s3.cache(data=base64_data, file_ext=".wav", format=BASE64_FORMAT) if url: return f"[{text}]({url})" diff --git a/metagpt/tools/openai_text_to_embedding.py b/metagpt/tools/openai_text_to_embedding.py index 52b2cc9eb..3eb9faac4 100644 --- a/metagpt/tools/openai_text_to_embedding.py +++ b/metagpt/tools/openai_text_to_embedding.py @@ -13,7 +13,7 @@ import aiohttp import requests from pydantic import BaseModel, Field -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.logs import logger @@ -47,7 +47,8 @@ class OpenAIText2Embedding: """ :param openai_api_key: OpenAI API key, For more details, checkout: `https://platform.openai.com/account/api-keys` """ - self.openai_api_key = openai_api_key or CONFIG.OPENAI_API_KEY + self.openai_llm = config.get_openai_llm() + self.openai_api_key = openai_api_key or self.openai_llm.api_key async def text_2_embedding(self, text, model="text-embedding-ada-002"): """Text to embedding @@ -57,7 +58,7 @@ class OpenAIText2Embedding: :return: A json object of :class:`ResultEmbedding` class if successful, otherwise `{}`. """ - proxies = {"proxy": CONFIG.openai_proxy} if CONFIG.openai_proxy else {} + proxies = {"proxy": self.openai_llm.proxy} if self.openai_llm.proxy else {} headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openai_api_key}"} data = {"input": text, "model": model} url = "https://api.openai.com/v1/embeddings" @@ -83,5 +84,5 @@ async def oas3_openai_text_to_embedding(text, model="text-embedding-ada-002", op if not text: return "" if not openai_api_key: - openai_api_key = CONFIG.OPENAI_API_KEY + openai_api_key = config.get_openai_llm().api_key return await OpenAIText2Embedding(openai_api_key).text_2_embedding(text, model=model) diff --git a/metagpt/tools/sd_engine.py b/metagpt/tools/sd_engine.py deleted file mode 100644 index c56b335ca..000000000 --- a/metagpt/tools/sd_engine.py +++ /dev/null @@ -1,133 +0,0 @@ -# -*- coding: utf-8 -*- -# @Date : 2023/7/19 16:28 -# @Author : stellahong (stellahong@deepwisdom.ai) -# @Desc : -import asyncio -import base64 -import io -import json -from os.path import join -from typing import List - -from aiohttp import ClientSession -from PIL import Image, PngImagePlugin - -from metagpt.config import CONFIG -from metagpt.const import SD_OUTPUT_FILE_REPO -from metagpt.logs import logger - -payload = { - "prompt": "", - "negative_prompt": "(easynegative:0.8),black, dark,Low resolution", - "override_settings": {"sd_model_checkpoint": "galaxytimemachinesGTM_photoV20"}, - "seed": -1, - "batch_size": 1, - "n_iter": 1, - "steps": 20, - "cfg_scale": 7, - "width": 512, - "height": 768, - "restore_faces": False, - "tiling": False, - "do_not_save_samples": False, - "do_not_save_grid": False, - "enable_hr": False, - "hr_scale": 2, - "hr_upscaler": "Latent", - "hr_second_pass_steps": 0, - "hr_resize_x": 0, - "hr_resize_y": 0, - "hr_upscale_to_x": 0, - "hr_upscale_to_y": 0, - "truncate_x": 0, - "truncate_y": 0, - "applied_old_hires_behavior_to": None, - "eta": None, - "sampler_index": "DPM++ SDE Karras", - "alwayson_scripts": {}, -} - -default_negative_prompt = "(easynegative:0.8),black, dark,Low resolution" - - -class SDEngine: - def __init__(self): - # Initialize the SDEngine with configuration - self.sd_url = CONFIG.get("SD_URL") - self.sd_t2i_url = f"{self.sd_url}{CONFIG.get('SD_T2I_API')}" - # Define default payload settings for SD API - self.payload = payload - logger.info(self.sd_t2i_url) - - def construct_payload( - self, - prompt, - negtive_prompt=default_negative_prompt, - width=512, - height=512, - sd_model="galaxytimemachinesGTM_photoV20", - ): - # Configure the payload with provided inputs - self.payload["prompt"] = prompt - self.payload["negtive_prompt"] = negtive_prompt - self.payload["width"] = width - self.payload["height"] = height - self.payload["override_settings"]["sd_model_checkpoint"] = sd_model - logger.info(f"call sd payload is {self.payload}") - return self.payload - - def _save(self, imgs, save_name=""): - save_dir = CONFIG.path / SD_OUTPUT_FILE_REPO - if not save_dir.exists(): - save_dir.mkdir(parents=True, exist_ok=True) - batch_decode_base64_to_image(imgs, str(save_dir), save_name=save_name) - - async def run_t2i(self, prompts: List): - # Asynchronously run the SD API for multiple prompts - session = ClientSession() - for payload_idx, payload in enumerate(prompts): - results = await self.run(url=self.sd_t2i_url, payload=payload, session=session) - self._save(results, save_name=f"output_{payload_idx}") - await session.close() - - async def run(self, url, payload, session): - # Perform the HTTP POST request to the SD API - async with session.post(url, json=payload, timeout=600) as rsp: - data = await rsp.read() - - rsp_json = json.loads(data) - imgs = rsp_json["images"] - logger.info(f"callback rsp json is {rsp_json.keys()}") - return imgs - - async def run_i2i(self): - # todo: 添加图生图接口调用 - raise NotImplementedError - - async def run_sam(self): - # todo:添加SAM接口调用 - raise NotImplementedError - - -def decode_base64_to_image(img, save_name): - image = Image.open(io.BytesIO(base64.b64decode(img.split(",", 1)[0]))) - pnginfo = PngImagePlugin.PngInfo() - logger.info(save_name) - image.save(f"{save_name}.png", pnginfo=pnginfo) - return pnginfo, image - - -def batch_decode_base64_to_image(imgs, save_dir="", save_name=""): - for idx, _img in enumerate(imgs): - save_name = join(save_dir, save_name) - decode_base64_to_image(_img, save_name=save_name) - - -if __name__ == "__main__": - engine = SDEngine() - prompt = "pixel style, game design, a game interface should be minimalistic and intuitive with the score and high score displayed at the top. The snake and its food should be easily distinguishable. The game should have a simple color scheme, with a contrasting color for the snake and its food. Complete interface boundary" - - engine.construct_payload(prompt) - - event_loop = asyncio.get_event_loop() - event_loop.run_until_complete(engine.run_t2i(prompt)) diff --git a/metagpt/tools/search_engine_ddg.py b/metagpt/tools/search_engine_ddg.py index 57bc61b82..3d004a4ee 100644 --- a/metagpt/tools/search_engine_ddg.py +++ b/metagpt/tools/search_engine_ddg.py @@ -7,6 +7,8 @@ import json from concurrent import futures from typing import Literal, overload +from metagpt.config2 import config + try: from duckduckgo_search import DDGS except ImportError: @@ -15,8 +17,6 @@ except ImportError: "You can install it by running the command: `pip install -e.[search-ddg]`" ) -from metagpt.config import CONFIG - class DDGAPIWrapper: """Wrapper around duckduckgo_search API. @@ -31,8 +31,8 @@ class DDGAPIWrapper: executor: futures.Executor | None = None, ): kwargs = {} - if CONFIG.global_proxy: - kwargs["proxies"] = CONFIG.global_proxy + if config.proxy: + kwargs["proxies"] = config.proxy self.loop = loop self.executor = executor self.ddgs = DDGS(**kwargs) diff --git a/metagpt/tools/search_engine_googleapi.py b/metagpt/tools/search_engine_googleapi.py index 8aca3aee2..65e1af109 100644 --- a/metagpt/tools/search_engine_googleapi.py +++ b/metagpt/tools/search_engine_googleapi.py @@ -11,7 +11,7 @@ from urllib.parse import urlparse import httplib2 from pydantic import BaseModel, ConfigDict, Field, field_validator -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.logs import logger try: @@ -35,7 +35,7 @@ class GoogleAPIWrapper(BaseModel): @field_validator("google_api_key", mode="before") @classmethod def check_google_api_key(cls, val: str): - val = val or CONFIG.google_api_key + val = val or config.search["google"].api_key if not val: raise ValueError( "To use, make sure you provide the google_api_key when constructing an object. Alternatively, " @@ -47,7 +47,7 @@ class GoogleAPIWrapper(BaseModel): @field_validator("google_cse_id", mode="before") @classmethod def check_google_cse_id(cls, val: str): - val = val or CONFIG.google_cse_id + val = val or config.search["google"].cse_id if not val: raise ValueError( "To use, make sure you provide the google_cse_id when constructing an object. Alternatively, " @@ -59,8 +59,8 @@ class GoogleAPIWrapper(BaseModel): @property def google_api_client(self): build_kwargs = {"developerKey": self.google_api_key} - if CONFIG.global_proxy: - parse_result = urlparse(CONFIG.global_proxy) + if config.proxy: + parse_result = urlparse(config.proxy) proxy_type = parse_result.scheme if proxy_type == "https": proxy_type = "http" diff --git a/metagpt/tools/search_engine_serpapi.py b/metagpt/tools/search_engine_serpapi.py index 9d2d20af6..2d21aa85c 100644 --- a/metagpt/tools/search_engine_serpapi.py +++ b/metagpt/tools/search_engine_serpapi.py @@ -10,7 +10,7 @@ from typing import Any, Dict, Optional, Tuple import aiohttp from pydantic import BaseModel, ConfigDict, Field, field_validator -from metagpt.config import CONFIG +from metagpt.config2 import config class SerpAPIWrapper(BaseModel): @@ -32,7 +32,7 @@ class SerpAPIWrapper(BaseModel): @field_validator("serpapi_api_key", mode="before") @classmethod def check_serpapi_api_key(cls, val: str): - val = val or CONFIG.serpapi_api_key + val = val or config.search["serpapi"].api_key if not val: raise ValueError( "To use, make sure you provide the serpapi_api_key when constructing an object. Alternatively, " diff --git a/metagpt/tools/search_engine_serper.py b/metagpt/tools/search_engine_serper.py index 3dc1d3591..d67148e14 100644 --- a/metagpt/tools/search_engine_serper.py +++ b/metagpt/tools/search_engine_serper.py @@ -11,7 +11,7 @@ from typing import Any, Dict, Optional, Tuple import aiohttp from pydantic import BaseModel, ConfigDict, Field, field_validator -from metagpt.config import CONFIG +from metagpt.config2 import config class SerperWrapper(BaseModel): @@ -25,7 +25,7 @@ class SerperWrapper(BaseModel): @field_validator("serper_api_key", mode="before") @classmethod def check_serper_api_key(cls, val: str): - val = val or CONFIG.serper_api_key + val = val or config.search["serper"].api_key if not val: raise ValueError( "To use, make sure you provide the serper_api_key when constructing an object. Alternatively, " diff --git a/metagpt/tools/web_browser_engine.py b/metagpt/tools/web_browser_engine.py index abd84cc8d..3493a5398 100644 --- a/metagpt/tools/web_browser_engine.py +++ b/metagpt/tools/web_browser_engine.py @@ -8,7 +8,6 @@ from __future__ import annotations import importlib from typing import Any, Callable, Coroutine, overload -from metagpt.config import CONFIG from metagpt.tools import WebBrowserEngineType from metagpt.utils.parse_html import WebPage @@ -19,7 +18,6 @@ class WebBrowserEngine: engine: WebBrowserEngineType | None = None, run_func: Callable[..., Coroutine[Any, Any, WebPage | list[WebPage]]] | None = None, ): - engine = engine or CONFIG.web_browser_engine if engine is None: raise NotImplementedError diff --git a/metagpt/tools/web_browser_engine_playwright.py b/metagpt/tools/web_browser_engine_playwright.py index a45f6a12e..00f2c6bab 100644 --- a/metagpt/tools/web_browser_engine_playwright.py +++ b/metagpt/tools/web_browser_engine_playwright.py @@ -12,7 +12,7 @@ from typing import Literal from playwright.async_api import async_playwright -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.logs import logger from metagpt.utils.parse_html import WebPage @@ -33,13 +33,13 @@ class PlaywrightWrapper: **kwargs, ) -> None: if browser_type is None: - browser_type = CONFIG.playwright_browser_type + browser_type = config.browser["playwright"].driver self.browser_type = browser_type launch_kwargs = launch_kwargs or {} - if CONFIG.global_proxy and "proxy" not in launch_kwargs: + if config.proxy and "proxy" not in launch_kwargs: args = launch_kwargs.get("args", []) if not any(str.startswith(i, "--proxy-server=") for i in args): - launch_kwargs["proxy"] = {"server": CONFIG.global_proxy} + launch_kwargs["proxy"] = {"server": config.proxy} self.launch_kwargs = launch_kwargs context_kwargs = {} if "ignore_https_errors" in kwargs: @@ -79,8 +79,8 @@ class PlaywrightWrapper: executable_path = Path(browser_type.executable_path) if not executable_path.exists() and "executable_path" not in self.launch_kwargs: kwargs = {} - if CONFIG.global_proxy: - kwargs["env"] = {"ALL_PROXY": CONFIG.global_proxy} + if config.proxy: + kwargs["env"] = {"ALL_PROXY": config.proxy} await _install_browsers(self.browser_type, **kwargs) if self._has_run_precheck: diff --git a/metagpt/tools/web_browser_engine_selenium.py b/metagpt/tools/web_browser_engine_selenium.py index 70b651935..18e5db974 100644 --- a/metagpt/tools/web_browser_engine_selenium.py +++ b/metagpt/tools/web_browser_engine_selenium.py @@ -17,7 +17,7 @@ from selenium.webdriver.support.wait import WebDriverWait from webdriver_manager.core.download_manager import WDMDownloadManager from webdriver_manager.core.http import WDMHttpClient -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.utils.parse_html import WebPage @@ -41,12 +41,10 @@ class SeleniumWrapper: loop: asyncio.AbstractEventLoop | None = None, executor: futures.Executor | None = None, ) -> None: - if browser_type is None: - browser_type = CONFIG.selenium_browser_type self.browser_type = browser_type launch_kwargs = launch_kwargs or {} - if CONFIG.global_proxy and "proxy-server" not in launch_kwargs: - launch_kwargs["proxy-server"] = CONFIG.global_proxy + if config.proxy and "proxy-server" not in launch_kwargs: + launch_kwargs["proxy-server"] = config.proxy self.executable_path = launch_kwargs.pop("executable_path", None) self.launch_args = [f"--{k}={v}" for k, v in launch_kwargs.items()] @@ -97,8 +95,8 @@ _webdriver_manager_types = { class WDMHttpProxyClient(WDMHttpClient): def get(self, url, **kwargs): - if "proxies" not in kwargs and CONFIG.global_proxy: - kwargs["proxies"] = {"all_proxy": CONFIG.global_proxy} + if "proxies" not in kwargs and config.proxy: + kwargs["proxies"] = {"all_proxy": config.proxy} return super().get(url, **kwargs) diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py index 235b4979c..893d05be0 100644 --- a/metagpt/utils/mermaid.py +++ b/metagpt/utils/mermaid.py @@ -12,7 +12,7 @@ from pathlib import Path import aiofiles -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.logs import logger from metagpt.utils.common import check_cmd_exists @@ -35,9 +35,9 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, await f.write(mermaid_code) # tmp.write_text(mermaid_code, encoding="utf-8") - engine = CONFIG.mermaid_engine.lower() + engine = config.mermaid["default"].engine if engine == "nodejs": - if check_cmd_exists(CONFIG.mmdc) != 0: + if check_cmd_exists(config.mmdc) != 0: logger.warning( "RUN `npm install -g @mermaid-js/mermaid-cli` to install mmdc," "or consider changing MERMAID_ENGINE to `playwright`, `pyppeteer`, or `ink`." @@ -49,11 +49,11 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, # Call the `mmdc` command to convert the Mermaid code to a PNG logger.info(f"Generating {output_file}..") - if CONFIG.puppeteer_config: + if config.puppeteer_config: commands = [ - CONFIG.mmdc, + config.mmdc, "-p", - CONFIG.puppeteer_config, + config.puppeteer_config, "-i", str(tmp), "-o", @@ -64,7 +64,7 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, str(height), ] else: - commands = [CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)] + commands = [config.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)] process = await asyncio.create_subprocess_shell( " ".join(commands), stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) diff --git a/metagpt/utils/mmdc_pyppeteer.py b/metagpt/utils/mmdc_pyppeteer.py index 7125cafc5..d80098b7d 100644 --- a/metagpt/utils/mmdc_pyppeteer.py +++ b/metagpt/utils/mmdc_pyppeteer.py @@ -10,7 +10,7 @@ from urllib.parse import urljoin from pyppeteer import launch -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.logs import logger @@ -30,10 +30,10 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, suffixes = ["png", "svg", "pdf"] __dirname = os.path.dirname(os.path.abspath(__file__)) - if CONFIG.pyppeteer_executable_path: + if config.pyppeteer_executable_path: browser = await launch( headless=True, - executablePath=CONFIG.pyppeteer_executable_path, + executablePath=config.pyppeteer_executable_path, args=["--disable-extensions", "--no-sandbox"], ) else: diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index a96c3dce0..ec2da53f8 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -9,7 +9,7 @@ from typing import Callable, Union import regex as re from tenacity import RetryCallState, retry, stop_after_attempt, wait_fixed -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.logs import logger from metagpt.utils.custom_decoder import CustomDecoder @@ -152,7 +152,7 @@ def repair_llm_raw_output(output: str, req_keys: list[str], repair_type: RepairT target: { xxx } output: { xxx }] """ - if not CONFIG.repair_llm_output: + if not config.repair_llm_output: return output # do the repairation usually for non-openai models @@ -231,7 +231,7 @@ def run_after_exp_and_passon_next_retry(logger: "loguru.Logger") -> Callable[["R func_param_output = retry_state.kwargs.get("output", "") exp_str = str(retry_state.outcome.exception()) - fix_str = "try to fix it, " if CONFIG.repair_llm_output else "" + fix_str = "try to fix it, " if config.repair_llm_output else "" logger.warning( f"parse json from content inside [CONTENT][/CONTENT] failed at retry " f"{retry_state.attempt_number}, {fix_str}exp: {exp_str}" @@ -244,7 +244,7 @@ def run_after_exp_and_passon_next_retry(logger: "loguru.Logger") -> Callable[["R @retry( - stop=stop_after_attempt(3 if CONFIG.repair_llm_output else 0), + stop=stop_after_attempt(3 if config.repair_llm_output else 0), wait=wait_fixed(1), after=run_after_exp_and_passon_next_retry(logger), ) diff --git a/tests/metagpt/actions/test_rebuild_class_view.py b/tests/metagpt/actions/test_rebuild_class_view.py index 207ba4be1..cc23cc8dc 100644 --- a/tests/metagpt/actions/test_rebuild_class_view.py +++ b/tests/metagpt/actions/test_rebuild_class_view.py @@ -11,7 +11,6 @@ from pathlib import Path import pytest from metagpt.actions.rebuild_class_view import RebuildClassView -from metagpt.config import CONFIG from metagpt.const import GRAPH_REPO_FILE_REPO from metagpt.llm import LLM @@ -22,7 +21,7 @@ async def test_rebuild(): name="RedBean", context=str(Path(__file__).parent.parent.parent.parent / "metagpt"), llm=LLM() ) await action.run() - graph_file_repo = CONFIG.git_repo.new_file_repository(relative_path=GRAPH_REPO_FILE_REPO) + graph_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=GRAPH_REPO_FILE_REPO) assert graph_file_repo.changed_files diff --git a/tests/metagpt/actions/test_rebuild_sequence_view.py b/tests/metagpt/actions/test_rebuild_sequence_view.py index 939412fe7..62f64b666 100644 --- a/tests/metagpt/actions/test_rebuild_sequence_view.py +++ b/tests/metagpt/actions/test_rebuild_sequence_view.py @@ -10,7 +10,6 @@ from pathlib import Path import pytest from metagpt.actions.rebuild_sequence_view import RebuildSequenceView -from metagpt.config import CONFIG from metagpt.const import GRAPH_REPO_FILE_REPO from metagpt.llm import LLM from metagpt.utils.common import aread @@ -22,20 +21,20 @@ from metagpt.utils.git_repository import ChangeType async def test_rebuild(): # Mock data = await aread(filename=Path(__file__).parent / "../../data/graph_db/networkx.json") - graph_db_filename = Path(CONFIG.git_repo.workdir.name).with_suffix(".json") + graph_db_filename = Path(CONTEXT.git_repo.workdir.name).with_suffix(".json") await FileRepository.save_file( filename=str(graph_db_filename), relative_path=GRAPH_REPO_FILE_REPO, content=data, ) - CONFIG.git_repo.add_change({f"{GRAPH_REPO_FILE_REPO}/{graph_db_filename}": ChangeType.UNTRACTED}) - CONFIG.git_repo.commit("commit1") + CONTEXT.git_repo.add_change({f"{GRAPH_REPO_FILE_REPO}/{graph_db_filename}": ChangeType.UNTRACTED}) + CONTEXT.git_repo.commit("commit1") action = RebuildSequenceView( name="RedBean", context=str(Path(__file__).parent.parent.parent.parent / "metagpt"), llm=LLM() ) await action.run() - graph_file_repo = CONFIG.git_repo.new_file_repository(relative_path=GRAPH_REPO_FILE_REPO) + graph_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=GRAPH_REPO_FILE_REPO) assert graph_file_repo.changed_files diff --git a/tests/metagpt/actions/test_summarize_code.py b/tests/metagpt/actions/test_summarize_code.py index 2f7b5c61d..081636a21 100644 --- a/tests/metagpt/actions/test_summarize_code.py +++ b/tests/metagpt/actions/test_summarize_code.py @@ -9,7 +9,6 @@ import pytest from metagpt.actions.summarize_code import SummarizeCode -from metagpt.config import CONFIG from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO from metagpt.context import CONTEXT from metagpt.logs import logger @@ -181,12 +180,12 @@ async def test_summarize_code(): CONTEXT.src_workspace = CONTEXT.git_repo.workdir / "src" await CONTEXT.file_repo.save_file(filename="1.json", relative_path=SYSTEM_DESIGN_FILE_REPO, content=DESIGN_CONTENT) await CONTEXT.file_repo.save_file(filename="1.json", relative_path=TASK_FILE_REPO, content=TASK_CONTENT) - await CONTEXT.file_repo.save_file(filename="food.py", relative_path=CONFIG.src_workspace, content=FOOD_PY) - await CONTEXT.file_repo.save_file(filename="game.py", relative_path=CONFIG.src_workspace, content=GAME_PY) - await CONTEXT.file_repo.save_file(filename="main.py", relative_path=CONFIG.src_workspace, content=MAIN_PY) - await CONTEXT.file_repo.save_file(filename="snake.py", relative_path=CONFIG.src_workspace, content=SNAKE_PY) + await CONTEXT.file_repo.save_file(filename="food.py", relative_path=CONTEXT.src_workspace, content=FOOD_PY) + await CONTEXT.file_repo.save_file(filename="game.py", relative_path=CONTEXT.src_workspace, content=GAME_PY) + await CONTEXT.file_repo.save_file(filename="main.py", relative_path=CONTEXT.src_workspace, content=MAIN_PY) + await CONTEXT.file_repo.save_file(filename="snake.py", relative_path=CONTEXT.src_workspace, content=SNAKE_PY) - src_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=CONFIG.src_workspace) + src_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=CONTEXT.src_workspace) all_files = src_file_repo.all_files ctx = CodeSummarizeContext(design_filename="1.json", task_filename="1.json", codes_filenames=all_files) action = SummarizeCode(context=ctx) diff --git a/tests/metagpt/learn/test_skill_loader.py b/tests/metagpt/learn/test_skill_loader.py index 529a490c8..45697160b 100644 --- a/tests/metagpt/learn/test_skill_loader.py +++ b/tests/metagpt/learn/test_skill_loader.py @@ -10,13 +10,13 @@ from pathlib import Path import pytest -from metagpt.config import CONFIG +from metagpt.context import CONTEXT from metagpt.learn.skill_loader import SkillsDeclaration @pytest.mark.asyncio async def test_suite(): - CONFIG.agent_skills = [ + CONTEXT.kwargs.agent_skills = [ {"id": 1, "name": "text_to_speech", "type": "builtin", "config": {}, "enabled": True}, {"id": 2, "name": "text_to_image", "type": "builtin", "config": {}, "enabled": True}, {"id": 3, "name": "ai_call", "type": "builtin", "config": {}, "enabled": True}, diff --git a/tests/metagpt/learn/test_text_to_embedding.py b/tests/metagpt/learn/test_text_to_embedding.py index cbd1bbbbc..cbc8ddf18 100644 --- a/tests/metagpt/learn/test_text_to_embedding.py +++ b/tests/metagpt/learn/test_text_to_embedding.py @@ -9,14 +9,14 @@ import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.learn.text_to_embedding import text_to_embedding @pytest.mark.asyncio async def test_text_to_embedding(): # Prerequisites - assert CONFIG.OPENAI_API_KEY + assert config.get_openai_llm() v = await text_to_embedding(text="Panda emoji") assert len(v.data) > 0 diff --git a/tests/metagpt/tools/test_azure_tts.py b/tests/metagpt/tools/test_azure_tts.py index dca71544e..a33925a5c 100644 --- a/tests/metagpt/tools/test_azure_tts.py +++ b/tests/metagpt/tools/test_azure_tts.py @@ -12,6 +12,7 @@ import pytest from azure.cognitiveservices.speech import ResultReason from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.tools.azure_tts import AzureTTS @@ -32,7 +33,7 @@ async def test_azure_tts(): “Writing a binary file in Python is similar to writing a regular text file, but you'll work with bytes instead of strings.” """ - path = CONFIG.path / "tts" + path = config.workspace.path / "tts" path.mkdir(exist_ok=True, parents=True) filename = path / "girl.wav" filename.unlink(missing_ok=True) diff --git a/tests/metagpt/tools/test_metagpt_oas3_api_svc.py b/tests/metagpt/tools/test_metagpt_oas3_api_svc.py index 5f52b28cc..3cf5e515b 100644 --- a/tests/metagpt/tools/test_metagpt_oas3_api_svc.py +++ b/tests/metagpt/tools/test_metagpt_oas3_api_svc.py @@ -12,14 +12,14 @@ from pathlib import Path import pytest import requests -from metagpt.config import CONFIG +from metagpt.context import CONTEXT @pytest.mark.asyncio async def test_oas2_svc(): workdir = Path(__file__).parent.parent.parent.parent script_pathname = workdir / "metagpt/tools/metagpt_oas3_api_svc.py" - env = CONFIG.new_environ() + env = CONTEXT.new_environ() env["PYTHONPATH"] = str(workdir) + ":" + env.get("PYTHONPATH", "") process = subprocess.Popen(["python", str(script_pathname)], cwd=str(workdir), env=env) await asyncio.sleep(5) diff --git a/tests/metagpt/tools/test_metagpt_text_to_image.py b/tests/metagpt/tools/test_metagpt_text_to_image.py index b765119f0..0dcad20d2 100644 --- a/tests/metagpt/tools/test_metagpt_text_to_image.py +++ b/tests/metagpt/tools/test_metagpt_text_to_image.py @@ -10,7 +10,7 @@ from unittest.mock import AsyncMock import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.tools.metagpt_text_to_image import oas3_metagpt_text_to_image @@ -24,7 +24,7 @@ async def test_draw(mocker): mock_post.return_value.__aenter__.return_value = mock_response # Prerequisites - assert CONFIG.METAGPT_TEXT_TO_IMAGE_MODEL_URL + assert config.METAGPT_TEXT_TO_IMAGE_MODEL_URL binary_data = await oas3_metagpt_text_to_image("Panda emoji") assert binary_data diff --git a/tests/metagpt/tools/test_moderation.py b/tests/metagpt/tools/test_moderation.py index e1226484a..8dc9e9d5e 100644 --- a/tests/metagpt/tools/test_moderation.py +++ b/tests/metagpt/tools/test_moderation.py @@ -8,7 +8,7 @@ import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.llm import LLM from metagpt.tools.moderation import Moderation @@ -24,9 +24,7 @@ from metagpt.tools.moderation import Moderation ) async def test_amoderation(content): # Prerequisites - assert CONFIG.OPENAI_API_KEY and CONFIG.OPENAI_API_KEY != "YOUR_API_KEY" - assert not CONFIG.OPENAI_API_TYPE - assert CONFIG.OPENAI_API_MODEL + assert config.get_openai_llm() moderation = Moderation(LLM()) results = await moderation.amoderation(content=content) diff --git a/tests/metagpt/tools/test_openai_text_to_embedding.py b/tests/metagpt/tools/test_openai_text_to_embedding.py index 086c9d45b..58c38d480 100644 --- a/tests/metagpt/tools/test_openai_text_to_embedding.py +++ b/tests/metagpt/tools/test_openai_text_to_embedding.py @@ -8,16 +8,14 @@ import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.tools.openai_text_to_embedding import oas3_openai_text_to_embedding @pytest.mark.asyncio async def test_embedding(): # Prerequisites - assert CONFIG.OPENAI_API_KEY and CONFIG.OPENAI_API_KEY != "YOUR_API_KEY" - assert not CONFIG.OPENAI_API_TYPE - assert CONFIG.OPENAI_API_MODEL + assert config.get_openai_llm() result = await oas3_openai_text_to_embedding("Panda emoji") assert result diff --git a/tests/metagpt/tools/test_openai_text_to_image.py b/tests/metagpt/tools/test_openai_text_to_image.py index e560da798..1a1c9540f 100644 --- a/tests/metagpt/tools/test_openai_text_to_image.py +++ b/tests/metagpt/tools/test_openai_text_to_image.py @@ -8,7 +8,7 @@ import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.tools.openai_text_to_image import ( OpenAIText2Image, oas3_openai_text_to_image, @@ -18,9 +18,7 @@ from metagpt.tools.openai_text_to_image import ( @pytest.mark.asyncio async def test_draw(): # Prerequisites - assert CONFIG.OPENAI_API_KEY and CONFIG.OPENAI_API_KEY != "YOUR_API_KEY" - assert not CONFIG.OPENAI_API_TYPE - assert CONFIG.OPENAI_API_MODEL + assert config.get_openai_llm() binary_data = await oas3_openai_text_to_image("Panda emoji") assert binary_data diff --git a/tests/metagpt/tools/test_openapi_v3_hello.py b/tests/metagpt/tools/test_openapi_v3_hello.py index 5726cf8e0..daa5d21c6 100644 --- a/tests/metagpt/tools/test_openapi_v3_hello.py +++ b/tests/metagpt/tools/test_openapi_v3_hello.py @@ -12,14 +12,14 @@ from pathlib import Path import pytest import requests -from metagpt.config import CONFIG +from metagpt.context import CONTEXT @pytest.mark.asyncio async def test_hello(): workdir = Path(__file__).parent.parent.parent.parent script_pathname = workdir / "metagpt/tools/openapi_v3_hello.py" - env = CONFIG.new_environ() + env = CONTEXT.new_environ() env["PYTHONPATH"] = str(workdir) + ":" + env.get("PYTHONPATH", "") process = subprocess.Popen(["python", str(script_pathname)], cwd=workdir, env=env) await asyncio.sleep(5) diff --git a/tests/metagpt/tools/test_sd_tool.py b/tests/metagpt/tools/test_sd_tool.py deleted file mode 100644 index 52b970229..000000000 --- a/tests/metagpt/tools/test_sd_tool.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- coding: utf-8 -*- -# @Date : 2023/7/22 02:40 -# @Author : stellahong (stellahong@deepwisdom.ai) -# -import os - -from metagpt.config import CONFIG -from metagpt.tools.sd_engine import SDEngine - - -def test_sd_engine_init(): - sd_engine = SDEngine() - assert sd_engine.payload["seed"] == -1 - - -def test_sd_engine_generate_prompt(): - sd_engine = SDEngine() - sd_engine.construct_payload(prompt="test") - assert sd_engine.payload["prompt"] == "test" - - -async def test_sd_engine_run_t2i(): - sd_engine = SDEngine() - await sd_engine.run_t2i(prompts=["test"]) - img_path = CONFIG.path / "resources" / "SD_Output" / "output_0.png" - assert os.path.exists(img_path) diff --git a/tests/metagpt/tools/test_search_engine.py b/tests/metagpt/tools/test_search_engine.py index dab466af7..411929f64 100644 --- a/tests/metagpt/tools/test_search_engine.py +++ b/tests/metagpt/tools/test_search_engine.py @@ -14,7 +14,7 @@ from typing import Callable import pytest import tests.data.search -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.logs import logger from metagpt.tools import SearchEngineType from metagpt.tools.search_engine import SearchEngine @@ -50,13 +50,12 @@ async def test_search_engine(search_engine_type, run_func: Callable, max_results # Prerequisites cache_json_path = None if search_engine_type is SearchEngineType.SERPAPI_GOOGLE: - assert CONFIG.SERPAPI_API_KEY and CONFIG.SERPAPI_API_KEY != "YOUR_API_KEY" + assert config.search["serpapi"] cache_json_path = search_cache_path / f"serpapi-metagpt-{max_results}.json" elif search_engine_type is SearchEngineType.DIRECT_GOOGLE: - assert CONFIG.GOOGLE_API_KEY and CONFIG.GOOGLE_API_KEY != "YOUR_API_KEY" - assert CONFIG.GOOGLE_CSE_ID and CONFIG.GOOGLE_CSE_ID != "YOUR_CSE_ID" + assert config.search["google"] elif search_engine_type is SearchEngineType.SERPER_GOOGLE: - assert CONFIG.SERPER_API_KEY and CONFIG.SERPER_API_KEY != "YOUR_API_KEY" + assert config.search["serper"] cache_json_path = search_cache_path / f"serper-metagpt-{max_results}.json" if cache_json_path: diff --git a/tests/metagpt/tools/test_ut_writer.py b/tests/metagpt/tools/test_ut_writer.py index eac28d56f..29b6572c2 100644 --- a/tests/metagpt/tools/test_ut_writer.py +++ b/tests/metagpt/tools/test_ut_writer.py @@ -9,7 +9,7 @@ from pathlib import Path import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.const import API_QUESTIONS_PATH, UT_PY_PATH from metagpt.tools.ut_writer import YFT_PROMPT_PREFIX, UTGenerator @@ -20,9 +20,7 @@ class TestUTWriter: # Prerequisites swagger_file = Path(__file__).parent / "../../data/ut_writer/yft_swaggerApi.json" assert swagger_file.exists() - assert CONFIG.OPENAI_API_KEY and CONFIG.OPENAI_API_KEY != "YOUR_API_KEY" - assert not CONFIG.OPENAI_API_TYPE - assert CONFIG.OPENAI_API_MODEL + assert config.get_openai_llm() tags = ["测试", "作业"] # 这里在文件中手动加入了两个测试标签的API diff --git a/tests/metagpt/utils/test_mermaid.py b/tests/metagpt/utils/test_mermaid.py index 486742524..6345e9c51 100644 --- a/tests/metagpt/utils/test_mermaid.py +++ b/tests/metagpt/utils/test_mermaid.py @@ -9,6 +9,7 @@ import pytest from metagpt.config import CONFIG +from metagpt.context import CONTEXT from metagpt.utils.common import check_cmd_exists from metagpt.utils.mermaid import MMC1, mermaid_to_file @@ -22,7 +23,7 @@ async def test_mermaid(engine): assert check_cmd_exists("npm") == 0 CONFIG.mermaid_engine = engine - save_to = CONFIG.git_repo.workdir / f"{CONFIG.mermaid_engine}/1" + save_to = CONTEXT.git_repo.workdir / f"{CONFIG.mermaid_engine}/1" await mermaid_to_file(MMC1, save_to) # ink does not support pdf diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py index 1970c6443..bd6169d71 100644 --- a/tests/metagpt/utils/test_repair_llm_raw_output.py +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -2,13 +2,13 @@ # -*- coding: utf-8 -*- # @Desc : unittest of repair_llm_raw_output -from metagpt.config import CONFIG +from metagpt.config2 import config """ CONFIG.repair_llm_output should be True before retry_parse_json_text imported. so we move `from ... impot ...` into each `test_xx` to avoid `Module level import not at top of file` format warning. """ -CONFIG.repair_llm_output = True +config.repair_llm_output = True def test_repair_case_sensitivity(): From e743ef1a12622de4d4016929495a344f4787ba1f Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 20:21:06 +0800 Subject: [PATCH 41/50] fix bug --- metagpt/context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/context.py b/metagpt/context.py index 35892f3f3..1c351ef22 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -173,7 +173,7 @@ class ContextMixin(BaseModel): @property def llm(self) -> BaseLLM: """Role llm: role llm > context llm""" - print(f"class:{self.__class__.__name__}({self.name}), llm: {self._llm}, llm_config: {self._llm_config}") + # print(f"class:{self.__class__.__name__}({self.name}), llm: {self._llm}, llm_config: {self._llm_config}") if self._llm_config and not self._llm: self._llm = self.context.llm_with_cost_manager_from_llm_config(self._llm_config) return self._llm or self.context.llm() From 2e4ceb70b97fd38e5eeade82339cb9677968fa34 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 20:34:42 +0800 Subject: [PATCH 42/50] use config --- metagpt/actions/design_api.py | 9 ++-- metagpt/actions/research.py | 5 +-- metagpt/config2.py | 6 +++ metagpt/learn/text_to_speech.py | 9 ++-- metagpt/tools/azure_tts.py | 9 +--- metagpt/tools/iflytek_tts.py | 15 ++----- metagpt/tools/search_engine.py | 2 - metagpt/utils/mermaid.py | 3 +- tests/metagpt/learn/test_text_to_speech.py | 41 +++++++++++-------- tests/metagpt/tools/test_azure_tts.py | 5 +-- tests/metagpt/tools/test_iflytek_tts.py | 14 +++---- .../test_web_browser_engine_playwright.py | 8 ++-- .../tools/test_web_browser_engine_selenium.py | 8 ++-- tests/metagpt/utils/test_mermaid.py | 6 +-- 14 files changed, 64 insertions(+), 76 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 3e978f823..5f973bb60 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -110,7 +110,7 @@ class WriteDesign(Action): if not data_api_design: return pathname = self.git_repo.workdir / DATA_API_DESIGN_FILE_REPO / Path(design_doc.filename).with_suffix("") - await WriteDesign._save_mermaid_file(data_api_design, pathname) + await self._save_mermaid_file(data_api_design, pathname) logger.info(f"Save class view to {str(pathname)}") async def _save_seq_flow(self, design_doc): @@ -119,13 +119,12 @@ class WriteDesign(Action): if not seq_flow: return pathname = self.git_repo.workdir / Path(SEQ_FLOW_FILE_REPO) / Path(design_doc.filename).with_suffix("") - await WriteDesign._save_mermaid_file(seq_flow, pathname) + await self._save_mermaid_file(seq_flow, pathname) logger.info(f"Saving sequence flow to {str(pathname)}") async def _save_pdf(self, design_doc): await self.file_repo.save_as(doc=design_doc, with_suffix=".md", relative_path=SYSTEM_DESIGN_PDF_FILE_REPO) - @staticmethod - async def _save_mermaid_file(data: str, pathname: Path): + async def _save_mermaid_file(self, data: str, pathname: Path): pathname.parent.mkdir(parents=True, exist_ok=True) - await mermaid_to_file(data, pathname) + await mermaid_to_file(self.config.mermaid_engine, data, pathname) diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index a635714ef..0af49a1cf 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -8,7 +8,6 @@ from typing import Callable, Optional, Union from pydantic import Field, parse_obj_as from metagpt.actions import Action -from metagpt.config import CONFIG from metagpt.config2 import config from metagpt.logs import logger from metagpt.tools.search_engine import SearchEngine @@ -216,9 +215,7 @@ class WebBrowseAndSummarize(Action): for u, content in zip([url, *urls], contents): content = content.inner_text chunk_summaries = [] - for prompt in generate_prompt_chunk( - content, prompt_template, self.llm.model, system_text, CONFIG.max_tokens_rsp - ): + for prompt in generate_prompt_chunk(content, prompt_template, self.llm.model, system_text, 4096): logger.debug(prompt) summary = await self._aask(prompt, [system_text]) if summary == "Not relevant.": diff --git a/metagpt/config2.py b/metagpt/config2.py index 2a9611627..6345c1b8c 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -74,6 +74,12 @@ class Config(CLIParams, YamlModel): mmdc: str = "mmdc" puppeteer_config: str = "" pyppeteer_executable_path: str = "" + IFLYTEK_APP_ID: str = "" + IFLYTEK_APP_SECRET: str = "" + IFLYTEK_APP_KEY: str = "" + AZURE_TTS_SUBSCRIPTION_KEY: str = "" + AZURE_TTS_REGION: str = "" + mermaid_engine: str = "nodejs" @classmethod def default(cls): diff --git a/metagpt/learn/text_to_speech.py b/metagpt/learn/text_to_speech.py index f12e52b8e..8ffafbd0e 100644 --- a/metagpt/learn/text_to_speech.py +++ b/metagpt/learn/text_to_speech.py @@ -7,7 +7,6 @@ @Desc : Text-to-Speech skill, which provides text-to-speech functionality """ -from metagpt.config import CONFIG from metagpt.config2 import config from metagpt.const import BASE64_FORMAT from metagpt.tools.azure_tts import oas3_azsure_tts @@ -45,7 +44,7 @@ async def text_to_speech( """ - if (CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_REGION) or (subscription_key and region): + if subscription_key and region: audio_declaration = "data:audio/wav;base64," base64_data = await oas3_azsure_tts(text, lang, voice, style, role, subscription_key, region) s3 = S3(config.s3) @@ -53,14 +52,12 @@ async def text_to_speech( if url: return f"[{text}]({url})" return audio_declaration + base64_data if base64_data else base64_data - if (CONFIG.IFLYTEK_APP_ID and CONFIG.IFLYTEK_API_KEY and CONFIG.IFLYTEK_API_SECRET) or ( - iflytek_app_id and iflytek_api_key and iflytek_api_secret - ): + if iflytek_app_id and iflytek_api_key and iflytek_api_secret: audio_declaration = "data:audio/mp3;base64," base64_data = await oas3_iflytek_tts( text=text, app_id=iflytek_app_id, api_key=iflytek_api_key, api_secret=iflytek_api_secret ) - s3 = S3() + s3 = S3(config.s3) url = await s3.cache(data=base64_data, file_ext=".mp3", format=BASE64_FORMAT) if url: return f"[{text}]({url})" diff --git a/metagpt/tools/azure_tts.py b/metagpt/tools/azure_tts.py index f4f8aa0a2..2e0e2267c 100644 --- a/metagpt/tools/azure_tts.py +++ b/metagpt/tools/azure_tts.py @@ -13,7 +13,6 @@ from uuid import uuid4 import aiofiles from azure.cognitiveservices.speech import AudioConfig, SpeechConfig, SpeechSynthesizer -from metagpt.config import CONFIG from metagpt.logs import logger @@ -25,8 +24,8 @@ class AzureTTS: :param subscription_key: key is used to access your Azure AI service API, see: `https://portal.azure.com/` > `Resource Management` > `Keys and Endpoint` :param region: This is the location (or region) of your resource. You may need to use this field when making calls to this API. """ - self.subscription_key = subscription_key if subscription_key else CONFIG.AZURE_TTS_SUBSCRIPTION_KEY - self.region = region if region else CONFIG.AZURE_TTS_REGION + self.subscription_key = subscription_key + self.region = region # 参数参考:https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=tts#voice-styles-and-roles async def synthesize_speech(self, lang, voice, text, output_file): @@ -83,10 +82,6 @@ async def oas3_azsure_tts(text, lang="", voice="", style="", role="", subscripti role = "Girl" if not style: style = "affectionate" - if not subscription_key: - subscription_key = CONFIG.AZURE_TTS_SUBSCRIPTION_KEY - if not region: - region = CONFIG.AZURE_TTS_REGION xml_value = AzureTTS.role_style_text(role=role, style=style, text=text) tts = AzureTTS(subscription_key=subscription_key, region=region) diff --git a/metagpt/tools/iflytek_tts.py b/metagpt/tools/iflytek_tts.py index ad2395362..6ce48826b 100644 --- a/metagpt/tools/iflytek_tts.py +++ b/metagpt/tools/iflytek_tts.py @@ -23,7 +23,6 @@ import aiofiles import websockets as websockets from pydantic import BaseModel -from metagpt.config import CONFIG from metagpt.logs import logger @@ -56,9 +55,9 @@ class IFlyTekTTS(object): :param api_key: WebAPI argument, see: `https://console.xfyun.cn/services/tts` :param api_secret: WebAPI argument, see: `https://console.xfyun.cn/services/tts` """ - self.app_id = app_id or CONFIG.IFLYTEK_APP_ID - self.api_key = api_key or CONFIG.IFLYTEK_API_KEY - self.api_secret = api_secret or CONFIG.API_SECRET + self.app_id = app_id + self.api_key = api_key + self.api_secret = api_secret async def synthesize_speech(self, text, output_file: str, voice=DEFAULT_IFLYTEK_VOICE): url = self._create_url() @@ -127,14 +126,6 @@ async def oas3_iflytek_tts(text: str, voice: str = "", app_id: str = "", api_key :return: Returns the Base64-encoded .mp3 file data if successful, otherwise an empty string. """ - if not app_id: - app_id = CONFIG.IFLYTEK_APP_ID - if not api_key: - api_key = CONFIG.IFLYTEK_API_KEY - if not api_secret: - api_secret = CONFIG.IFLYTEK_API_SECRET - if not voice: - voice = CONFIG.IFLYTEK_VOICE or DEFAULT_IFLYTEK_VOICE filename = Path(__file__).parent / (uuid.uuid4().hex + ".mp3") try: diff --git a/metagpt/tools/search_engine.py b/metagpt/tools/search_engine.py index 64388a11f..fd237d537 100644 --- a/metagpt/tools/search_engine.py +++ b/metagpt/tools/search_engine.py @@ -10,7 +10,6 @@ from typing import Callable, Coroutine, Literal, Optional, Union, overload from semantic_kernel.skill_definition import sk_function -from metagpt.config import CONFIG from metagpt.tools import SearchEngineType @@ -46,7 +45,6 @@ class SearchEngine: engine: Optional[SearchEngineType] = None, run_func: Callable[[str, int, bool], Coroutine[None, None, Union[str, list[str]]]] = None, ): - engine = engine or CONFIG.search_engine if engine == SearchEngineType.SERPAPI_GOOGLE: module = "metagpt.tools.search_engine_serpapi" run_func = importlib.import_module(module).SerpAPIWrapper().run diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py index 893d05be0..3f6a2ef12 100644 --- a/metagpt/utils/mermaid.py +++ b/metagpt/utils/mermaid.py @@ -17,7 +17,7 @@ from metagpt.logs import logger from metagpt.utils.common import check_cmd_exists -async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int: +async def mermaid_to_file(engine, mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int: """suffix: png/svg/pdf :param mermaid_code: mermaid code @@ -35,7 +35,6 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, await f.write(mermaid_code) # tmp.write_text(mermaid_code, encoding="utf-8") - engine = config.mermaid["default"].engine if engine == "nodejs": if check_cmd_exists(config.mmdc) != 0: logger.warning( diff --git a/tests/metagpt/learn/test_text_to_speech.py b/tests/metagpt/learn/test_text_to_speech.py index aca08b9a2..41611171c 100644 --- a/tests/metagpt/learn/test_text_to_speech.py +++ b/tests/metagpt/learn/test_text_to_speech.py @@ -9,34 +9,43 @@ import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.learn.text_to_speech import text_to_speech @pytest.mark.asyncio async def test_text_to_speech(): # Prerequisites - assert CONFIG.IFLYTEK_APP_ID - assert CONFIG.IFLYTEK_API_KEY - assert CONFIG.IFLYTEK_API_SECRET - assert CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY" - assert CONFIG.AZURE_TTS_REGION + assert config.IFLYTEK_APP_ID + assert config.IFLYTEK_API_KEY + assert config.IFLYTEK_API_SECRET + assert config.AZURE_TTS_SUBSCRIPTION_KEY and config.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY" + assert config.AZURE_TTS_REGION + i = config.copy() # test azure - data = await text_to_speech("panda emoji") + data = await text_to_speech( + "panda emoji", + subscription_key=i.AZURE_TTS_SUBSCRIPTION_KEY, + region=i.AZURE_TTS_REGION, + iflytek_api_key=i.IFLYTEK_API_KEY, + iflytek_api_secret=i.IFLYTEK_API_SECRET, + iflytek_app_id=i.IFLYTEK_APP_ID, + ) assert "base64" in data or "http" in data # test iflytek ## Mock session env - old_options = CONFIG.options.copy() - new_options = old_options.copy() - new_options["AZURE_TTS_SUBSCRIPTION_KEY"] = "" - CONFIG.set_context(new_options) - try: - data = await text_to_speech("panda emoji") - assert "base64" in data or "http" in data - finally: - CONFIG.set_context(old_options) + i.AZURE_TTS_SUBSCRIPTION_KEY = "" + data = await text_to_speech( + "panda emoji", + subscription_key=i.AZURE_TTS_SUBSCRIPTION_KEY, + region=i.AZURE_TTS_REGION, + iflytek_api_key=i.IFLYTEK_API_KEY, + iflytek_api_secret=i.IFLYTEK_API_SECRET, + iflytek_app_id=i.IFLYTEK_APP_ID, + ) + assert "base64" in data or "http" in data if __name__ == "__main__": diff --git a/tests/metagpt/tools/test_azure_tts.py b/tests/metagpt/tools/test_azure_tts.py index a33925a5c..e856d3b27 100644 --- a/tests/metagpt/tools/test_azure_tts.py +++ b/tests/metagpt/tools/test_azure_tts.py @@ -11,7 +11,6 @@ import pytest from azure.cognitiveservices.speech import ResultReason -from metagpt.config import CONFIG from metagpt.config2 import config from metagpt.tools.azure_tts import AzureTTS @@ -19,8 +18,8 @@ from metagpt.tools.azure_tts import AzureTTS @pytest.mark.asyncio async def test_azure_tts(): # Prerequisites - assert CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY" - assert CONFIG.AZURE_TTS_REGION + assert config.AZURE_TTS_SUBSCRIPTION_KEY and config.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY" + assert config.AZURE_TTS_REGION azure_tts = AzureTTS(subscription_key="", region="") text = """ diff --git a/tests/metagpt/tools/test_iflytek_tts.py b/tests/metagpt/tools/test_iflytek_tts.py index 58d8a83ce..18af0a723 100644 --- a/tests/metagpt/tools/test_iflytek_tts.py +++ b/tests/metagpt/tools/test_iflytek_tts.py @@ -7,22 +7,22 @@ """ import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.tools.iflytek_tts import oas3_iflytek_tts @pytest.mark.asyncio async def test_tts(): # Prerequisites - assert CONFIG.IFLYTEK_APP_ID - assert CONFIG.IFLYTEK_API_KEY - assert CONFIG.IFLYTEK_API_SECRET + assert config.IFLYTEK_APP_ID + assert config.IFLYTEK_API_KEY + assert config.IFLYTEK_API_SECRET result = await oas3_iflytek_tts( text="你好,hello", - app_id=CONFIG.IFLYTEK_APP_ID, - api_key=CONFIG.IFLYTEK_API_KEY, - api_secret=CONFIG.IFLYTEK_API_SECRET, + app_id=config.IFLYTEK_APP_ID, + api_key=config.IFLYTEK_API_KEY, + api_secret=config.IFLYTEK_API_SECRET, ) assert result diff --git a/tests/metagpt/tools/test_web_browser_engine_playwright.py b/tests/metagpt/tools/test_web_browser_engine_playwright.py index 0f2679531..32019bad9 100644 --- a/tests/metagpt/tools/test_web_browser_engine_playwright.py +++ b/tests/metagpt/tools/test_web_browser_engine_playwright.py @@ -4,7 +4,7 @@ import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.tools import web_browser_engine_playwright from metagpt.utils.parse_html import WebPage @@ -20,11 +20,11 @@ from metagpt.utils.parse_html import WebPage ids=["chromium-normal", "firefox-normal", "webkit-normal"], ) async def test_scrape_web_page(browser_type, use_proxy, kwagrs, url, urls, proxy, capfd): - global_proxy = CONFIG.global_proxy + global_proxy = config.proxy try: if use_proxy: server, proxy = await proxy - CONFIG.global_proxy = proxy + config.proxy = proxy browser = web_browser_engine_playwright.PlaywrightWrapper(browser_type=browser_type, **kwagrs) result = await browser.run(url) assert isinstance(result, WebPage) @@ -39,7 +39,7 @@ async def test_scrape_web_page(browser_type, use_proxy, kwagrs, url, urls, proxy server.close() assert "Proxy:" in capfd.readouterr().out finally: - CONFIG.global_proxy = global_proxy + config.proxy = global_proxy if __name__ == "__main__": diff --git a/tests/metagpt/tools/test_web_browser_engine_selenium.py b/tests/metagpt/tools/test_web_browser_engine_selenium.py index 8fe365352..bd5abcb9b 100644 --- a/tests/metagpt/tools/test_web_browser_engine_selenium.py +++ b/tests/metagpt/tools/test_web_browser_engine_selenium.py @@ -4,7 +4,7 @@ import pytest -from metagpt.config import CONFIG +from metagpt.config2 import config from metagpt.tools import web_browser_engine_selenium from metagpt.utils.parse_html import WebPage @@ -23,11 +23,11 @@ async def test_scrape_web_page(browser_type, use_proxy, url, urls, proxy, capfd) # Prerequisites # firefox, chrome, Microsoft Edge - global_proxy = CONFIG.global_proxy + global_proxy = config.proxy try: if use_proxy: server, proxy = await proxy - CONFIG.global_proxy = proxy + config.proxy = proxy browser = web_browser_engine_selenium.SeleniumWrapper(browser_type=browser_type) result = await browser.run(url) assert isinstance(result, WebPage) @@ -42,7 +42,7 @@ async def test_scrape_web_page(browser_type, use_proxy, url, urls, proxy, capfd) server.close() assert "Proxy:" in capfd.readouterr().out finally: - CONFIG.global_proxy = global_proxy + config.proxy = global_proxy if __name__ == "__main__": diff --git a/tests/metagpt/utils/test_mermaid.py b/tests/metagpt/utils/test_mermaid.py index 6345e9c51..367223332 100644 --- a/tests/metagpt/utils/test_mermaid.py +++ b/tests/metagpt/utils/test_mermaid.py @@ -8,7 +8,6 @@ import pytest -from metagpt.config import CONFIG from metagpt.context import CONTEXT from metagpt.utils.common import check_cmd_exists from metagpt.utils.mermaid import MMC1, mermaid_to_file @@ -22,9 +21,8 @@ async def test_mermaid(engine): # playwright prerequisites: playwright install --with-deps chromium assert check_cmd_exists("npm") == 0 - CONFIG.mermaid_engine = engine - save_to = CONTEXT.git_repo.workdir / f"{CONFIG.mermaid_engine}/1" - await mermaid_to_file(MMC1, save_to) + save_to = CONTEXT.git_repo.workdir / f"{engine}/1" + await mermaid_to_file(engine, MMC1, save_to) # ink does not support pdf if engine == "ink": From b41c6923b3516c5fac6a378c4b7fbc64c81a7c14 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 20:36:28 +0800 Subject: [PATCH 43/50] fix bug --- metagpt/actions/write_prd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 728ddfbf9..a838dea8e 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -164,7 +164,7 @@ class WritePRD(Action): pathname = self.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc.filename).with_suffix("") if not pathname.parent.exists(): pathname.parent.mkdir(parents=True, exist_ok=True) - await mermaid_to_file(quadrant_chart, pathname) + await mermaid_to_file(self.config.mermaid_engine, quadrant_chart, pathname) async def _save_pdf(self, prd_doc): await self.file_repo.save_as(doc=prd_doc, with_suffix=".md", relative_path=PRD_PDF_FILE_REPO) From 8af1488613fcd712a02a5de1fcb0677b0e6588be Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 21:23:03 +0800 Subject: [PATCH 44/50] fix bugs --- metagpt/actions/rebuild_sequence_view.py | 2 ++ metagpt/config2.py | 4 ++-- metagpt/subscription.py | 2 +- tests/metagpt/actions/test_debug_error.py | 2 +- tests/metagpt/actions/test_prepare_documents.py | 2 +- tests/metagpt/actions/test_rebuild_class_view.py | 3 ++- .../metagpt/actions/test_rebuild_sequence_view.py | 3 ++- tests/metagpt/actions/test_run_code.py | 6 +++--- tests/metagpt/actions/test_summarize_code.py | 2 +- tests/metagpt/actions/test_talk_action.py | 9 ++++----- tests/metagpt/actions/test_write_code_review.py | 2 +- tests/metagpt/actions/test_write_prd.py | 4 ++-- tests/metagpt/actions/test_write_teaching_plan.py | 2 +- tests/metagpt/actions/test_write_test.py | 2 +- tests/metagpt/learn/test_text_to_image.py | 4 +++- .../serialize_deserialize/test_write_code.py | 2 +- .../test_write_code_review.py | 2 +- tests/metagpt/test_config.py | 6 +----- tests/metagpt/test_role.py | 14 +++++++------- 19 files changed, 37 insertions(+), 36 deletions(-) diff --git a/metagpt/actions/rebuild_sequence_view.py b/metagpt/actions/rebuild_sequence_view.py index 8785e6245..b701e66de 100644 --- a/metagpt/actions/rebuild_sequence_view.py +++ b/metagpt/actions/rebuild_sequence_view.py @@ -12,7 +12,9 @@ from pathlib import Path from typing import List from metagpt.actions import Action +from metagpt.config2 import config from metagpt.const import GRAPH_REPO_FILE_REPO +from metagpt.context import CONTEXT from metagpt.logs import logger from metagpt.utils.common import aread, list_files from metagpt.utils.di_graph_repository import DiGraphRepository diff --git a/metagpt/config2.py b/metagpt/config2.py index 6345c1b8c..c0991a6a0 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -75,8 +75,8 @@ class Config(CLIParams, YamlModel): puppeteer_config: str = "" pyppeteer_executable_path: str = "" IFLYTEK_APP_ID: str = "" - IFLYTEK_APP_SECRET: str = "" - IFLYTEK_APP_KEY: str = "" + IFLYTEK_API_SECRET: str = "" + IFLYTEK_API_KEY: str = "" AZURE_TTS_SUBSCRIPTION_KEY: str = "" AZURE_TTS_REGION: str = "" mermaid_engine: str = "nodejs" diff --git a/metagpt/subscription.py b/metagpt/subscription.py index e2b0916ac..d225a5d87 100644 --- a/metagpt/subscription.py +++ b/metagpt/subscription.py @@ -13,7 +13,7 @@ class SubscriptionRunner(BaseModel): Example: >>> import asyncio - >>> from metagpt.subscription import SubscriptionRunner + >>> from metagpt.address import SubscriptionRunner >>> from metagpt.roles import Searcher >>> from metagpt.schema import Message diff --git a/tests/metagpt/actions/test_debug_error.py b/tests/metagpt/actions/test_debug_error.py index 922aa8613..2e57a95c9 100644 --- a/tests/metagpt/actions/test_debug_error.py +++ b/tests/metagpt/actions/test_debug_error.py @@ -144,7 +144,7 @@ async def test_debug_error(): await repo.save_file( filename=ctx.output_filename, content=output_data.model_dump_json(), relative_path=TEST_OUTPUTS_FILE_REPO ) - debug_error = DebugError(context=ctx) + debug_error = DebugError(i_context=ctx) rsp = await debug_error.run() diff --git a/tests/metagpt/actions/test_prepare_documents.py b/tests/metagpt/actions/test_prepare_documents.py index fde971f3c..317683113 100644 --- a/tests/metagpt/actions/test_prepare_documents.py +++ b/tests/metagpt/actions/test_prepare_documents.py @@ -22,7 +22,7 @@ async def test_prepare_documents(): CONTEXT.git_repo.delete_repository() CONTEXT.git_repo = None - await PrepareDocuments(g_context=CONTEXT).run(with_messages=[msg]) + await PrepareDocuments(context=CONTEXT).run(with_messages=[msg]) assert CONTEXT.git_repo doc = await CONTEXT.file_repo.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO) assert doc diff --git a/tests/metagpt/actions/test_rebuild_class_view.py b/tests/metagpt/actions/test_rebuild_class_view.py index cc23cc8dc..94295fd55 100644 --- a/tests/metagpt/actions/test_rebuild_class_view.py +++ b/tests/metagpt/actions/test_rebuild_class_view.py @@ -12,13 +12,14 @@ import pytest from metagpt.actions.rebuild_class_view import RebuildClassView from metagpt.const import GRAPH_REPO_FILE_REPO +from metagpt.context import CONTEXT from metagpt.llm import LLM @pytest.mark.asyncio async def test_rebuild(): action = RebuildClassView( - name="RedBean", context=str(Path(__file__).parent.parent.parent.parent / "metagpt"), llm=LLM() + name="RedBean", i_context=str(Path(__file__).parent.parent.parent.parent / "metagpt"), llm=LLM() ) await action.run() graph_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=GRAPH_REPO_FILE_REPO) diff --git a/tests/metagpt/actions/test_rebuild_sequence_view.py b/tests/metagpt/actions/test_rebuild_sequence_view.py index 62f64b666..8c515d976 100644 --- a/tests/metagpt/actions/test_rebuild_sequence_view.py +++ b/tests/metagpt/actions/test_rebuild_sequence_view.py @@ -11,6 +11,7 @@ import pytest from metagpt.actions.rebuild_sequence_view import RebuildSequenceView from metagpt.const import GRAPH_REPO_FILE_REPO +from metagpt.context import CONTEXT from metagpt.llm import LLM from metagpt.utils.common import aread from metagpt.utils.file_repository import FileRepository @@ -31,7 +32,7 @@ async def test_rebuild(): CONTEXT.git_repo.commit("commit1") action = RebuildSequenceView( - name="RedBean", context=str(Path(__file__).parent.parent.parent.parent / "metagpt"), llm=LLM() + name="RedBean", i_context=str(Path(__file__).parent.parent.parent.parent / "metagpt"), llm=LLM() ) await action.run() graph_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=GRAPH_REPO_FILE_REPO) diff --git a/tests/metagpt/actions/test_run_code.py b/tests/metagpt/actions/test_run_code.py index ad08b5738..76397734d 100644 --- a/tests/metagpt/actions/test_run_code.py +++ b/tests/metagpt/actions/test_run_code.py @@ -26,12 +26,12 @@ async def test_run_text(): @pytest.mark.asyncio async def test_run_script(): # Successful command - out, err = await RunCode.run_script(".", command=["echo", "Hello World"]) + out, err = await RunCode().run_script(".", command=["echo", "Hello World"]) assert out.strip() == "Hello World" assert err == "" # Unsuccessful command - out, err = await RunCode.run_script(".", command=["python", "-c", "print(1/0)"]) + out, err = await RunCode().run_script(".", command=["python", "-c", "print(1/0)"]) assert "ZeroDivisionError" in err @@ -61,5 +61,5 @@ async def test_run(): ), ] for ctx, result in inputs: - rsp = await RunCode(context=ctx).run() + rsp = await RunCode(i_context=ctx).run() assert result in rsp.summary diff --git a/tests/metagpt/actions/test_summarize_code.py b/tests/metagpt/actions/test_summarize_code.py index 081636a21..b617b59ae 100644 --- a/tests/metagpt/actions/test_summarize_code.py +++ b/tests/metagpt/actions/test_summarize_code.py @@ -188,7 +188,7 @@ async def test_summarize_code(): src_file_repo = CONTEXT.git_repo.new_file_repository(relative_path=CONTEXT.src_workspace) all_files = src_file_repo.all_files ctx = CodeSummarizeContext(design_filename="1.json", task_filename="1.json", codes_filenames=all_files) - action = SummarizeCode(context=ctx) + action = SummarizeCode(i_context=ctx) rsp = await action.run() assert rsp logger.info(rsp) diff --git a/tests/metagpt/actions/test_talk_action.py b/tests/metagpt/actions/test_talk_action.py index 6d01dcc3f..b722d7c40 100644 --- a/tests/metagpt/actions/test_talk_action.py +++ b/tests/metagpt/actions/test_talk_action.py @@ -9,7 +9,7 @@ import pytest from metagpt.actions.talk_action import TalkAction -from metagpt.context import Context +from metagpt.context import CONTEXT from metagpt.schema import Message @@ -35,11 +35,10 @@ from metagpt.schema import Message ) async def test_prompt(agent_description, language, context, knowledge, history_summary): # Prerequisites - g_context = Context() - g_context.kwargs["agent_description"] = agent_description - g_context.kwargs["language"] = language + CONTEXT.kwargs.agent_description = agent_description + CONTEXT.kwargs.language = language - action = TalkAction(context=context, knowledge=knowledge, history_summary=history_summary) + action = TalkAction(i_context=context, knowledge=knowledge, history_summary=history_summary) assert "{" not in action.prompt assert "{" not in action.prompt_gpt4 diff --git a/tests/metagpt/actions/test_write_code_review.py b/tests/metagpt/actions/test_write_code_review.py index 3343b42b4..951929b76 100644 --- a/tests/metagpt/actions/test_write_code_review.py +++ b/tests/metagpt/actions/test_write_code_review.py @@ -21,7 +21,7 @@ def add(a, b): filename="math.py", design_doc=Document(content="编写一个从a加b的函数,返回a+b"), code_doc=Document(content=code) ) - context = await WriteCodeReview(context=context).run() + context = await WriteCodeReview(i_context=context).run() # 我们不能精确地预测生成的代码评审,但我们可以检查返回的是否为字符串 assert isinstance(context.code_doc.content, str) diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index faa5b77a4..1a897ac2e 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -16,14 +16,14 @@ from metagpt.roles.product_manager import ProductManager from metagpt.roles.role import RoleReactMode from metagpt.schema import Message from metagpt.utils.common import any_to_str -from metagpt.utils.file_repository import FileRepository @pytest.mark.asyncio async def test_write_prd(new_filename): product_manager = ProductManager() requirements = "开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结" - await FileRepository.save_file(filename=REQUIREMENT_FILENAME, content=requirements, relative_path=DOCS_FILE_REPO) + repo = CONTEXT.file_repo + await repo.save_file(filename=REQUIREMENT_FILENAME, content=requirements, relative_path=DOCS_FILE_REPO) product_manager.rc.react_mode = RoleReactMode.BY_ORDER prd = await product_manager.run(Message(content=requirements, cause_by=UserRequirement)) assert prd.cause_by == any_to_str(WritePRD) diff --git a/tests/metagpt/actions/test_write_teaching_plan.py b/tests/metagpt/actions/test_write_teaching_plan.py index 57a4f5eb0..3d556ab92 100644 --- a/tests/metagpt/actions/test_write_teaching_plan.py +++ b/tests/metagpt/actions/test_write_teaching_plan.py @@ -17,7 +17,7 @@ from metagpt.actions.write_teaching_plan import WriteTeachingPlanPart [("Title", "Lesson 1: Learn to draw an apple."), ("Teaching Content", "Lesson 1: Learn to draw an apple.")], ) async def test_write_teaching_plan_part(topic, context): - action = WriteTeachingPlanPart(topic=topic, context=context) + action = WriteTeachingPlanPart(topic=topic, i_context=context) rsp = await action.run() assert rsp diff --git a/tests/metagpt/actions/test_write_test.py b/tests/metagpt/actions/test_write_test.py index 9649b9abb..e09038414 100644 --- a/tests/metagpt/actions/test_write_test.py +++ b/tests/metagpt/actions/test_write_test.py @@ -26,7 +26,7 @@ async def test_write_test(): self.position = (random.randint(1, max_y - 1), random.randint(1, max_x - 1)) """ context = TestingContext(filename="food.py", code_doc=Document(filename="food.py", content=code)) - write_test = WriteTest(context=context) + write_test = WriteTest(i_context=context) context = await write_test.run() logger.info(context.model_dump_json()) diff --git a/tests/metagpt/learn/test_text_to_image.py b/tests/metagpt/learn/test_text_to_image.py index 2c43297c2..7c133149d 100644 --- a/tests/metagpt/learn/test_text_to_image.py +++ b/tests/metagpt/learn/test_text_to_image.py @@ -27,7 +27,9 @@ async def test_text_to_image(mocker): config = Config.default() assert config.METAGPT_TEXT_TO_IMAGE_MODEL_URL - data = await text_to_image("Panda emoji", size_type="512x512", model_url=config.METAGPT_TEXT_TO_IMAGE_MODEL_URL) + data = await text_to_image( + "Panda emoji", size_type="512x512", model_url=config.METAGPT_TEXT_TO_IMAGE_MODEL_URL, config=config + ) assert "base64" in data or "http" in data diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 12dc49c3b..132f343bc 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -22,7 +22,7 @@ async def test_write_code_serdeser(): filename="test_code.py", design_doc=Document(content="write add function to calculate two numbers") ) doc = Document(content=context.model_dump_json()) - action = WriteCode(context=doc) + action = WriteCode(i_context=doc) serialized_data = action.model_dump() new_action = WriteCode(**serialized_data) diff --git a/tests/metagpt/serialize_deserialize/test_write_code_review.py b/tests/metagpt/serialize_deserialize/test_write_code_review.py index d1a9bff24..70a4f2077 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code_review.py +++ b/tests/metagpt/serialize_deserialize/test_write_code_review.py @@ -20,7 +20,7 @@ def div(a: int, b: int = 0): code_doc=Document(content=code_content), ) - action = WriteCodeReview(context=context) + action = WriteCodeReview(i_context=context) serialized_data = action.model_dump() assert serialized_data["name"] == "WriteCodeReview" diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index cfde7a04c..c804702dd 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -7,7 +7,7 @@ """ from pydantic import BaseModel -from metagpt.config2 import Config, config +from metagpt.config2 import Config from metagpt.configs.llm_config import LLMType from metagpt.context import ContextMixin from tests.metagpt.provider.mock_llm_config import mock_llm_config @@ -20,10 +20,6 @@ def test_config_1(): assert llm.api_type == LLMType.OPENAI -def test_config_2(): - assert config == Config.default() - - def test_config_from_dict(): cfg = Config(llm={"default": mock_llm_config}) assert cfg diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 351ba9051..20a366db8 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -38,11 +38,11 @@ class MockRole(Role): def test_basic(): mock_role = MockRole() - assert mock_role.subscription == {"tests.metagpt.test_role.MockRole"} + assert mock_role.addresses == ({"tests.metagpt.test_role.MockRole"}) assert mock_role.rc.watch == {"metagpt.actions.add_requirement.UserRequirement"} mock_role = MockRole(name="mock_role") - assert mock_role.subscription == {"tests.metagpt.test_role.MockRole", "mock_role"} + assert mock_role.addresses == {"tests.metagpt.test_role.MockRole", "mock_role"} @pytest.mark.asyncio @@ -53,7 +53,7 @@ async def test_react(): goal: str constraints: str desc: str - subscription: str + address: str inputs = [ { @@ -71,7 +71,7 @@ async def test_react(): role = MockRole( name=seed.name, profile=seed.profile, goal=seed.goal, constraints=seed.constraints, desc=seed.desc ) - role.subscribe({seed.subscription}) + role.set_addresses({seed.address}) assert role.rc.watch == {any_to_str(UserRequirement)} assert role.name == seed.name assert role.profile == seed.profile @@ -81,13 +81,13 @@ async def test_react(): assert role.is_idle env = Environment() env.add_role(role) - assert env.get_subscription(role) == {seed.subscription} - env.publish_message(Message(content="test", msg_to=seed.subscription)) + assert env.get_addresses(role) == {seed.address} + env.publish_message(Message(content="test", msg_to=seed.address)) assert not role.is_idle while not env.is_idle: await env.run() assert role.is_idle - env.publish_message(Message(content="test", cause_by=seed.subscription)) + env.publish_message(Message(content="test", cause_by=seed.address)) assert not role.is_idle while not env.is_idle: await env.run() From 4de8fa36828b380a922ce5c8bbd920717c577962 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 10 Jan 2024 22:02:44 +0800 Subject: [PATCH 45/50] fix bugs --- examples/example.pkl | Bin 624 -> 624 bytes metagpt/actions/debug_error.py | 2 +- metagpt/actions/research.py | 2 +- metagpt/actions/talk_action.py | 6 +++--- metagpt/roles/product_manager.py | 2 +- metagpt/roles/qa_engineer.py | 5 ----- metagpt/tools/search_engine.py | 2 +- metagpt/tools/ut_writer.py | 3 ++- metagpt/tools/web_browser_engine.py | 2 +- tests/conftest.py | 3 ++- tests/data/rsp_cache.json | 14 +++++++++++++- .../actions/test_rebuild_sequence_view.py | 4 ++-- tests/metagpt/test_role.py | 8 ++++---- tests/metagpt/test_schema.py | 2 +- tests/metagpt/utils/test_redis.py | 2 +- 15 files changed, 33 insertions(+), 24 deletions(-) diff --git a/examples/example.pkl b/examples/example.pkl index f706fd803328b14547ee12efb4cf90f9fd2be99c..94e0fe63b7128ac56fa5d3ebd823c2f7d07dafa0 100644 GIT binary patch delta 88 zcmWN{%ME}a3;@uOFbdZuwv^v2o`kk*xPplbxPqHF3M0tno!<1*UwdG|F)Saj2@7zu n4!tK`AeJbVvD$lr3x%|ZQ3B~1ffegIl%bi`NUAE0?$13xtmqn% delta 88 zcmWN@O$~rB3 Message: msg, format_msgs, system_msgs = self.aask_args diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index ec80d7bb0..fbe139a99 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -43,7 +43,7 @@ class ProductManager(Role): self._set_state(1) else: self._set_state(0) - self.context.config.git_reinit = False + self.config.git_reinit = False self.todo_action = any_to_name(WritePRD) return bool(self.rc.todo) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 783fde9b6..cd043b551 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -17,7 +17,6 @@ from metagpt.actions import DebugError, RunCode, WriteTest from metagpt.actions.summarize_code import SummarizeCode -from metagpt.config2 import Config from metagpt.const import ( MESSAGE_ROUTE_TO_NONE, TEST_CODES_FILE_REPO, @@ -48,10 +47,6 @@ class QaEngineer(Role): self._watch([SummarizeCode, WriteTest, RunCode, DebugError]) self.test_round = 0 - @property - def config(self) -> Config: - return self.context.config - async def _write_test(self, message: Message) -> None: src_file_repo = self.context.git_repo.new_file_repository(self.context.src_workspace) changed_files = set(src_file_repo.changed_files.keys()) diff --git a/metagpt/tools/search_engine.py b/metagpt/tools/search_engine.py index fd237d537..4111dd106 100644 --- a/metagpt/tools/search_engine.py +++ b/metagpt/tools/search_engine.py @@ -42,7 +42,7 @@ class SearchEngine: def __init__( self, - engine: Optional[SearchEngineType] = None, + engine: Optional[SearchEngineType] = SearchEngineType.SERPER_GOOGLE, run_func: Callable[[str, int, bool], Coroutine[None, None, Union[str, list[str]]]] = None, ): if engine == SearchEngineType.SERPAPI_GOOGLE: diff --git a/metagpt/tools/ut_writer.py b/metagpt/tools/ut_writer.py index f2f2bf51c..a155c27ab 100644 --- a/metagpt/tools/ut_writer.py +++ b/metagpt/tools/ut_writer.py @@ -4,6 +4,7 @@ import json from pathlib import Path +from metagpt.config2 import config from metagpt.provider.openai_api import OpenAILLM as GPTAPI from metagpt.utils.common import awrite @@ -281,6 +282,6 @@ class UTGenerator: """Choose based on different calling methods""" result = "" if self.chatgpt_method == "API": - result = await GPTAPI().aask_code(messages=messages) + result = await GPTAPI(config.get_llm_config()).aask_code(messages=messages) return result diff --git a/metagpt/tools/web_browser_engine.py b/metagpt/tools/web_browser_engine.py index 3493a5398..ff1f46a36 100644 --- a/metagpt/tools/web_browser_engine.py +++ b/metagpt/tools/web_browser_engine.py @@ -15,7 +15,7 @@ from metagpt.utils.parse_html import WebPage class WebBrowserEngine: def __init__( self, - engine: WebBrowserEngineType | None = None, + engine: WebBrowserEngineType | None = WebBrowserEngineType.PLAYWRIGHT, run_func: Callable[..., Coroutine[Any, Any, WebPage | list[WebPage]]] | None = None, ): if engine is None: diff --git a/tests/conftest.py b/tests/conftest.py index faa2d92e9..9ad05e1a0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -146,7 +146,8 @@ def setup_and_teardown_git_repo(request): # Destroy git repo at the end of the test session. def fin(): - CONTEXT.git_repo.delete_repository() + if CONTEXT.git_repo: + CONTEXT.git_repo.delete_repository() # Register the function for destroying the environment. request.addfinalizer(fin) diff --git a/tests/data/rsp_cache.json b/tests/data/rsp_cache.json index 0ed13593e..b173c789b 100644 --- a/tests/data/rsp_cache.json +++ b/tests/data/rsp_cache.json @@ -154,5 +154,17 @@ "Do not refer to the context of the previous conversation records, start the conversation anew.\n\nFormation: \"Capacity and role\" defines the role you are currently playing;\n\t\"[LESSON_BEGIN]\" and \"[LESSON_END]\" tags enclose the content of textbook;\n\t\"Statement\" defines the work detail you need to complete at this stage;\n\t\"Answer options\" defines the format requirements for your responses;\n\t\"Constraint\" defines the conditions that your responses must comply with.\n\nCapacity and role: You are a {teaching_language} Teacher, named Lily, your goal is writing a {language} teaching plan part by part. the constraint is writing in {language}. \nStatement: Write the \"Choice Questions\" part of teaching plan, WITHOUT ANY content unrelated to \"Choice Questions\"!!\nStatement: Based on the content of the textbook enclosed by \"[LESSON_BEGIN]\" and \"[LESSON_END]\", create choice questions. 10 questions.\nAnswer options: Enclose the teaching plan content with \"[TEACHING_PLAN_BEGIN]\" and \"[TEACHING_PLAN_END]\" tags.\nAnswer options: Using proper markdown format from second-level header format.\nConstraint: Writing in Chinese.\n[LESSON_BEGIN]\n\n UNIT 1 Making New Friends\n TOPIC 1 Welcome to China!\n Section A\n\n 1a Listen and number the following names.\n Jane Mari Kangkang Michael\n Look, listen and understand. Then practice the conversation.\n Work in groups. Introduce yourself using\n I ’m ... Then practice 1a\n with your own hometown or the following places.\n\n 1b Listen and number the following names\n Jane Michael Maria Kangkang\n 1c Work in groups. Introduce yourself using I ’m ... Then practice 1a with your own hometown or the following places.\n China the USA the UK Hong Kong Beijing\n\n 2a Look, listen and understand. Then practice the conversation\n Hello! \n Hello! \n Hello! \n Hello! Are you Maria? \n No, I’m not. I’m Jane.\n Oh, nice to meet you, Jane\n Nice to meet you, too.\n Hi, Maria!\n Hi, Kangkang!\n Welcome to China!\n Thanks.\n\n 2b Work in groups. Make up a conversation with your own name and the\n following structures.\n A: Hello! / Good morning! / Hi! I’m ... Are you ... ?\n B: ...\n\n 3a Listen, say and trace\n Aa Bb Cc Dd Ee Ff Gg\n\n 3b Listen and number the following letters. Then circle the letters with the same sound as Bb.\n Aa Bb Cc Dd Ee Ff Gg\n\n 3c Match the big letters with the small ones. Then write them on the lines.\n \n[LESSON_END]": "[TEACHING_PLAN_BEGIN]\n\n## 选择题\n\n1. 在1a部分,听到的名字按顺序应该是?\n - A. Jane B. Mari C. Kangkang D. Michael\n\n2. 在1b部分,听到的名字按顺序应该是?\n - A. Jane B. Michael C. Maria D. Kangkang\n\n3. 在2a部分,对话中提到了哪些地方?\n - A. 中国 B. 美国 C. 英国 D. 香港\n\n4. 在3b部分,与Bb发音相同的字母是?\n - A. Aa B. Cc C. Dd D. Gg\n\n5. 在3c部分,大写字母和小写字母的正确匹配是?\n - A. Aa - a B. Bb - b C. Cc - c D. Dd - d\n\n6. 在1a部分,听到的名字按顺序应该是?\n - A. Jane B. Mari C. Kangkang D. Michael\n\n7. 在1b部分,听到的名字按顺序应该是?\n - A. Jane B. Michael C. Maria D. Kangkang\n\n8. 在2a部分,对话中提到了哪些地方?\n - A. 中国 B. 美国 C. 英国 D. 香港\n\n9. 在3b部分,与Bb发音相同的字母是?\n - A. Aa B. Cc C. Dd D. Gg\n\n10. 在3c部分,大写字母和小写字母的正确匹配是?\n - A. Aa - a B. Bb - b C. Cc - c D. Dd - d\n\n[TEACHING_PLAN_END]", "Do not refer to the context of the previous conversation records, start the conversation anew.\n\nFormation: \"Capacity and role\" defines the role you are currently playing;\n\t\"[LESSON_BEGIN]\" and \"[LESSON_END]\" tags enclose the content of textbook;\n\t\"Statement\" defines the work detail you need to complete at this stage;\n\t\"Answer options\" defines the format requirements for your responses;\n\t\"Constraint\" defines the conditions that your responses must comply with.\n\nCapacity and role: You are a {teaching_language} Teacher, named Lily, your goal is writing a {language} teaching plan part by part. the constraint is writing in {language}. \nStatement: Write the \"Grammar Questions\" part of teaching plan, WITHOUT ANY content unrelated to \"Grammar Questions\"!!\nStatement: Based on the content of the textbook enclosed by \"[LESSON_BEGIN]\" and \"[LESSON_END]\", create grammar questions. 10 questions.\nAnswer options: Enclose the teaching plan content with \"[TEACHING_PLAN_BEGIN]\" and \"[TEACHING_PLAN_END]\" tags.\nAnswer options: Using proper markdown format from second-level header format.\nConstraint: Writing in Chinese.\n[LESSON_BEGIN]\n\n UNIT 1 Making New Friends\n TOPIC 1 Welcome to China!\n Section A\n\n 1a Listen and number the following names.\n Jane Mari Kangkang Michael\n Look, listen and understand. Then practice the conversation.\n Work in groups. Introduce yourself using\n I ’m ... Then practice 1a\n with your own hometown or the following places.\n\n 1b Listen and number the following names\n Jane Michael Maria Kangkang\n 1c Work in groups. Introduce yourself using I ’m ... Then practice 1a with your own hometown or the following places.\n China the USA the UK Hong Kong Beijing\n\n 2a Look, listen and understand. Then practice the conversation\n Hello! \n Hello! \n Hello! \n Hello! Are you Maria? \n No, I’m not. I’m Jane.\n Oh, nice to meet you, Jane\n Nice to meet you, too.\n Hi, Maria!\n Hi, Kangkang!\n Welcome to China!\n Thanks.\n\n 2b Work in groups. Make up a conversation with your own name and the\n following structures.\n A: Hello! / Good morning! / Hi! I’m ... Are you ... ?\n B: ...\n\n 3a Listen, say and trace\n Aa Bb Cc Dd Ee Ff Gg\n\n 3b Listen and number the following letters. Then circle the letters with the same sound as Bb.\n Aa Bb Cc Dd Ee Ff Gg\n\n 3c Match the big letters with the small ones. Then write them on the lines.\n \n[LESSON_END]": "[TEACHING_PLAN_BEGIN]\n\n## Grammar Questions\n\n1. 请问在1a中,学生需要做什么?\n2. 请问在1b中,学生需要做什么?\n3. 请问在2a中,学生需要做什么?\n4. 请问在2b中,学生需要做什么?\n5. 请问在3a中,学生需要做什么?\n6. 请问在3b中,学生需要做什么?\n7. 请问在3c中,学生需要做什么?\n8. 请问在1a中,学生需要听什么?\n9. 请问在2a中,学生需要看什么?\n10. 请问在3a中,学生需要说什么?\n\n[TEACHING_PLAN_END]", "Do not refer to the context of the previous conversation records, start the conversation anew.\n\nFormation: \"Capacity and role\" defines the role you are currently playing;\n\t\"[LESSON_BEGIN]\" and \"[LESSON_END]\" tags enclose the content of textbook;\n\t\"Statement\" defines the work detail you need to complete at this stage;\n\t\"Answer options\" defines the format requirements for your responses;\n\t\"Constraint\" defines the conditions that your responses must comply with.\n\nCapacity and role: You are a {teaching_language} Teacher, named Lily, your goal is writing a {language} teaching plan part by part. the constraint is writing in {language}. \nStatement: Write the \"Translation Questions\" part of teaching plan, WITHOUT ANY content unrelated to \"Translation Questions\"!!\nStatement: Based on the content of the textbook enclosed by \"[LESSON_BEGIN]\" and \"[LESSON_END]\", create translation questions. The translation should include 10 {language} questions with {teaching_language} answers, and it should also include 10 {teaching_language} questions with {language} answers.\nAnswer options: Enclose the teaching plan content with \"[TEACHING_PLAN_BEGIN]\" and \"[TEACHING_PLAN_END]\" tags.\nAnswer options: Using proper markdown format from second-level header format.\nConstraint: Writing in Chinese.\n[LESSON_BEGIN]\n\n UNIT 1 Making New Friends\n TOPIC 1 Welcome to China!\n Section A\n\n 1a Listen and number the following names.\n Jane Mari Kangkang Michael\n Look, listen and understand. Then practice the conversation.\n Work in groups. Introduce yourself using\n I ’m ... Then practice 1a\n with your own hometown or the following places.\n\n 1b Listen and number the following names\n Jane Michael Maria Kangkang\n 1c Work in groups. Introduce yourself using I ’m ... Then practice 1a with your own hometown or the following places.\n China the USA the UK Hong Kong Beijing\n\n 2a Look, listen and understand. Then practice the conversation\n Hello! \n Hello! \n Hello! \n Hello! Are you Maria? \n No, I’m not. I’m Jane.\n Oh, nice to meet you, Jane\n Nice to meet you, too.\n Hi, Maria!\n Hi, Kangkang!\n Welcome to China!\n Thanks.\n\n 2b Work in groups. Make up a conversation with your own name and the\n following structures.\n A: Hello! / Good morning! / Hi! I’m ... Are you ... ?\n B: ...\n\n 3a Listen, say and trace\n Aa Bb Cc Dd Ee Ff Gg\n\n 3b Listen and number the following letters. Then circle the letters with the same sound as Bb.\n Aa Bb Cc Dd Ee Ff Gg\n\n 3c Match the big letters with the small ones. Then write them on the lines.\n \n[LESSON_END]": "[TEACHING_PLAN_BEGIN]\n\n## Translation Questions\n\n### {language} Questions with {teaching_language} Answers\n1. 你能听懂这些名字吗? (Can you understand these names?)\n - 能,我能听懂。 (Yes, I can understand.)\n2. 请用“我是...”介绍一下你自己。 (Please introduce yourself using \"I am...\")\n - 我是... (I am...)\n3. 你能用这些结构编一个对话吗? (Can you make up a conversation with these structures?)\n - 能,我能编一个对话。 (Yes, I can make up a conversation.)\n4. 你能说出这些字母的名字吗? (Can you say the names of these letters?)\n - 能,我能说出来。 (Yes, I can say them.)\n5. 你能把大写字母和小写字母配对吗? (Can you match the uppercase letters with the lowercase letters?)\n - 能,我能配对。 (Yes, I can match them.)\n\n### {teaching_language} Questions with {language} Answers\n1. Can you understand these names?\n - Yes, I can understand.\n2. Please introduce yourself using \"I am...\"\n - I am...\n3. Can you make up a conversation with these structures?\n - Yes, I can make up a conversation.\n4. Can you say the names of these letters?\n - Yes, I can say them.\n5. Can you match the uppercase letters with the lowercase letters?\n - Yes, I can match them.\n\n[TEACHING_PLAN_END]", - "The given text repeatedly describes Lily as a girl. It emphasizes that Lily is a girl multiple times. The content consistently refers to Lily as a girl.\nTranslate the above summary into a English title of less than 5 words.": "\"Emphasizing Lily's Gender\"" + "The given text repeatedly describes Lily as a girl. It emphasizes that Lily is a girl multiple times. The content consistently refers to Lily as a girl.\nTranslate the above summary into a English title of less than 5 words.": "\"Emphasizing Lily's Gender\"", + "\n## context\n\n### Project Name\n20240110212347\n\n### Original Requirements\n['需要一个基于LLM做总结的搜索引擎']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"zh_cn\",\n \"Programming Language\": \"LLM\",\n \"Original Requirements\": \"需要一个基于LLM做总结的搜索引擎\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]", + "\n## context\n\n### Project Name\n20240101\n\n### Original Requirements\n['Make a cli snake game']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Make a cli snake game\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"Please provide more details on the product goals and user stories.\"\n}\n[/CONTENT]", + "\n## context\n{\"Language\":\"en_us\",\"Programming Language\":\"Python\",\"Original Requirements\":\"Make a cli snake game\",\"Product Goals\":[],\"User Stories\":[],\"Competitive Analysis\":[],\"Competitive Quadrant Chart\":\"\",\"Requirement Analysis\":\"\",\"Requirement Pool\":[],\"UI Design draft\":\"\",\"Anything UNCLEAR\":\"Please provide more details on the product goals and user stories.\"}\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Implementation approach\": \"We will ...\",\n \"File list\": [\n \"main.py\",\n \"game.py\"\n ],\n \"Data structures and interfaces\": \"\\nclassDiagram\\n class Main {\\n -SearchEngine search_engine\\n +main() str\\n }\\n class SearchEngine {\\n -Index index\\n -Ranking ranking\\n -Summary summary\\n +search(query: str) str\\n }\\n class Index {\\n -KnowledgeBase knowledge_base\\n +create_index(data: dict)\\n +query_index(query: str) list\\n }\\n class Ranking {\\n +rank_results(results: list) list\\n }\\n class Summary {\\n +summarize_results(results: list) str\\n }\\n class KnowledgeBase {\\n +update(data: dict)\\n +fetch_data(query: str) dict\\n }\\n Main --> SearchEngine\\n SearchEngine --> Index\\n SearchEngine --> Ranking\\n SearchEngine --> Summary\\n Index --> KnowledgeBase\\n\",\n \"Program call flow\": \"\\nsequenceDiagram\\n participant M as Main\\n participant SE as SearchEngine\\n participant I as Index\\n participant R as Ranking\\n participant S as Summary\\n participant KB as KnowledgeBase\\n M->>SE: search(query)\\n SE->>I: query_index(query)\\n I->>KB: fetch_data(query)\\n KB-->>I: return data\\n I-->>SE: return results\\n SE->>R: rank_results(results)\\n R-->>SE: return ranked_results\\n SE->>S: summarize_results(ranked_results)\\n S-->>SE: return summary\\n SE-->>M: return summary\\n\",\n \"Anything UNCLEAR\": \"Clarification needed on third-party API integration, ...\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Implementation approach: # Analyze the difficult points of the requirements, select the appropriate open-source framework\n- File list: typing.List[str] # Only need relative paths. ALWAYS write a main.py or app.py here\n- Data structures and interfaces: # Use mermaid classDiagram code syntax, including classes, method(__init__ etc.) and functions with type annotations, CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design.\n- Program call flow: # Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT.\n- Anything UNCLEAR: # Mention unclear project aspects, then try to clarify it.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Product Goals\": [\n \"Create a command-line interface (CLI) snake game\",\n \"Implement game logic for movement, collision, and scoring\",\n \"Provide a user-friendly and interactive gaming experience\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to control the snake's movement using arrow keys\",\n \"As a player, I want the game to end when the snake collides with the walls or itself\",\n \"As a player, I want to see my score displayed on the screen during and after the game\"\n ],\n \"Anything UNCLEAR\": \"Please provide more details on the specific features and functionalities expected in the snake game.\"\n}\n[/CONTENT]", + "\n## context\n{\"Implementation approach\":\"We will use Python and the curses library to create the snake game. The game logic will be implemented in a separate module, and the main.py file will handle the user interface and game loop.\",\"File list\":[\"main.py\",\"game.py\"],\"Data structures and interfaces\":\"\\nclassDiagram\\n class Game {\\n -Snake snake\\n -Food food\\n -Score score\\n +__init__(width: int, height: int)\\n +start_game()\\n +move_snake(direction: str)\\n +generate_food()\\n +update_score(points: int)\\n }\\n class Snake {\\n -body list\\n -direction str\\n +__init__(x: int, y: int)\\n +move(direction: str)\\n +grow()\\n +collides_with_self() bool\\n }\\n class Food {\\n -position tuple\\n +__init__(x: int, y: int)\\n +get_position() tuple\\n }\\n class Score {\\n -points int\\n +__init__()\\n +increase(points: int)\\n }\\n Game --> Snake\\n Game --> Food\\n Game --> Score\\n\",\"Program call flow\":\"\\nsequenceDiagram\\n participant M as Main\\n participant G as Game\\n M->>G: start_game()\\n M->>G: move_snake(direction)\\n G->>G: generate_food()\\n G->>G: update_score(points)\\n\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Required Python packages\": [\n \"flask==1.1.2\",\n \"bcrypt==3.2.0\"\n ],\n \"Required Other language third-party packages\": [\n \"No third-party dependencies required\"\n ],\n \"Logic Analysis\": [\n [\n \"game.py\",\n \"Contains Game class and ... functions\"\n ],\n [\n \"main.py\",\n \"Contains main function, from game import Game\"\n ]\n ],\n \"Task list\": [\n \"game.py\",\n \"main.py\"\n ],\n \"Full API spec\": \"openapi: 3.0.0 ...\",\n \"Shared Knowledge\": \"'game.py' contains functions shared across the project.\",\n \"Anything UNCLEAR\": \"Clarification needed on how to start and initialize third-party libraries.\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Required Python packages: typing.List[str] # Provide required Python packages in requirements.txt format.\n- Required Other language third-party packages: typing.List[str] # List down the required packages for languages other than Python.\n- Logic Analysis: typing.List[typing.List[str]] # Provide a list of files with the classes/methods/functions to be implemented, including dependency analysis and imports.\n- Task list: typing.List[str] # Break down the tasks into a list of filenames, prioritized by dependency order.\n- Full API spec: # Describe all APIs using OpenAPI 3.0 spec that may be used by both frontend and backend. If front-end and back-end communication is not required, leave it blank.\n- Shared Knowledge: # Detail any shared knowledge, like common utility functions or configuration variables.\n- Anything UNCLEAR: # Mention any unclear aspects in the project management context and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Required Python packages\": [\n \"curses==2.2.0\"\n ],\n \"Required Other language third-party packages\": [\n \"No third-party dependencies required\"\n ],\n \"Logic Analysis\": [\n [\n \"game.py\",\n \"Contains Game class and ... functions\"\n ],\n [\n \"main.py\",\n \"Contains main function, from game import Game\"\n ]\n ],\n \"Task list\": [\n \"game.py\",\n \"main.py\"\n ],\n \"Full API spec\": \"\",\n \"Shared Knowledge\": \"'game.py' contains functions shared across the project.\",\n \"Anything UNCLEAR\": \"Please provide more details on the game mechanics and user interactions.\"\n}\n[/CONTENT]", + "\nNOTICE\nRole: You are a professional engineer; the main goal is to write google-style, elegant, modular, easy to read and maintain code\nLanguage: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese.\nATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced \"Format example\".\n\n# Context\n## Design\n{\"Implementation approach\":\"We will use Python and the curses library to create the snake game. The game logic will be implemented in a separate module, and the main.py file will handle the user interface and game loop.\",\"File list\":[\"main.py\",\"game.py\"],\"Data structures and interfaces\":\"\\nclassDiagram\\n class Game {\\n -Snake snake\\n -Food food\\n -Score score\\n +__init__(width: int, height: int)\\n +start_game()\\n +move_snake(direction: str)\\n +generate_food()\\n +update_score(points: int)\\n }\\n class Snake {\\n -body list\\n -direction str\\n +__init__(x: int, y: int)\\n +move(direction: str)\\n +grow()\\n +collides_with_self() bool\\n }\\n class Food {\\n -position tuple\\n +__init__(x: int, y: int)\\n +get_position() tuple\\n }\\n class Score {\\n -points int\\n +__init__()\\n +increase(points: int)\\n }\\n Game --> Snake\\n Game --> Food\\n Game --> Score\\n\",\"Program call flow\":\"\\nsequenceDiagram\\n participant M as Main\\n participant G as Game\\n M->>G: start_game()\\n M->>G: move_snake(direction)\\n G->>G: generate_food()\\n G->>G: update_score(points)\\n\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Tasks\n{\"Required Python packages\":[\"curses==2.2.0\"],\"Required Other language third-party packages\":[\"No third-party dependencies required\"],\"Logic Analysis\":[[\"game.py\",\"Contains Game class and ... functions\"],[\"main.py\",\"Contains main function, from game import Game\"]],\"Task list\":[\"game.py\",\"main.py\"],\"Full API spec\":\"\",\"Shared Knowledge\":\"'game.py' contains functions shared across the project.\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Legacy Code\n```Code\n\n```\n\n## Debug logs\n```text\n\n\n\n```\n\n## Bug Feedback logs\n```text\n\n```\n\n# Format example\n## Code: game.py\n```python\n## game.py\n...\n```\n\n# Instruction: Based on the context, follow \"Format example\", write code.\n\n## Code: game.py. Write code with triple quoto, based on the following attentions and context.\n1. Only One file: do your best to implement THIS ONLY ONE FILE.\n2. COMPLETE CODE: Your code will be part of the entire project, so please implement complete, reliable, reusable code snippets.\n3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. AVOID circular import.\n4. Follow design: YOU MUST FOLLOW \"Data structures and interfaces\". DONT CHANGE ANY DESIGN. Do not use public member functions that do not exist in your design.\n5. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE.\n6. Before using a external variable/module, make sure you import it first.\n7. Write out EVERY CODE DETAIL, DON'T LEAVE TODO.\n\n": "```python\n## game.py\n\nclass Snake:\n def __init__(self, x: int, y: int):\n self.body = [(x, y)]\n self.direction = 'right'\n\n def move(self, direction: str):\n if direction == 'up' and self.direction != 'down':\n self.direction = 'up'\n elif direction == 'down' and self.direction != 'up':\n self.direction = 'down'\n elif direction == 'left' and self.direction != 'right':\n self.direction = 'left'\n elif direction == 'right' and self.direction != 'left':\n self.direction = 'right'\n\n head = self.body[0]\n x, y = head\n if self.direction == 'up':\n new_head = (x, y - 1)\n elif self.direction == 'down':\n new_head = (x, y + 1)\n elif self.direction == 'left':\n new_head = (x - 1, y)\n elif self.direction == 'right':\n new_head = (x + 1, y)\n self.body.insert(0, new_head)\n\n def grow(self):\n tail = self.body[-1]\n x, y = tail\n if self.direction == 'up':\n new_tail = (x, y + 1)\n elif self.direction == 'down':\n new_tail = (x, y - 1)\n elif self.direction == 'left':\n new_tail = (x + 1, y)\n elif self.direction == 'right':\n new_tail = (x - 1, y)\n self.body.append(new_tail)\n\n def collides_with_self(self) -> bool:\n return len(self.body) != len(set(self.body))\n\nclass Food:\n def __init__(self, x: int, y: int):\n self.position = (x, y)\n\n def get_position(self) -> tuple:\n return self.position\n\nclass Score:\n def __init__(self):\n self.points = 0\n\n def increase(self, points: int):\n self.points += points\n\nclass Game:\n def __init__(self, width: int, height: int):\n self.snake = Snake(width // 2, height // 2)\n self.food = None\n self.score = Score()\n self.width = width\n self.height = height\n\n def start_game(self):\n self.generate_food()\n\n def move_snake(self, direction: str):\n self.snake.move(direction)\n if self.check_collision():\n return False\n self.check_eat()\n return True\n\n def generate_food(self):\n import random\n x = random.randint(0, self.width - 1)\n y = random.randint(0, self.height - 1)\n self.food = Food(x, y)\n\n def update_score(self, points: int):\n self.score.increase(points)\n\n def check_collision(self) -> bool:\n head = self.snake.body[0]\n if head[0] < 0 or head[0] >= self.width or head[1] < 0 or head[1] >= self.height:\n return True\n if self.snake.collides_with_self():\n return True\n return False\n\n def check_eat(self):\n if self.snake.body[0] == self.food.get_position():\n self.snake.grow()\n self.generate_food()\n self.update_score(10)\n```", + "\n# System\nRole: You are a professional software engineer, and your main task is to review and revise the code. You need to ensure that the code conforms to the google-style standards, is elegantly designed and modularized, easy to read and maintain.\nLanguage: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese.\nATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced \"Format example\".\n\n# Context\n## System Design\n{\"Implementation approach\":\"We will use Python and the curses library to create the snake game. The game logic will be implemented in a separate module, and the main.py file will handle the user interface and game loop.\",\"File list\":[\"main.py\",\"game.py\"],\"Data structures and interfaces\":\"\\nclassDiagram\\n class Game {\\n -Snake snake\\n -Food food\\n -Score score\\n +__init__(width: int, height: int)\\n +start_game()\\n +move_snake(direction: str)\\n +generate_food()\\n +update_score(points: int)\\n }\\n class Snake {\\n -body list\\n -direction str\\n +__init__(x: int, y: int)\\n +move(direction: str)\\n +grow()\\n +collides_with_self() bool\\n }\\n class Food {\\n -position tuple\\n +__init__(x: int, y: int)\\n +get_position() tuple\\n }\\n class Score {\\n -points int\\n +__init__()\\n +increase(points: int)\\n }\\n Game --> Snake\\n Game --> Food\\n Game --> Score\\n\",\"Program call flow\":\"\\nsequenceDiagram\\n participant M as Main\\n participant G as Game\\n M->>G: start_game()\\n M->>G: move_snake(direction)\\n G->>G: generate_food()\\n G->>G: update_score(points)\\n\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Tasks\n{\"Required Python packages\":[\"curses==2.2.0\"],\"Required Other language third-party packages\":[\"No third-party dependencies required\"],\"Logic Analysis\":[[\"game.py\",\"Contains Game class and ... functions\"],[\"main.py\",\"Contains main function, from game import Game\"]],\"Task list\":[\"game.py\",\"main.py\"],\"Full API spec\":\"\",\"Shared Knowledge\":\"'game.py' contains functions shared across the project.\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Code Files\n\n\n\n## Code to be Reviewed: game.py\n```Code\n## game.py\n\nclass Snake:\n def __init__(self, x: int, y: int):\n self.body = [(x, y)]\n self.direction = 'right'\n\n def move(self, direction: str):\n if direction == 'up' and self.direction != 'down':\n self.direction = 'up'\n elif direction == 'down' and self.direction != 'up':\n self.direction = 'down'\n elif direction == 'left' and self.direction != 'right':\n self.direction = 'left'\n elif direction == 'right' and self.direction != 'left':\n self.direction = 'right'\n\n head = self.body[0]\n x, y = head\n if self.direction == 'up':\n new_head = (x, y - 1)\n elif self.direction == 'down':\n new_head = (x, y + 1)\n elif self.direction == 'left':\n new_head = (x - 1, y)\n elif self.direction == 'right':\n new_head = (x + 1, y)\n self.body.insert(0, new_head)\n\n def grow(self):\n tail = self.body[-1]\n x, y = tail\n if self.direction == 'up':\n new_tail = (x, y + 1)\n elif self.direction == 'down':\n new_tail = (x, y - 1)\n elif self.direction == 'left':\n new_tail = (x + 1, y)\n elif self.direction == 'right':\n new_tail = (x - 1, y)\n self.body.append(new_tail)\n\n def collides_with_self(self) -> bool:\n return len(self.body) != len(set(self.body))\n\nclass Food:\n def __init__(self, x: int, y: int):\n self.position = (x, y)\n\n def get_position(self) -> tuple:\n return self.position\n\nclass Score:\n def __init__(self):\n self.points = 0\n\n def increase(self, points: int):\n self.points += points\n\nclass Game:\n def __init__(self, width: int, height: int):\n self.snake = Snake(width // 2, height // 2)\n self.food = None\n self.score = Score()\n self.width = width\n self.height = height\n\n def start_game(self):\n self.generate_food()\n\n def move_snake(self, direction: str):\n self.snake.move(direction)\n if self.check_collision():\n return False\n self.check_eat()\n return True\n\n def generate_food(self):\n import random\n x = random.randint(0, self.width - 1)\n y = random.randint(0, self.height - 1)\n self.food = Food(x, y)\n\n def update_score(self, points: int):\n self.score.increase(points)\n\n def check_collision(self) -> bool:\n head = self.snake.body[0]\n if head[0] < 0 or head[0] >= self.width or head[1] < 0 or head[1] >= self.height:\n return True\n if self.snake.collides_with_self():\n return True\n return False\n\n def check_eat(self):\n if self.snake.body[0] == self.food.get_position():\n self.snake.grow()\n self.generate_food()\n self.update_score(10)\n\n```\n\n\n\n# Format example 1\n## Code Review: game.py\n1. No, we should fix the logic of class A due to ...\n2. ...\n3. ...\n4. No, function B is not implemented, ...\n5. ...\n6. ...\n\n## Actions\n1. Fix the `handle_events` method to update the game state only if a move is successful.\n ```python\n def handle_events(self):\n for event in pygame.event.get():\n if event.type == pygame.QUIT:\n return False\n if event.type == pygame.KEYDOWN:\n moved = False\n if event.key == pygame.K_UP:\n moved = self.game.move('UP')\n elif event.key == pygame.K_DOWN:\n moved = self.game.move('DOWN')\n elif event.key == pygame.K_LEFT:\n moved = self.game.move('LEFT')\n elif event.key == pygame.K_RIGHT:\n moved = self.game.move('RIGHT')\n if moved:\n # Update the game state only if a move was successful\n self.render()\n return True\n ```\n2. Implement function B\n\n## Code Review Result\nLBTM\n\n# Format example 2\n## Code Review: game.py\n1. Yes.\n2. Yes.\n3. Yes.\n4. Yes.\n5. Yes.\n6. Yes.\n\n## Actions\npass\n\n## Code Review Result\nLGTM\n\n\n\n# Instruction: Based on the actual code situation, follow one of the \"Format example\". Return only 1 file under review.\n\n## Code Review: Ordered List. Based on the \"Code to be Reviewed\", provide key, clear, concise, and specific answer. If any answer is no, explain how to fix it step by step.\n1. Is the code implemented as per the requirements? If not, how to achieve it? Analyse it step by step.\n2. Is the code logic completely correct? If there are errors, please indicate how to correct them.\n3. Does the existing code follow the \"Data structures and interfaces\"?\n4. Are all functions implemented? If there is no implementation, please indicate how to achieve it step by step.\n5. Have all necessary pre-dependencies been imported? If not, indicate which ones need to be imported\n6. Are methods from other files being reused correctly?\n\n## Actions: Ordered List. Things that should be done after CR, such as implementing class A and function B\n\n## Code Review Result: str. If the code doesn't have bugs, we don't need to rewrite it, so answer LGTM and stop. ONLY ANSWER LGTM/LBTM.\nLGTM/LBTM\n\n": "## Code Review: game.py\n1. Yes, the code is implemented as per the requirements. The Snake, Food, and Score classes are correctly implemented, and the Game class handles the game logic as specified.\n2. Yes, the code logic is correct. The movement, growth, collision detection, and scoring mechanisms are implemented accurately.\n3. Yes, the existing code follows the \"Data structures and interfaces\" as specified in the class diagram.\n4. Yes, all functions are implemented as required.\n5. No, the necessary pre-dependencies have not been imported. The `curses` package needs to be imported at the beginning of the file using `import curses`.\n6. Yes, methods from other files are being reused correctly.\n\n## Actions\n1. Import the necessary pre-dependency `curses` at the beginning of the file using `import curses`.\n\n## Code Review Result\nLBTM", + "\n# System\nRole: You are a professional software engineer, and your main task is to review and revise the code. You need to ensure that the code conforms to the google-style standards, is elegantly designed and modularized, easy to read and maintain.\nLanguage: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese.\nATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced \"Format example\".\n\n# Context\n## System Design\n{\"Implementation approach\":\"We will use Python and the curses library to create the snake game. The game logic will be implemented in a separate module, and the main.py file will handle the user interface and game loop.\",\"File list\":[\"main.py\",\"game.py\"],\"Data structures and interfaces\":\"\\nclassDiagram\\n class Game {\\n -Snake snake\\n -Food food\\n -Score score\\n +__init__(width: int, height: int)\\n +start_game()\\n +move_snake(direction: str)\\n +generate_food()\\n +update_score(points: int)\\n }\\n class Snake {\\n -body list\\n -direction str\\n +__init__(x: int, y: int)\\n +move(direction: str)\\n +grow()\\n +collides_with_self() bool\\n }\\n class Food {\\n -position tuple\\n +__init__(x: int, y: int)\\n +get_position() tuple\\n }\\n class Score {\\n -points int\\n +__init__()\\n +increase(points: int)\\n }\\n Game --> Snake\\n Game --> Food\\n Game --> Score\\n\",\"Program call flow\":\"\\nsequenceDiagram\\n participant M as Main\\n participant G as Game\\n M->>G: start_game()\\n M->>G: move_snake(direction)\\n G->>G: generate_food()\\n G->>G: update_score(points)\\n\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Tasks\n{\"Required Python packages\":[\"curses==2.2.0\"],\"Required Other language third-party packages\":[\"No third-party dependencies required\"],\"Logic Analysis\":[[\"game.py\",\"Contains Game class and ... functions\"],[\"main.py\",\"Contains main function, from game import Game\"]],\"Task list\":[\"game.py\",\"main.py\"],\"Full API spec\":\"\",\"Shared Knowledge\":\"'game.py' contains functions shared across the project.\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Code Files\n\n\n\n## Code to be Reviewed: game.py\n```Code\n## game.py\n\nclass Snake:\n def __init__(self, x: int, y: int):\n self.body = [(x, y)]\n self.direction = 'right'\n\n def move(self, direction: str):\n if direction == 'up' and self.direction != 'down':\n self.direction = 'up'\n elif direction == 'down' and self.direction != 'up':\n self.direction = 'down'\n elif direction == 'left' and self.direction != 'right':\n self.direction = 'left'\n elif direction == 'right' and self.direction != 'left':\n self.direction = 'right'\n\n head = self.body[0]\n x, y = head\n if self.direction == 'up':\n new_head = (x, y - 1)\n elif self.direction == 'down':\n new_head = (x, y + 1)\n elif self.direction == 'left':\n new_head = (x - 1, y)\n elif self.direction == 'right':\n new_head = (x + 1, y)\n self.body.insert(0, new_head)\n\n def grow(self):\n tail = self.body[-1]\n x, y = tail\n if self.direction == 'up':\n new_tail = (x, y + 1)\n elif self.direction == 'down':\n new_tail = (x, y - 1)\n elif self.direction == 'left':\n new_tail = (x + 1, y)\n elif self.direction == 'right':\n new_tail = (x - 1, y)\n self.body.append(new_tail)\n\n def collides_with_self(self) -> bool:\n return len(self.body) != len(set(self.body))\n\nclass Food:\n def __init__(self, x: int, y: int):\n self.position = (x, y)\n\n def get_position(self) -> tuple:\n return self.position\n\nclass Score:\n def __init__(self):\n self.points = 0\n\n def increase(self, points: int):\n self.points += points\n\nclass Game:\n def __init__(self, width: int, height: int):\n self.snake = Snake(width // 2, height // 2)\n self.food = None\n self.score = Score()\n self.width = width\n self.height = height\n\n def start_game(self):\n self.generate_food()\n\n def move_snake(self, direction: str):\n self.snake.move(direction)\n if self.check_collision():\n return False\n self.check_eat()\n return True\n\n def generate_food(self):\n import random\n x = random.randint(0, self.width - 1)\n y = random.randint(0, self.height - 1)\n self.food = Food(x, y)\n\n def update_score(self, points: int):\n self.score.increase(points)\n\n def check_collision(self) -> bool:\n head = self.snake.body[0]\n if head[0] < 0 or head[0] >= self.width or head[1] < 0 or head[1] >= self.height:\n return True\n if self.snake.collides_with_self():\n return True\n return False\n\n def check_eat(self):\n if self.snake.body[0] == self.food.get_position():\n self.snake.grow()\n self.generate_food()\n self.update_score(10)\n\n```\n\n## Code Review: game.py\n1. Yes, the code is implemented as per the requirements. The Snake, Food, and Score classes are correctly implemented, and the Game class handles the game logic as specified.\n2. Yes, the code logic is correct. The movement, growth, collision detection, and scoring mechanisms are implemented accurately.\n3. Yes, the existing code follows the \"Data structures and interfaces\" as specified in the class diagram.\n4. Yes, all functions are implemented as required.\n5. No, the necessary pre-dependencies have not been imported. The `curses` package needs to be imported at the beginning of the file using `import curses`.\n6. Yes, methods from other files are being reused correctly.\n\n## Actions\n1. Import the necessary pre-dependency `curses` at the beginning of the file using `import curses`.\n\n## Code Review Result\nLBTM\n\n# Instruction: rewrite code based on the Code Review and Actions\n## Rewrite Code: CodeBlock. If it still has some bugs, rewrite game.py with triple quotes. Do your utmost to optimize THIS SINGLE FILE. Return all completed codes and prohibit the return of unfinished codes.\n```Code\n## game.py\n...\n```\n": "```Code\n## game.py\n\nimport curses\n\nclass Snake:\n def __init__(self, x: int, y: int):\n self.body = [(x, y)]\n self.direction = 'right'\n\n def move(self, direction: str):\n if direction == 'up' and self.direction != 'down':\n self.direction = 'up'\n elif direction == 'down' and self.direction != 'up':\n self.direction = 'down'\n elif direction == 'left' and self.direction != 'right':\n self.direction = 'left'\n elif direction == 'right' and self.direction != 'left':\n self.direction = 'right'\n\n head = self.body[0]\n x, y = head\n if self.direction == 'up':\n new_head = (x, y - 1)\n elif self.direction == 'down':\n new_head = (x, y + 1)\n elif self.direction == 'left':\n new_head = (x - 1, y)\n elif self.direction == 'right':\n new_head = (x + 1, y)\n self.body.insert(0, new_head)\n\n def grow(self):\n tail = self.body[-1]\n x, y = tail\n if self.direction == 'up':\n new_tail = (x, y + 1)\n elif self.direction == 'down':\n new_tail = (x, y - 1)\n elif self.direction == 'left':\n new_tail = (x + 1, y)\n elif self.direction == 'right':\n new_tail = (x - 1, y)\n self.body.append(new_tail)\n\n def collides_with_self(self) -> bool:\n return len(self.body) != len(set(self.body))\n\nclass Food:\n def __init__(self, x: int, y: int):\n self.position = (x, y)\n\n def get_position(self) -> tuple:\n return self.position\n\nclass Score:\n def __init__(self):\n self.points = 0\n\n def increase(self, points: int):\n self.points += points\n\nclass Game:\n def __init__(self, width: int, height: int):\n self.snake = Snake(width // 2, height // 2)\n self.food = None\n self.score = Score()\n self.width = width\n self.height = height\n\n def start_game(self):\n self.generate_food()\n\n def move_snake(self, direction: str):\n self.snake.move(direction)\n if self.check_collision():\n return False\n self.check_eat()\n return True\n\n def generate_food(self):\n import random\n x = random.randint(0, self.width - 1)\n y = random.randint(0, self.height - 1)\n self.food = Food(x, y)\n\n def update_score(self, points: int):\n self.score.increase(points)\n\n def check_collision(self) -> bool:\n head = self.snake.body[0]\n if head[0] < 0 or head[0] >= self.width or head[1] < 0 or head[1] >= self.height:\n return True\n if self.snake.collides_with_self():\n return True\n return False\n\n def check_eat(self):\n if self.snake.body[0] == self.food.get_position():\n self.snake.grow()\n self.generate_food()\n self.update_score(10)\n\n```", + "\n# System\nRole: You are a professional software engineer, and your main task is to review and revise the code. You need to ensure that the code conforms to the google-style standards, is elegantly designed and modularized, easy to read and maintain.\nLanguage: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese.\nATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced \"Format example\".\n\n# Context\n## System Design\n{\"Implementation approach\":\"We will use Python and the curses library to create the snake game. The game logic will be implemented in a separate module, and the main.py file will handle the user interface and game loop.\",\"File list\":[\"main.py\",\"game.py\"],\"Data structures and interfaces\":\"\\nclassDiagram\\n class Game {\\n -Snake snake\\n -Food food\\n -Score score\\n +__init__(width: int, height: int)\\n +start_game()\\n +move_snake(direction: str)\\n +generate_food()\\n +update_score(points: int)\\n }\\n class Snake {\\n -body list\\n -direction str\\n +__init__(x: int, y: int)\\n +move(direction: str)\\n +grow()\\n +collides_with_self() bool\\n }\\n class Food {\\n -position tuple\\n +__init__(x: int, y: int)\\n +get_position() tuple\\n }\\n class Score {\\n -points int\\n +__init__()\\n +increase(points: int)\\n }\\n Game --> Snake\\n Game --> Food\\n Game --> Score\\n\",\"Program call flow\":\"\\nsequenceDiagram\\n participant M as Main\\n participant G as Game\\n M->>G: start_game()\\n M->>G: move_snake(direction)\\n G->>G: generate_food()\\n G->>G: update_score(points)\\n\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Tasks\n{\"Required Python packages\":[\"curses==2.2.0\"],\"Required Other language third-party packages\":[\"No third-party dependencies required\"],\"Logic Analysis\":[[\"game.py\",\"Contains Game class and ... functions\"],[\"main.py\",\"Contains main function, from game import Game\"]],\"Task list\":[\"game.py\",\"main.py\"],\"Full API spec\":\"\",\"Shared Knowledge\":\"'game.py' contains functions shared across the project.\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Code Files\n\n\n\n## Code to be Reviewed: game.py\n```Code\n## game.py\n\nimport curses\n\nclass Snake:\n def __init__(self, x: int, y: int):\n self.body = [(x, y)]\n self.direction = 'right'\n\n def move(self, direction: str):\n if direction == 'up' and self.direction != 'down':\n self.direction = 'up'\n elif direction == 'down' and self.direction != 'up':\n self.direction = 'down'\n elif direction == 'left' and self.direction != 'right':\n self.direction = 'left'\n elif direction == 'right' and self.direction != 'left':\n self.direction = 'right'\n\n head = self.body[0]\n x, y = head\n if self.direction == 'up':\n new_head = (x, y - 1)\n elif self.direction == 'down':\n new_head = (x, y + 1)\n elif self.direction == 'left':\n new_head = (x - 1, y)\n elif self.direction == 'right':\n new_head = (x + 1, y)\n self.body.insert(0, new_head)\n\n def grow(self):\n tail = self.body[-1]\n x, y = tail\n if self.direction == 'up':\n new_tail = (x, y + 1)\n elif self.direction == 'down':\n new_tail = (x, y - 1)\n elif self.direction == 'left':\n new_tail = (x + 1, y)\n elif self.direction == 'right':\n new_tail = (x - 1, y)\n self.body.append(new_tail)\n\n def collides_with_self(self) -> bool:\n return len(self.body) != len(set(self.body))\n\nclass Food:\n def __init__(self, x: int, y: int):\n self.position = (x, y)\n\n def get_position(self) -> tuple:\n return self.position\n\nclass Score:\n def __init__(self):\n self.points = 0\n\n def increase(self, points: int):\n self.points += points\n\nclass Game:\n def __init__(self, width: int, height: int):\n self.snake = Snake(width // 2, height // 2)\n self.food = None\n self.score = Score()\n self.width = width\n self.height = height\n\n def start_game(self):\n self.generate_food()\n\n def move_snake(self, direction: str):\n self.snake.move(direction)\n if self.check_collision():\n return False\n self.check_eat()\n return True\n\n def generate_food(self):\n import random\n x = random.randint(0, self.width - 1)\n y = random.randint(0, self.height - 1)\n self.food = Food(x, y)\n\n def update_score(self, points: int):\n self.score.increase(points)\n\n def check_collision(self) -> bool:\n head = self.snake.body[0]\n if head[0] < 0 or head[0] >= self.width or head[1] < 0 or head[1] >= self.height:\n return True\n if self.snake.collides_with_self():\n return True\n return False\n\n def check_eat(self):\n if self.snake.body[0] == self.food.get_position():\n self.snake.grow()\n self.generate_food()\n self.update_score(10)\n\n\n```\n\n\n\n# Format example 1\n## Code Review: game.py\n1. No, we should fix the logic of class A due to ...\n2. ...\n3. ...\n4. No, function B is not implemented, ...\n5. ...\n6. ...\n\n## Actions\n1. Fix the `handle_events` method to update the game state only if a move is successful.\n ```python\n def handle_events(self):\n for event in pygame.event.get():\n if event.type == pygame.QUIT:\n return False\n if event.type == pygame.KEYDOWN:\n moved = False\n if event.key == pygame.K_UP:\n moved = self.game.move('UP')\n elif event.key == pygame.K_DOWN:\n moved = self.game.move('DOWN')\n elif event.key == pygame.K_LEFT:\n moved = self.game.move('LEFT')\n elif event.key == pygame.K_RIGHT:\n moved = self.game.move('RIGHT')\n if moved:\n # Update the game state only if a move was successful\n self.render()\n return True\n ```\n2. Implement function B\n\n## Code Review Result\nLBTM\n\n# Format example 2\n## Code Review: game.py\n1. Yes.\n2. Yes.\n3. Yes.\n4. Yes.\n5. Yes.\n6. Yes.\n\n## Actions\npass\n\n## Code Review Result\nLGTM\n\n\n\n# Instruction: Based on the actual code situation, follow one of the \"Format example\". Return only 1 file under review.\n\n## Code Review: Ordered List. Based on the \"Code to be Reviewed\", provide key, clear, concise, and specific answer. If any answer is no, explain how to fix it step by step.\n1. Is the code implemented as per the requirements? If not, how to achieve it? Analyse it step by step.\n2. Is the code logic completely correct? If there are errors, please indicate how to correct them.\n3. Does the existing code follow the \"Data structures and interfaces\"?\n4. Are all functions implemented? If there is no implementation, please indicate how to achieve it step by step.\n5. Have all necessary pre-dependencies been imported? If not, indicate which ones need to be imported\n6. Are methods from other files being reused correctly?\n\n## Actions: Ordered List. Things that should be done after CR, such as implementing class A and function B\n\n## Code Review Result: str. If the code doesn't have bugs, we don't need to rewrite it, so answer LGTM and stop. ONLY ANSWER LGTM/LBTM.\nLGTM/LBTM\n\n": "## Code Review: game.py\n1. Yes, the code is implemented as per the requirements.\n2. Yes, the code logic is completely correct.\n3. Yes, the existing code follows the \"Data structures and interfaces\".\n4. Yes, all functions are implemented.\n5. Yes, all necessary pre-dependencies have been imported.\n6. Yes, methods from other files are being reused correctly.\n\n## Code Review Result\nLGTM", + "\nNOTICE\nRole: You are a professional engineer; the main goal is to write google-style, elegant, modular, easy to read and maintain code\nLanguage: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese.\nATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced \"Format example\".\n\n# Context\n## Design\n{\"Implementation approach\":\"We will use Python and the curses library to create the snake game. The game logic will be implemented in a separate module, and the main.py file will handle the user interface and game loop.\",\"File list\":[\"main.py\",\"game.py\"],\"Data structures and interfaces\":\"\\nclassDiagram\\n class Game {\\n -Snake snake\\n -Food food\\n -Score score\\n +__init__(width: int, height: int)\\n +start_game()\\n +move_snake(direction: str)\\n +generate_food()\\n +update_score(points: int)\\n }\\n class Snake {\\n -body list\\n -direction str\\n +__init__(x: int, y: int)\\n +move(direction: str)\\n +grow()\\n +collides_with_self() bool\\n }\\n class Food {\\n -position tuple\\n +__init__(x: int, y: int)\\n +get_position() tuple\\n }\\n class Score {\\n -points int\\n +__init__()\\n +increase(points: int)\\n }\\n Game --> Snake\\n Game --> Food\\n Game --> Score\\n\",\"Program call flow\":\"\\nsequenceDiagram\\n participant M as Main\\n participant G as Game\\n M->>G: start_game()\\n M->>G: move_snake(direction)\\n G->>G: generate_food()\\n G->>G: update_score(points)\\n\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Tasks\n{\"Required Python packages\":[\"curses==2.2.0\"],\"Required Other language third-party packages\":[\"No third-party dependencies required\"],\"Logic Analysis\":[[\"game.py\",\"Contains Game class and ... functions\"],[\"main.py\",\"Contains main function, from game import Game\"]],\"Task list\":[\"game.py\",\"main.py\"],\"Full API spec\":\"\",\"Shared Knowledge\":\"'game.py' contains functions shared across the project.\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Legacy Code\n```Code\n----- game.py\n## game.py\n\nimport curses\n\nclass Snake:\n def __init__(self, x: int, y: int):\n self.body = [(x, y)]\n self.direction = 'right'\n\n def move(self, direction: str):\n if direction == 'up' and self.direction != 'down':\n self.direction = 'up'\n elif direction == 'down' and self.direction != 'up':\n self.direction = 'down'\n elif direction == 'left' and self.direction != 'right':\n self.direction = 'left'\n elif direction == 'right' and self.direction != 'left':\n self.direction = 'right'\n\n head = self.body[0]\n x, y = head\n if self.direction == 'up':\n new_head = (x, y - 1)\n elif self.direction == 'down':\n new_head = (x, y + 1)\n elif self.direction == 'left':\n new_head = (x - 1, y)\n elif self.direction == 'right':\n new_head = (x + 1, y)\n self.body.insert(0, new_head)\n\n def grow(self):\n tail = self.body[-1]\n x, y = tail\n if self.direction == 'up':\n new_tail = (x, y + 1)\n elif self.direction == 'down':\n new_tail = (x, y - 1)\n elif self.direction == 'left':\n new_tail = (x + 1, y)\n elif self.direction == 'right':\n new_tail = (x - 1, y)\n self.body.append(new_tail)\n\n def collides_with_self(self) -> bool:\n return len(self.body) != len(set(self.body))\n\nclass Food:\n def __init__(self, x: int, y: int):\n self.position = (x, y)\n\n def get_position(self) -> tuple:\n return self.position\n\nclass Score:\n def __init__(self):\n self.points = 0\n\n def increase(self, points: int):\n self.points += points\n\nclass Game:\n def __init__(self, width: int, height: int):\n self.snake = Snake(width // 2, height // 2)\n self.food = None\n self.score = Score()\n self.width = width\n self.height = height\n\n def start_game(self):\n self.generate_food()\n\n def move_snake(self, direction: str):\n self.snake.move(direction)\n if self.check_collision():\n return False\n self.check_eat()\n return True\n\n def generate_food(self):\n import random\n x = random.randint(0, self.width - 1)\n y = random.randint(0, self.height - 1)\n self.food = Food(x, y)\n\n def update_score(self, points: int):\n self.score.increase(points)\n\n def check_collision(self) -> bool:\n head = self.snake.body[0]\n if head[0] < 0 or head[0] >= self.width or head[1] < 0 or head[1] >= self.height:\n return True\n if self.snake.collides_with_self():\n return True\n return False\n\n def check_eat(self):\n if self.snake.body[0] == self.food.get_position():\n self.snake.grow()\n self.generate_food()\n self.update_score(10)\n\n\n```\n\n## Debug logs\n```text\n\n\n\n```\n\n## Bug Feedback logs\n```text\n\n```\n\n# Format example\n## Code: main.py\n```python\n## main.py\n...\n```\n\n# Instruction: Based on the context, follow \"Format example\", write code.\n\n## Code: main.py. Write code with triple quoto, based on the following attentions and context.\n1. Only One file: do your best to implement THIS ONLY ONE FILE.\n2. COMPLETE CODE: Your code will be part of the entire project, so please implement complete, reliable, reusable code snippets.\n3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. AVOID circular import.\n4. Follow design: YOU MUST FOLLOW \"Data structures and interfaces\". DONT CHANGE ANY DESIGN. Do not use public member functions that do not exist in your design.\n5. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE.\n6. Before using a external variable/module, make sure you import it first.\n7. Write out EVERY CODE DETAIL, DON'T LEAVE TODO.\n\n": "```python\n## main.py\n\nfrom game import Game\n\ndef main():\n width = 20 # Set default width\n height = 20 # Set default height\n game = Game(width, height)\n game.start_game()\n\n while True:\n # Get user input for direction\n direction = input(\"Enter direction (up/down/left/right): \")\n if direction in ['up', 'down', 'left', 'right']:\n if not game.move_snake(direction):\n print(\"Game over!\")\n break\n else:\n print(\"Invalid direction!\")\n\nif __name__ == \"__main__\":\n main()\n```", + "\n# System\nRole: You are a professional software engineer, and your main task is to review and revise the code. You need to ensure that the code conforms to the google-style standards, is elegantly designed and modularized, easy to read and maintain.\nLanguage: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese.\nATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced \"Format example\".\n\n# Context\n## System Design\n{\"Implementation approach\":\"We will use Python and the curses library to create the snake game. The game logic will be implemented in a separate module, and the main.py file will handle the user interface and game loop.\",\"File list\":[\"main.py\",\"game.py\"],\"Data structures and interfaces\":\"\\nclassDiagram\\n class Game {\\n -Snake snake\\n -Food food\\n -Score score\\n +__init__(width: int, height: int)\\n +start_game()\\n +move_snake(direction: str)\\n +generate_food()\\n +update_score(points: int)\\n }\\n class Snake {\\n -body list\\n -direction str\\n +__init__(x: int, y: int)\\n +move(direction: str)\\n +grow()\\n +collides_with_self() bool\\n }\\n class Food {\\n -position tuple\\n +__init__(x: int, y: int)\\n +get_position() tuple\\n }\\n class Score {\\n -points int\\n +__init__()\\n +increase(points: int)\\n }\\n Game --> Snake\\n Game --> Food\\n Game --> Score\\n\",\"Program call flow\":\"\\nsequenceDiagram\\n participant M as Main\\n participant G as Game\\n M->>G: start_game()\\n M->>G: move_snake(direction)\\n G->>G: generate_food()\\n G->>G: update_score(points)\\n\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Tasks\n{\"Required Python packages\":[\"curses==2.2.0\"],\"Required Other language third-party packages\":[\"No third-party dependencies required\"],\"Logic Analysis\":[[\"game.py\",\"Contains Game class and ... functions\"],[\"main.py\",\"Contains main function, from game import Game\"]],\"Task list\":[\"game.py\",\"main.py\"],\"Full API spec\":\"\",\"Shared Knowledge\":\"'game.py' contains functions shared across the project.\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Code Files\n----- game.py\n## game.py\n\nimport curses\n\nclass Snake:\n def __init__(self, x: int, y: int):\n self.body = [(x, y)]\n self.direction = 'right'\n\n def move(self, direction: str):\n if direction == 'up' and self.direction != 'down':\n self.direction = 'up'\n elif direction == 'down' and self.direction != 'up':\n self.direction = 'down'\n elif direction == 'left' and self.direction != 'right':\n self.direction = 'left'\n elif direction == 'right' and self.direction != 'left':\n self.direction = 'right'\n\n head = self.body[0]\n x, y = head\n if self.direction == 'up':\n new_head = (x, y - 1)\n elif self.direction == 'down':\n new_head = (x, y + 1)\n elif self.direction == 'left':\n new_head = (x - 1, y)\n elif self.direction == 'right':\n new_head = (x + 1, y)\n self.body.insert(0, new_head)\n\n def grow(self):\n tail = self.body[-1]\n x, y = tail\n if self.direction == 'up':\n new_tail = (x, y + 1)\n elif self.direction == 'down':\n new_tail = (x, y - 1)\n elif self.direction == 'left':\n new_tail = (x + 1, y)\n elif self.direction == 'right':\n new_tail = (x - 1, y)\n self.body.append(new_tail)\n\n def collides_with_self(self) -> bool:\n return len(self.body) != len(set(self.body))\n\nclass Food:\n def __init__(self, x: int, y: int):\n self.position = (x, y)\n\n def get_position(self) -> tuple:\n return self.position\n\nclass Score:\n def __init__(self):\n self.points = 0\n\n def increase(self, points: int):\n self.points += points\n\nclass Game:\n def __init__(self, width: int, height: int):\n self.snake = Snake(width // 2, height // 2)\n self.food = None\n self.score = Score()\n self.width = width\n self.height = height\n\n def start_game(self):\n self.generate_food()\n\n def move_snake(self, direction: str):\n self.snake.move(direction)\n if self.check_collision():\n return False\n self.check_eat()\n return True\n\n def generate_food(self):\n import random\n x = random.randint(0, self.width - 1)\n y = random.randint(0, self.height - 1)\n self.food = Food(x, y)\n\n def update_score(self, points: int):\n self.score.increase(points)\n\n def check_collision(self) -> bool:\n head = self.snake.body[0]\n if head[0] < 0 or head[0] >= self.width or head[1] < 0 or head[1] >= self.height:\n return True\n if self.snake.collides_with_self():\n return True\n return False\n\n def check_eat(self):\n if self.snake.body[0] == self.food.get_position():\n self.snake.grow()\n self.generate_food()\n self.update_score(10)\n\n\n\n\n## Code to be Reviewed: main.py\n```Code\n## main.py\n\nfrom game import Game\n\ndef main():\n width = 20 # Set default width\n height = 20 # Set default height\n game = Game(width, height)\n game.start_game()\n\n while True:\n # Get user input for direction\n direction = input(\"Enter direction (up/down/left/right): \")\n if direction in ['up', 'down', 'left', 'right']:\n if not game.move_snake(direction):\n print(\"Game over!\")\n break\n else:\n print(\"Invalid direction!\")\n\nif __name__ == \"__main__\":\n main()\n\n```\n\n\n\n# Format example 1\n## Code Review: main.py\n1. No, we should fix the logic of class A due to ...\n2. ...\n3. ...\n4. No, function B is not implemented, ...\n5. ...\n6. ...\n\n## Actions\n1. Fix the `handle_events` method to update the game state only if a move is successful.\n ```python\n def handle_events(self):\n for event in pygame.event.get():\n if event.type == pygame.QUIT:\n return False\n if event.type == pygame.KEYDOWN:\n moved = False\n if event.key == pygame.K_UP:\n moved = self.game.move('UP')\n elif event.key == pygame.K_DOWN:\n moved = self.game.move('DOWN')\n elif event.key == pygame.K_LEFT:\n moved = self.game.move('LEFT')\n elif event.key == pygame.K_RIGHT:\n moved = self.game.move('RIGHT')\n if moved:\n # Update the game state only if a move was successful\n self.render()\n return True\n ```\n2. Implement function B\n\n## Code Review Result\nLBTM\n\n# Format example 2\n## Code Review: main.py\n1. Yes.\n2. Yes.\n3. Yes.\n4. Yes.\n5. Yes.\n6. Yes.\n\n## Actions\npass\n\n## Code Review Result\nLGTM\n\n\n\n# Instruction: Based on the actual code situation, follow one of the \"Format example\". Return only 1 file under review.\n\n## Code Review: Ordered List. Based on the \"Code to be Reviewed\", provide key, clear, concise, and specific answer. If any answer is no, explain how to fix it step by step.\n1. Is the code implemented as per the requirements? If not, how to achieve it? Analyse it step by step.\n2. Is the code logic completely correct? If there are errors, please indicate how to correct them.\n3. Does the existing code follow the \"Data structures and interfaces\"?\n4. Are all functions implemented? If there is no implementation, please indicate how to achieve it step by step.\n5. Have all necessary pre-dependencies been imported? If not, indicate which ones need to be imported\n6. Are methods from other files being reused correctly?\n\n## Actions: Ordered List. Things that should be done after CR, such as implementing class A and function B\n\n## Code Review Result: str. If the code doesn't have bugs, we don't need to rewrite it, so answer LGTM and stop. ONLY ANSWER LGTM/LBTM.\nLGTM/LBTM\n\n": "## Code Review: main.py\n1. The code is implemented as per the requirements. The main function initializes the game and handles user input for the snake's direction.\n2. The code logic is correct. It correctly handles user input and updates the game state based on the snake's movement.\n3. The existing code follows the \"Data structures and interfaces\" as defined in the class diagram.\n4. All necessary functions are implemented in the code.\n5. The necessary pre-dependencies have been imported. The required Python package \"curses==2.2.0\" has been imported.\n6. The methods from the \"game.py\" file are being reused correctly.\n\n## Actions\npass\n\n## Code Review Result\nLGTM", + "\n## context\n\n### Project Name\n20240110212717\n\n### Original Requirements\n['开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"zh_cn\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]", + "\n## context\n\n### Project Name\n20240110212717\n\n### Original Requirements\n['']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]" } \ No newline at end of file diff --git a/tests/metagpt/actions/test_rebuild_sequence_view.py b/tests/metagpt/actions/test_rebuild_sequence_view.py index 8c515d976..0511f0308 100644 --- a/tests/metagpt/actions/test_rebuild_sequence_view.py +++ b/tests/metagpt/actions/test_rebuild_sequence_view.py @@ -14,7 +14,6 @@ from metagpt.const import GRAPH_REPO_FILE_REPO from metagpt.context import CONTEXT from metagpt.llm import LLM from metagpt.utils.common import aread -from metagpt.utils.file_repository import FileRepository from metagpt.utils.git_repository import ChangeType @@ -23,7 +22,8 @@ async def test_rebuild(): # Mock data = await aread(filename=Path(__file__).parent / "../../data/graph_db/networkx.json") graph_db_filename = Path(CONTEXT.git_repo.workdir.name).with_suffix(".json") - await FileRepository.save_file( + repo = CONTEXT.file_repo + await repo.save_file( filename=str(graph_db_filename), relative_path=GRAPH_REPO_FILE_REPO, content=data, diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 20a366db8..1b843795c 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -62,7 +62,7 @@ async def test_react(): "goal": "Test", "constraints": "constraints", "desc": "desc", - "subscription": "start", + "address": "start", } ] @@ -93,8 +93,8 @@ async def test_react(): await env.run() assert role.is_idle tag = uuid.uuid4().hex - role.subscribe({tag}) - assert env.get_subscription(role) == {tag} + role.set_addresses({tag}) + assert env.get_addresses(role) == {tag} @pytest.mark.asyncio @@ -131,7 +131,7 @@ async def test_recover(): role.recovered = True role.latest_observed_msg = Message(content="recover_test") role.rc.state = 0 - assert role.todo == any_to_name(MockAction) + assert role.first_action == any_to_name(MockAction) rsp = await role.run() assert rsp.cause_by == any_to_str(MockAction) diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index c4f071d85..0929e6c4a 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -102,7 +102,7 @@ def test_message_serdeser(): new_message = Message.model_validate(message_dict) assert new_message.content == message.content assert new_message.instruct_content.model_dump() == message.instruct_content.model_dump() - assert new_message.instruct_content != message.instruct_content # TODO + assert new_message.instruct_content == message.instruct_content # TODO assert new_message.cause_by == message.cause_by assert new_message.instruct_content.field3 == out_data["field3"] diff --git a/tests/metagpt/utils/test_redis.py b/tests/metagpt/utils/test_redis.py index 95eff4f61..8e9cf710a 100644 --- a/tests/metagpt/utils/test_redis.py +++ b/tests/metagpt/utils/test_redis.py @@ -22,7 +22,7 @@ async def async_mock_from_url(*args, **kwargs): @pytest.mark.asyncio @mock.patch("aioredis.from_url", return_value=async_mock_from_url()) -async def test_redis(): +async def test_redis(i): redis = Config.default().redis conn = Redis(redis) From c275f28a3709970c6fc8879d57e2bfdca5a8a869 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 11 Jan 2024 15:10:07 +0800 Subject: [PATCH 46/50] remove Dict, use direct LLMConfig / Browser. / Search. / Mermaid. instead --- config/config2.yaml | 5 +- examples/example.pkl | Bin 624 -> 624 bytes metagpt/actions/research.py | 2 +- metagpt/config2.py | 51 ++++-------------- metagpt/configs/llm_config.py | 1 + metagpt/context.py | 12 ++--- metagpt/llm.py | 6 +-- metagpt/tools/search_engine_googleapi.py | 4 +- metagpt/tools/search_engine_serpapi.py | 2 +- metagpt/tools/search_engine_serper.py | 2 +- metagpt/tools/ut_writer.py | 2 +- .../tools/web_browser_engine_playwright.py | 4 +- tests/data/rsp_cache.json | 15 +++++- tests/metagpt/provider/test_openai.py | 6 +-- tests/metagpt/test_config.py | 23 ++++---- tests/metagpt/tools/test_search_engine.py | 7 +-- 16 files changed, 60 insertions(+), 82 deletions(-) diff --git a/config/config2.yaml b/config/config2.yaml index 0040023a8..5e7f34809 100644 --- a/config/config2.yaml +++ b/config/config2.yaml @@ -1,4 +1,3 @@ llm: - gpt3t: - api_key: "YOUR_API_KEY" - model: "gpt-3.5-turbo-1106" \ No newline at end of file + api_key: "YOUR_API_KEY" + model: "gpt-3.5-turbo-1106" \ No newline at end of file diff --git a/examples/example.pkl b/examples/example.pkl index 94e0fe63b7128ac56fa5d3ebd823c2f7d07dafa0..b7454edeee4a61125ae0fbdb5560f6c8bbfb0d89 100644 GIT binary patch delta 88 zcmV~$K@EUF3%aco1}Thc>DWC53=tT! lK?~`1oL1%X9Owqwm7z^AS1F*I!7L0pH>ZbYOme;5{R4Pv8JYk9 delta 88 zcmWN{%ME}a3;@uOFbdZuwv^v2o`kk*xPplbxPqHF3M0tno!<1*UwdG|F)Saj2@7zu n4!tK`AeJbVvD$lr3x%|ZQ3B~1ffegIl%bi`NUAE0?$13xtmqn% diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index 6fd6ca139..2755628c9 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -184,7 +184,7 @@ class WebBrowseAndSummarize(Action): super().__init__(**kwargs) self.web_browser_engine = WebBrowserEngine( - engine=WebBrowserEngineType.CUSTOM if self.browse_func else None, + engine=WebBrowserEngineType.CUSTOM if self.browse_func else WebBrowserEngineType.PLAYWRIGHT, run_func=self.browse_func, ) diff --git a/metagpt/config2.py b/metagpt/config2.py index c0991a6a0..c916b9b60 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -9,7 +9,7 @@ import os from pathlib import Path from typing import Dict, Iterable, List, Literal, Optional -from pydantic import BaseModel, Field, model_validator +from pydantic import BaseModel, model_validator from metagpt.configs.browser_config import BrowserConfig from metagpt.configs.llm_config import LLMConfig, LLMType @@ -44,15 +44,15 @@ class Config(CLIParams, YamlModel): """Configurations for MetaGPT""" # Key Parameters - llm: Dict[str, LLMConfig] = Field(default_factory=Dict) + llm: LLMConfig # Global Proxy. Will be used if llm.proxy is not set proxy: str = "" # Tool Parameters - search: Dict[str, SearchConfig] = {} - browser: Dict[str, BrowserConfig] = {"default": BrowserConfig()} - mermaid: Dict[str, MermaidConfig] = {"default": MermaidConfig()} + search: Optional[SearchConfig] = None + browser: BrowserConfig = BrowserConfig() + mermaid: MermaidConfig = MermaidConfig() # Storage Parameters s3: Optional[S3Config] = None @@ -110,46 +110,17 @@ class Config(CLIParams, YamlModel): self.reqa_file = reqa_file self.max_auto_summarize_code = max_auto_summarize_code - def _get_llm_config(self, name: Optional[str] = None) -> LLMConfig: - """Get LLM instance by name""" - if name is None: - # Use the first LLM as default - name = list(self.llm.keys())[0] - if name not in self.llm: - raise ValueError(f"LLM {name} not found in config") - return self.llm[name] - - def get_llm_configs_by_type(self, llm_type: LLMType) -> List[LLMConfig]: - """Get LLM instance by type""" - return [v for k, v in self.llm.items() if v.api_type == llm_type] - - def get_llm_config_by_type(self, llm_type: LLMType) -> Optional[LLMConfig]: - """Get LLM instance by type""" - llm = self.get_llm_configs_by_type(llm_type) - if llm: - return llm[0] - return None - - def get_llm_config(self, name: Optional[str] = None, provider: LLMType = None) -> LLMConfig: - """Return a LLMConfig instance""" - if provider: - llm_configs = self.get_llm_configs_by_type(provider) - - if len(llm_configs) == 0: - raise ValueError(f"Cannot find llm config with name {name} and provider {provider}") - # return the first one if name is None, or return the only one - llm_config = llm_configs[0] - else: - llm_config = self._get_llm_config(name) - return llm_config - def get_openai_llm(self) -> Optional[LLMConfig]: """Get OpenAI LLMConfig by name. If no OpenAI, raise Exception""" - return self.get_llm_config_by_type(LLMType.OPENAI) + if self.llm.api_type == LLMType.OPENAI: + return self.llm + return None def get_azure_llm(self) -> Optional[LLMConfig]: """Get Azure LLMConfig by name. If no Azure, raise Exception""" - return self.get_llm_config_by_type(LLMType.AZURE) + if self.llm.api_type == LLMType.AZURE: + return self.llm + return None def merge_dict(dicts: Iterable[Dict]) -> Dict: diff --git a/metagpt/configs/llm_config.py b/metagpt/configs/llm_config.py index 620827630..626d4242f 100644 --- a/metagpt/configs/llm_config.py +++ b/metagpt/configs/llm_config.py @@ -40,6 +40,7 @@ class LLMConfig(YamlModel): api_type: LLMType = LLMType.OPENAI base_url: str = "https://api.openai.com/v1" api_version: Optional[str] = None + model: Optional[str] = None # also stands for DEPLOYMENT_NAME # For Spark(Xunfei), maybe remove later diff --git a/metagpt/context.py b/metagpt/context.py index 1c351ef22..663c1730a 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -12,7 +12,7 @@ from typing import Optional from pydantic import BaseModel, ConfigDict from metagpt.config2 import Config -from metagpt.configs.llm_config import LLMConfig, LLMType +from metagpt.configs.llm_config import LLMConfig from metagpt.const import OPTIONS from metagpt.provider.base_llm import BaseLLM from metagpt.provider.llm_provider_registry import create_llm_instance @@ -77,10 +77,10 @@ class Context(BaseModel): # self._llm = None # return self._llm - def llm(self, name: Optional[str] = None, provider: LLMType = None) -> BaseLLM: + def llm(self) -> BaseLLM: """Return a LLM instance, fixme: support cache""" # if self._llm is None: - self._llm = create_llm_instance(self.config.get_llm_config(name, provider)) + self._llm = create_llm_instance(self.config.llm) if self._llm.cost_manager is None: self._llm.cost_manager = self.cost_manager return self._llm @@ -140,12 +140,6 @@ class ContextMixin(BaseModel): """Set llm""" self.set("_llm", llm, override) - def use_llm(self, name: Optional[str] = None, provider: LLMType = None) -> BaseLLM: - """Use a LLM instance""" - self._llm_config = self.config.get_llm_config(name, provider) - self._llm = None - return self.llm - @property def config(self) -> Config: """Role config: role config > context config""" diff --git a/metagpt/llm.py b/metagpt/llm.py index d393738bb..4c9993441 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -6,14 +6,12 @@ @File : llm.py """ -from typing import Optional -from metagpt.configs.llm_config import LLMType from metagpt.context import CONTEXT from metagpt.provider.base_llm import BaseLLM -def LLM(name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM: +def LLM() -> BaseLLM: """get the default llm provider if name is None""" # context.use_llm(name=name, provider=provider) - return CONTEXT.llm(name=name, provider=provider) + return CONTEXT.llm() diff --git a/metagpt/tools/search_engine_googleapi.py b/metagpt/tools/search_engine_googleapi.py index 65e1af109..0a8f796cb 100644 --- a/metagpt/tools/search_engine_googleapi.py +++ b/metagpt/tools/search_engine_googleapi.py @@ -35,7 +35,7 @@ class GoogleAPIWrapper(BaseModel): @field_validator("google_api_key", mode="before") @classmethod def check_google_api_key(cls, val: str): - val = val or config.search["google"].api_key + val = val or config.search.api_key if not val: raise ValueError( "To use, make sure you provide the google_api_key when constructing an object. Alternatively, " @@ -47,7 +47,7 @@ class GoogleAPIWrapper(BaseModel): @field_validator("google_cse_id", mode="before") @classmethod def check_google_cse_id(cls, val: str): - val = val or config.search["google"].cse_id + val = val or config.search.cse_id if not val: raise ValueError( "To use, make sure you provide the google_cse_id when constructing an object. Alternatively, " diff --git a/metagpt/tools/search_engine_serpapi.py b/metagpt/tools/search_engine_serpapi.py index 2d21aa85c..a8d5b49d0 100644 --- a/metagpt/tools/search_engine_serpapi.py +++ b/metagpt/tools/search_engine_serpapi.py @@ -32,7 +32,7 @@ class SerpAPIWrapper(BaseModel): @field_validator("serpapi_api_key", mode="before") @classmethod def check_serpapi_api_key(cls, val: str): - val = val or config.search["serpapi"].api_key + val = val or config.search.api_key if not val: raise ValueError( "To use, make sure you provide the serpapi_api_key when constructing an object. Alternatively, " diff --git a/metagpt/tools/search_engine_serper.py b/metagpt/tools/search_engine_serper.py index d67148e14..39cb936b8 100644 --- a/metagpt/tools/search_engine_serper.py +++ b/metagpt/tools/search_engine_serper.py @@ -25,7 +25,7 @@ class SerperWrapper(BaseModel): @field_validator("serper_api_key", mode="before") @classmethod def check_serper_api_key(cls, val: str): - val = val or config.search["serper"].api_key + val = val or config.search.api_key if not val: raise ValueError( "To use, make sure you provide the serper_api_key when constructing an object. Alternatively, " diff --git a/metagpt/tools/ut_writer.py b/metagpt/tools/ut_writer.py index a155c27ab..243871aff 100644 --- a/metagpt/tools/ut_writer.py +++ b/metagpt/tools/ut_writer.py @@ -282,6 +282,6 @@ class UTGenerator: """Choose based on different calling methods""" result = "" if self.chatgpt_method == "API": - result = await GPTAPI(config.get_llm_config()).aask_code(messages=messages) + result = await GPTAPI(config.get_openai_llm()).aask_code(messages=messages) return result diff --git a/metagpt/tools/web_browser_engine_playwright.py b/metagpt/tools/web_browser_engine_playwright.py index 00f2c6bab..14c19816d 100644 --- a/metagpt/tools/web_browser_engine_playwright.py +++ b/metagpt/tools/web_browser_engine_playwright.py @@ -28,12 +28,10 @@ class PlaywrightWrapper: def __init__( self, - browser_type: Literal["chromium", "firefox", "webkit"] | None = None, + browser_type: Literal["chromium", "firefox", "webkit"] | None = "chromium", launch_kwargs: dict | None = None, **kwargs, ) -> None: - if browser_type is None: - browser_type = config.browser["playwright"].driver self.browser_type = browser_type launch_kwargs = launch_kwargs or {} if config.proxy and "proxy" not in launch_kwargs: diff --git a/tests/data/rsp_cache.json b/tests/data/rsp_cache.json index b173c789b..25f7ae0b4 100644 --- a/tests/data/rsp_cache.json +++ b/tests/data/rsp_cache.json @@ -166,5 +166,18 @@ "\nNOTICE\nRole: You are a professional engineer; the main goal is to write google-style, elegant, modular, easy to read and maintain code\nLanguage: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese.\nATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced \"Format example\".\n\n# Context\n## Design\n{\"Implementation approach\":\"We will use Python and the curses library to create the snake game. The game logic will be implemented in a separate module, and the main.py file will handle the user interface and game loop.\",\"File list\":[\"main.py\",\"game.py\"],\"Data structures and interfaces\":\"\\nclassDiagram\\n class Game {\\n -Snake snake\\n -Food food\\n -Score score\\n +__init__(width: int, height: int)\\n +start_game()\\n +move_snake(direction: str)\\n +generate_food()\\n +update_score(points: int)\\n }\\n class Snake {\\n -body list\\n -direction str\\n +__init__(x: int, y: int)\\n +move(direction: str)\\n +grow()\\n +collides_with_self() bool\\n }\\n class Food {\\n -position tuple\\n +__init__(x: int, y: int)\\n +get_position() tuple\\n }\\n class Score {\\n -points int\\n +__init__()\\n +increase(points: int)\\n }\\n Game --> Snake\\n Game --> Food\\n Game --> Score\\n\",\"Program call flow\":\"\\nsequenceDiagram\\n participant M as Main\\n participant G as Game\\n M->>G: start_game()\\n M->>G: move_snake(direction)\\n G->>G: generate_food()\\n G->>G: update_score(points)\\n\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Tasks\n{\"Required Python packages\":[\"curses==2.2.0\"],\"Required Other language third-party packages\":[\"No third-party dependencies required\"],\"Logic Analysis\":[[\"game.py\",\"Contains Game class and ... functions\"],[\"main.py\",\"Contains main function, from game import Game\"]],\"Task list\":[\"game.py\",\"main.py\"],\"Full API spec\":\"\",\"Shared Knowledge\":\"'game.py' contains functions shared across the project.\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Legacy Code\n```Code\n----- game.py\n## game.py\n\nimport curses\n\nclass Snake:\n def __init__(self, x: int, y: int):\n self.body = [(x, y)]\n self.direction = 'right'\n\n def move(self, direction: str):\n if direction == 'up' and self.direction != 'down':\n self.direction = 'up'\n elif direction == 'down' and self.direction != 'up':\n self.direction = 'down'\n elif direction == 'left' and self.direction != 'right':\n self.direction = 'left'\n elif direction == 'right' and self.direction != 'left':\n self.direction = 'right'\n\n head = self.body[0]\n x, y = head\n if self.direction == 'up':\n new_head = (x, y - 1)\n elif self.direction == 'down':\n new_head = (x, y + 1)\n elif self.direction == 'left':\n new_head = (x - 1, y)\n elif self.direction == 'right':\n new_head = (x + 1, y)\n self.body.insert(0, new_head)\n\n def grow(self):\n tail = self.body[-1]\n x, y = tail\n if self.direction == 'up':\n new_tail = (x, y + 1)\n elif self.direction == 'down':\n new_tail = (x, y - 1)\n elif self.direction == 'left':\n new_tail = (x + 1, y)\n elif self.direction == 'right':\n new_tail = (x - 1, y)\n self.body.append(new_tail)\n\n def collides_with_self(self) -> bool:\n return len(self.body) != len(set(self.body))\n\nclass Food:\n def __init__(self, x: int, y: int):\n self.position = (x, y)\n\n def get_position(self) -> tuple:\n return self.position\n\nclass Score:\n def __init__(self):\n self.points = 0\n\n def increase(self, points: int):\n self.points += points\n\nclass Game:\n def __init__(self, width: int, height: int):\n self.snake = Snake(width // 2, height // 2)\n self.food = None\n self.score = Score()\n self.width = width\n self.height = height\n\n def start_game(self):\n self.generate_food()\n\n def move_snake(self, direction: str):\n self.snake.move(direction)\n if self.check_collision():\n return False\n self.check_eat()\n return True\n\n def generate_food(self):\n import random\n x = random.randint(0, self.width - 1)\n y = random.randint(0, self.height - 1)\n self.food = Food(x, y)\n\n def update_score(self, points: int):\n self.score.increase(points)\n\n def check_collision(self) -> bool:\n head = self.snake.body[0]\n if head[0] < 0 or head[0] >= self.width or head[1] < 0 or head[1] >= self.height:\n return True\n if self.snake.collides_with_self():\n return True\n return False\n\n def check_eat(self):\n if self.snake.body[0] == self.food.get_position():\n self.snake.grow()\n self.generate_food()\n self.update_score(10)\n\n\n```\n\n## Debug logs\n```text\n\n\n\n```\n\n## Bug Feedback logs\n```text\n\n```\n\n# Format example\n## Code: main.py\n```python\n## main.py\n...\n```\n\n# Instruction: Based on the context, follow \"Format example\", write code.\n\n## Code: main.py. Write code with triple quoto, based on the following attentions and context.\n1. Only One file: do your best to implement THIS ONLY ONE FILE.\n2. COMPLETE CODE: Your code will be part of the entire project, so please implement complete, reliable, reusable code snippets.\n3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. AVOID circular import.\n4. Follow design: YOU MUST FOLLOW \"Data structures and interfaces\". DONT CHANGE ANY DESIGN. Do not use public member functions that do not exist in your design.\n5. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE.\n6. Before using a external variable/module, make sure you import it first.\n7. Write out EVERY CODE DETAIL, DON'T LEAVE TODO.\n\n": "```python\n## main.py\n\nfrom game import Game\n\ndef main():\n width = 20 # Set default width\n height = 20 # Set default height\n game = Game(width, height)\n game.start_game()\n\n while True:\n # Get user input for direction\n direction = input(\"Enter direction (up/down/left/right): \")\n if direction in ['up', 'down', 'left', 'right']:\n if not game.move_snake(direction):\n print(\"Game over!\")\n break\n else:\n print(\"Invalid direction!\")\n\nif __name__ == \"__main__\":\n main()\n```", "\n# System\nRole: You are a professional software engineer, and your main task is to review and revise the code. You need to ensure that the code conforms to the google-style standards, is elegantly designed and modularized, easy to read and maintain.\nLanguage: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese.\nATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced \"Format example\".\n\n# Context\n## System Design\n{\"Implementation approach\":\"We will use Python and the curses library to create the snake game. The game logic will be implemented in a separate module, and the main.py file will handle the user interface and game loop.\",\"File list\":[\"main.py\",\"game.py\"],\"Data structures and interfaces\":\"\\nclassDiagram\\n class Game {\\n -Snake snake\\n -Food food\\n -Score score\\n +__init__(width: int, height: int)\\n +start_game()\\n +move_snake(direction: str)\\n +generate_food()\\n +update_score(points: int)\\n }\\n class Snake {\\n -body list\\n -direction str\\n +__init__(x: int, y: int)\\n +move(direction: str)\\n +grow()\\n +collides_with_self() bool\\n }\\n class Food {\\n -position tuple\\n +__init__(x: int, y: int)\\n +get_position() tuple\\n }\\n class Score {\\n -points int\\n +__init__()\\n +increase(points: int)\\n }\\n Game --> Snake\\n Game --> Food\\n Game --> Score\\n\",\"Program call flow\":\"\\nsequenceDiagram\\n participant M as Main\\n participant G as Game\\n M->>G: start_game()\\n M->>G: move_snake(direction)\\n G->>G: generate_food()\\n G->>G: update_score(points)\\n\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Tasks\n{\"Required Python packages\":[\"curses==2.2.0\"],\"Required Other language third-party packages\":[\"No third-party dependencies required\"],\"Logic Analysis\":[[\"game.py\",\"Contains Game class and ... functions\"],[\"main.py\",\"Contains main function, from game import Game\"]],\"Task list\":[\"game.py\",\"main.py\"],\"Full API spec\":\"\",\"Shared Knowledge\":\"'game.py' contains functions shared across the project.\",\"Anything UNCLEAR\":\"Please provide more details on the game mechanics and user interactions.\"}\n\n## Code Files\n----- game.py\n## game.py\n\nimport curses\n\nclass Snake:\n def __init__(self, x: int, y: int):\n self.body = [(x, y)]\n self.direction = 'right'\n\n def move(self, direction: str):\n if direction == 'up' and self.direction != 'down':\n self.direction = 'up'\n elif direction == 'down' and self.direction != 'up':\n self.direction = 'down'\n elif direction == 'left' and self.direction != 'right':\n self.direction = 'left'\n elif direction == 'right' and self.direction != 'left':\n self.direction = 'right'\n\n head = self.body[0]\n x, y = head\n if self.direction == 'up':\n new_head = (x, y - 1)\n elif self.direction == 'down':\n new_head = (x, y + 1)\n elif self.direction == 'left':\n new_head = (x - 1, y)\n elif self.direction == 'right':\n new_head = (x + 1, y)\n self.body.insert(0, new_head)\n\n def grow(self):\n tail = self.body[-1]\n x, y = tail\n if self.direction == 'up':\n new_tail = (x, y + 1)\n elif self.direction == 'down':\n new_tail = (x, y - 1)\n elif self.direction == 'left':\n new_tail = (x + 1, y)\n elif self.direction == 'right':\n new_tail = (x - 1, y)\n self.body.append(new_tail)\n\n def collides_with_self(self) -> bool:\n return len(self.body) != len(set(self.body))\n\nclass Food:\n def __init__(self, x: int, y: int):\n self.position = (x, y)\n\n def get_position(self) -> tuple:\n return self.position\n\nclass Score:\n def __init__(self):\n self.points = 0\n\n def increase(self, points: int):\n self.points += points\n\nclass Game:\n def __init__(self, width: int, height: int):\n self.snake = Snake(width // 2, height // 2)\n self.food = None\n self.score = Score()\n self.width = width\n self.height = height\n\n def start_game(self):\n self.generate_food()\n\n def move_snake(self, direction: str):\n self.snake.move(direction)\n if self.check_collision():\n return False\n self.check_eat()\n return True\n\n def generate_food(self):\n import random\n x = random.randint(0, self.width - 1)\n y = random.randint(0, self.height - 1)\n self.food = Food(x, y)\n\n def update_score(self, points: int):\n self.score.increase(points)\n\n def check_collision(self) -> bool:\n head = self.snake.body[0]\n if head[0] < 0 or head[0] >= self.width or head[1] < 0 or head[1] >= self.height:\n return True\n if self.snake.collides_with_self():\n return True\n return False\n\n def check_eat(self):\n if self.snake.body[0] == self.food.get_position():\n self.snake.grow()\n self.generate_food()\n self.update_score(10)\n\n\n\n\n## Code to be Reviewed: main.py\n```Code\n## main.py\n\nfrom game import Game\n\ndef main():\n width = 20 # Set default width\n height = 20 # Set default height\n game = Game(width, height)\n game.start_game()\n\n while True:\n # Get user input for direction\n direction = input(\"Enter direction (up/down/left/right): \")\n if direction in ['up', 'down', 'left', 'right']:\n if not game.move_snake(direction):\n print(\"Game over!\")\n break\n else:\n print(\"Invalid direction!\")\n\nif __name__ == \"__main__\":\n main()\n\n```\n\n\n\n# Format example 1\n## Code Review: main.py\n1. No, we should fix the logic of class A due to ...\n2. ...\n3. ...\n4. No, function B is not implemented, ...\n5. ...\n6. ...\n\n## Actions\n1. Fix the `handle_events` method to update the game state only if a move is successful.\n ```python\n def handle_events(self):\n for event in pygame.event.get():\n if event.type == pygame.QUIT:\n return False\n if event.type == pygame.KEYDOWN:\n moved = False\n if event.key == pygame.K_UP:\n moved = self.game.move('UP')\n elif event.key == pygame.K_DOWN:\n moved = self.game.move('DOWN')\n elif event.key == pygame.K_LEFT:\n moved = self.game.move('LEFT')\n elif event.key == pygame.K_RIGHT:\n moved = self.game.move('RIGHT')\n if moved:\n # Update the game state only if a move was successful\n self.render()\n return True\n ```\n2. Implement function B\n\n## Code Review Result\nLBTM\n\n# Format example 2\n## Code Review: main.py\n1. Yes.\n2. Yes.\n3. Yes.\n4. Yes.\n5. Yes.\n6. Yes.\n\n## Actions\npass\n\n## Code Review Result\nLGTM\n\n\n\n# Instruction: Based on the actual code situation, follow one of the \"Format example\". Return only 1 file under review.\n\n## Code Review: Ordered List. Based on the \"Code to be Reviewed\", provide key, clear, concise, and specific answer. If any answer is no, explain how to fix it step by step.\n1. Is the code implemented as per the requirements? If not, how to achieve it? Analyse it step by step.\n2. Is the code logic completely correct? If there are errors, please indicate how to correct them.\n3. Does the existing code follow the \"Data structures and interfaces\"?\n4. Are all functions implemented? If there is no implementation, please indicate how to achieve it step by step.\n5. Have all necessary pre-dependencies been imported? If not, indicate which ones need to be imported\n6. Are methods from other files being reused correctly?\n\n## Actions: Ordered List. Things that should be done after CR, such as implementing class A and function B\n\n## Code Review Result: str. If the code doesn't have bugs, we don't need to rewrite it, so answer LGTM and stop. ONLY ANSWER LGTM/LBTM.\nLGTM/LBTM\n\n": "## Code Review: main.py\n1. The code is implemented as per the requirements. The main function initializes the game and handles user input for the snake's direction.\n2. The code logic is correct. It correctly handles user input and updates the game state based on the snake's movement.\n3. The existing code follows the \"Data structures and interfaces\" as defined in the class diagram.\n4. All necessary functions are implemented in the code.\n5. The necessary pre-dependencies have been imported. The required Python package \"curses==2.2.0\" has been imported.\n6. The methods from the \"game.py\" file are being reused correctly.\n\n## Actions\npass\n\n## Code Review Result\nLGTM", "\n## context\n\n### Project Name\n20240110212717\n\n### Original Requirements\n['开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"zh_cn\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]", - "\n## context\n\n### Project Name\n20240110212717\n\n### Original Requirements\n['']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]" + "\n## context\n\n### Project Name\n20240110212717\n\n### Original Requirements\n['']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]", + "\n## context\n\n### Project Name\n20240110220803\n\n### Original Requirements\n['需要一个基于LLM做总结的搜索引擎']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"zh_cn\",\n \"Programming Language\": \"LLM\",\n \"Original Requirements\": \"需要一个基于LLM做总结的搜索引擎\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]", + "You are a python code to Mermaid Sequence Diagram translator in function detail#SYSTEM_MSG_END#```python\n#!/usr/bin/env python\n# -*- coding: utf-8 -*-\nimport asyncio\nimport shutil\nfrom pathlib import Path\n\nimport typer\n\nfrom metagpt.config2 import config\nfrom metagpt.const import METAGPT_ROOT\n\napp = typer.Typer(add_completion=False, pretty_exceptions_show_locals=False)\n\n\ndef generate_repo(\n idea,\n investment,\n n_round,\n code_review,\n run_tests,\n implement,\n project_name,\n inc,\n project_path,\n reqa_file,\n max_auto_summarize_code,\n recover_path,\n):\n \"\"\"Run the startup logic. Can be called from CLI or other Python scripts.\"\"\"\n from metagpt.roles import (\n Architect,\n Engineer,\n ProductManager,\n ProjectManager,\n QaEngineer,\n )\n from metagpt.team import Team\n\n config.update_via_cli(project_path, project_name, inc, reqa_file, max_auto_summarize_code)\n\n if not recover_path:\n company = Team()\n company.hire(\n [\n ProductManager(),\n Architect(),\n ProjectManager(),\n ]\n )\n\n if implement or code_review:\n company.hire([Engineer(n_borg=5, use_code_review=code_review)])\n\n if run_tests:\n company.hire([QaEngineer()])\n else:\n stg_path = Path(recover_path)\n if not stg_path.exists() or not str(stg_path).endswith(\"team\"):\n raise FileNotFoundError(f\"{recover_path} not exists or not endswith `team`\")\n\n company = Team.deserialize(stg_path=stg_path)\n idea = company.idea\n\n company.invest(investment)\n company.run_project(idea)\n asyncio.run(company.run(n_round=n_round))\n\n\n@app.command(\"\", help=\"Start a new project.\")\ndef startup(\n idea: str = typer.Argument(None, help=\"Your innovative idea, such as 'Create a 2048 game.'\"),\n investment: float = typer.Option(default=3.0, help=\"Dollar amount to invest in the AI company.\"),\n n_round: int = typer.Option(default=5, help=\"Number of rounds for the simulation.\"),\n code_review: bool = typer.Option(default=True, help=\"Whether to use code review.\"),\n run_tests: bool = typer.Option(default=False, help=\"Whether to enable QA for adding & running tests.\"),\n implement: bool = typer.Option(default=True, help=\"Enable or disable code implementation.\"),\n project_name: str = typer.Option(default=\"\", help=\"Unique project name, such as 'game_2048'.\"),\n inc: bool = typer.Option(default=False, help=\"Incremental mode. Use it to coop with existing repo.\"),\n project_path: str = typer.Option(\n default=\"\",\n help=\"Specify the directory path of the old version project to fulfill the incremental requirements.\",\n ),\n reqa_file: str = typer.Option(\n default=\"\", help=\"Specify the source file name for rewriting the quality assurance code.\"\n ),\n max_auto_summarize_code: int = typer.Option(\n default=0,\n help=\"The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating \"\n \"unlimited. This parameter is used for debugging the workflow.\",\n ),\n recover_path: str = typer.Option(default=None, help=\"recover the project from existing serialized storage\"),\n init_config: bool = typer.Option(default=False, help=\"Initialize the configuration file for MetaGPT.\"),\n):\n \"\"\"Run a startup. Be a boss.\"\"\"\n if init_config:\n copy_config_to()\n return\n\n if idea is None:\n typer.echo(\"Missing argument 'IDEA'. Run 'metagpt --help' for more information.\")\n raise typer.Exit()\n\n return generate_repo(\n idea,\n investment,\n n_round,\n code_review,\n run_tests,\n implement,\n project_name,\n inc,\n project_path,\n reqa_file,\n max_auto_summarize_code,\n recover_path,\n )\n\n\ndef copy_config_to(config_path=METAGPT_ROOT / \"config\" / \"config2.yaml\"):\n \"\"\"Initialize the configuration file for MetaGPT.\"\"\"\n target_path = Path.home() / \".metagpt\" / \"config2.yaml\"\n\n # 创建目标目录(如果不存在)\n target_path.parent.mkdir(parents=True, exist_ok=True)\n\n # 如果目标文件已经存在,则重命名为 .bak\n if target_path.exists():\n backup_path = target_path.with_suffix(\".bak\")\n target_path.rename(backup_path)\n print(f\"Existing configuration file backed up at {backup_path}\")\n\n # 复制文件\n shutil.copy(str(config_path), target_path)\n print(f\"Configuration file initialized at {target_path}\")\n\n\nif __name__ == \"__main__\":\n app()\n\n```\n\n---\nTranslate the code above into Mermaid Sequence Diagram.": "```mermaid\nsequenceDiagram\n participant app as app\n participant typer as typer\n participant generate_repo as generate_repo\n participant Team as company\n participant ProductManager as ProductManager\n participant Architect as Architect\n participant ProjectManager as ProjectManager\n participant Engineer as Engineer\n participant QaEngineer as QaEngineer\n\n app -> typer: startup()\n typer -> generate_repo: generate_repo()\n generate_repo -> config: config.update_via_cli()\n generate_repo -> company: company.hire([ProductManager, Architect, ProjectManager])\n generate_repo -> company: company.hire([Engineer])\n generate_repo -> company: company.hire([QaEngineer])\n generate_repo -> company: company.invest()\n generate_repo -> company: company.run_project()\n generate_repo -> company: asyncio.run(company.run())\n\n Note right of generate_repo: If recover_path is provided,
deserialize Team from recover_path\n```", + "You are a python code to Mermaid Sequence Diagram translator in function detail#SYSTEM_MSG_END#```python\n#!/usr/bin/env python\n\nfrom __future__ import annotations\n\nimport asyncio\nimport json\nfrom concurrent import futures\nfrom typing import Literal, overload\n\nfrom metagpt.config2 import config\n\ntry:\n from duckduckgo_search import DDGS\nexcept ImportError:\n raise ImportError(\n \"To use this module, you should have the `duckduckgo_search` Python package installed. \"\n \"You can install it by running the command: `pip install -e.[search-ddg]`\"\n )\n\n\nclass DDGAPIWrapper:\n \"\"\"Wrapper around duckduckgo_search API.\n\n To use this module, you should have the `duckduckgo_search` Python package installed.\n \"\"\"\n\n def __init__(\n self,\n *,\n loop: asyncio.AbstractEventLoop | None = None,\n executor: futures.Executor | None = None,\n ):\n kwargs = {}\n if config.proxy:\n kwargs[\"proxies\"] = config.proxy\n self.loop = loop\n self.executor = executor\n self.ddgs = DDGS(**kwargs)\n\n @overload\n def run(\n self,\n query: str,\n max_results: int = 8,\n as_string: Literal[True] = True,\n focus: list[str] | None = None,\n ) -> str:\n ...\n\n @overload\n def run(\n self,\n query: str,\n max_results: int = 8,\n as_string: Literal[False] = False,\n focus: list[str] | None = None,\n ) -> list[dict[str, str]]:\n ...\n\n async def run(\n self,\n query: str,\n max_results: int = 8,\n as_string: bool = True,\n ) -> str | list[dict]:\n \"\"\"Return the results of a Google search using the official Google API\n\n Args:\n query: The search query.\n max_results: The number of results to return.\n as_string: A boolean flag to determine the return type of the results. If True, the function will\n return a formatted string with the search results. If False, it will return a list of dictionaries\n containing detailed information about each search result.\n\n Returns:\n The results of the search.\n \"\"\"\n loop = self.loop or asyncio.get_event_loop()\n future = loop.run_in_executor(\n self.executor,\n self._search_from_ddgs,\n query,\n max_results,\n )\n search_results = await future\n\n # Return the list of search result URLs\n if as_string:\n return json.dumps(search_results, ensure_ascii=False)\n return search_results\n\n def _search_from_ddgs(self, query: str, max_results: int):\n return [\n {\"link\": i[\"href\"], \"snippet\": i[\"body\"], \"title\": i[\"title\"]}\n for (_, i) in zip(range(max_results), self.ddgs.text(query))\n ]\n\n\nif __name__ == \"__main__\":\n import fire\n\n fire.Fire(DDGAPIWrapper().run)\n\n```\n\n---\nTranslate the code above into Mermaid Sequence Diagram.": "```mermaid\nsequenceDiagram\n participant User\n participant DDGAPIWrapper\n participant asyncio\n participant futures\n participant DDGS\n participant config\n\n User->>DDGAPIWrapper: run(query, max_results, as_string)\n DDGAPIWrapper->>asyncio: get_event_loop()\n asyncio->>DDGAPIWrapper: loop\n alt config.proxy\n DDGAPIWrapper->>config: proxy\n end\n DDGAPIWrapper->>futures: Executor\n futures->>DDGAPIWrapper: executor\n DDGAPIWrapper->>DDGS: __init__(**kwargs)\n DDGAPIWrapper->>asyncio: run_in_executor(executor, _search_from_ddgs, query, max_results)\n asyncio->>DDGAPIWrapper: future\n DDGAPIWrapper->>DDGS: text(query)\n DDGS-->>DDGAPIWrapper: search results\n DDGAPIWrapper-->>asyncio: search_results\n asyncio-->>DDGAPIWrapper: await future\n DDGAPIWrapper-->>User: search results\n```", + "You are a python code to Mermaid Sequence Diagram translator in function detail#SYSTEM_MSG_END#```python\n#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n\"\"\"\n@Time : 2023/5/23 18:27\n@Author : alexanderwu\n@File : search_engine_serpapi.py\n\"\"\"\nfrom typing import Any, Dict, Optional, Tuple\n\nimport aiohttp\nfrom pydantic import BaseModel, ConfigDict, Field, field_validator\n\nfrom metagpt.config2 import config\n\n\nclass SerpAPIWrapper(BaseModel):\n model_config = ConfigDict(arbitrary_types_allowed=True)\n\n search_engine: Any = None #: :meta private:\n params: dict = Field(\n default_factory=lambda: {\n \"engine\": \"google\",\n \"google_domain\": \"google.com\",\n \"gl\": \"us\",\n \"hl\": \"en\",\n }\n )\n # should add `validate_default=True` to check with default value\n serpapi_api_key: Optional[str] = Field(default=None, validate_default=True)\n aiosession: Optional[aiohttp.ClientSession] = None\n\n @field_validator(\"serpapi_api_key\", mode=\"before\")\n @classmethod\n def check_serpapi_api_key(cls, val: str):\n val = val or config.search[\"serpapi\"].api_key\n if not val:\n raise ValueError(\n \"To use, make sure you provide the serpapi_api_key when constructing an object. Alternatively, \"\n \"ensure that the environment variable SERPAPI_API_KEY is set with your API key. You can obtain \"\n \"an API key from https://serpapi.com/.\"\n )\n return val\n\n async def run(self, query, max_results: int = 8, as_string: bool = True, **kwargs: Any) -> str:\n \"\"\"Run query through SerpAPI and parse result async.\"\"\"\n result = await self.results(query, max_results)\n return self._process_response(result, as_string=as_string)\n\n async def results(self, query: str, max_results: int) -> dict:\n \"\"\"Use aiohttp to run query through SerpAPI and return the results async.\"\"\"\n\n def construct_url_and_params() -> Tuple[str, Dict[str, str]]:\n params = self.get_params(query)\n params[\"source\"] = \"python\"\n params[\"num\"] = max_results\n params[\"output\"] = \"json\"\n url = \"https://serpapi.com/search\"\n return url, params\n\n url, params = construct_url_and_params()\n if not self.aiosession:\n async with aiohttp.ClientSession() as session:\n async with session.get(url, params=params) as response:\n res = await response.json()\n else:\n async with self.aiosession.get(url, params=params) as response:\n res = await response.json()\n\n return res\n\n def get_params(self, query: str) -> Dict[str, str]:\n \"\"\"Get parameters for SerpAPI.\"\"\"\n _params = {\n \"api_key\": self.serpapi_api_key,\n \"q\": query,\n }\n params = {**self.params, **_params}\n return params\n\n @staticmethod\n def _process_response(res: dict, as_string: bool) -> str:\n \"\"\"Process response from SerpAPI.\"\"\"\n # logger.debug(res)\n focus = [\"title\", \"snippet\", \"link\"]\n get_focused = lambda x: {i: j for i, j in x.items() if i in focus}\n\n if \"error\" in res.keys():\n raise ValueError(f\"Got error from SerpAPI: {res['error']}\")\n if \"answer_box\" in res.keys() and \"answer\" in res[\"answer_box\"].keys():\n toret = res[\"answer_box\"][\"answer\"]\n elif \"answer_box\" in res.keys() and \"snippet\" in res[\"answer_box\"].keys():\n toret = res[\"answer_box\"][\"snippet\"]\n elif \"answer_box\" in res.keys() and \"snippet_highlighted_words\" in res[\"answer_box\"].keys():\n toret = res[\"answer_box\"][\"snippet_highlighted_words\"][0]\n elif \"sports_results\" in res.keys() and \"game_spotlight\" in res[\"sports_results\"].keys():\n toret = res[\"sports_results\"][\"game_spotlight\"]\n elif \"knowledge_graph\" in res.keys() and \"description\" in res[\"knowledge_graph\"].keys():\n toret = res[\"knowledge_graph\"][\"description\"]\n elif \"snippet\" in res[\"organic_results\"][0].keys():\n toret = res[\"organic_results\"][0][\"snippet\"]\n else:\n toret = \"No good search result found\"\n\n toret_l = []\n if \"answer_box\" in res.keys() and \"snippet\" in res[\"answer_box\"].keys():\n toret_l += [get_focused(res[\"answer_box\"])]\n if res.get(\"organic_results\"):\n toret_l += [get_focused(i) for i in res.get(\"organic_results\")]\n\n return str(toret) + \"\\n\" + str(toret_l) if as_string else toret_l\n\n\nif __name__ == \"__main__\":\n import fire\n\n fire.Fire(SerpAPIWrapper().run)\n\n```\n\n---\nTranslate the code above into Mermaid Sequence Diagram.": "```mermaid\nsequenceDiagram\n participant SerpAPIWrapper\n participant aiohttp\n participant session\n participant response\n participant fire\n\n Note over SerpAPIWrapper: Initialization\n SerpAPIWrapper->>SerpAPIWrapper: __init__\n\n Note over SerpAPIWrapper: Run query through SerpAPI\n SerpAPIWrapper->>SerpAPIWrapper: run(query, max_results, as_string, **kwargs)\n SerpAPIWrapper->>SerpAPIWrapper: results(query, max_results)\n SerpAPIWrapper->>SerpAPIWrapper: get_params(query)\n SerpAPIWrapper->>aiohttp: session.get(url, params)\n aiohttp->>session: get(url, params)\n session->>response: response.json()\n response-->>session: res\n session-->>aiohttp: res\n aiohttp-->>SerpAPIWrapper: res\n SerpAPIWrapper-->>SerpAPIWrapper: _process_response(result, as_string)\n\n Note over SerpAPIWrapper: Use aiohttp to run query through SerpAPI\n SerpAPIWrapper->>SerpAPIWrapper: results(query, max_results)\n SerpAPIWrapper->>SerpAPIWrapper: get_params(query)\n SerpAPIWrapper->>aiohttp: ClientSession()\n aiohttp->>session: get(url, params)\n session->>response: response.json()\n response-->>session: res\n session-->>aiohttp: res\n aiohttp-->>SerpAPIWrapper: res\n\n Note over SerpAPIWrapper: Get parameters for SerpAPI\n SerpAPIWrapper->>SerpAPIWrapper: get_params(query)\n\n Note over SerpAPIWrapper: Process response from SerpAPI\n SerpAPIWrapper->>SerpAPIWrapper: _process_response(res, as_string)\n\n Note over fire: Main function\n fire->>SerpAPIWrapper: run\n\n```", + "You are a python code to Mermaid Sequence Diagram translator in function detail#SYSTEM_MSG_END#```python\n#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n\"\"\"\n@Time : 2023/5/23 18:27\n@Author : alexanderwu\n@File : search_engine_serpapi.py\n\"\"\"\nimport json\nfrom typing import Any, Dict, Optional, Tuple\n\nimport aiohttp\nfrom pydantic import BaseModel, ConfigDict, Field, field_validator\n\nfrom metagpt.config2 import config\n\n\nclass SerperWrapper(BaseModel):\n model_config = ConfigDict(arbitrary_types_allowed=True)\n\n search_engine: Any = None #: :meta private:\n payload: dict = Field(default_factory=lambda: {\"page\": 1, \"num\": 10})\n serper_api_key: Optional[str] = Field(default=None, validate_default=True)\n aiosession: Optional[aiohttp.ClientSession] = None\n\n @field_validator(\"serper_api_key\", mode=\"before\")\n @classmethod\n def check_serper_api_key(cls, val: str):\n val = val or config.search[\"serper\"].api_key\n if not val:\n raise ValueError(\n \"To use, make sure you provide the serper_api_key when constructing an object. Alternatively, \"\n \"ensure that the environment variable SERPER_API_KEY is set with your API key. You can obtain \"\n \"an API key from https://serper.dev/.\"\n )\n return val\n\n async def run(self, query: str, max_results: int = 8, as_string: bool = True, **kwargs: Any) -> str:\n \"\"\"Run query through Serper and parse result async.\"\"\"\n if isinstance(query, str):\n return self._process_response((await self.results([query], max_results))[0], as_string=as_string)\n else:\n results = [self._process_response(res, as_string) for res in await self.results(query, max_results)]\n return \"\\n\".join(results) if as_string else results\n\n async def results(self, queries: list[str], max_results: int = 8) -> dict:\n \"\"\"Use aiohttp to run query through Serper and return the results async.\"\"\"\n\n def construct_url_and_payload_and_headers() -> Tuple[str, Dict[str, str]]:\n payloads = self.get_payloads(queries, max_results)\n url = \"https://google.serper.dev/search\"\n headers = self.get_headers()\n return url, payloads, headers\n\n url, payloads, headers = construct_url_and_payload_and_headers()\n if not self.aiosession:\n async with aiohttp.ClientSession() as session:\n async with session.post(url, data=payloads, headers=headers) as response:\n res = await response.json()\n else:\n async with self.aiosession.get.post(url, data=payloads, headers=headers) as response:\n res = await response.json()\n\n return res\n\n def get_payloads(self, queries: list[str], max_results: int) -> Dict[str, str]:\n \"\"\"Get payloads for Serper.\"\"\"\n payloads = []\n for query in queries:\n _payload = {\n \"q\": query,\n \"num\": max_results,\n }\n payloads.append({**self.payload, **_payload})\n return json.dumps(payloads, sort_keys=True)\n\n def get_headers(self) -> Dict[str, str]:\n headers = {\"X-API-KEY\": self.serper_api_key, \"Content-Type\": \"application/json\"}\n return headers\n\n @staticmethod\n def _process_response(res: dict, as_string: bool = False) -> str:\n \"\"\"Process response from SerpAPI.\"\"\"\n # logger.debug(res)\n focus = [\"title\", \"snippet\", \"link\"]\n\n def get_focused(x):\n return {i: j for i, j in x.items() if i in focus}\n\n if \"error\" in res.keys():\n raise ValueError(f\"Got error from SerpAPI: {res['error']}\")\n if \"answer_box\" in res.keys() and \"answer\" in res[\"answer_box\"].keys():\n toret = res[\"answer_box\"][\"answer\"]\n elif \"answer_box\" in res.keys() and \"snippet\" in res[\"answer_box\"].keys():\n toret = res[\"answer_box\"][\"snippet\"]\n elif \"answer_box\" in res.keys() and \"snippet_highlighted_words\" in res[\"answer_box\"].keys():\n toret = res[\"answer_box\"][\"snippet_highlighted_words\"][0]\n elif \"sports_results\" in res.keys() and \"game_spotlight\" in res[\"sports_results\"].keys():\n toret = res[\"sports_results\"][\"game_spotlight\"]\n elif \"knowledge_graph\" in res.keys() and \"description\" in res[\"knowledge_graph\"].keys():\n toret = res[\"knowledge_graph\"][\"description\"]\n elif \"snippet\" in res[\"organic\"][0].keys():\n toret = res[\"organic\"][0][\"snippet\"]\n else:\n toret = \"No good search result found\"\n\n toret_l = []\n if \"answer_box\" in res.keys() and \"snippet\" in res[\"answer_box\"].keys():\n toret_l += [get_focused(res[\"answer_box\"])]\n if res.get(\"organic\"):\n toret_l += [get_focused(i) for i in res.get(\"organic\")]\n\n return str(toret) + \"\\n\" + str(toret_l) if as_string else toret_l\n\n\nif __name__ == \"__main__\":\n import fire\n\n fire.Fire(SerperWrapper().run)\n\n```\n\n---\nTranslate the code above into Mermaid Sequence Diagram.": "```mermaid\nsequenceDiagram\n participant User\n participant SerperWrapper\n participant aiohttp\n participant pydantic\n participant config\n\n User ->> SerperWrapper: run(query: str, max_results: int, as_string: bool, **kwargs: Any)\n SerperWrapper ->> SerperWrapper: _process_response(response: dict, as_string: bool)\n SerperWrapper ->> SerperWrapper: get_payloads(queries: list[str], max_results: int)\n SerperWrapper ->> SerperWrapper: get_headers()\n SerperWrapper ->> aiohttp: ClientSession.post(url, data, headers)\n aiohttp ->> SerperWrapper: response\n SerperWrapper ->> aiohttp: ClientSession.get.post(url, data, headers)\n aiohttp ->> SerperWrapper: response\n SerperWrapper ->> aiohttp: ClientSession.post(url, data, headers)\n aiohttp ->> SerperWrapper: response\n SerperWrapper ->> User: str\n```", + "You are a python code to Mermaid Sequence Diagram translator in function detail#SYSTEM_MSG_END#```python\n#!/usr/bin/env python\n# -*- coding: utf-8 -*-\nfrom __future__ import annotations\n\nimport asyncio\nimport json\nfrom concurrent import futures\nfrom typing import Optional\nfrom urllib.parse import urlparse\n\nimport httplib2\nfrom pydantic import BaseModel, ConfigDict, Field, field_validator\n\nfrom metagpt.config2 import config\nfrom metagpt.logs import logger\n\ntry:\n from googleapiclient.discovery import build\n from googleapiclient.errors import HttpError\nexcept ImportError:\n raise ImportError(\n \"To use this module, you should have the `google-api-python-client` Python package installed. \"\n \"You can install it by running the command: `pip install -e.[search-google]`\"\n )\n\n\nclass GoogleAPIWrapper(BaseModel):\n model_config = ConfigDict(arbitrary_types_allowed=True)\n\n google_api_key: Optional[str] = Field(default=None, validate_default=True)\n google_cse_id: Optional[str] = Field(default=None, validate_default=True)\n loop: Optional[asyncio.AbstractEventLoop] = None\n executor: Optional[futures.Executor] = None\n\n @field_validator(\"google_api_key\", mode=\"before\")\n @classmethod\n def check_google_api_key(cls, val: str):\n val = val or config.search[\"google\"].api_key\n if not val:\n raise ValueError(\n \"To use, make sure you provide the google_api_key when constructing an object. Alternatively, \"\n \"ensure that the environment variable GOOGLE_API_KEY is set with your API key. You can obtain \"\n \"an API key from https://console.cloud.google.com/apis/credentials.\"\n )\n return val\n\n @field_validator(\"google_cse_id\", mode=\"before\")\n @classmethod\n def check_google_cse_id(cls, val: str):\n val = val or config.search[\"google\"].cse_id\n if not val:\n raise ValueError(\n \"To use, make sure you provide the google_cse_id when constructing an object. Alternatively, \"\n \"ensure that the environment variable GOOGLE_CSE_ID is set with your API key. You can obtain \"\n \"an API key from https://programmablesearchengine.google.com/controlpanel/create.\"\n )\n return val\n\n @property\n def google_api_client(self):\n build_kwargs = {\"developerKey\": self.google_api_key}\n if config.proxy:\n parse_result = urlparse(config.proxy)\n proxy_type = parse_result.scheme\n if proxy_type == \"https\":\n proxy_type = \"http\"\n build_kwargs[\"http\"] = httplib2.Http(\n proxy_info=httplib2.ProxyInfo(\n getattr(httplib2.socks, f\"PROXY_TYPE_{proxy_type.upper()}\"),\n parse_result.hostname,\n parse_result.port,\n ),\n )\n service = build(\"customsearch\", \"v1\", **build_kwargs)\n return service.cse()\n\n async def run(\n self,\n query: str,\n max_results: int = 8,\n as_string: bool = True,\n focus: list[str] | None = None,\n ) -> str | list[dict]:\n \"\"\"Return the results of a Google search using the official Google API.\n\n Args:\n query: The search query.\n max_results: The number of results to return.\n as_string: A boolean flag to determine the return type of the results. If True, the function will\n return a formatted string with the search results. If False, it will return a list of dictionaries\n containing detailed information about each search result.\n focus: Specific information to be focused on from each search result.\n\n Returns:\n The results of the search.\n \"\"\"\n loop = self.loop or asyncio.get_event_loop()\n future = loop.run_in_executor(\n self.executor, self.google_api_client.list(q=query, num=max_results, cx=self.google_cse_id).execute\n )\n try:\n result = await future\n # Extract the search result items from the response\n search_results = result.get(\"items\", [])\n\n except HttpError as e:\n # Handle errors in the API call\n logger.exception(f\"fail to search {query} for {e}\")\n search_results = []\n\n focus = focus or [\"snippet\", \"link\", \"title\"]\n details = [{i: j for i, j in item_dict.items() if i in focus} for item_dict in search_results]\n # Return the list of search result URLs\n if as_string:\n return safe_google_results(details)\n\n return details\n\n\ndef safe_google_results(results: str | list) -> str:\n \"\"\"Return the results of a google search in a safe format.\n\n Args:\n results: The search results.\n\n Returns:\n The results of the search.\n \"\"\"\n if isinstance(results, list):\n safe_message = json.dumps([result for result in results])\n else:\n safe_message = results.encode(\"utf-8\", \"ignore\").decode(\"utf-8\")\n return safe_message\n\n\nif __name__ == \"__main__\":\n import fire\n\n fire.Fire(GoogleAPIWrapper().run)\n\n```\n\n---\nTranslate the code above into Mermaid Sequence Diagram.": "```mermaid\nsequenceDiagram\n participant BaseModel\n participant ConfigDict\n participant Field\n participant field_validator\n participant asyncio\n participant futures\n participant urlparse\n participant httplib2\n participant logger\n participant build\n participant HttpError\n participant GoogleAPIWrapper\n participant fire\n\n BaseModel ->> ConfigDict: model_config\n BaseModel ->> Field: google_api_key\n BaseModel ->> Field: google_cse_id\n BaseModel ->> Field: loop\n BaseModel ->> Field: executor\n Field ->> field_validator: check_google_api_key\n Field ->> field_validator: check_google_cse_id\n GoogleAPIWrapper ->> urlparse: parse_result\n urlparse ->> httplib2: Http\n urlparse ->> httplib2: ProxyInfo\n httplib2 ->> logger: exception\n build ->> GoogleAPIWrapper: google_api_client\n GoogleAPIWrapper ->> asyncio: run\n asyncio ->> futures: run_in_executor\n futures ->> GoogleAPIWrapper: google_api_client.list\n GoogleAPIWrapper ->> HttpError: HttpError\n HttpError ->> logger: exception\n GoogleAPIWrapper ->> safe_google_results: safe_google_results\n safe_google_results -->> GoogleAPIWrapper: safe_message\n GoogleAPIWrapper -->> fire: run\n```", + "You are a python code to Mermaid Sequence Diagram translator in function detail#SYSTEM_MSG_END#```python\nNone\n```\n\n---\nTranslate the code above into Mermaid Sequence Diagram.": "```mermaid\nsequenceDiagram\n participant PythonCode\n PythonCode->>Mermaid: None\n```", + "You are a python code to Mermaid Sequence Diagram translator in function detail#SYSTEM_MSG_END#```python\n#!/usr/bin/env python\n\"\"\"\n@Modified By: mashenquan, 2023/8/22. A definition has been provided for the return value of _think: returning false indicates that further reasoning cannot continue.\n@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of\n the `cause_by` value in the `Message` to a string to support the new message distribution feature.\n\"\"\"\n\nimport asyncio\nimport re\n\nfrom pydantic import BaseModel\n\nfrom metagpt.actions import Action, CollectLinks, ConductResearch, WebBrowseAndSummarize\nfrom metagpt.actions.research import get_research_system_text\nfrom metagpt.const import RESEARCH_PATH\nfrom metagpt.logs import logger\nfrom metagpt.roles.role import Role, RoleReactMode\nfrom metagpt.schema import Message\n\n\nclass Report(BaseModel):\n topic: str\n links: dict[str, list[str]] = None\n summaries: list[tuple[str, str]] = None\n content: str = \"\"\n\n\nclass Researcher(Role):\n name: str = \"David\"\n profile: str = \"Researcher\"\n goal: str = \"Gather information and conduct research\"\n constraints: str = \"Ensure accuracy and relevance of information\"\n language: str = \"en-us\"\n\n def __init__(self, **kwargs):\n super().__init__(**kwargs)\n self.set_actions(\n [CollectLinks(name=self.name), WebBrowseAndSummarize(name=self.name), ConductResearch(name=self.name)]\n )\n self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value)\n if self.language not in (\"en-us\", \"zh-cn\"):\n logger.warning(f\"The language `{self.language}` has not been tested, it may not work.\")\n\n async def _think(self) -> bool:\n if self.rc.todo is None:\n self._set_state(0)\n return True\n\n if self.rc.state + 1 < len(self.states):\n self._set_state(self.rc.state + 1)\n else:\n self.set_todo(None)\n return False\n\n async def _act(self) -> Message:\n logger.info(f\"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})\")\n todo = self.rc.todo\n msg = self.rc.memory.get(k=1)[0]\n if isinstance(msg.instruct_content, Report):\n instruct_content = msg.instruct_content\n topic = instruct_content.topic\n else:\n topic = msg.content\n\n research_system_text = self.research_system_text(topic, todo)\n if isinstance(todo, CollectLinks):\n links = await todo.run(topic, 4, 4)\n ret = Message(\n content=\"\", instruct_content=Report(topic=topic, links=links), role=self.profile, cause_by=todo\n )\n elif isinstance(todo, WebBrowseAndSummarize):\n links = instruct_content.links\n todos = (todo.run(*url, query=query, system_text=research_system_text) for (query, url) in links.items())\n summaries = await asyncio.gather(*todos)\n summaries = list((url, summary) for i in summaries for (url, summary) in i.items() if summary)\n ret = Message(\n content=\"\", instruct_content=Report(topic=topic, summaries=summaries), role=self.profile, cause_by=todo\n )\n else:\n summaries = instruct_content.summaries\n summary_text = \"\\n---\\n\".join(f\"url: {url}\\nsummary: {summary}\" for (url, summary) in summaries)\n content = await self.rc.todo.run(topic, summary_text, system_text=research_system_text)\n ret = Message(\n content=\"\",\n instruct_content=Report(topic=topic, content=content),\n role=self.profile,\n cause_by=self.rc.todo,\n )\n self.rc.memory.add(ret)\n return ret\n\n def research_system_text(self, topic, current_task: Action) -> str:\n \"\"\"BACKWARD compatible\n This allows sub-class able to define its own system prompt based on topic.\n return the previous implementation to have backward compatible\n Args:\n topic:\n language:\n\n Returns: str\n \"\"\"\n return get_research_system_text(topic, self.language)\n\n async def react(self) -> Message:\n msg = await super().react()\n report = msg.instruct_content\n self.write_report(report.topic, report.content)\n return msg\n\n def write_report(self, topic: str, content: str):\n filename = re.sub(r'[\\\\/:\"*?<>|]+', \" \", topic)\n filename = filename.replace(\"\\n\", \"\")\n if not RESEARCH_PATH.exists():\n RESEARCH_PATH.mkdir(parents=True)\n filepath = RESEARCH_PATH / f\"{filename}.md\"\n filepath.write_text(content)\n\n\nif __name__ == \"__main__\":\n import fire\n\n async def main(topic: str, language=\"en-us\"):\n role = Researcher(language=language)\n await role.run(topic)\n\n fire.Fire(main)\n\n```\n\n---\nTranslate the code above into Mermaid Sequence Diagram.": "```mermaid\nsequenceDiagram\n participant Role\n participant CollectLinks\n participant WebBrowseAndSummarize\n participant ConductResearch\n participant Message\n participant Report\n\n Role->>Role: Gather information and conduct research\n Role->>Role: Ensure accuracy and relevance of information\n Role->>Role: Set react mode to BY_ORDER\n\n Role->>Role: to do {todo}({todo.name})\n Role->>CollectLinks: run(topic, 4, 4)\n CollectLinks-->>Role: links\n Role->>Message: Report(topic, links)\n Role->>Role: Add message to memory\n\n Role->>WebBrowseAndSummarize: run(url, query, system_text)\n WebBrowseAndSummarize-->>Role: summaries\n Role->>Message: Report(topic, summaries)\n Role->>Role: Add message to memory\n\n Role->>ConductResearch: run(topic, summary_text, system_text)\n ConductResearch-->>Role: content\n Role->>Message: Report(topic, content)\n Role->>Role: Add message to memory\n\n Role->>Role: React\n Role->>Role: Write report to file\n```", + "You are a python code to Mermaid Sequence Diagram translator in function detail#SYSTEM_MSG_END#```python\n\"\"\"Code Docstring Generator.\n\nThis script provides a tool to automatically generate docstrings for Python code. It uses the specified style to create\ndocstrings for the given code and system text.\n\nUsage:\n python3 -m metagpt.actions.write_docstring [--overwrite] [--style=]\n\nArguments:\n filename The path to the Python file for which you want to generate docstrings.\n\nOptions:\n --overwrite If specified, overwrite the original file with the code containing docstrings.\n --style= Specify the style of the generated docstrings.\n Valid values: 'google', 'numpy', or 'sphinx'.\n Default: 'google'\n\nExample:\n python3 -m metagpt.actions.write_docstring ./metagpt/startup.py --overwrite False --style=numpy\n\nThis script uses the 'fire' library to create a command-line interface. It generates docstrings for the given Python code using\nthe specified docstring style and adds them to the code.\n\"\"\"\nfrom __future__ import annotations\n\nimport ast\nfrom pathlib import Path\nfrom typing import Literal, Optional\n\nfrom metagpt.actions.action import Action\nfrom metagpt.utils.common import OutputParser, aread, awrite\nfrom metagpt.utils.pycst import merge_docstring\n\nPYTHON_DOCSTRING_SYSTEM = \"\"\"### Requirements\n1. Add docstrings to the given code following the {style} style.\n2. Replace the function body with an Ellipsis object(...) to reduce output.\n3. If the types are already annotated, there is no need to include them in the docstring.\n4. Extract only class, function or the docstrings for the module parts from the given Python code, avoiding any other text.\n\n### Input Example\n```python\ndef function_with_pep484_type_annotations(param1: int) -> bool:\n return isinstance(param1, int)\n\nclass ExampleError(Exception):\n def __init__(self, msg: str):\n self.msg = msg\n```\n\n### Output Example\n```python\n{example}\n```\n\"\"\"\n\n# https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html\n\nPYTHON_DOCSTRING_EXAMPLE_GOOGLE = '''\ndef function_with_pep484_type_annotations(param1: int) -> bool:\n \"\"\"Example function with PEP 484 type annotations.\n\n Extended description of function.\n\n Args:\n param1: The first parameter.\n\n Returns:\n The return value. True for success, False otherwise.\n \"\"\"\n ...\n\nclass ExampleError(Exception):\n \"\"\"Exceptions are documented in the same way as classes.\n\n The __init__ method was documented in the class level docstring.\n\n Args:\n msg: Human readable string describing the exception.\n\n Attributes:\n msg: Human readable string describing the exception.\n \"\"\"\n ...\n'''\n\nPYTHON_DOCSTRING_EXAMPLE_NUMPY = '''\ndef function_with_pep484_type_annotations(param1: int) -> bool:\n \"\"\"\n Example function with PEP 484 type annotations.\n\n Extended description of function.\n\n Parameters\n ----------\n param1\n The first parameter.\n\n Returns\n -------\n bool\n The return value. True for success, False otherwise.\n \"\"\"\n ...\n\nclass ExampleError(Exception):\n \"\"\"\n Exceptions are documented in the same way as classes.\n\n The __init__ method was documented in the class level docstring.\n\n Parameters\n ----------\n msg\n Human readable string describing the exception.\n\n Attributes\n ----------\n msg\n Human readable string describing the exception.\n \"\"\"\n ...\n'''\n\nPYTHON_DOCSTRING_EXAMPLE_SPHINX = '''\ndef function_with_pep484_type_annotations(param1: int) -> bool:\n \"\"\"Example function with PEP 484 type annotations.\n\n Extended description of function.\n\n :param param1: The first parameter.\n :type param1: int\n\n :return: The return value. True for success, False otherwise.\n :rtype: bool\n \"\"\"\n ...\n\nclass ExampleError(Exception):\n \"\"\"Exceptions are documented in the same way as classes.\n\n The __init__ method was documented in the class level docstring.\n\n :param msg: Human-readable string describing the exception.\n :type msg: str\n \"\"\"\n ...\n'''\n\n_python_docstring_style = {\n \"google\": PYTHON_DOCSTRING_EXAMPLE_GOOGLE.strip(),\n \"numpy\": PYTHON_DOCSTRING_EXAMPLE_NUMPY.strip(),\n \"sphinx\": PYTHON_DOCSTRING_EXAMPLE_SPHINX.strip(),\n}\n\n\nclass WriteDocstring(Action):\n \"\"\"This class is used to write docstrings for code.\n\n Attributes:\n desc: A string describing the action.\n \"\"\"\n\n desc: str = \"Write docstring for code.\"\n i_context: Optional[str] = None\n\n async def run(\n self,\n code: str,\n system_text: str = PYTHON_DOCSTRING_SYSTEM,\n style: Literal[\"google\", \"numpy\", \"sphinx\"] = \"google\",\n ) -> str:\n \"\"\"Writes docstrings for the given code and system text in the specified style.\n\n Args:\n code: A string of Python code.\n system_text: A string of system text.\n style: A string specifying the style of the docstring. Can be 'google', 'numpy', or 'sphinx'.\n\n Returns:\n The Python code with docstrings added.\n \"\"\"\n system_text = system_text.format(style=style, example=_python_docstring_style[style])\n simplified_code = _simplify_python_code(code)\n documented_code = await self._aask(f\"```python\\n{simplified_code}\\n```\", [system_text])\n documented_code = OutputParser.parse_python_code(documented_code)\n return merge_docstring(code, documented_code)\n\n @staticmethod\n async def write_docstring(\n filename: str | Path, overwrite: bool = False, style: Literal[\"google\", \"numpy\", \"sphinx\"] = \"google\"\n ) -> str:\n data = await aread(str(filename))\n code = await WriteDocstring().run(data, style=style)\n if overwrite:\n await awrite(filename, code)\n return code\n\n\ndef _simplify_python_code(code: str) -> None:\n \"\"\"Simplifies the given Python code by removing expressions and the last if statement.\n\n Args:\n code: A string of Python code.\n\n Returns:\n The simplified Python code.\n \"\"\"\n code_tree = ast.parse(code)\n code_tree.body = [i for i in code_tree.body if not isinstance(i, ast.Expr)]\n if isinstance(code_tree.body[-1], ast.If):\n code_tree.body.pop()\n return ast.unparse(code_tree)\n\n\nif __name__ == \"__main__\":\n import fire\n\n fire.Fire(WriteDocstring.write_docstring)\n\n```\n\n---\nTranslate the code above into Mermaid Sequence Diagram.": "```mermaid\nsequenceDiagram\n participant User\n participant \"WriteDocstring\" as WD\n participant \"OutputParser\" as OP\n participant \"aread\" as AR\n participant \"awrite\" as AW\n\n User ->> WD: write_docstring(filename, overwrite, style)\n WD ->> AR: aread(filename)\n AR -->> WD: data\n WD ->> WD: run(data, style)\n WD ->> OP: parse_python_code(documented_code)\n OP -->> WD: documented_code\n WD ->> WD: merge_docstring(code, documented_code)\n WD ->> AW: awrite(filename, code)\n AW -->> WD: code\n WD -->> User: code\n```", + "\n## context\n\n### Project Name\n20240110221009\n\n### Original Requirements\n['开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"zh_cn\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]", + "\n## context\n\n### Project Name\n20240110221525\n\n### Original Requirements\n['需要一个基于LLM做总结的搜索引擎']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"zh_cn\",\n \"Programming Language\": \"LLM\",\n \"Original Requirements\": \"需要一个基于LLM做总结的搜索引擎\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]", + "\n## context\n\n### Project Name\n20240110221737\n\n### Original Requirements\n['开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"zh_cn\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]", + "\n## context\n\n### Project Name\n20240110221737\n\n### Original Requirements\n['']\n\n### Search Information\n-\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"Create a 2048 game\",\n \"Product Goals\": [\n \"Create an engaging user experience\",\n \"Improve accessibility, be responsive\",\n \"More beautiful UI\"\n ],\n \"User Stories\": [\n \"As a player, I want to be able to choose difficulty levels\",\n \"As a player, I want to see my score after each game\",\n \"As a player, I want to get restart button when I lose\",\n \"As a player, I want to see beautiful UI that make me feel good\",\n \"As a player, I want to play game via mobile phone\"\n ],\n \"Competitive Analysis\": [\n \"2048 Game A: Simple interface, lacks responsive features\",\n \"play2048.co: Beautiful and responsive UI with my best score shown\",\n \"2048game.com: Responsive UI with my best score shown, but many ads\"\n ],\n \"Competitive Quadrant Chart\": \"quadrantChart\\n title \\\"Reach and engagement of campaigns\\\"\\n x-axis \\\"Low Reach\\\" --> \\\"High Reach\\\"\\n y-axis \\\"Low Engagement\\\" --> \\\"High Engagement\\\"\\n quadrant-1 \\\"We should expand\\\"\\n quadrant-2 \\\"Need to promote\\\"\\n quadrant-3 \\\"Re-evaluate\\\"\\n quadrant-4 \\\"May be improved\\\"\\n \\\"Campaign A\\\": [0.3, 0.6]\\n \\\"Campaign B\\\": [0.45, 0.23]\\n \\\"Campaign C\\\": [0.57, 0.69]\\n \\\"Campaign D\\\": [0.78, 0.34]\\n \\\"Campaign E\\\": [0.40, 0.34]\\n \\\"Campaign F\\\": [0.35, 0.78]\\n \\\"Our Target Product\\\": [0.5, 0.6]\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [\n [\n \"P0\",\n \"The main code ...\"\n ],\n [\n \"P0\",\n \"The game algorithm ...\"\n ]\n ],\n \"UI Design draft\": \"Basic function description with a simple style and layout.\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- Language: # Provide the language used in the project, typically matching the user's requirement language.\n- Programming Language: # Python/JavaScript or other mainstream programming language.\n- Original Requirements: # Place the original user's requirements here.\n- Product Goals: typing.List[str] # Provide up to three clear, orthogonal product goals.\n- User Stories: typing.List[str] # Provide up to 3 to 5 scenario-based user stories.\n- Competitive Analysis: typing.List[str] # Provide 5 to 7 competitive products.\n- Competitive Quadrant Chart: # Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1\n- Requirement Analysis: # Provide a detailed analysis of the requirements.\n- Requirement Pool: typing.List[typing.List[str]] # List down the top-5 requirements with their priority (P0, P1, P2).\n- UI Design draft: # Provide a simple description of UI elements, functions, style, and layout.\n- Anything UNCLEAR: # Mention any aspects of the project that are unclear and try to clarify them.\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"Language\": \"en_us\",\n \"Programming Language\": \"Python\",\n \"Original Requirements\": \"\",\n \"Product Goals\": [],\n \"User Stories\": [],\n \"Competitive Analysis\": [],\n \"Competitive Quadrant Chart\": \"\",\n \"Requirement Analysis\": \"\",\n \"Requirement Pool\": [],\n \"UI Design draft\": \"\",\n \"Anything UNCLEAR\": \"\"\n}\n[/CONTENT]" } \ No newline at end of file diff --git a/tests/metagpt/provider/test_openai.py b/tests/metagpt/provider/test_openai.py index ee69da861..ca9e918da 100644 --- a/tests/metagpt/provider/test_openai.py +++ b/tests/metagpt/provider/test_openai.py @@ -12,7 +12,7 @@ from tests.metagpt.provider.mock_llm_config import ( @pytest.mark.asyncio async def test_aask_code(): - llm = LLM(name="gpt3t") + llm = LLM() msg = [{"role": "user", "content": "Write a python hello world code."}] rsp = await llm.aask_code(msg) # -> {'language': 'python', 'code': "print('Hello, World!')"} @@ -24,7 +24,7 @@ async def test_aask_code(): @pytest.mark.asyncio async def test_aask_code_str(): - llm = LLM(name="gpt3t") + llm = LLM() msg = "Write a python hello world code." rsp = await llm.aask_code(msg) # -> {'language': 'python', 'code': "print('Hello, World!')"} assert "language" in rsp @@ -34,7 +34,7 @@ async def test_aask_code_str(): @pytest.mark.asyncio async def test_aask_code_message(): - llm = LLM(name="gpt3t") + llm = LLM() msg = UserMessage("Write a python hello world code.") rsp = await llm.aask_code(msg) # -> {'language': 'python', 'code': "print('Hello, World!')"} assert "language" in rsp diff --git a/tests/metagpt/test_config.py b/tests/metagpt/test_config.py index c804702dd..97d84ed09 100644 --- a/tests/metagpt/test_config.py +++ b/tests/metagpt/test_config.py @@ -10,7 +10,10 @@ from pydantic import BaseModel from metagpt.config2 import Config from metagpt.configs.llm_config import LLMType from metagpt.context import ContextMixin -from tests.metagpt.provider.mock_llm_config import mock_llm_config +from tests.metagpt.provider.mock_llm_config import ( + mock_llm_config, + mock_llm_config_proxy, +) def test_config_1(): @@ -21,9 +24,9 @@ def test_config_1(): def test_config_from_dict(): - cfg = Config(llm={"default": mock_llm_config}) + cfg = Config(llm=mock_llm_config) assert cfg - assert cfg.llm["default"].api_key == "mock_api_key" + assert cfg.llm.api_key == "mock_api_key" class ModelX(ContextMixin, BaseModel): @@ -47,11 +50,11 @@ def test_config_mixin_1(): def test_config_mixin_2(): - i = Config(llm={"default": mock_llm_config}) - j = Config(llm={"new": mock_llm_config}) + i = Config(llm=mock_llm_config) + j = Config(llm=mock_llm_config_proxy) obj = ModelX(config=i) assert obj._config == i - assert obj._config.llm["default"] == mock_llm_config + assert obj._config.llm == mock_llm_config obj.set_config(j) # obj already has a config, so it will not be set @@ -60,16 +63,16 @@ def test_config_mixin_2(): def test_config_mixin_3(): """Test config mixin with multiple inheritance""" - i = Config(llm={"default": mock_llm_config}) - j = Config(llm={"new": mock_llm_config}) + i = Config(llm=mock_llm_config) + j = Config(llm=mock_llm_config_proxy) obj = ModelY(config=i) assert obj._config == i - assert obj._config.llm["default"] == mock_llm_config + assert obj._config.llm == mock_llm_config obj.set_config(j) # obj already has a config, so it will not be set assert obj._config == i - assert obj._config.llm["default"] == mock_llm_config + assert obj._config.llm == mock_llm_config assert obj.a == "a" assert obj.b == "b" diff --git a/tests/metagpt/tools/test_search_engine.py b/tests/metagpt/tools/test_search_engine.py index 411929f64..1cdecb3e9 100644 --- a/tests/metagpt/tools/test_search_engine.py +++ b/tests/metagpt/tools/test_search_engine.py @@ -49,13 +49,14 @@ class MockSearchEnine: async def test_search_engine(search_engine_type, run_func: Callable, max_results: int, as_string: bool, aiohttp_mocker): # Prerequisites cache_json_path = None + # FIXME: 不能使用全局的config,而是要自己实例化对应的config if search_engine_type is SearchEngineType.SERPAPI_GOOGLE: - assert config.search["serpapi"] + assert config.search cache_json_path = search_cache_path / f"serpapi-metagpt-{max_results}.json" elif search_engine_type is SearchEngineType.DIRECT_GOOGLE: - assert config.search["google"] + assert config.search elif search_engine_type is SearchEngineType.SERPER_GOOGLE: - assert config.search["serper"] + assert config.search cache_json_path = search_cache_path / f"serper-metagpt-{max_results}.json" if cache_json_path: From 6dab0ad19ec0842338e787b164f672fd666aeaf8 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 11 Jan 2024 15:37:51 +0800 Subject: [PATCH 47/50] remove Dict, use direct LLMConfig / Browser. / Search. / Mermaid. instead --- tests/metagpt/memory/test_brain_memory.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/metagpt/memory/test_brain_memory.py b/tests/metagpt/memory/test_brain_memory.py index c06b5cf1a..72ffcc538 100644 --- a/tests/metagpt/memory/test_brain_memory.py +++ b/tests/metagpt/memory/test_brain_memory.py @@ -8,7 +8,6 @@ import pytest -from metagpt.configs.llm_config import LLMType from metagpt.llm import LLM from metagpt.memory.brain_memory import BrainMemory from metagpt.schema import Message @@ -46,7 +45,7 @@ def test_extract_info(input, tag, val): @pytest.mark.asyncio -@pytest.mark.parametrize("llm", [LLM(provider=LLMType.OPENAI)]) # , LLM(provider=LLMType.METAGPT) +@pytest.mark.parametrize("llm", [LLM()]) async def test_memory_llm(llm): memory = BrainMemory() for i in range(500): From 9a12ae36a4aeca3c9b140681126f547fe8efda87 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 11 Jan 2024 15:44:12 +0800 Subject: [PATCH 48/50] remove config.py and test --- config/config.yaml | 144 --------------- config/config2.yaml.example | 41 ++--- config/config2.yaml.mock | 55 ------ metagpt/config.py | 270 ----------------------------- tests/metagpt/utils/test_config.py | 38 ---- 5 files changed, 14 insertions(+), 534 deletions(-) delete mode 100644 config/config.yaml delete mode 100644 config/config2.yaml.mock delete mode 100644 metagpt/config.py delete mode 100644 tests/metagpt/utils/test_config.py diff --git a/config/config.yaml b/config/config.yaml deleted file mode 100644 index 6dff55b4e..000000000 --- a/config/config.yaml +++ /dev/null @@ -1,144 +0,0 @@ -# DO NOT MODIFY THIS FILE, create a new key.yaml, define OPENAI_API_KEY. -# The configuration of key.yaml has a higher priority and will not enter git - -#### Project Path Setting -# WORKSPACE_PATH: "Path for placing output files" - -#### if OpenAI -## The official OPENAI_BASE_URL is https://api.openai.com/v1 -## If the official OPENAI_BASE_URL is not available, we recommend using the [openai-forward](https://github.com/beidongjiedeguang/openai-forward). -## Or, you can configure OPENAI_PROXY to access official OPENAI_BASE_URL. -OPENAI_BASE_URL: "https://api.openai.com/v1" -#OPENAI_PROXY: "http://127.0.0.1:8118" -#OPENAI_API_KEY: "YOUR_API_KEY" # set the value to sk-xxx if you host the openai interface for open llm model -OPENAI_API_MODEL: "gpt-4-1106-preview" -MAX_TOKENS: 4096 -RPM: 10 -TIMEOUT: 60 # Timeout for llm invocation -#DEFAULT_PROVIDER: openai - -#### if Spark -#SPARK_APPID : "YOUR_APPID" -#SPARK_API_SECRET : "YOUR_APISecret" -#SPARK_API_KEY : "YOUR_APIKey" -#DOMAIN : "generalv2" -#SPARK_URL : "ws://spark-api.xf-yun.com/v2.1/chat" - -#### if Anthropic -#ANTHROPIC_API_KEY: "YOUR_API_KEY" - -#### if AZURE, check https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb -#OPENAI_API_TYPE: "azure" -#OPENAI_BASE_URL: "YOUR_AZURE_ENDPOINT" -#OPENAI_API_KEY: "YOUR_AZURE_API_KEY" -#OPENAI_API_VERSION: "YOUR_AZURE_API_VERSION" -#DEPLOYMENT_NAME: "YOUR_DEPLOYMENT_NAME" - -#### if zhipuai from `https://open.bigmodel.cn`. You can set here or export API_KEY="YOUR_API_KEY" -# ZHIPUAI_API_KEY: "YOUR_API_KEY" - -#### if Google Gemini from `https://ai.google.dev/` and API_KEY from `https://makersuite.google.com/app/apikey`. -#### You can set here or export GOOGLE_API_KEY="YOUR_API_KEY" -# GEMINI_API_KEY: "YOUR_API_KEY" - -#### if use self-host open llm model with openai-compatible interface -#OPEN_LLM_API_BASE: "http://127.0.0.1:8000/v1" -#OPEN_LLM_API_MODEL: "llama2-13b" -# -##### if use Fireworks api -#FIREWORKS_API_KEY: "YOUR_API_KEY" -#FIREWORKS_API_BASE: "https://api.fireworks.ai/inference/v1" -#FIREWORKS_API_MODEL: "YOUR_LLM_MODEL" # example, accounts/fireworks/models/llama-v2-13b-chat - -#### if use self-host open llm model by ollama -# OLLAMA_API_BASE: http://127.0.0.1:11434/api -# OLLAMA_API_MODEL: llama2 - -#### for Search - -## Supported values: serpapi/google/serper/ddg -#SEARCH_ENGINE: serpapi - -## Visit https://serpapi.com/ to get key. -#SERPAPI_API_KEY: "YOUR_API_KEY" - -## Visit https://console.cloud.google.com/apis/credentials to get key. -#GOOGLE_API_KEY: "YOUR_API_KEY" -## Visit https://programmablesearchengine.google.com/controlpanel/create to get id. -#GOOGLE_CSE_ID: "YOUR_CSE_ID" - -## Visit https://serper.dev/ to get key. -#SERPER_API_KEY: "YOUR_API_KEY" - -#### for web access - -## Supported values: playwright/selenium -#WEB_BROWSER_ENGINE: playwright - -## Supported values: chromium/firefox/webkit, visit https://playwright.dev/python/docs/api/class-browsertype -##PLAYWRIGHT_BROWSER_TYPE: chromium - -## Supported values: chrome/firefox/edge/ie, visit https://www.selenium.dev/documentation/webdriver/browsers/ -# SELENIUM_BROWSER_TYPE: chrome - -#### for TTS - -#AZURE_TTS_SUBSCRIPTION_KEY: "YOUR_API_KEY" -#AZURE_TTS_REGION: "eastus" - -#### for Stable Diffusion -## Use SD service, based on https://github.com/AUTOMATIC1111/stable-diffusion-webui -#SD_URL: "YOUR_SD_URL" -#SD_T2I_API: "/sdapi/v1/txt2img" - -#### for Execution -#LONG_TERM_MEMORY: false - -#### for Mermaid CLI -## If you installed mmdc (Mermaid CLI) only for metagpt then enable the following configuration. -#PUPPETEER_CONFIG: "./config/puppeteer-config.json" -#MMDC: "./node_modules/.bin/mmdc" - - -### for calc_usage -# CALC_USAGE: false - -### for Research -# MODEL_FOR_RESEARCHER_SUMMARY: gpt-3.5-turbo -# MODEL_FOR_RESEARCHER_REPORT: gpt-3.5-turbo-16k - -### choose the engine for mermaid conversion, -# default is nodejs, you can change it to playwright,pyppeteer or ink -# MERMAID_ENGINE: nodejs - -### browser path for pyppeteer engine, support Chrome, Chromium,MS Edge -#PYPPETEER_EXECUTABLE_PATH: "/usr/bin/google-chrome-stable" - -### for repair non-openai LLM's output when parse json-text if PROMPT_FORMAT=json -### due to non-openai LLM's output will not always follow the instruction, so here activate a post-process -### repair operation on the content extracted from LLM's raw output. Warning, it improves the result but not fix all cases. -# REPAIR_LLM_OUTPUT: false - -# PROMPT_FORMAT: json #json or markdown - -### Agent configurations -# RAISE_NOT_CONFIG_ERROR: true # "true" if the LLM key is not configured, throw a NotConfiguredException, else "false". -# WORKSPACE_PATH_WITH_UID: false # "true" if using `{workspace}/{uid}` as the workspace path; "false" use `{workspace}`. - -### Meta Models -#METAGPT_TEXT_TO_IMAGE_MODEL: MODEL_URL - -### S3 config -#S3_ACCESS_KEY: "YOUR_S3_ACCESS_KEY" -#S3_SECRET_KEY: "YOUR_S3_SECRET_KEY" -#S3_ENDPOINT_URL: "YOUR_S3_ENDPOINT_URL" -#S3_SECURE: true # true/false -#S3_BUCKET: "YOUR_S3_BUCKET" - -### Redis config -#REDIS_HOST: "YOUR_REDIS_HOST" -#REDIS_PORT: "YOUR_REDIS_PORT" -#REDIS_PASSWORD: "YOUR_REDIS_PASSWORD" -#REDIS_DB: "YOUR_REDIS_DB_INDEX, str, 0-based" - -# DISABLE_LLM_PROVIDER_CHECK: false diff --git a/config/config2.yaml.example b/config/config2.yaml.example index 2c655f881..35575e5a5 100644 --- a/config/config2.yaml.example +++ b/config/config2.yaml.example @@ -1,33 +1,20 @@ llm: - gpt3t: - base_url: "YOUR_BASE_URL" - api_key: "YOUR_API_KEY" - model: "gpt-3.5-turbo-1106" # or gpt-4-1106-preview - azure-gpt3t: - api_type: "azure" - base_url: "YOUR_BASE_URL" - api_key: "YOUR_API_KEY" - model: "gpt35turbo" - -search: - serpapi: - api_type: "serpapi" - api_key: "YOUR_API_KEY" - google: - api_type: "google" - api_key: "YOUR_API_KEY" - cse_id: "YOUR_CSE_ID" - serper: - api_type: "serper" - api_key: "YOUR_API_KEY" - -mermaid: - pyppeteer: - engine: "pyppeteer" - path: "/Applications/Google Chrome.app" + api_type: "openai" + base_url: "YOUR_BASE_URL" + api_key: "YOUR_API_KEY" + model: "gpt-3.5-turbo-1106" # or gpt-4-1106-preview proxy: "YOUR_PROXY" +search: + api_type: "google" + api_key: "YOUR_API_KEY" + cse_id: "YOUR_CSE_ID" + +mermaid: + engine: "pyppeteer" + path: "/Applications/Google Chrome.app" + redis: host: "YOUR_HOST" port: 32582 @@ -36,7 +23,7 @@ redis: s3: access_key: "YOUR_ACCESS_KEY" - secret_key: "YOUR_SECRET_KEY + secret_key: "YOUR_SECRET_KEY" endpoint: "YOUR_ENDPOINT" secure: false bucket: "test" diff --git a/config/config2.yaml.mock b/config/config2.yaml.mock deleted file mode 100644 index 2c655f881..000000000 --- a/config/config2.yaml.mock +++ /dev/null @@ -1,55 +0,0 @@ -llm: - gpt3t: - base_url: "YOUR_BASE_URL" - api_key: "YOUR_API_KEY" - model: "gpt-3.5-turbo-1106" # or gpt-4-1106-preview - azure-gpt3t: - api_type: "azure" - base_url: "YOUR_BASE_URL" - api_key: "YOUR_API_KEY" - model: "gpt35turbo" - -search: - serpapi: - api_type: "serpapi" - api_key: "YOUR_API_KEY" - google: - api_type: "google" - api_key: "YOUR_API_KEY" - cse_id: "YOUR_CSE_ID" - serper: - api_type: "serper" - api_key: "YOUR_API_KEY" - -mermaid: - pyppeteer: - engine: "pyppeteer" - path: "/Applications/Google Chrome.app" - -proxy: "YOUR_PROXY" - -redis: - host: "YOUR_HOST" - port: 32582 - password: "YOUR_PASSWORD" - db: "0" - -s3: - access_key: "YOUR_ACCESS_KEY" - secret_key: "YOUR_SECRET_KEY - endpoint: "YOUR_ENDPOINT" - secure: false - bucket: "test" - - -AZURE_TTS_SUBSCRIPTION_KEY: "YOUR_SUBSCRIPTION_KEY" -AZURE_TTS_REGION: "eastus" - -IFLYTEK_APP_ID: "YOUR_APP_ID" -IFLYTEK_API_KEY: "YOUR_API_KEY" -IFLYTEK_API_SECRET: "YOUR_API_SECRET" - -METAGPT_TEXT_TO_IMAGE_MODEL_URL: "YOUR_MODEL_URL" - -PYPPETEER_EXECUTABLE_PATH: "/Applications/Google Chrome.app" - diff --git a/metagpt/config.py b/metagpt/config.py deleted file mode 100644 index 952ccc962..000000000 --- a/metagpt/config.py +++ /dev/null @@ -1,270 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -Provide configuration, singleton -@Modified By: mashenquan, 2023/11/27. - 1. According to Section 2.2.3.11 of RFC 135, add git repository support. - 2. Add the parameter `src_workspace` for the old version project path. -""" -import datetime -import json -import os -import warnings -from copy import deepcopy -from pathlib import Path -from typing import Any -from uuid import uuid4 - -import yaml - -from metagpt.configs.llm_config import LLMType -from metagpt.const import DEFAULT_WORKSPACE_ROOT, METAGPT_ROOT, OPTIONS -from metagpt.logs import logger -from metagpt.tools import SearchEngineType, WebBrowserEngineType -from metagpt.utils.common import require_python_version -from metagpt.utils.cost_manager import CostManager -from metagpt.utils.singleton import Singleton - - -class NotConfiguredException(Exception): - """Exception raised for errors in the configuration. - - Attributes: - message -- explanation of the error - """ - - def __init__(self, message="The required configuration is not set"): - self.message = message - super().__init__(self.message) - - -class Config(metaclass=Singleton): - """ - Regular usage method: - config = Config("config.yaml") - secret_key = config.get_key("MY_SECRET_KEY") - print("Secret key:", secret_key) - """ - - _instance = None - home_yaml_file = Path.home() / ".metagpt/config.yaml" - key_yaml_file = METAGPT_ROOT / "config/key.yaml" - default_yaml_file = METAGPT_ROOT / "config/config.yaml" - - def __init__(self, yaml_file=default_yaml_file, cost_data=""): - global_options = OPTIONS.get() - # cli paras - self.project_path = "" - self.project_name = "" - self.inc = False - self.reqa_file = "" - self.max_auto_summarize_code = 0 - self.git_reinit = False - - self._init_with_config_files_and_env(yaml_file) - # The agent needs to be billed per user, so billing information cannot be destroyed when the session ends. - self.cost_manager = CostManager(**json.loads(cost_data)) if cost_data else CostManager() - self._update() - global_options.update(OPTIONS.get()) - logger.debug("Config loading done.") - - def get_default_llm_provider_enum(self) -> LLMType: - """Get first valid LLM provider enum""" - mappings = { - LLMType.OPENAI: bool( - self._is_valid_llm_key(self.OPENAI_API_KEY) and not self.OPENAI_API_TYPE and self.OPENAI_API_MODEL - ), - LLMType.ANTHROPIC: self._is_valid_llm_key(self.ANTHROPIC_API_KEY), - LLMType.ZHIPUAI: self._is_valid_llm_key(self.ZHIPUAI_API_KEY), - LLMType.FIREWORKS: self._is_valid_llm_key(self.FIREWORKS_API_KEY), - LLMType.OPEN_LLM: self._is_valid_llm_key(self.OPEN_LLM_API_BASE), - LLMType.GEMINI: self._is_valid_llm_key(self.GEMINI_API_KEY), - LLMType.METAGPT: bool(self._is_valid_llm_key(self.OPENAI_API_KEY) and self.OPENAI_API_TYPE == "metagpt"), - LLMType.AZURE: bool( - self._is_valid_llm_key(self.OPENAI_API_KEY) - and self.OPENAI_API_TYPE == "azure" - and self.DEPLOYMENT_NAME - and self.OPENAI_API_VERSION - ), - LLMType.OLLAMA: self._is_valid_llm_key(self.OLLAMA_API_BASE), - } - provider = None - for k, v in mappings.items(): - if v: - provider = k - break - if provider is None: - if self.DEFAULT_PROVIDER: - provider = LLMType(self.DEFAULT_PROVIDER) - else: - raise NotConfiguredException("You should config a LLM configuration first") - - if provider is LLMType.GEMINI and not require_python_version(req_version=(3, 10)): - warnings.warn("Use Gemini requires Python >= 3.10") - model_name = self.get_model_name(provider=provider) - if model_name: - logger.info(f"{provider} Model: {model_name}") - if provider: - logger.info(f"API: {provider}") - return provider - - def get_model_name(self, provider=None) -> str: - provider = provider or self.get_default_llm_provider_enum() - model_mappings = { - LLMType.OPENAI: self.OPENAI_API_MODEL, - LLMType.AZURE: self.DEPLOYMENT_NAME, - } - return model_mappings.get(provider, "") - - @staticmethod - def _is_valid_llm_key(k: str) -> bool: - return bool(k and k != "YOUR_API_KEY") - - def _update(self): - self.global_proxy = self._get("GLOBAL_PROXY") - - self.openai_api_key = self._get("OPENAI_API_KEY") - self.anthropic_api_key = self._get("ANTHROPIC_API_KEY") - self.zhipuai_api_key = self._get("ZHIPUAI_API_KEY") - self.open_llm_api_base = self._get("OPEN_LLM_API_BASE") - self.open_llm_api_model = self._get("OPEN_LLM_API_MODEL") - self.fireworks_api_key = self._get("FIREWORKS_API_KEY") - self.gemini_api_key = self._get("GEMINI_API_KEY") - self.ollama_api_base = self._get("OLLAMA_API_BASE") - self.ollama_api_model = self._get("OLLAMA_API_MODEL") - - # if not self._get("DISABLE_LLM_PROVIDER_CHECK"): - # _ = self.get_default_llm_provider_enum() - - self.openai_base_url = self._get("OPENAI_BASE_URL") - self.openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy - self.openai_api_type = self._get("OPENAI_API_TYPE") - self.openai_api_version = self._get("OPENAI_API_VERSION") - self.openai_api_rpm = self._get("RPM", 3) - self.openai_api_model = self._get("OPENAI_API_MODEL", "gpt-4-1106-preview") - self.max_tokens_rsp = self._get("MAX_TOKENS", 2048) - self.deployment_name = self._get("DEPLOYMENT_NAME", "gpt-4") - - self.spark_appid = self._get("SPARK_APPID") - self.spark_api_secret = self._get("SPARK_API_SECRET") - self.spark_api_key = self._get("SPARK_API_KEY") - self.domain = self._get("DOMAIN") - self.spark_url = self._get("SPARK_URL") - - self.fireworks_api_base = self._get("FIREWORKS_API_BASE") - self.fireworks_api_model = self._get("FIREWORKS_API_MODEL") - - self.claude_api_key = self._get("ANTHROPIC_API_KEY") - - self.serpapi_api_key = self._get("SERPAPI_API_KEY") - self.serper_api_key = self._get("SERPER_API_KEY") - self.google_api_key = self._get("GOOGLE_API_KEY") - self.google_cse_id = self._get("GOOGLE_CSE_ID") - self.search_engine = SearchEngineType(self._get("SEARCH_ENGINE", SearchEngineType.SERPAPI_GOOGLE)) - self.web_browser_engine = WebBrowserEngineType(self._get("WEB_BROWSER_ENGINE", WebBrowserEngineType.PLAYWRIGHT)) - self.playwright_browser_type = self._get("PLAYWRIGHT_BROWSER_TYPE", "chromium") - self.selenium_browser_type = self._get("SELENIUM_BROWSER_TYPE", "chrome") - - self.long_term_memory = self._get("LONG_TERM_MEMORY", False) - if self.long_term_memory: - logger.warning("LONG_TERM_MEMORY is True") - self.cost_manager.max_budget = self._get("MAX_BUDGET", 10.0) - self.code_review_k_times = 2 - - self.puppeteer_config = self._get("PUPPETEER_CONFIG", "") - self.mmdc = self._get("MMDC", "mmdc") - self.calc_usage = self._get("CALC_USAGE", True) - self.model_for_researcher_summary = self._get("MODEL_FOR_RESEARCHER_SUMMARY") - self.model_for_researcher_report = self._get("MODEL_FOR_RESEARCHER_REPORT") - self.mermaid_engine = self._get("MERMAID_ENGINE", "nodejs") - self.pyppeteer_executable_path = self._get("PYPPETEER_EXECUTABLE_PATH", "") - - workspace_uid = ( - self._get("WORKSPACE_UID") or f"{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}-{uuid4().hex[-8:]}" - ) - self.repair_llm_output = self._get("REPAIR_LLM_OUTPUT", False) - self.prompt_schema = self._get("PROMPT_FORMAT", "json") - self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) - val = self._get("WORKSPACE_PATH_WITH_UID") - if val and val.lower() == "true": # for agent - self.workspace_path = self.workspace_path / workspace_uid - self._ensure_workspace_exists() - self.max_auto_summarize_code = self.max_auto_summarize_code or self._get("MAX_AUTO_SUMMARIZE_CODE", 1) - self.timeout = int(self._get("TIMEOUT", 60)) - - def update_via_cli(self, project_path, project_name, inc, reqa_file, max_auto_summarize_code): - """update config via cli""" - - # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. - if project_path: - inc = True - project_name = project_name or Path(project_path).name - self.project_path = project_path - self.project_name = project_name - self.inc = inc - self.reqa_file = reqa_file - self.max_auto_summarize_code = max_auto_summarize_code - - def _ensure_workspace_exists(self): - self.workspace_path.mkdir(parents=True, exist_ok=True) - logger.debug(f"WORKSPACE_PATH set to {self.workspace_path}") - - def _init_with_config_files_and_env(self, yaml_file): - """Load from config/key.yaml, config/config.yaml, and env in decreasing order of priority""" - configs = dict(os.environ) - - for _yaml_file in [yaml_file, self.key_yaml_file, self.home_yaml_file]: - if not _yaml_file.exists(): - continue - - # Load local YAML file - with open(_yaml_file, "r", encoding="utf-8") as file: - yaml_data = yaml.safe_load(file) - if not yaml_data: - continue - configs.update(yaml_data) - OPTIONS.set(configs) - - @staticmethod - def _get(*args, **kwargs): - i = OPTIONS.get() - return i.get(*args, **kwargs) - - def get(self, key, *args, **kwargs): - """Retrieve values from config/key.yaml, config/config.yaml, and environment variables. - Throw an error if not found.""" - value = self._get(key, *args, **kwargs) - if value is None: - raise ValueError(f"Key '{key}' not found in environment variables or in the YAML file") - return value - - def __setattr__(self, name: str, value: Any) -> None: - OPTIONS.get()[name] = value - - def __getattr__(self, name: str) -> Any: - i = OPTIONS.get() - return i.get(name) - - def set_context(self, options: dict): - """Update current config""" - if not options: - return - opts = deepcopy(OPTIONS.get()) - opts.update(options) - OPTIONS.set(opts) - self._update() - - @property - def options(self): - """Return all key-values""" - return OPTIONS.get() - - def new_environ(self): - """Return a new os.environ object""" - env = os.environ.copy() - i = self.options - env.update({k: v for k, v in i.items() if isinstance(v, str)}) - return env - - -CONFIG = Config() diff --git a/tests/metagpt/utils/test_config.py b/tests/metagpt/utils/test_config.py deleted file mode 100644 index 4ca7a225c..000000000 --- a/tests/metagpt/utils/test_config.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/1 11:19 -@Author : alexanderwu -@File : test_config.py -@Modified By: mashenquan, 2013/8/20, Add `test_options`; remove global configuration `CONFIG`, enable configuration support for business isolation. -""" -from pathlib import Path - -import pytest - -from metagpt.config import Config - - -def test_config_class_get_key_exception(): - with pytest.raises(Exception) as exc_info: - config = Config() - config.get("wtf") - assert str(exc_info.value) == "Key 'wtf' not found in environment variables or in the YAML file" - - -def test_config_yaml_file_not_exists(): - # FIXME: 由于这里是单例,所以会导致Config重新创建失效。后续要将Config改为非单例模式。 - _ = Config("wtf.yaml") - # with pytest.raises(Exception) as exc_info: - # config.get("OPENAI_BASE_URL") - # assert str(exc_info.value) == "Set OPENAI_API_KEY or Anthropic_API_KEY first" - - -def test_options(): - filename = Path(__file__).resolve().parent.parent.parent.parent / "config/config.yaml" - config = Config(filename) - assert config.options - - -if __name__ == "__main__": - test_options() From f0b052dadc42e78a6c35aa0b5937bdc29027c9fd Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 11 Jan 2024 15:44:53 +0800 Subject: [PATCH 49/50] remove config.py and test --- metagpt/document_store/base_store.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/metagpt/document_store/base_store.py b/metagpt/document_store/base_store.py index b719d1083..8228cfab7 100644 --- a/metagpt/document_store/base_store.py +++ b/metagpt/document_store/base_store.py @@ -8,8 +8,6 @@ from abc import ABC, abstractmethod from pathlib import Path -from metagpt.config import Config - class BaseStore(ABC): """FIXME: consider add_index, set_index and think about granularity.""" From decde3290bd164a31e3931f7dfc1227323cbc857 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 11 Jan 2024 15:54:18 +0800 Subject: [PATCH 50/50] simplify llm usage --- metagpt/context.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/metagpt/context.py b/metagpt/context.py index 663c1730a..377744046 100644 --- a/metagpt/context.py +++ b/metagpt/context.py @@ -103,7 +103,6 @@ class ContextMixin(BaseModel): _config: Optional[Config] = None # Env/Role/Action will use this llm as private llm, or use self.context._llm instance - _llm_config: Optional[LLMConfig] = None _llm: Optional[BaseLLM] = None def __init__( @@ -132,10 +131,6 @@ class ContextMixin(BaseModel): """Set config""" self.set("_config", config, override) - def set_llm_config(self, llm_config: LLMConfig, override=False): - """Set llm config""" - self.set("_llm_config", llm_config, override) - def set_llm(self, llm: BaseLLM, override=False): """Set llm""" self.set("_llm", llm, override) @@ -166,11 +161,11 @@ class ContextMixin(BaseModel): @property def llm(self) -> BaseLLM: - """Role llm: role llm > context llm""" + """Role llm: if not existed, init from role.config""" # print(f"class:{self.__class__.__name__}({self.name}), llm: {self._llm}, llm_config: {self._llm_config}") - if self._llm_config and not self._llm: - self._llm = self.context.llm_with_cost_manager_from_llm_config(self._llm_config) - return self._llm or self.context.llm() + if not self._llm: + self._llm = self.context.llm_with_cost_manager_from_llm_config(self.config.llm) + return self._llm @llm.setter def llm(self, llm: BaseLLM) -> None: