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] 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)