Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for structured outputs #938

Merged
merged 20 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions instructor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ def from_openai(
instructor.Mode.FUNCTIONS,
instructor.Mode.PARALLEL_TOOLS,
instructor.Mode.MD_JSON,
instructor.Mode.STRUCTURED_OUTPUTS,
}

if isinstance(client, openai.OpenAI):
Expand Down
2 changes: 1 addition & 1 deletion instructor/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def from_response(
Mode.warn_mode_functions_deprecation()
return cls.parse_functions(completion, validation_context, strict)

if mode in {Mode.TOOLS, Mode.MISTRAL_TOOLS}:
if mode in {Mode.TOOLS, Mode.MISTRAL_TOOLS, Mode.STRUCTURED_OUTPUTS}:
ivanleomk marked this conversation as resolved.
Show resolved Hide resolved
return cls.parse_tools(completion, validation_context, strict)

if mode in {Mode.JSON, Mode.JSON_SCHEMA, Mode.MD_JSON}:
Expand Down
1 change: 1 addition & 0 deletions instructor/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Mode(enum.Enum):
VERTEXAI_JSON = "vertexai_json"
GEMINI_JSON = "gemini_json"
COHERE_JSON_SCHEMA = "json_object"
STRUCTURED_OUTPUTS = "structured_output"
ivanleomk marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def warn_mode_functions_deprecation(cls):
Expand Down
13 changes: 13 additions & 0 deletions instructor/process_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,18 @@ def handle_response_model(
Mode.warn_mode_functions_deprecation()
new_kwargs["functions"] = [response_model.openai_schema]
new_kwargs["function_call"] = {"name": response_model.openai_schema["name"]}
elif mode in {Mode.STRUCTURED_OUTPUTS}:
ivanleomk marked this conversation as resolved.
Show resolved Hide resolved
new_kwargs["tools"] = [
{
"type": "function",
"function": response_model.openai_schema,
"strict": True,
}
]
new_kwargs["tool_choice"] = {
"type": "function",
"function": {"name": response_model.openai_schema["name"]},
}
elif mode in {Mode.TOOLS, Mode.MISTRAL_TOOLS}:
new_kwargs["tools"] = [
{
Expand All @@ -263,6 +275,7 @@ def handle_response_model(
"type": "function",
"function": {"name": response_model.openai_schema["name"]},
}

elif mode in {Mode.JSON, Mode.MD_JSON, Mode.JSON_SCHEMA}:
# If its a JSON Mode we need to massage the prompt a bit
# in order to get the response we want in a json format
Expand Down
2,143 changes: 1,122 additions & 1,021 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "instructor"
version = "1.3.7"
version = "1.4.0"
description = "structured outputs for llm"
authors = ["Jason Liu <jason@jxnl.co>"]
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions tests/llm/test_openai/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
models = ["gpt-4o-mini"]
modes = [
instructor.Mode.TOOLS,
instructor.Mode.STRUCTURED_OUTPUTS,
]
Loading