Skip to content

instructor-go

Structured large language model (LLM) extraction in Go, designed for simplicity, transparency, and control.


Twitter Follow LinkedIn Follow Documentation GitHub issues Discord

Check us out in Python, JS/TS, Elixir and PHP.

If you want to port Instructor to another language, please reach out to us on Twitter we'd love to help you get started!

Usage

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/instructor-ai/instructor-go/pkg/instructor"
    openai "github.com/sashabaranov/go-openai"
)

type Person struct {
    Name string `json:"name"          jsonschema:"title=the name,description=The name of the person,example=joe,example=lucy"`
    Age  int    `json:"age,omitempty" jsonschema:"title=the age,description=The age of the person,example=25,example=67"`
}

func main() {
    ctx := context.Background()

    client := instructor.FromOpenAI(
        openai.NewClient(os.Getenv("OPENAI_API_KEY")),
        instructor.WithMode(instructor.ModeJSON),
        instructor.WithMaxRetries(3),
    )

    var person Person
    resp, err := client.CreateChatCompletion(
        ctx,
        openai.ChatCompletionRequest{
            Model: openai.GPT4o,
            Messages: []openai.ChatCompletionMessage{
                {
                    Role:    openai.ChatMessageRoleUser,
                    Content: "Extract Robby is 22 years old.",
                },
            },
        },
        &person,
    )
    _ = resp // sends back original response so no information loss from original API
    if err != nil {
        panic(err)
    }

    fmt.Printf(`
Name: %s
Age:  %d
`, person.Name, person.Age)
    /*
        Name: Robby
        Age:  22
    */
}

Contributing

If you want to help out, checkout some of the issues marked as good-first-issue or help-wanted. Found here. They could be anything from code improvements, a guest blog post, or a new cook book.

License

This project is licensed under the terms of the MIT License.