From d1deb0ff7ccaa9d4f74eeb632b579cd080944c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Sun, 4 Feb 2024 18:03:27 +0800 Subject: [PATCH 1/4] Remove _parse_arguments function and comment out handle_exception decorator on get_choice_function_arguments. --- metagpt/provider/openai_api.py | 41 +++++----------------------------- 1 file changed, 6 insertions(+), 35 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 206701efd..3ab25c276 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -8,7 +8,6 @@ """ import json -import re from typing import AsyncIterator, Optional, Union from openai import APIConnectionError, AsyncOpenAI, AsyncStream @@ -195,31 +194,7 @@ class OpenAILLM(BaseLLM): rsp = await self._achat_completion_function(messages, **kwargs) return self.get_choice_function_arguments(rsp) - def _parse_arguments(self, arguments: str) -> dict: - """parse arguments in openai function call""" - if "langugae" not in arguments and "code" not in arguments: - logger.warning(f"Not found `code`, `language`, We assume it is pure code:\n {arguments}\n. ") - return {"language": "python", "code": arguments} - - # 匹配language - language_pattern = re.compile(r'[\"\']?language[\"\']?\s*:\s*["\']([^"\']+?)["\']', re.DOTALL) - language_match = language_pattern.search(arguments) - language_value = language_match.group(1) if language_match else "python" - - # 匹配code - code_pattern = r'(["\'`]{3}|["\'`])([\s\S]*?)\1' - try: - code_value = re.findall(code_pattern, arguments)[-1][-1] - except Exception as e: - logger.error(f"{e}, when re.findall({code_pattern}, {arguments})") - code_value = None - - if code_value is None: - raise ValueError(f"Parse code error for {arguments}") - # arguments只有code的情况 - return {"language": language_value, "code": code_value} - - @handle_exception + # @handle_exception def get_choice_function_arguments(self, rsp: ChatCompletion) -> dict: """Required to provide the first function arguments of choice. @@ -237,19 +212,15 @@ class OpenAILLM(BaseLLM): try: return json.loads(message.tool_calls[0].function.arguments, strict=False) except json.decoder.JSONDecodeError as e: - logger.warning( - "\n".join( - [ - (f"Got JSONDecodeError for \n{'--'*40} \n{message.tool_calls[0].function.arguments}"), - (f"{'--'*40}\nwe will use RegExp to parse code. JSONDecodeError is: {e}"), - ] - ) + error_msg = ( + f"Got JSONDecodeError for \n{'--'*40} \n{message.tool_calls[0].function.arguments}, {str(e)}" ) - return self._parse_arguments(message.tool_calls[0].function.arguments) + logger.error(error_msg) + raise json.decoder.JSONDecodeError(error_msg, e.doc, e.pos) elif message.tool_calls is None and message.content is not None: # reponse is code, fix openai tools_call respond bug, # The response content is `code``, but it appears in the content instead of the arguments. - code_formats = ("```", '"""', "'''") + code_formats = "```" if message.content.startswith(code_formats) and message.content.endswith(code_formats): code = CodeParser.parse_code(None, message.content) return {"language": "python", "code": code} From 4caa1ece816737c696438de00c3b51578ce25a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Sun, 4 Feb 2024 18:07:16 +0800 Subject: [PATCH 2/4] Revert CodeParser.parse_code function to version 0.6.6. --- metagpt/utils/common.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 9d6a6bb24..d7eef5bd9 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -8,7 +8,6 @@ Add generic class-to-string and object-to-string conversion functionality. @Modified By: mashenquan, 2023/11/27. Bug fix: `parse_recipient` failed to parse the recipient in certain GPT-3.5 responses. -@Modified By: liubangbang, 2024/01/23. Update: support [```, ''', \"\"\" ] codes in CodeParser.parse_code. """ from __future__ import annotations @@ -268,19 +267,16 @@ class CodeParser: def parse_code(cls, block: str, text: str, lang: str = "") -> str: if block: text = cls.parse_block(block, text) - start_ends = ["```", "'''", '"""'] - patterns = [] - for start_end in start_ends: - pattern = rf"{start_end}{lang}.*?\s+(.*?){start_end}" - match = re.search(pattern, text, re.DOTALL) - if match: - code = match.group(1) - return code - patterns.append(pattern) - logger.error(f"{patterns} not match following text:") - logger.error(text) - # raise Exception - return text # just assume original text is code + pattern = rf"```{lang}.*?\s+(.*?)```" + match = re.search(pattern, text, re.DOTALL) + if match: + code = match.group(1) + else: + logger.error(f"{pattern} not match following text:") + logger.error(text) + # raise Exception + return text # just assume original text is code + return code @classmethod def parse_str(cls, block: str, text: str, lang: str = ""): From 4b912cc527ec6567eff896b8a2891f33b5fbcc98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Sun, 4 Feb 2024 18:07:43 +0800 Subject: [PATCH 3/4] update test. --- tests/metagpt/provider/test_openai.py | 31 +++++++++++++++------------ 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/metagpt/provider/test_openai.py b/tests/metagpt/provider/test_openai.py index a49d7e85b..a48e27432 100644 --- a/tests/metagpt/provider/test_openai.py +++ b/tests/metagpt/provider/test_openai.py @@ -1,3 +1,5 @@ +import json + import pytest from openai.types.chat import ( ChatCompletion, @@ -40,16 +42,6 @@ async def test_speech_to_text(): def tool_calls_rsp(): function_rsps = [ Function(arguments='{\n"language": "python",\n"code": "print(\'hello world\')"}', name="execute"), - Function(arguments='{\n"language": "python",\n"code": \'print("hello world")\'}', name="execute"), - Function(arguments='{\n"language": \'python\',\n"code": "print(\'hello world\')"}', name="execute"), - Function(arguments='{\n"language": "python",\n"code": "print(\'hello world\')"}', name="execute"), - Function(arguments='{\n"language": "python",\n"code": ```print("hello world")```}', name="execute"), - Function(arguments='{\n"language": "python",\n"code": """print("hello world")"""}', name="execute"), - Function(arguments='\nprint("hello world")\\n', name="execute"), - # only `{` in arguments - Function(arguments='{\n"language": "python",\n"code": "print(\'hello world\')"', name="execute"), - # no `{`, `}` in arguments - Function(arguments='\n"language": "python",\n"code": "print(\'hello world\')"', name="execute"), ] tool_calls = [ ChatCompletionMessageToolCall(type="function", id=f"call_{i}", function=f) for i, f in enumerate(function_rsps) @@ -63,10 +55,6 @@ def tool_calls_rsp(): messages.extend( [ ChatCompletionMessage(content="```python\nprint('hello world')```", role="assistant", tool_calls=None), - ChatCompletionMessage(content="'''python\nprint('hello world')'''", role="assistant", tool_calls=None), - ChatCompletionMessage(content='"""python\nprint(\'hello world\')"""', role="assistant", tool_calls=None), - ChatCompletionMessage(content="'''python\nprint(\"hello world\")'''", role="assistant", tool_calls=None), - ChatCompletionMessage(content="```python\nprint('hello world')```", role="assistant", tool_calls=None), ] ) choices = [ @@ -78,6 +66,15 @@ def tool_calls_rsp(): ] +@pytest.fixture +def json_decode_error(): + function_rsp = Function(arguments='{\n"language": \'python\',\n"code": "print(\'hello world\')"}', name="execute") + tool_calls = [ChatCompletionMessageToolCall(type="function", id=f"call_{0}", function=function_rsp)] + message = ChatCompletionMessage(content=None, role="assistant", tool_calls=tool_calls) + choices = [Choice(finish_reason="tool_calls", logprobs=None, index=0, message=message)] + return ChatCompletion(id="0", choices=choices, created=0, model="gpt-4", object="chat.completion") + + class TestOpenAI: def test_make_client_kwargs_without_proxy(self): instance = OpenAILLM(mock_llm_config) @@ -105,3 +102,9 @@ class TestOpenAI: code["language"] == "markdown" else: code["language"] == "python" + + def test_aask_code_JSONDecodeError(self, json_decode_error): + instance = OpenAILLM(mock_llm_config) + with pytest.raises(json.decoder.JSONDecodeError) as e: + instance.get_choice_function_arguments(json_decode_error) + assert "JSONDecodeError" in str(e) From b4d032c8bffecf06fcf5f1869e620e62cbd5eb96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Sun, 4 Feb 2024 18:22:33 +0800 Subject: [PATCH 4/4] chore. --- tests/metagpt/provider/test_openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/metagpt/provider/test_openai.py b/tests/metagpt/provider/test_openai.py index a48e27432..3883aab2e 100644 --- a/tests/metagpt/provider/test_openai.py +++ b/tests/metagpt/provider/test_openai.py @@ -103,7 +103,7 @@ class TestOpenAI: else: code["language"] == "python" - def test_aask_code_JSONDecodeError(self, json_decode_error): + def test_aask_code_json_decode_error(self, json_decode_error): instance = OpenAILLM(mock_llm_config) with pytest.raises(json.decoder.JSONDecodeError) as e: instance.get_choice_function_arguments(json_decode_error)