跳到内容

instructor-go

在 Go 中进行结构化的大型语言模型 (LLM) 提取,设计旨在实现简单、透明和可控。


Twitter Follow LinkedIn Follow Documentation GitHub issues Discord

也可以查看我们的 Python, JS/TS, ElixirPHP 版本。

如果你想将 Instructor 移植到其他语言,请在 Twitter 上联系我们,我们很乐意帮助你开始!

用法

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
    */
}

贡献

如果你想提供帮助,可以查看标记为 good-first-issuehelp-wanted 的问题。可以在 这里 找到它们。这可能包括代码改进、撰写博客文章或新的指南。

许可证

本项目采用 MIT 许可证授权。