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 Structured Output support #121

Open
AndreasKarasenko opened this issue Jan 23, 2025 · 0 comments
Open

Add Structured Output support #121

AndreasKarasenko opened this issue Jan 23, 2025 · 0 comments

Comments

@AndreasKarasenko
Copy link
Contributor

Structured outputs allow users to define an output scheme using pydantic. OpenAI and most others support this now (see e.g. OpenAI and their docs).

from pydantic import BaseModel
from openai import OpenAI

client = OpenAI()

class CalendarEvent(BaseModel):
    name: str
    date: str
    participants: list[str]

completion = client.beta.chat.completions.parse(
    model="gpt-4o-2024-08-06",
    messages=[
        {"role": "system", "content": "Extract the event information."},
        {"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
    ],
    response_format=CalendarEvent,
)

event = completion.choices[0].message.parsed

In my own tests using scikit-ollama I found the models to adhere much better to the output scheme. It barely ever had to fall back to the default label which made it overall more accurate.
It was also usually faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant