mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
Co-authored-by: Jason Liu <jxnl@users.noreply.github.com>
This commit is contained in:
+113
-5
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user