From 7cead197013b9e805c1b322527b3c33ffb79e3f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Wed, 31 Jan 2024 13:35:48 +0800 Subject: [PATCH 1/2] fix: add arg for OpenAILLM in test_get_choice_function_arguments_for_aask_code. --- tests/metagpt/provider/test_openai.py | 90 +++++++++++++-------------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/tests/metagpt/provider/test_openai.py b/tests/metagpt/provider/test_openai.py index df9355f7c..1698518f5 100644 --- a/tests/metagpt/provider/test_openai.py +++ b/tests/metagpt/provider/test_openai.py @@ -36,6 +36,48 @@ async def test_speech_to_text(): assert "你好" == resp.text +@pytest.fixture +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) + ] + messages = [ChatCompletionMessage(content=None, role="assistant", tool_calls=[t]) for t in tool_calls] + # 添加一个纯文本响应 + messages.append( + ChatCompletionMessage(content="Completed a python code for hello world!", role="assistant", tool_calls=None) + ) + # 添加 openai tool calls respond bug, code 出现在ChatCompletionMessage.content中 + 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 = [ + Choice(finish_reason="tool_calls", logprobs=None, index=i, message=msg) for i, msg in enumerate(messages) + ] + return [ + ChatCompletion(id=str(i), choices=[c], created=i, model="gpt-4", object="chat.completion") + for i, c in enumerate(choices) + ] + + class TestOpenAI: def test_make_client_kwargs_without_proxy(self): instance = OpenAILLM(mock_llm_config) @@ -50,7 +92,7 @@ class TestOpenAI: assert "http_client" in kwargs def test_get_choice_function_arguments_for_aask_code(self, tool_calls_rsp): - instance = OpenAILLM() + instance = OpenAILLM(mock_llm_config_proxy) for i, rsp in enumerate(tool_calls_rsp): code = instance.get_choice_function_arguments(rsp) logger.info(f"\ntest get function call arguments {i}: {code}") @@ -67,49 +109,3 @@ class TestOpenAI: def test_make_client_kwargs_without_proxy_azure(self, config_azure): instance = OpenAILLM() instance.config = config_azure - - @pytest.fixture - def tool_calls_rsp(self): - 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) - ] - messages = [ChatCompletionMessage(content=None, role="assistant", tool_calls=[t]) for t in tool_calls] - # 添加一个纯文本响应 - messages.append( - ChatCompletionMessage(content="Completed a python code for hello world!", role="assistant", tool_calls=None) - ) - # 添加 openai tool calls respond bug, code 出现在ChatCompletionMessage.content中 - 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 = [ - Choice(finish_reason="tool_calls", logprobs=None, index=i, message=msg) for i, msg in enumerate(messages) - ] - return [ - ChatCompletion(id=str(i), choices=[c], created=i, model="gpt-4", object="chat.completion") - for i, c in enumerate(choices) - ] From 56f640db96816eb066a28d6b26b3945d8efb1688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Wed, 31 Jan 2024 13:55:21 +0800 Subject: [PATCH 2/2] delete test_make_client_kwargs_without_proxy_azure. --- tests/metagpt/provider/test_openai.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/metagpt/provider/test_openai.py b/tests/metagpt/provider/test_openai.py index 1698518f5..a49d7e85b 100644 --- a/tests/metagpt/provider/test_openai.py +++ b/tests/metagpt/provider/test_openai.py @@ -105,7 +105,3 @@ class TestOpenAI: code["language"] == "markdown" else: code["language"] == "python" - - def test_make_client_kwargs_without_proxy_azure(self, config_azure): - instance = OpenAILLM() - instance.config = config_azure