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

Add OpenAI support for structured outputs #184

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion aisuite/providers/openai_provider.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import openai
from pydantic import BaseModel
from typing import Type
import os
from aisuite.provider import Provider, LLMError

Expand All @@ -23,9 +25,21 @@ def __init__(self, **config):
# Pass the entire config to the OpenAI client constructor
self.client = openai.OpenAI(**config)

def chat_completions_create(self, model, messages, **kwargs):
def chat_completions_create(
self, model, messages, response_format: Type[BaseModel] | None = None, **kwargs
):
# Any exception raised by OpenAI will be returned to the caller.
# Maybe we should catch them and raise a custom LLMError.
if response_format is not None:
response = self.client.beta.chat.completions.parse(
model=model,
messages=messages,
response_format=response_format,
**kwargs # Pass any additional arguments to the OpenAI API
)
response.choices[0].message.content = response.choices[0].message.parsed
return response

response = self.client.chat.completions.create(
model=model,
messages=messages,
Expand Down
33 changes: 33 additions & 0 deletions tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from aisuite import Client
from pydantic import BaseModel


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -107,6 +108,38 @@ def test_client_chat_completions(
assert model_response == expected_response


@pytest.mark.parametrize(
argnames=("patch_target", "provider", "model"),
argvalues=[
(
"aisuite.providers.openai_provider.OpenaiProvider.chat_completions_create",
"openai",
"gpt-4o",
),
],
)
def test_client_chat_completions_with_structured_output(
provider_configs: dict, patch_target: str, provider: str, model: str
):
expected_response = f"{patch_target}_{provider}_{model}"
with patch(patch_target) as mock_provider:
mock_provider.return_value = expected_response
client = Client()
client.configure(provider_configs)
messages = [
{"role": "user", "content": "Tell me a pirate joke."},
]

class Joke(BaseModel):
joke: str

model_str = f"{provider}:{model}"
model_response = client.chat.completions.create(
model_str, messages=messages, response_format=Joke
)
assert model_response == expected_response


def test_invalid_provider_in_client_config():
# Testing an invalid provider name in the configuration
invalid_provider_configs = {
Expand Down