diff --git a/metagpt/actions/mi/write_analysis_code.py b/metagpt/actions/mi/write_analysis_code.py index aa2ced892..64e58920a 100644 --- a/metagpt/actions/mi/write_analysis_code.py +++ b/metagpt/actions/mi/write_analysis_code.py @@ -19,7 +19,7 @@ from metagpt.prompts.mi.write_analysis_code import ( STRUCTUAL_PROMPT, TOOL_RECOMMENDATION_PROMPT, ) -from metagpt.schema import Message, Plan, SystemMessage +from metagpt.schema import Message, Plan from metagpt.tools import TOOL_REGISTRY from metagpt.tools.tool_registry import validate_tool_names from metagpt.tools.tool_type import ToolType @@ -33,11 +33,6 @@ class WriteCodeWithTools(Action): # selected tools to choose from, listed by their names. An empty list means selection from all tools. selected_tools: list[str] = [] - def _insert_system_message(self, context: list[Message], system_msg: str = None): - system_msg = system_msg or self.DEFAULT_SYSTEM_MSG - context.insert(0, SystemMessage(content=system_msg)) if context[0].role != "system" else None - return context - def _get_tools_by_type(self, tool_type: str) -> dict: """ Retreive tools by tool type from registry, but filtered by pre-selected tool list @@ -85,15 +80,18 @@ class WriteCodeWithTools(Action): return tool_schemas - async def _prepare_tools(self, plan: Plan) -> Tuple[dict, str]: + async def _prepare_tools(self, plan: Plan) -> Tuple[dict, str, str]: """Prepare tool schemas and usage instructions according to current task Args: plan (Plan): The overall plan containing task information. Returns: - Tuple[dict, str]: A tool schemas ({tool_name: tool_schema_dict}) and a usage prompt for the type of tools selected + Tuple[dict, str, str]: A tool schemas ({tool_name: tool_schema_dict}), a usage prompt for the type of tools selected, and examples of using the tools """ + if not self.use_tools: + return {}, "", "" + # find tool type from task type through exact match, can extend to retrieval in the future tool_type = plan.current_task.task_type diff --git a/metagpt/prompts/mi/write_analysis_code.py b/metagpt/prompts/mi/write_analysis_code.py index 0974f368f..12ca9d4ae 100644 --- a/metagpt/prompts/mi/write_analysis_code.py +++ b/metagpt/prompts/mi/write_analysis_code.py @@ -56,13 +56,11 @@ assert add(1, 2) == 4 # output: -1 The implementation failed the test cases where the input integers are 1 and 2. The issue arises because the code does not add the two integers together, but instead subtracts the second integer from the first. To fix this issue, we should change the operator from `-` to `+` in the return statement. This will ensure that the function returns the correct output for the given input. [improved impl]: -```python def add(a: int, b: int) -> int: """ Given integers a and b, return the total value of a and b. """ return a + b -``` ''' REFLECTION_PROMPT = """ @@ -107,7 +105,7 @@ column_info = get_column_info(df) print("column_info") print(column_info) ```end -Otherwise, you may write any codes you see fit. Return an empty string if you think there is no important data to check. +Otherwise, print out any key variables you see fit. Return an empty string if you think there is no important data to check. # Constraints: - Your code is to be added to a new cell in jupyter. diff --git a/metagpt/provider/base_llm.py b/metagpt/provider/base_llm.py index 966a9fcd6..140ed3570 100644 --- a/metagpt/provider/base_llm.py +++ b/metagpt/provider/base_llm.py @@ -105,9 +105,7 @@ class BaseLLM(ABC): context.append(self._assistant_msg(rsp_text)) return self._extract_assistant_rsp(context) - async def aask_code( - self, messages: Union[str, Message, list[dict]], timeout=3, include_language: bool = False, **kwargs - ) -> dict: + async def aask_code(self, messages: Union[str, Message, list[dict]], timeout=3, **kwargs) -> dict: raise NotImplementedError @abstractmethod diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 388149256..3616debc4 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -26,7 +26,7 @@ from tenacity import ( from metagpt.configs.llm_config import LLMConfig, LLMType from metagpt.logs import log_llm_stream, logger from metagpt.provider.base_llm import BaseLLM -from metagpt.provider.constant import CODE_ONLY_FUNCTION_SCHEMA, GENERAL_FUNCTION_SCHEMA +from metagpt.provider.constant import GENERAL_FUNCTION_SCHEMA from metagpt.provider.llm_provider_registry import register_provider from metagpt.utils.common import CodeParser, decode_image, process_message from metagpt.utils.cost_manager import CostManager, Costs @@ -153,7 +153,7 @@ class OpenAILLM(BaseLLM): self._update_costs(rsp.usage) return rsp - async def aask_code(self, messages: list[dict], timeout: int = 3, include_language: bool = False, **kwargs) -> dict: + async def aask_code(self, messages: list[dict], timeout: int = 3, **kwargs) -> dict: """Use function of tools to ask a code. Note: Keep kwargs consistent with https://platform.openai.com/docs/api-reference/chat/create @@ -161,13 +161,10 @@ class OpenAILLM(BaseLLM): >>> llm = OpenAILLM() >>> msg = [{'role': 'user', 'content': "Write a python hello world code."}] >>> rsp = await llm.aask_code(msg) - # -> {'code': "print('Hello, World!')"} - >>> rsp = await llm.aask_code(msg, include_language=True) # -> {'language': 'python', 'code': "print('Hello, World!')"} """ if "tools" not in kwargs: - function_schema = GENERAL_FUNCTION_SCHEMA if include_language else CODE_ONLY_FUNCTION_SCHEMA - configs = {"tools": [{"type": "function", "function": function_schema}]} + configs = {"tools": [{"type": "function", "function": GENERAL_FUNCTION_SCHEMA}]} kwargs.update(configs) rsp = await self._achat_completion_function(messages, **kwargs) return self.get_choice_function_arguments(rsp) diff --git a/metagpt/roles/mi/interpreter.py b/metagpt/roles/mi/interpreter.py index 0e26dd71a..0a99c0e69 100644 --- a/metagpt/roles/mi/interpreter.py +++ b/metagpt/roles/mi/interpreter.py @@ -96,7 +96,7 @@ class Interpreter(Role): return logger.info("Check updated data") code = await CheckData().run(self.planner.plan) - if not code: + if not code.strip(): return success = False result, success = await self.execute_code.run(code)