Fix #244 -- Refactor dump_message function to force content key (#245)

Co-authored-by: Jason Liu <jxnl@users.noreply.github.com>
This commit is contained in:
Guillaume Pouyat
2023-12-02 01:01:33 +01:00
committed by GitHub
parent 0477f58d0f
commit a5ea6e5c41
2 changed files with 136 additions and 18 deletions
+113 -5
View File
@@ -1,11 +1,16 @@
import functools
import pytest
from openai import AsyncOpenAI, OpenAI
from openai.types.chat import ChatCompletionMessage, ChatCompletionMessageParam
from openai.types.chat.chat_completion_message import FunctionCall
from openai.types.chat.chat_completion_message_tool_call import (
ChatCompletionMessageToolCall,
Function,
)
import instructor
from openai import OpenAI, AsyncOpenAI
from instructor.patch import is_async, wrap_chatcompletion, OVERRIDE_DOCS
from instructor.patch import OVERRIDE_DOCS, dump_message, is_async, wrap_chatcompletion
def test_patch_completes_successfully():
@@ -66,3 +71,106 @@ def test_override_docs():
assert (
"response_model" in OVERRIDE_DOCS
), "response_model should be in OVERRIDE_DOCS"
@pytest.mark.parametrize(
"name_of_test, message, expected",
[
(
"tool_calls and content and no function_call",
ChatCompletionMessage(
role="assistant",
content="Hello, world!",
tool_calls=[
ChatCompletionMessageToolCall(
id="test_tool",
function=Function(arguments="", name="test_tool"),
type="function",
)
],
),
{
"role": "assistant",
"content": 'Hello, world![{"id": "test_tool", "function": {"arguments": "", "name": "test_tool"}, "type": "function"}]',
},
),
(
"tool_calls and no content and no function_call",
ChatCompletionMessage(
role="assistant",
content=None,
tool_calls=[
ChatCompletionMessageToolCall(
id="test_tool",
function=Function(arguments="", name="test_tool"),
type="function",
)
],
),
{
"role": "assistant",
"content": '[{"id": "test_tool", "function": {"arguments": "", "name": "test_tool"}, "type": "function"}]',
},
),
(
"no tool_calls and no content no function_call",
ChatCompletionMessage(
role="assistant",
content=None,
),
{
"role": "assistant",
"content": "",
},
),
(
"no tool_calls and content and function_call",
ChatCompletionMessage(
role="assistant",
content="Hello, world!",
function_call=FunctionCall(arguments="", name="test_tool"),
),
{
"role": "assistant",
"content": 'Hello, world!{"arguments": "", "name": "test_tool"}',
},
),
(
"no tool_calls and no content and function_call",
ChatCompletionMessage(
role="assistant",
content=None,
function_call=FunctionCall(arguments="", name="test_tool"),
),
{
"role": "assistant",
"content": '{"arguments": "", "name": "test_tool"}',
},
),
(
"tool_calls and no content and function_call",
ChatCompletionMessage(
role="assistant",
content=None,
function_call=FunctionCall(arguments="", name="test_tool"),
tool_calls=[
ChatCompletionMessageToolCall(
id="test_tool",
function=Function(arguments="", name="test_tool"),
type="function",
)
],
),
{
"role": "assistant",
"content": '[{"id": "test_tool", "function": {"arguments": "", "name": "test_tool"}, "type": "function"}]{"arguments": "", "name": "test_tool"}',
},
),
],
)
def test_dump_message(
name_of_test: str,
message: ChatCompletionMessage,
expected: ChatCompletionMessageParam,
):
assert dump_message(message) == expected, name_of_test