-
-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
415 additions
and
25 deletions.
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,22 @@ | ||
# OpenAI STRUCTURED JSON Example | ||
|
||
This example demonstrates how to use the OpenAI model with the LangChain Go library to generate structured JSON output. | ||
|
||
## What does this example do? | ||
|
||
This nifty little program does the following: | ||
|
||
1. Sets up a connection to the OpenAI API using the GPT-4o model. | ||
|
||
2. Prompts the AI to generate a structured JSON output. | ||
|
||
|
||
## How to Run | ||
|
||
1. Make sure you have Go installed on your system. | ||
2. Set up your OpenAI API key as an environment variable. | ||
3. Run the program: | ||
|
||
``` | ||
go run openai_jsonformat.go | ||
``` |
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,11 @@ | ||
module github.com/tmc/langchaingo/examples/openai-jsonformat-example | ||
|
||
go 1.23 | ||
|
||
require github.com/tmc/langchaingo v0.1.13-pre.0.0.20250106145851-f1fde1f9e4a0 | ||
|
||
require ( | ||
github.com/dlclark/regexp2 v1.10.0 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/pkoukk/tiktoken-go v0.1.6 // indirect | ||
) |
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/tmc/langchaingo/llms" | ||
"github.com/tmc/langchaingo/llms/openai" | ||
"log" | ||
) | ||
|
||
type User struct { | ||
Name string `json:"name"` | ||
Age int `json:"age"` | ||
} | ||
|
||
func main() { | ||
fomat := &openai.ResponseFormat{ | ||
Type: "json_schema", | ||
JSONSchema: &openai.ResponseFormatJSONSchema{ | ||
Name: "object", | ||
Schema: &openai.ResponseFormatJSONSchemaProperty{ | ||
Type: "object", | ||
Properties: map[string]*openai.ResponseFormatJSONSchemaProperty{ | ||
"name": { | ||
Type: "string", | ||
Description: "The name of the user", | ||
}, | ||
"age": { | ||
Type: "integer", | ||
Description: "The age of the user", | ||
}, | ||
"role": { | ||
Type: "string", | ||
Description: "The role of the user", | ||
}, | ||
}, | ||
AdditionalProperties: false, | ||
Required: []string{"name", "age", "role"}, | ||
}, | ||
Strict: true, | ||
}, | ||
} | ||
llm, err := openai.New(openai.WithModel("gpt-4o"), openai.WithResponseFormat(fomat)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ctx := context.Background() | ||
|
||
content := []llms.MessageContent{ | ||
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."), | ||
llms.TextParts(llms.ChatMessageTypeHuman, "please tell me the most famous people in history"), | ||
} | ||
|
||
completion, err := llm.GenerateContent(ctx, content, llms.WithJSONMode()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Fatal(completion.Choices[0].Content) | ||
} |
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
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
Oops, something went wrong.