Skip to content

Commit 29259e5

Browse files
authored
Merge pull request #1098 from lifejwang11/main
add OpenAI json structured example
2 parents bc77e45 + 76e2051 commit 29259e5

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# OpenAI STRUCTURED JSON Example
2+
3+
This example demonstrates how to use the OpenAI model with the LangChain Go library to generate structured JSON output.
4+
5+
## What does this example do?
6+
7+
This nifty little program does the following:
8+
9+
1. Sets up a connection to the OpenAI API using the GPT-4o model.
10+
11+
2. Prompts the AI to generate a structured JSON output.
12+
13+
14+
## How to Run
15+
16+
1. Make sure you have Go installed on your system.
17+
2. Set up your OpenAI API key as an environment variable.
18+
3. Run the program:
19+
20+
```
21+
go run openai_jsonformat.go
22+
```
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/tmc/langchaingo/examples/openai-jsonformat-example
2+
3+
go 1.23
4+
5+
require github.com/tmc/langchaingo v0.1.13-pre.0.0.20250106145851-f1fde1f9e4a0
6+
7+
require (
8+
github.com/dlclark/regexp2 v1.10.0 // indirect
9+
github.com/google/uuid v1.6.0 // indirect
10+
github.com/pkoukk/tiktoken-go v0.1.6 // indirect
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"github.com/tmc/langchaingo/llms"
6+
"github.com/tmc/langchaingo/llms/openai"
7+
"log"
8+
)
9+
10+
type User struct {
11+
Name string `json:"name"`
12+
Age int `json:"age"`
13+
}
14+
15+
func main() {
16+
fomat := &openai.ResponseFormat{
17+
Type: "json_schema",
18+
JSONSchema: &openai.ResponseFormatJSONSchema{
19+
Name: "object",
20+
Schema: &openai.ResponseFormatJSONSchemaProperty{
21+
Type: "object",
22+
Properties: map[string]*openai.ResponseFormatJSONSchemaProperty{
23+
"name": {
24+
Type: "string",
25+
Description: "The name of the user",
26+
},
27+
"age": {
28+
Type: "integer",
29+
Description: "The age of the user",
30+
},
31+
"role": {
32+
Type: "string",
33+
Description: "The role of the user",
34+
},
35+
},
36+
AdditionalProperties: false,
37+
Required: []string{"name", "age", "role"},
38+
},
39+
Strict: true,
40+
},
41+
}
42+
llm, err := openai.New(openai.WithModel("gpt-4o"), openai.WithResponseFormat(fomat))
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
ctx := context.Background()
47+
48+
content := []llms.MessageContent{
49+
llms.TextParts(llms.ChatMessageTypeSystem, "You are an expert at structured data extraction. You will be given unstructured text from a research paper and should convert it into the given structure."),
50+
llms.TextParts(llms.ChatMessageTypeHuman, "please tell me the most famous people in history"),
51+
}
52+
53+
completion, err := llm.GenerateContent(ctx, content, llms.WithJSONMode())
54+
if err != nil {
55+
log.Fatal(err)
56+
}
57+
log.Fatal(completion.Choices[0].Content)
58+
}

0 commit comments

Comments
 (0)