sashabaranov / go-openai

OpenAI ChatGPT, GPT-3, GPT-4, DALL·E, Whisper API wrapper for Go
Apache License 2.0
8.96k stars 1.37k forks source link

Support for Structured Outputs #814

Closed hari4698 closed 2 weeks ago

hari4698 commented 1 month ago

I was wondering if there will support in the future for Structured Outputs. Open AI released this feature on August 6th.

Python example from the Open API 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
h0rv commented 1 month ago

I am looking to add support for the strict json schema too.

instructor-go currently gives a similar experience as the python API provides, and in place for Pydantic it use Go struct tags and jsonschema. Adding the API support will improve the performance of the model generating the correct format.

modeledge commented 1 month ago

Great news.

I'm not sure how you will approach implementing the actual json schema definition within the request but consider allowing for different implementations of the actual json schema creation. I started with field tags, as in instructor-go, but have since moved away from that approach and think field tags are too limiting.

h0rv commented 1 month ago

For instructor, JSON tags has been sufficient.

I have been struggling to convert the json schema to the currently defined struct here.

I think it would be great to optionally pass a raw schema to response_format.

For example, the current struct does not support defs, which are crucial, especially with complex data types. The OpenAI API supports this: https://platform.openai.com/docs/guides/structured-outputs/supported-schemas.

monstercameron commented 1 month ago

This enhancement would be great!

rjcorwin commented 2 weeks ago

Looks like this issue is resolved? https://github.com/sashabaranov/go-openai/pull/813

h0rv commented 2 weeks ago

My issue above is resolved: for those wanting to use their own schema string, and not use the go-openai/jsonschema, you can do the following:

JSONSchema: &openai.ChatCompletionResponseFormatJSONSchema{
    Name:        "my-name",
    Schema:      json.RawMessage(schemaJSONString),
    Strict:      true,
}
hari4698 commented 2 weeks ago

Support added for Structured Output. #813