-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples/structure-outputs): created an example for using struct…
…ured outputs Basic example which uses the invopop/jsonschema library
- Loading branch information
1 parent
29e80e7
commit 7d1e71e
Showing
5 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/invopop/jsonschema" | ||
"github.com/openai/openai-go" | ||
) | ||
|
||
// A struct that will be converted to a Structured Outputs response schema | ||
type HistoricalComputer struct { | ||
Origin Origin `json:"origin" jsonschema_description:"The origin of the computer"` | ||
Name string `json:"full_name" jsonschema_description:"The name of the device model"` | ||
Legacy string `json:"legacy" jsonschema:"enum=positive,enum=neutral,enum=negative" jsonschema_description:"Its influence on the field of computing"` | ||
NotableFacts []string `json:"notable_facts" jsonschema_description:"A few key facts about the computer"` | ||
} | ||
|
||
type Origin struct { | ||
YearBuilt int64 `json:"year_of_construction" jsonschema_description:"The year it was made"` | ||
Organization string `json:"organization" jsonschema_description:"The organization that was in charge of its development"` | ||
} | ||
|
||
func GenerateSchema[T any]() interface{} { | ||
// Structured Outputs uses a subset of JSON schema | ||
// These flags are necessary to comply with the subset | ||
reflector := jsonschema.Reflector{ | ||
AllowAdditionalProperties: false, | ||
DoNotReference: true, | ||
} | ||
var v T | ||
schema := reflector.Reflect(v) | ||
return schema | ||
} | ||
|
||
// Generate the JSON schema at initialization time | ||
var HistoricalComputerResponseSchema = GenerateSchema[HistoricalComputer]() | ||
|
||
func main() { | ||
client := openai.NewClient() | ||
ctx := context.Background() | ||
|
||
question := "What computer ran the first neural network?" | ||
|
||
print("> ") | ||
println(question) | ||
|
||
schemaParam := openai.ResponseFormatJSONSchemaJSONSchemaParam{ | ||
Name: openai.F("biography"), | ||
Description: openai.F("Notable information about a person"), | ||
Schema: openai.F(HistoricalComputerResponseSchema), | ||
Strict: openai.Bool(true), | ||
} | ||
|
||
// Query the Chat Completions API | ||
chat, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{ | ||
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{ | ||
openai.UserMessage(question), | ||
}), | ||
ResponseFormat: openai.F[openai.ChatCompletionNewParamsResponseFormatUnion]( | ||
openai.ResponseFormatJSONSchemaParam{ | ||
Type: openai.F(openai.ResponseFormatJSONSchemaTypeJSONSchema), | ||
JSONSchema: openai.F(schemaParam), | ||
}, | ||
), | ||
// Only certain models can perform structured outputs | ||
Model: openai.F(openai.ChatModelGPT4o2024_08_06), | ||
}) | ||
|
||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// The model responds with a JSON string, so parse it into a struct | ||
historicalComputer := HistoricalComputer{} | ||
err = json.Unmarshal([]byte(chat.Choices[0].Message.Content), &historicalComputer) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// Use the model's structured response with a native Go struct | ||
fmt.Printf("Name: %v\n", historicalComputer.Name) | ||
fmt.Printf("Year: %v\n", historicalComputer.Origin.YearBuilt) | ||
fmt.Printf("Org: %v\n", historicalComputer.Origin.Organization) | ||
fmt.Printf("Legacy: %v\n", historicalComputer.Legacy) | ||
fmt.Printf("Facts:\n") | ||
for i, fact := range historicalComputer.NotableFacts { | ||
fmt.Printf("%v. %v\n", i+1, fact) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters