openai

package module
v1.17.5-vision-2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

README

Go OpenAI

Go Reference Go Report Card codecov

This library provides unofficial Go clients for OpenAI API. We support:

  • ChatGPT
  • GPT-3, GPT-4
  • DALL·E 2
  • Whisper

Installation

go get github.com/sashabaranov/go-openai

Currently, go-openai requires Go version 1.18 or greater.

Usage

ChatGPT example usage:
package main

import (
	"context"
	"fmt"
	openai "github.com/sashabaranov/go-openai"
)

func main() {
	client := openai.NewClient("your token")
	resp, err := client.CreateChatCompletion(
		context.Background(),
		openai.ChatCompletionRequest{
			Model: openai.GPT3Dot5Turbo,
			Messages: []openai.ChatCompletionMessage{
				{
					Role:    openai.ChatMessageRoleUser,
					Content: &openai.ChatMessageContent{
				    	Type: openai.ChatMessageContentTypeText,
				    	Text: "Hello!",
				    },
				},
			},
		},
	)

	if err != nil {
		fmt.Printf("ChatCompletion error: %v\n", err)
		return
	}

	fmt.Println(resp.Choices[0].Message.Content)
}

Getting an OpenAI API Key:
  1. Visit the OpenAI website at https://platform.openai.com/account/api-keys.
  2. If you don't have an account, click on "Sign Up" to create one. If you do, click "Log In".
  3. Once logged in, navigate to your API key management page.
  4. Click on "Create new secret key".
  5. Enter a name for your new key, then click "Create secret key".
  6. Your new API key will be displayed. Use this key to interact with the OpenAI API.

Note: Your API key is sensitive information. Do not share it with anyone.

Other examples:
ChatGPT streaming completion
package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	openai "github.com/sashabaranov/go-openai"
)

func main() {
	c := openai.NewClient("your token")
	ctx := context.Background()

	req := openai.ChatCompletionRequest{
		Model:     openai.GPT3Dot5Turbo,
		MaxTokens: 20,
		Messages: []openai.ChatCompletionMessage{
			{
				Role:    openai.ChatMessageRoleUser,
				Content: &openai.ChatMessageContent{
                    Type: openai.ChatMessageContentTypeText,
                    Text: "Lorem ipsum",
                }
			},
		},
		Stream: true,
	}
	stream, err := c.CreateChatCompletionStream(ctx, req)
	if err != nil {
		fmt.Printf("ChatCompletionStream error: %v\n", err)
		return
	}
	defer stream.Close()

	fmt.Printf("Stream response: ")
	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			fmt.Println("\nStream finished")
			return
		}

		if err != nil {
			fmt.Printf("\nStream error: %v\n", err)
			return
		}

		fmt.Printf(response.Choices[0].Delta.Content)
	}
}
GPT-3 completion
package main

import (
	"context"
	"fmt"
	openai "github.com/sashabaranov/go-openai"
)

func main() {
	c := openai.NewClient("your token")
	ctx := context.Background()

	req := openai.CompletionRequest{
		Model:     openai.GPT3Ada,
		MaxTokens: 5,
		Prompt:    "Lorem ipsum",
	}
	resp, err := c.CreateCompletion(ctx, req)
	if err != nil {
		fmt.Printf("Completion error: %v\n", err)
		return
	}
	fmt.Println(resp.Choices[0].Text)
}
GPT-3 streaming completion
package main

import (
	"errors"
	"context"
	"fmt"
	"io"
	openai "github.com/sashabaranov/go-openai"
)

func main() {
	c := openai.NewClient("your token")
	ctx := context.Background()

	req := openai.CompletionRequest{
		Model:     openai.GPT3Ada,
		MaxTokens: 5,
		Prompt:    "Lorem ipsum",
		Stream:    true,
	}
	stream, err := c.CreateCompletionStream(ctx, req)
	if err != nil {
		fmt.Printf("CompletionStream error: %v\n", err)
		return
	}
	defer stream.Close()

	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			fmt.Println("Stream finished")
			return
		}

		if err != nil {
			fmt.Printf("Stream error: %v\n", err)
			return
		}


		fmt.Printf("Stream response: %v\n", response)
	}
}
Audio Speech-To-Text
package main

import (
	"context"
	"fmt"

	openai "github.com/sashabaranov/go-openai"
)

func main() {
	c := openai.NewClient("your token")
	ctx := context.Background()

	req := openai.AudioRequest{
		Model:    openai.Whisper1,
		FilePath: "recording.mp3",
	}
	resp, err := c.CreateTranscription(ctx, req)
	if err != nil {
		fmt.Printf("Transcription error: %v\n", err)
		return
	}
	fmt.Println(resp.Text)
}
Audio Captions
package main

import (
	"context"
	"fmt"
	"os"

	openai "github.com/sashabaranov/go-openai"
)

func main() {
	c := openai.NewClient(os.Getenv("OPENAI_KEY"))

	req := openai.AudioRequest{
		Model:    openai.Whisper1,
		FilePath: os.Args[1],
		Format:   openai.AudioResponseFormatSRT,
	}
	resp, err := c.CreateTranscription(context.Background(), req)
	if err != nil {
		fmt.Printf("Transcription error: %v\n", err)
		return
	}
	f, err := os.Create(os.Args[1] + ".srt")
	if err != nil {
		fmt.Printf("Could not open file: %v\n", err)
		return
	}
	defer f.Close()
	if _, err := f.WriteString(resp.Text); err != nil {
		fmt.Printf("Error writing to file: %v\n", err)
		return
	}
}
DALL-E 2 image generation
package main

import (
	"bytes"
	"context"
	"encoding/base64"
	"fmt"
	openai "github.com/sashabaranov/go-openai"
	"image/png"
	"os"
)

func main() {
	c := openai.NewClient("your token")
	ctx := context.Background()

	// Sample image by link
	reqUrl := openai.ImageRequest{
		Prompt:         "Parrot on a skateboard performs a trick, cartoon style, natural light, high detail",
		Size:           openai.CreateImageSize256x256,
		ResponseFormat: openai.CreateImageResponseFormatURL,
		N:              1,
	}

	respUrl, err := c.CreateImage(ctx, reqUrl)
	if err != nil {
		fmt.Printf("Image creation error: %v\n", err)
		return
	}
	fmt.Println(respUrl.Data[0].URL)

	// Example image as base64
	reqBase64 := openai.ImageRequest{
		Prompt:         "Portrait of a humanoid parrot in a classic costume, high detail, realistic light, unreal engine",
		Size:           openai.CreateImageSize256x256,
		ResponseFormat: openai.CreateImageResponseFormatB64JSON,
		N:              1,
	}

	respBase64, err := c.CreateImage(ctx, reqBase64)
	if err != nil {
		fmt.Printf("Image creation error: %v\n", err)
		return
	}

	imgBytes, err := base64.StdEncoding.DecodeString(respBase64.Data[0].B64JSON)
	if err != nil {
		fmt.Printf("Base64 decode error: %v\n", err)
		return
	}

	r := bytes.NewReader(imgBytes)
	imgData, err := png.Decode(r)
	if err != nil {
		fmt.Printf("PNG decode error: %v\n", err)
		return
	}

	file, err := os.Create("example.png")
	if err != nil {
		fmt.Printf("File creation error: %v\n", err)
		return
	}
	defer file.Close()

	if err := png.Encode(file, imgData); err != nil {
		fmt.Printf("PNG encode error: %v\n", err)
		return
	}

	fmt.Println("The image was saved as example.png")
}

Configuring proxy
config := openai.DefaultConfig("token")
proxyUrl, err := url.Parse("http://localhost:{port}")
if err != nil {
	panic(err)
}
transport := &http.Transport{
	Proxy: http.ProxyURL(proxyUrl),
}
config.HTTPClient = &http.Client{
	Transport: transport,
}

c := openai.NewClientWithConfig(config)

See also: https://pkg.go.dev/github.com/sashabaranov/go-openai#ClientConfig

ChatGPT support context
package main

import (
	"bufio"
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/sashabaranov/go-openai"
)

func main() {
	client := openai.NewClient("your token")
	messages := make([]openai.ChatCompletionMessage, 0)
	reader := bufio.NewReader(os.Stdin)
	fmt.Println("Conversation")
	fmt.Println("---------------------")

	for {
		fmt.Print("-> ")
		text, _ := reader.ReadString('\n')
		// convert CRLF to LF
		text = strings.Replace(text, "\n", "", -1)
		messages = append(messages, openai.ChatCompletionMessage{
			Role:    openai.ChatMessageRoleUser,
			Content: text,
		})

		resp, err := client.CreateChatCompletion(
			context.Background(),
			openai.ChatCompletionRequest{
				Model:    openai.GPT3Dot5Turbo,
				Messages: messages,
			},
		)

		if err != nil {
			fmt.Printf("ChatCompletion error: %v\n", err)
			continue
		}

		content := resp.Choices[0].Message.Content
		messages = append(messages, openai.ChatCompletionMessage{
			Role:    openai.ChatMessageRoleAssistant,
			Content: content,
		})
		fmt.Println(content)
	}
}
Azure OpenAI ChatGPT
package main

import (
	"context"
	"fmt"

	openai "github.com/sashabaranov/go-openai"
)

func main() {
	config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint")
	// If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function
	// config.AzureModelMapperFunc = func(model string) string {
	// 	azureModelMapping = map[string]string{
	// 		"gpt-3.5-turbo": "your gpt-3.5-turbo deployment name",
	// 	}
	// 	return azureModelMapping[model]
	// }

	client := openai.NewClientWithConfig(config)
	resp, err := client.CreateChatCompletion(
		context.Background(),
		openai.ChatCompletionRequest{
			Model: openai.GPT3Dot5Turbo,
			Messages: []openai.ChatCompletionMessage{
				{
					Role:    openai.ChatMessageRoleUser,
					Content: "Hello Azure OpenAI!",
				},
			},
		},
	)
	if err != nil {
		fmt.Printf("ChatCompletion error: %v\n", err)
		return
	}

	fmt.Println(resp.Choices[0].Message.Content)
}

Embedding Semantic Similarity
package main

import (
	"context"
	"log"
	openai "github.com/sashabaranov/go-openai"

)

func main() {
	client := openai.NewClient("your-token")

	// Create an EmbeddingRequest for the user query
	queryReq := openai.EmbeddingRequest{
		Input: []string{"How many chucks would a woodchuck chuck"},
		Model: openai.AdaEmbeddingV2,
	}

	// Create an embedding for the user query
	queryResponse, err := client.CreateEmbeddings(context.Background(), queryReq)
	if err != nil {
		log.Fatal("Error creating query embedding:", err)
	}

	// Create an EmbeddingRequest for the target text
	targetReq := openai.EmbeddingRequest{
		Input: []string{"How many chucks would a woodchuck chuck if the woodchuck could chuck wood"},
		Model: openai.AdaEmbeddingV2,
	}

	// Create an embedding for the target text
	targetResponse, err := client.CreateEmbeddings(context.Background(), targetReq)
	if err != nil {
		log.Fatal("Error creating target embedding:", err)
	}

	// Now that we have the embeddings for the user query and the target text, we
	// can calculate their similarity.
	queryEmbedding := queryResponse.Data[0]
	targetEmbedding := targetResponse.Data[0]

	similarity, err := queryEmbedding.DotProduct(&targetEmbedding)
	if err != nil {
		log.Fatal("Error calculating dot product:", err)
	}

	log.Printf("The similarity score between the query and the target is %f", similarity)
}

Azure OpenAI Embeddings
package main

import (
	"context"
	"fmt"

	openai "github.com/sashabaranov/go-openai"
)

func main() {

	config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint")
	config.APIVersion = "2023-05-15" // optional update to latest API version

	//If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function
	//config.AzureModelMapperFunc = func(model string) string {
	//    azureModelMapping = map[string]string{
	//        "gpt-3.5-turbo":"your gpt-3.5-turbo deployment name",
	//    }
	//    return azureModelMapping[model]
	//}

	input := "Text to vectorize"

	client := openai.NewClientWithConfig(config)
	resp, err := client.CreateEmbeddings(
		context.Background(),
		openai.EmbeddingRequest{
			Input: []string{input},
			Model: openai.AdaEmbeddingV2,
		})

	if err != nil {
		fmt.Printf("CreateEmbeddings error: %v\n", err)
		return
	}

	vectors := resp.Data[0].Embedding // []float32 with 1536 dimensions

	fmt.Println(vectors[:10], "...", vectors[len(vectors)-10:])
}
JSON Schema for function calling

It is now possible for chat completion to choose to call a function for more information (see developer docs here).

In order to describe the type of functions that can be called, a JSON schema must be provided. Many JSON schema libraries exist and are more advanced than what we can offer in this library, however we have included a simple jsonschema package for those who want to use this feature without formatting their own JSON schema payload.

The developer documents give this JSON schema definition as an example:

{
  "name":"get_current_weather",
  "description":"Get the current weather in a given location",
  "parameters":{
    "type":"object",
    "properties":{
        "location":{
          "type":"string",
          "description":"The city and state, e.g. San Francisco, CA"
        },
        "unit":{
          "type":"string",
          "enum":[
              "celsius",
              "fahrenheit"
          ]
        }
    },
    "required":[
        "location"
    ]
  }
}

Using the jsonschema package, this schema could be created using structs as such:

FunctionDefinition{
  Name: "get_current_weather",
  Parameters: jsonschema.Definition{
    Type: jsonschema.Object,
    Properties: map[string]jsonschema.Definition{
      "location": {
        Type: jsonschema.String,
        Description: "The city and state, e.g. San Francisco, CA",
      },
      "unit": {
        Type: jsonschema.String,
        Enum: []string{"celcius", "fahrenheit"},
      },
    },
    Required: []string{"location"},
  },
}

The Parameters field of a FunctionDefinition can accept either of the above styles, or even a nested struct from another library (as long as it can be marshalled into JSON).

Error handling

Open-AI maintains clear documentation on how to handle API errors

example:

e := &openai.APIError{}
if errors.As(err, &e) {
  switch e.HTTPStatusCode {
    case 401:
      // invalid auth or key (do not retry)
    case 429:
      // rate limiting or engine overload (wait and retry) 
    case 500:
      // openai server error (retry)
    default:
      // unhandled
  }
}

Fine Tune Model
package main

import (
	"context"
	"fmt"
	"github.com/sashabaranov/go-openai"
)

func main() {
	client := openai.NewClient("your token")
	ctx := context.Background()

	// create a .jsonl file with your training data for conversational model
	// {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
	// {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
	// {"prompt": "<prompt text>", "completion": "<ideal generated text>"}

	// chat models are trained using the following file format:
	// {"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "What's the capital of France?"}, {"role": "assistant", "content": "Paris, as if everyone doesn't know that already."}]}
	// {"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "Who wrote 'Romeo and Juliet'?"}, {"role": "assistant", "content": "Oh, just some guy named William Shakespeare. Ever heard of him?"}]}
	// {"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "How far is the Moon from Earth?"}, {"role": "assistant", "content": "Around 384,400 kilometers. Give or take a few, like that really matters."}]}

	// you can use openai cli tool to validate the data
	// For more info - https://platform.openai.com/docs/guides/fine-tuning

	file, err := client.CreateFile(ctx, openai.FileRequest{
		FilePath: "training_prepared.jsonl",
		Purpose:  "fine-tune",
	})
	if err != nil {
		fmt.Printf("Upload JSONL file error: %v\n", err)
		return
	}

	// create a fine tuning job
	// Streams events until the job is done (this often takes minutes, but can take hours if there are many jobs in the queue or your dataset is large)
	// use below get method to know the status of your model
	fineTuningJob, err := client.CreateFineTuningJob(ctx, openai.FineTuningJobRequest{
		TrainingFile: file.ID,
		Model:        "davinci-002", // gpt-3.5-turbo-0613, babbage-002.
	})
	if err != nil {
		fmt.Printf("Creating new fine tune model error: %v\n", err)
		return
	}

	fineTuningJob, err = client.RetrieveFineTuningJob(ctx, fineTuningJob.ID)
	if err != nil {
		fmt.Printf("Getting fine tune model error: %v\n", err)
		return
	}
	fmt.Println(fineTuningJob.FineTunedModel)

	// once the status of fineTuningJob is `succeeded`, you can use your fine tune model in Completion Request or Chat Completion Request

	// resp, err := client.CreateCompletion(ctx, openai.CompletionRequest{
	//	 Model:  fineTuningJob.FineTunedModel,
	//	 Prompt: "your prompt",
	// })
	// if err != nil {
	//	 fmt.Printf("Create completion error %v\n", err)
	//	 return
	// }
	//
	// fmt.Println(resp.Choices[0].Text)
}
See the `examples/` folder for more.

Frequently Asked Questions

Why don't we get the same answer when specifying a temperature field of 0 and asking the same question?

Even when specifying a temperature field of 0, it doesn't guarantee that you'll always get the same response. Several factors come into play.

  1. Go OpenAI Behavior: When you specify a temperature field of 0 in Go OpenAI, the omitempty tag causes that field to be removed from the request. Consequently, the OpenAI API applies the default value of 1.
  2. Token Count for Input/Output: If there's a large number of tokens in the input and output, setting the temperature to 0 can still result in non-deterministic behavior. In particular, when using around 32k tokens, the likelihood of non-deterministic behavior becomes highest even with a temperature of 0.

Due to the factors mentioned above, different answers may be returned even for the same question.

Workarounds:

  1. As of November 2023, use the new seed parameter in conjunction with the system_fingerprint response field, alongside Temperature management.
  2. Try using math.SmallestNonzeroFloat32: By specifying math.SmallestNonzeroFloat32 in the temperature field instead of 0, you can mimic the behavior of setting it to 0.
  3. Limiting Token Count: By limiting the number of tokens in the input and output and especially avoiding large requests close to 32k tokens, you can reduce the risk of non-deterministic behavior.

By adopting these strategies, you can expect more consistent results.

Related Issues:
omitempty option of request struct will generate incorrect request when parameter is 0.

Does Go OpenAI provide a method to count tokens?

No, Go OpenAI does not offer a feature to count tokens, and there are no plans to provide such a feature in the future. However, if there's a way to implement a token counting feature with zero dependencies, it might be possible to merge that feature into Go OpenAI. Otherwise, it would be more appropriate to implement it in a dedicated library or repository.

For counting tokens, you might find the following links helpful:

Related Issues:
Is it possible to join the implementation of GPT3 Tokenizer

Contributing

By following Contributing Guidelines, we hope to ensure that your contributions are made smoothly and efficiently.

Thank you

We want to take a moment to express our deepest gratitude to the contributors and sponsors of this project:

To all of you: thank you. You've helped us achieve more than we ever imagined possible. Can't wait to see where we go next, together!

Documentation

Overview

Example
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
resp, err := client.CreateChatCompletion(
	context.Background(),
	openai.ChatCompletionRequest{
		Model: openai.GPT3Dot5Turbo,
		Messages: []openai.ChatCompletionMessage{
			{
				Role: openai.ChatMessageRoleUser,
				Content: []openai.ChatMessageContent{
					{
						Type: openai.ChatMessageContentTypeText,
						Text: "Hello!",
					},
				},
			},
		},
	},
)
if err != nil {
	fmt.Printf("ChatCompletion error: %v\n", err)
	return
}

fmt.Println(resp.Choices[0].Message.Content)
Output:

Example (Chatbot)
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

req := openai.ChatCompletionRequest{
	Model: openai.GPT3Dot5Turbo,
	Messages: []openai.ChatCompletionMessage{
		{
			Role: openai.ChatMessageRoleSystem,
			Content: []openai.ChatMessageContent{
				{
					Type: openai.ChatMessageContentTypeText,
					Text: "you are a helpful chatbot",
				},
			},
		},
	},
}
fmt.Println("Conversation")
fmt.Println("---------------------")
fmt.Print("> ")
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
	req.Messages = append(req.Messages, openai.ChatCompletionMessage{
		Role: openai.ChatMessageRoleUser,
		Content: []openai.ChatMessageContent{
			{
				Type: openai.ChatMessageContentTypeText,
				Text: s.Text(),
			},
		},
	})
	resp, err := client.CreateChatCompletion(context.Background(), req)
	if err != nil {
		fmt.Printf("ChatCompletion error: %v\n", err)
		continue
	}
	fmt.Printf("%s\n\n", resp.Choices[0].Message.Content)
	req.Messages = append(req.Messages, resp.Choices[0].Message.ToChatCompleteMessage())
	fmt.Print("> ")
}
Output:

Index

Examples

Constants

View Source
const (
	ChatMessageRoleSystem    = "system"
	ChatMessageRoleUser      = "user"
	ChatMessageRoleAssistant = "assistant"
	ChatMessageRoleFunction  = "function"
	ChatMessageRoleTool      = "tool"
)

Chat message role defined by the OpenAI API.

View Source
const (
	ChatMessageContentTypeText  = "text"
	ChatMessageContentTypeImage = "image_url"
)
View Source
const (
	GPT432K0613           = "gpt-4-32k-0613"
	GPT432K0314           = "gpt-4-32k-0314"
	GPT432K               = "gpt-4-32k"
	GPT40613              = "gpt-4-0613"
	GPT40314              = "gpt-4-0314"
	GPT4TurboPreview      = "gpt-4-1106-preview"
	GPT4VisionPreview     = "gpt-4-vision-preview"
	GPT4                  = "gpt-4"
	GPT3Dot5Turbo1106     = "gpt-3.5-turbo-1106"
	GPT3Dot5Turbo0613     = "gpt-3.5-turbo-0613"
	GPT3Dot5Turbo0301     = "gpt-3.5-turbo-0301"
	GPT3Dot5Turbo16K      = "gpt-3.5-turbo-16k"
	GPT3Dot5Turbo16K0613  = "gpt-3.5-turbo-16k-0613"
	GPT3Dot5Turbo         = "gpt-3.5-turbo"
	GPT3Dot5TurboInstruct = "gpt-3.5-turbo-instruct"
	// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
	GPT3TextDavinci003 = "text-davinci-003"
	// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
	GPT3TextDavinci002 = "text-davinci-002"
	// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
	GPT3TextCurie001 = "text-curie-001"
	// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
	GPT3TextBabbage001 = "text-babbage-001"
	// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
	GPT3TextAda001 = "text-ada-001"
	// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
	GPT3TextDavinci001 = "text-davinci-001"
	// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
	GPT3DavinciInstructBeta = "davinci-instruct-beta"
	GPT3Davinci             = "davinci"
	GPT3Davinci002          = "davinci-002"
	// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
	GPT3CurieInstructBeta = "curie-instruct-beta"
	GPT3Curie             = "curie"
	GPT3Curie002          = "curie-002"
	GPT3Ada               = "ada"
	GPT3Ada002            = "ada-002"
	GPT3Babbage           = "babbage"
	GPT3Babbage002        = "babbage-002"
)

GPT3 Defines the models provided by OpenAI to use when generating completions from OpenAI. GPT3 Models are designed for text-based tasks. For code-specific tasks, please refer to the Codex series of models.

View Source
const (
	CodexCodeDavinci002 = "code-davinci-002"
	CodexCodeCushman001 = "code-cushman-001"
	CodexCodeDavinci001 = "code-davinci-001"
)

Codex Defines the models provided by OpenAI. These models are designed for code-specific tasks, and use a different tokenizer which optimizes for whitespace.

View Source
const (
	CreateImageSize256x256   = "256x256"
	CreateImageSize512x512   = "512x512"
	CreateImageSize1024x1024 = "1024x1024"
	// dall-e-3 supported only.
	CreateImageSize1792x1024 = "1792x1024"
	CreateImageSize1024x1792 = "1024x1792"
)

Image sizes defined by the OpenAI API.

View Source
const (
	CreateImageResponseFormatURL     = "url"
	CreateImageResponseFormatB64JSON = "b64_json"
)
View Source
const (
	CreateImageModelDallE2 = "dall-e-2"
	CreateImageModelDallE3 = "dall-e-3"
)
View Source
const (
	CreateImageQualityHD       = "hd"
	CreateImageQualityStandard = "standard"
)
View Source
const (
	CreateImageStyleVivid   = "vivid"
	CreateImageStyleNatural = "natural"
)
View Source
const (
	ModerationTextStable = "text-moderation-stable"
	ModerationTextLatest = "text-moderation-latest"
	// Deprecated: use ModerationTextStable and ModerationTextLatest instead.
	ModerationText001 = "text-moderation-001"
)

The default is text-moderation-latest which will be automatically upgraded over time. This ensures you are always using our most accurate model. If you use text-moderation-stable, we will provide advanced notice before updating the model. Accuracy of text-moderation-stable may be slightly lower than for text-moderation-latest.

View Source
const AzureAPIKeyHeader = "api-key"
View Source
const (
	Whisper1 = "whisper-1"
)

Whisper Defines the models provided by OpenAI to use when processing audio with OpenAI.

Variables

View Source
var (
	ErrChatCompletionInvalidModel       = errors.New("this model is not supported with this method, please use CreateCompletion client method instead") //nolint:lll
	ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateChatCompletionStream")              //nolint:lll
)
View Source
var (
	ErrCompletionUnsupportedModel              = errors.New("this model is not supported with this method, please use CreateChatCompletion client method instead") //nolint:lll
	ErrCompletionStreamNotSupported            = errors.New("streaming is not supported with this method, please use CreateCompletionStream")                      //nolint:lll
	ErrCompletionRequestPromptTypeNotSupported = errors.New("the type of CompletionRequest.Prompt only supports string and []string")                              //nolint:lll
)
View Source
var (
	ErrModerationInvalidModel = errors.New("this model is not supported with moderation, please use text-moderation-stable or text-moderation-latest instead") //nolint:lll
)
View Source
var (
	ErrTooManyEmptyStreamMessages = errors.New("stream has sent too many empty messages")
)
View Source
var ErrVectorLengthMismatch = errors.New("vector length mismatch")

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Code           any         `json:"code,omitempty"`
	Message        string      `json:"message"`
	Param          *string     `json:"param,omitempty"`
	Type           string      `json:"type"`
	HTTPStatusCode int         `json:"-"`
	InnerError     *InnerError `json:"innererror,omitempty"`
}

APIError provides error information returned by the OpenAI API. InnerError struct is only valid for Azure OpenAI Service.

Example

Open-AI maintains clear documentation on how to handle API errors.

see: https://platform.openai.com/docs/guides/error-codes/api-errors

var err error // Assume this is the error you are checking.
e := &openai.APIError{}
if errors.As(err, &e) {
	switch e.HTTPStatusCode {
	case 401:
	// invalid auth or key (do not retry)
	case 429:
	// rate limiting or engine overload (wait and retry)
	case 500:
	// openai server error (retry)
	default:
		// unhandled
	}
}
Output:

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) UnmarshalJSON

func (e *APIError) UnmarshalJSON(data []byte) (err error)

type APIType

type APIType string
const (
	APITypeOpenAI  APIType = "OPEN_AI"
	APITypeAzure   APIType = "AZURE"
	APITypeAzureAD APIType = "AZURE_AD"
)

type Assistant

type Assistant struct {
	ID           string          `json:"id"`
	Object       string          `json:"object"`
	CreatedAt    int64           `json:"created_at"`
	Name         *string         `json:"name,omitempty"`
	Description  *string         `json:"description,omitempty"`
	Model        string          `json:"model"`
	Instructions *string         `json:"instructions,omitempty"`
	Tools        []AssistantTool `json:"tools,omitempty"`
	// contains filtered or unexported fields
}

func (*Assistant) GetRateLimitHeaders

func (h *Assistant) GetRateLimitHeaders() RateLimitHeaders

func (*Assistant) Header

func (h *Assistant) Header() http.Header

func (*Assistant) SetHeader

func (h *Assistant) SetHeader(header http.Header)

type AssistantDeleteResponse

type AssistantDeleteResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Deleted bool   `json:"deleted"`
	// contains filtered or unexported fields
}

func (*AssistantDeleteResponse) GetRateLimitHeaders

func (h *AssistantDeleteResponse) GetRateLimitHeaders() RateLimitHeaders

func (*AssistantDeleteResponse) Header

func (h *AssistantDeleteResponse) Header() http.Header

func (*AssistantDeleteResponse) SetHeader

func (h *AssistantDeleteResponse) SetHeader(header http.Header)

type AssistantFile

type AssistantFile struct {
	ID          string `json:"id"`
	Object      string `json:"object"`
	CreatedAt   int64  `json:"created_at"`
	AssistantID string `json:"assistant_id"`
	// contains filtered or unexported fields
}

func (*AssistantFile) GetRateLimitHeaders

func (h *AssistantFile) GetRateLimitHeaders() RateLimitHeaders

func (*AssistantFile) Header

func (h *AssistantFile) Header() http.Header

func (*AssistantFile) SetHeader

func (h *AssistantFile) SetHeader(header http.Header)

type AssistantFileRequest

type AssistantFileRequest struct {
	FileID string `json:"file_id"`
}

type AssistantFilesList

type AssistantFilesList struct {
	AssistantFiles []AssistantFile `json:"data"`
	// contains filtered or unexported fields
}

func (*AssistantFilesList) GetRateLimitHeaders

func (h *AssistantFilesList) GetRateLimitHeaders() RateLimitHeaders

func (*AssistantFilesList) Header

func (h *AssistantFilesList) Header() http.Header

func (*AssistantFilesList) SetHeader

func (h *AssistantFilesList) SetHeader(header http.Header)

type AssistantRequest

type AssistantRequest struct {
	Model        string          `json:"model"`
	Name         *string         `json:"name,omitempty"`
	Description  *string         `json:"description,omitempty"`
	Instructions *string         `json:"instructions,omitempty"`
	Tools        []AssistantTool `json:"tools,omitempty"`
	FileIDs      []string        `json:"file_ids,omitempty"`
	Metadata     map[string]any  `json:"metadata,omitempty"`
}

type AssistantTool

type AssistantTool struct {
	Type     AssistantToolType   `json:"type"`
	Function *FunctionDefinition `json:"function,omitempty"`
}

type AssistantToolType

type AssistantToolType string
const (
	AssistantToolTypeCodeInterpreter AssistantToolType = "code_interpreter"
	AssistantToolTypeRetrieval       AssistantToolType = "retrieval"
	AssistantToolTypeFunction        AssistantToolType = "function"
)

type AssistantsList

type AssistantsList struct {
	Assistants []Assistant `json:"data"`
	// contains filtered or unexported fields
}

AssistantsList is a list of assistants.

func (*AssistantsList) GetRateLimitHeaders

func (h *AssistantsList) GetRateLimitHeaders() RateLimitHeaders

func (*AssistantsList) Header

func (h *AssistantsList) Header() http.Header

func (*AssistantsList) SetHeader

func (h *AssistantsList) SetHeader(header http.Header)

type AudioRequest

type AudioRequest struct {
	Model string

	// FilePath is either an existing file in your filesystem or a filename representing the contents of Reader.
	FilePath string

	// Reader is an optional io.Reader when you do not want to use an existing file.
	Reader io.Reader

	Prompt      string // For translation, it should be in English
	Temperature float32
	Language    string // For translation, just do not use it. It seems "en" works, not confirmed...
	Format      AudioResponseFormat
}

AudioRequest represents a request structure for audio API. ResponseFormat is not supported for now. We only return JSON text, which may be sufficient.

func (AudioRequest) HasJSONResponse

func (r AudioRequest) HasJSONResponse() bool

HasJSONResponse returns true if the response format is JSON.

type AudioResponse

type AudioResponse struct {
	Task     string  `json:"task"`
	Language string  `json:"language"`
	Duration float64 `json:"duration"`
	Segments []struct {
		ID               int     `json:"id"`
		Seek             int     `json:"seek"`
		Start            float64 `json:"start"`
		End              float64 `json:"end"`
		Text             string  `json:"text"`
		Tokens           []int   `json:"tokens"`
		Temperature      float64 `json:"temperature"`
		AvgLogprob       float64 `json:"avg_logprob"`
		CompressionRatio float64 `json:"compression_ratio"`
		NoSpeechProb     float64 `json:"no_speech_prob"`
		Transient        bool    `json:"transient"`
	} `json:"segments"`
	Text string `json:"text"`
	// contains filtered or unexported fields
}

AudioResponse represents a response structure for audio API.

func (*AudioResponse) GetRateLimitHeaders

func (h *AudioResponse) GetRateLimitHeaders() RateLimitHeaders

func (*AudioResponse) Header

func (h *AudioResponse) Header() http.Header

func (*AudioResponse) SetHeader

func (h *AudioResponse) SetHeader(header http.Header)

type AudioResponseFormat

type AudioResponseFormat string

Response formats; Whisper uses AudioResponseFormatJSON by default.

const (
	AudioResponseFormatJSON        AudioResponseFormat = "json"
	AudioResponseFormatText        AudioResponseFormat = "text"
	AudioResponseFormatSRT         AudioResponseFormat = "srt"
	AudioResponseFormatVerboseJSON AudioResponseFormat = "verbose_json"
	AudioResponseFormatVTT         AudioResponseFormat = "vtt"
)

type Base64Embedding

type Base64Embedding struct {
	Object    string       `json:"object"`
	Embedding base64String `json:"embedding"`
	Index     int          `json:"index"`
}

Base64Embedding is a container for base64 encoded embeddings.

type ChatCompletionChoice

type ChatCompletionChoice struct {
	Index   int                         `json:"index"`
	Message ChatCompletionChoiceMessage `json:"message"`
	// FinishReason
	// stop: API returned complete message,
	// or a message terminated by one of the stop sequences provided via the stop parameter
	// length: Incomplete model output due to max_tokens parameter or token limit
	// function_call: The model decided to call a function
	// content_filter: Omitted content due to a flag from our content filters
	// null: API response still in progress or incomplete
	FinishReason FinishReason `json:"finish_reason"`
}

type ChatCompletionChoiceMessage

type ChatCompletionChoiceMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`

	// This property isn't in the official documentation, but it's in
	// the documentation for the official library for python:
	// - https://github.com/openai/openai-python/blob/main/chatml.md
	// - https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
	Name string `json:"name,omitempty"`

	FunctionCall *FunctionCall `json:"function_call,omitempty"`
	ToolCalls    []ToolCall    `json:"tool_calls,omitempty"`
}

func (ChatCompletionChoiceMessage) ToChatCompleteMessage

func (message ChatCompletionChoiceMessage) ToChatCompleteMessage() ChatCompletionMessage

type ChatCompletionMessage

type ChatCompletionMessage struct {
	Role    string               `json:"role"`
	Content []ChatMessageContent `json:"content"`

	// This property isn't in the official documentation, but it's in
	// the documentation for the official library for python:
	// - https://github.com/openai/openai-python/blob/main/chatml.md
	// - https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
	Name string `json:"name,omitempty"`

	FunctionCall *FunctionCall `json:"function_call,omitempty"`
	ToolCalls    []ToolCall    `json:"tool_calls,omitempty"`
}

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model            string                        `json:"model"`
	Messages         []ChatCompletionMessage       `json:"messages"`
	MaxTokens        int                           `json:"max_tokens,omitempty"`
	Temperature      float32                       `json:"temperature,omitempty"`
	TopP             float32                       `json:"top_p,omitempty"`
	N                int                           `json:"n,omitempty"`
	Stream           bool                          `json:"stream,omitempty"`
	Stop             []string                      `json:"stop,omitempty"`
	PresencePenalty  float32                       `json:"presence_penalty,omitempty"`
	ResponseFormat   *ChatCompletionResponseFormat `json:"response_format,omitempty"`
	Seed             *int                          `json:"seed,omitempty"`
	FrequencyPenalty float32                       `json:"frequency_penalty,omitempty"`
	// LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string.
	// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
	// refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
	LogitBias map[string]int `json:"logit_bias,omitempty"`
	User      string         `json:"user,omitempty"`
	// Deprecated: use Tools instead.
	Functions []FunctionDefinition `json:"functions,omitempty"`
	// Deprecated: use ToolChoice instead.
	FunctionCall any    `json:"function_call,omitempty"`
	Tools        []Tool `json:"tools,omitempty"`
	// This can be either a string or an ToolChoice object.
	ToolChoiche any `json:"tool_choice,omitempty"`
}

ChatCompletionRequest represents a request structure for chat completion API.

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID      string                 `json:"id"`
	Object  string                 `json:"object"`
	Created int64                  `json:"created"`
	Model   string                 `json:"model"`
	Choices []ChatCompletionChoice `json:"choices"`
	Usage   Usage                  `json:"usage"`
	// contains filtered or unexported fields
}

ChatCompletionResponse represents a response structure for chat completion API.

func (*ChatCompletionResponse) GetRateLimitHeaders

func (h *ChatCompletionResponse) GetRateLimitHeaders() RateLimitHeaders

func (*ChatCompletionResponse) Header

func (h *ChatCompletionResponse) Header() http.Header

func (*ChatCompletionResponse) SetHeader

func (h *ChatCompletionResponse) SetHeader(header http.Header)

type ChatCompletionResponseFormat

type ChatCompletionResponseFormat struct {
	Type ChatCompletionResponseFormatType `json:"type"`
}

type ChatCompletionResponseFormatType

type ChatCompletionResponseFormatType string
const (
	ChatCompletionResponseFormatTypeJSONObject ChatCompletionResponseFormatType = "json_object"
	ChatCompletionResponseFormatTypeText       ChatCompletionResponseFormatType = "text"
)

type ChatCompletionStream

type ChatCompletionStream struct {
	// contains filtered or unexported fields
}

ChatCompletionStream Note: Perhaps it is more elegant to abstract Stream using generics.

func (ChatCompletionStream) Close

func (stream ChatCompletionStream) Close()

func (ChatCompletionStream) Recv

func (stream ChatCompletionStream) Recv() (response T, err error)

type ChatCompletionStreamChoice

type ChatCompletionStreamChoice struct {
	Index                int                             `json:"index"`
	Delta                ChatCompletionStreamChoiceDelta `json:"delta"`
	FinishReason         FinishReason                    `json:"finish_reason"`
	ContentFilterResults ContentFilterResults            `json:"content_filter_results,omitempty"`
}

type ChatCompletionStreamChoiceDelta

type ChatCompletionStreamChoiceDelta struct {
	Content      string        `json:"content,omitempty"`
	Role         string        `json:"role,omitempty"`
	FunctionCall *FunctionCall `json:"function_call,omitempty"`
	ToolCalls    []ToolCall    `json:"tool_calls,omitempty"`
}

type ChatCompletionStreamResponse

type ChatCompletionStreamResponse struct {
	ID                string                       `json:"id"`
	Object            string                       `json:"object"`
	Created           int64                        `json:"created"`
	Model             string                       `json:"model"`
	Choices           []ChatCompletionStreamChoice `json:"choices"`
	PromptAnnotations []PromptAnnotation           `json:"prompt_annotations,omitempty"`
}

type ChatMessageContent

type ChatMessageContent struct {
	Type     string               `json:"type"`
	Text     string               `json:"text,omitempty"`
	ImageURL *ChatMessageImageURL `json:"image_url,omitempty"`
}

type ChatMessageImageURL

type ChatMessageImageURL struct {
	Detail ImageURLDetail `json:"detail"`
	URL    string         `json:"url"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is OpenAI GPT-3 API client.

func NewClient

func NewClient(authToken string) *Client

NewClient creates new OpenAI API client.

func NewClientWithConfig

func NewClientWithConfig(config ClientConfig) *Client

NewClientWithConfig creates new OpenAI API client for specified config.

func NewOrgClient deprecated

func NewOrgClient(authToken, org string) *Client

NewOrgClient creates new OpenAI API client for specified Organization ID.

Deprecated: Please use NewClientWithConfig.

func (*Client) CancelFineTune

func (c *Client) CancelFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error)

CancelFineTune cancel a fine-tune job. Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*Client) CancelFineTuningJob

func (c *Client) CancelFineTuningJob(ctx context.Context, fineTuningJobID string) (response FineTuningJob, err error)

CancelFineTuningJob cancel a fine tuning job.

func (*Client) CreateAssistant

func (c *Client) CreateAssistant(ctx context.Context, request AssistantRequest) (response Assistant, err error)

CreateAssistant creates a new assistant.

func (*Client) CreateAssistantFile

func (c *Client) CreateAssistantFile(
	ctx context.Context,
	assistantID string,
	request AssistantFileRequest,
) (response AssistantFile, err error)

CreateAssistantFile creates a new assistant file.

func (*Client) CreateChatCompletion

func (c *Client) CreateChatCompletion(
	ctx context.Context,
	request ChatCompletionRequest,
) (response ChatCompletionResponse, err error)

CreateChatCompletion — API call to Create a completion for the chat message.

func (*Client) CreateChatCompletionStream

func (c *Client) CreateChatCompletionStream(
	ctx context.Context,
	request ChatCompletionRequest,
) (stream *ChatCompletionStream, err error)

CreateChatCompletionStream — API call to create a chat completion w/ streaming support. It sets whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.

Example
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

stream, err := client.CreateChatCompletionStream(
	context.Background(),
	openai.ChatCompletionRequest{
		Model:     openai.GPT3Dot5Turbo,
		MaxTokens: 20,
		Messages: []openai.ChatCompletionMessage{
			{
				Role: openai.ChatMessageRoleUser,
				Content: []openai.ChatMessageContent{
					{
						Type: openai.ChatMessageContentTypeText,
						Text: "Lorem ipsum",
					},
				},
			},
		},
		Stream: true,
	},
)
if err != nil {
	fmt.Printf("ChatCompletionStream error: %v\n", err)
	return
}
defer stream.Close()

fmt.Printf("Stream response: ")
for {
	var response openai.ChatCompletionStreamResponse
	response, err = stream.Recv()
	if errors.Is(err, io.EOF) {
		fmt.Println("\nStream finished")
		return
	}

	if err != nil {
		fmt.Printf("\nStream error: %v\n", err)
		return
	}

	fmt.Printf(response.Choices[0].Delta.Content)
}
Output:

func (*Client) CreateCompletion

func (c *Client) CreateCompletion(
	ctx context.Context,
	request CompletionRequest,
) (response CompletionResponse, err error)

CreateCompletion — API call to create a completion. This is the main endpoint of the API. Returns new text as well as, if requested, the probabilities over each alternative token at each position.

If using a fine-tuned model, simply provide the model's ID in the CompletionRequest object, and the server will use the model's parameters to generate the completion.

Example
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
resp, err := client.CreateCompletion(
	context.Background(),
	openai.CompletionRequest{
		Model:     openai.GPT3Ada,
		MaxTokens: 5,
		Prompt:    "Lorem ipsum",
	},
)
if err != nil {
	fmt.Printf("Completion error: %v\n", err)
	return
}
fmt.Println(resp.Choices[0].Text)
Output:

func (*Client) CreateCompletionStream

func (c *Client) CreateCompletionStream(
	ctx context.Context,
	request CompletionRequest,
) (stream *CompletionStream, err error)

CreateCompletionStream — API call to create a completion w/ streaming support. It sets whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.

Example
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
stream, err := client.CreateCompletionStream(
	context.Background(),
	openai.CompletionRequest{
		Model:     openai.GPT3Ada,
		MaxTokens: 5,
		Prompt:    "Lorem ipsum",
		Stream:    true,
	},
)
if err != nil {
	fmt.Printf("CompletionStream error: %v\n", err)
	return
}
defer stream.Close()

for {
	var response openai.CompletionResponse
	response, err = stream.Recv()
	if errors.Is(err, io.EOF) {
		fmt.Println("Stream finished")
		return
	}

	if err != nil {
		fmt.Printf("Stream error: %v\n", err)
		return
	}

	fmt.Printf("Stream response: %#v\n", response)
}
Output:

func (*Client) CreateEditImage

func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest) (response ImageResponse, err error)

CreateEditImage - API call to create an image. This is the main endpoint of the DALL-E API.

func (*Client) CreateEmbeddings

func (c *Client) CreateEmbeddings(
	ctx context.Context,
	conv EmbeddingRequestConverter,
) (res EmbeddingResponse, err error)

CreateEmbeddings returns an EmbeddingResponse which will contain an Embedding for every item in |body.Input|. https://beta.openai.com/docs/api-reference/embeddings/create

Body should be of type EmbeddingRequestStrings for embedding strings or EmbeddingRequestTokens for embedding groups of text already converted to tokens.

func (*Client) CreateFile

func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File, err error)

CreateFile uploads a jsonl file to GPT3 FilePath must be a local file path.

func (*Client) CreateFineTune deprecated

func (c *Client) CreateFineTune(ctx context.Context, request FineTuneRequest) (response FineTune, err error)

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*Client) CreateFineTuningJob

func (c *Client) CreateFineTuningJob(
	ctx context.Context,
	request FineTuningJobRequest,
) (response FineTuningJob, err error)

CreateFineTuningJob create a fine tuning job.

func (*Client) CreateImage

func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (response ImageResponse, err error)

CreateImage - API call to create an image. This is the main endpoint of the DALL-E API.

Example
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

respURL, err := client.CreateImage(
	context.Background(),
	openai.ImageRequest{
		Prompt:         "Parrot on a skateboard performs a trick, cartoon style, natural light, high detail",
		Size:           openai.CreateImageSize256x256,
		ResponseFormat: openai.CreateImageResponseFormatURL,
		N:              1,
	},
)
if err != nil {
	fmt.Printf("Image creation error: %v\n", err)
	return
}
fmt.Println(respURL.Data[0].URL)
Output:

Example (Base64)
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

resp, err := client.CreateImage(
	context.Background(),
	openai.ImageRequest{
		Prompt:         "Portrait of a humanoid parrot in a classic costume, high detail, realistic light, unreal engine",
		Size:           openai.CreateImageSize512x512,
		ResponseFormat: openai.CreateImageResponseFormatB64JSON,
		N:              1,
	},
)
if err != nil {
	fmt.Printf("Image creation error: %v\n", err)
	return
}

b, err := base64.StdEncoding.DecodeString(resp.Data[0].B64JSON)
if err != nil {
	fmt.Printf("Base64 decode error: %v\n", err)
	return
}

f, err := os.Create("example.png")
if err != nil {
	fmt.Printf("File creation error: %v\n", err)
	return
}
defer f.Close()

_, err = f.Write(b)
if err != nil {
	fmt.Printf("File write error: %v\n", err)
	return
}

fmt.Println("The image was saved as example.png")
Output:

func (*Client) CreateTranscription

func (c *Client) CreateTranscription(
	ctx context.Context,
	request AudioRequest,
) (response AudioResponse, err error)

CreateTranscription — API call to create a transcription. Returns transcribed text.

Example
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
resp, err := client.CreateTranscription(
	context.Background(),
	openai.AudioRequest{
		Model:    openai.Whisper1,
		FilePath: "recording.mp3",
	},
)
if err != nil {
	fmt.Printf("Transcription error: %v\n", err)
	return
}
fmt.Println(resp.Text)
Output:

Example (Captions)
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

resp, err := client.CreateTranscription(
	context.Background(),
	openai.AudioRequest{
		Model:    openai.Whisper1,
		FilePath: os.Args[1],
		Format:   openai.AudioResponseFormatSRT,
	},
)
if err != nil {
	fmt.Printf("Transcription error: %v\n", err)
	return
}
f, err := os.Create(os.Args[1] + ".srt")
if err != nil {
	fmt.Printf("Could not open file: %v\n", err)
	return
}
defer f.Close()
if _, err = f.WriteString(resp.Text); err != nil {
	fmt.Printf("Error writing to file: %v\n", err)
	return
}
Output:

func (*Client) CreateTranslation

func (c *Client) CreateTranslation(
	ctx context.Context,
	request AudioRequest,
) (response AudioResponse, err error)

CreateTranslation — API call to translate audio into English.

Example
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
resp, err := client.CreateTranslation(
	context.Background(),
	openai.AudioRequest{
		Model:    openai.Whisper1,
		FilePath: "recording.mp3",
	},
)
if err != nil {
	fmt.Printf("Translation error: %v\n", err)
	return
}
fmt.Println(resp.Text)
Output:

func (*Client) CreateVariImage

func (c *Client) CreateVariImage(ctx context.Context, request ImageVariRequest) (response ImageResponse, err error)

CreateVariImage - API call to create an image variation. This is the main endpoint of the DALL-E API. Use abbreviations(vari for variation) because ci-lint has a single-line length limit ...

func (*Client) DeleteAssistant

func (c *Client) DeleteAssistant(
	ctx context.Context,
	assistantID string,
) (response AssistantDeleteResponse, err error)

DeleteAssistant deletes an assistant.

func (*Client) DeleteAssistantFile

func (c *Client) DeleteAssistantFile(
	ctx context.Context,
	assistantID string,
	fileID string,
) (err error)

DeleteAssistantFile deletes an existing file.

func (*Client) DeleteFile

func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error)

DeleteFile deletes an existing file.

func (*Client) DeleteFineTune deprecated

func (c *Client) DeleteFineTune(ctx context.Context, fineTuneID string) (response FineTuneDeleteResponse, err error)

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*Client) DeleteFineTuneModel

func (c *Client) DeleteFineTuneModel(ctx context.Context, modelID string) (
	response FineTuneModelDeleteResponse, err error)

DeleteFineTuneModel Deletes a fine-tune model. You must have the Owner role in your organization to delete a model.

func (*Client) Edits

func (c *Client) Edits(ctx context.Context, request EditsRequest) (response EditsResponse, err error)

Edits Perform an API call to the Edits endpoint.

Deprecated: Users of the Edits API and its associated models (e.g., text-davinci-edit-001 or code-davinci-edit-001)

will need to migrate to GPT-3.5 Turbo by January 4, 2024. You can use CreateChatCompletion or CreateChatCompletionStream instead.

func (*Client) GetEngine

func (c *Client) GetEngine(
	ctx context.Context,
	engineID string,
) (engine Engine, err error)

GetEngine Retrieves an engine instance, providing basic information about the engine such as the owner and availability.

func (*Client) GetFile

func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error)

GetFile Retrieves a file instance, providing basic information about the file such as the file name and purpose.

func (*Client) GetFileContent

func (c *Client) GetFileContent(ctx context.Context, fileID string) (content io.ReadCloser, err error)

func (*Client) GetFineTune deprecated

func (c *Client) GetFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error)

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*Client) GetModel

func (c *Client) GetModel(ctx context.Context, modelID string) (model Model, err error)

GetModel Retrieves a model instance, providing basic information about the model such as the owner and permissioning.

func (*Client) ListAssistantFiles

func (c *Client) ListAssistantFiles(
	ctx context.Context,
	assistantID string,
	limit *int,
	order *string,
	after *string,
	before *string,
) (response AssistantFilesList, err error)

ListAssistantFiles Lists the currently available files for an assistant.

func (*Client) ListAssistants

func (c *Client) ListAssistants(
	ctx context.Context,
	limit *int,
	order *string,
	after *string,
	before *string,
) (reponse AssistantsList, err error)

ListAssistants Lists the currently available assistants.

func (*Client) ListEngines

func (c *Client) ListEngines(ctx context.Context) (engines EnginesList, err error)

ListEngines Lists the currently available engines, and provides basic information about each option such as the owner and availability.

func (*Client) ListFiles

func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error)

ListFiles Lists the currently available files, and provides basic information about each file such as the file name and purpose.

func (*Client) ListFineTuneEvents deprecated

func (c *Client) ListFineTuneEvents(ctx context.Context, fineTuneID string) (response FineTuneEventList, err error)

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*Client) ListFineTunes deprecated

func (c *Client) ListFineTunes(ctx context.Context) (response FineTuneList, err error)

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*Client) ListFineTuningJobEvents

func (c *Client) ListFineTuningJobEvents(
	ctx context.Context,
	fineTuningJobID string,
	setters ...ListFineTuningJobEventsParameter,
) (response FineTuningJobEventList, err error)

ListFineTuningJobs list fine tuning jobs events.

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) (models ModelsList, err error)

ListModels Lists the currently available models, and provides basic information about each model such as the model id and parent.

func (*Client) Moderations

func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (response ModerationResponse, err error)

Moderations — perform a moderation api call over a string. Input can be an array or slice but a string will reduce the complexity.

func (*Client) ModifyAssistant

func (c *Client) ModifyAssistant(
	ctx context.Context,
	assistantID string,
	request AssistantRequest,
) (response Assistant, err error)

ModifyAssistant modifies an assistant.

func (*Client) RetrieveAssistant

func (c *Client) RetrieveAssistant(
	ctx context.Context,
	assistantID string,
) (response Assistant, err error)

RetrieveAssistant retrieves an assistant.

func (*Client) RetrieveAssistantFile

func (c *Client) RetrieveAssistantFile(
	ctx context.Context,
	assistantID string,
	fileID string,
) (response AssistantFile, err error)

RetrieveAssistantFile retrieves an assistant file.

func (*Client) RetrieveFineTuningJob

func (c *Client) RetrieveFineTuningJob(
	ctx context.Context,
	fineTuningJobID string,
) (response FineTuningJob, err error)

RetrieveFineTuningJob retrieve a fine tuning job.

type ClientConfig

type ClientConfig struct {
	BaseURL              string
	OrgID                string
	APIType              APIType
	APIVersion           string                    // required when APIType is APITypeAzure or APITypeAzureAD
	AzureModelMapperFunc func(model string) string // replace model to azure deployment name func
	HTTPClient           *http.Client

	EmptyMessagesLimit uint
	// contains filtered or unexported fields
}

ClientConfig is a configuration of a client.

Example (ClientWithProxy)
config := openai.DefaultConfig(os.Getenv("OPENAI_API_KEY"))
port := os.Getenv("OPENAI_PROXY_PORT")
proxyURL, err := url.Parse(fmt.Sprintf("http://localhost:%s", port))
if err != nil {
	panic(err)
}
transport := &http.Transport{
	Proxy: http.ProxyURL(proxyURL),
}
config.HTTPClient = &http.Client{
	Transport: transport,
}

client := openai.NewClientWithConfig(config)

client.CreateChatCompletion( //nolint:errcheck // outside of the scope of this example.
	context.Background(),
	openai.ChatCompletionRequest{
		// etc...
	},
)
Output:

func DefaultAzureConfig

func DefaultAzureConfig(apiKey, baseURL string) ClientConfig
Example
azureKey := os.Getenv("AZURE_OPENAI_API_KEY")       // Your azure API key
azureEndpoint := os.Getenv("AZURE_OPENAI_ENDPOINT") // Your azure OpenAI endpoint
config := openai.DefaultAzureConfig(azureKey, azureEndpoint)
client := openai.NewClientWithConfig(config)
resp, err := client.CreateChatCompletion(
	context.Background(),
	openai.ChatCompletionRequest{
		Model: openai.GPT3Dot5Turbo,
		Messages: []openai.ChatCompletionMessage{
			{
				Role: openai.ChatMessageRoleUser,
				Content: []openai.ChatMessageContent{
					{
						Type: openai.ChatMessageContentTypeText,
						Text: "Hello Azure OpenAI!",
					},
				},
			},
		},
	},
)
if err != nil {
	fmt.Printf("ChatCompletion error: %v\n", err)
	return
}

fmt.Println(resp.Choices[0].Message.Content)
Output:

func DefaultConfig

func DefaultConfig(authToken string) ClientConfig

func (ClientConfig) GetAzureDeploymentByModel

func (c ClientConfig) GetAzureDeploymentByModel(model string) string

func (ClientConfig) String

func (ClientConfig) String() string

type CompletionChoice

type CompletionChoice struct {
	Text         string        `json:"text"`
	Index        int           `json:"index"`
	FinishReason string        `json:"finish_reason"`
	LogProbs     LogprobResult `json:"logprobs"`
}

CompletionChoice represents one of possible completions.

type CompletionRequest

type CompletionRequest struct {
	Model            string   `json:"model"`
	Prompt           any      `json:"prompt,omitempty"`
	Suffix           string   `json:"suffix,omitempty"`
	MaxTokens        int      `json:"max_tokens,omitempty"`
	Temperature      float32  `json:"temperature,omitempty"`
	TopP             float32  `json:"top_p,omitempty"`
	N                int      `json:"n,omitempty"`
	Stream           bool     `json:"stream,omitempty"`
	LogProbs         int      `json:"logprobs,omitempty"`
	Echo             bool     `json:"echo,omitempty"`
	Stop             []string `json:"stop,omitempty"`
	PresencePenalty  float32  `json:"presence_penalty,omitempty"`
	FrequencyPenalty float32  `json:"frequency_penalty,omitempty"`
	BestOf           int      `json:"best_of,omitempty"`
	// LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string.
	// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
	// refs: https://platform.openai.com/docs/api-reference/completions/create#completions/create-logit_bias
	LogitBias map[string]int `json:"logit_bias,omitempty"`
	User      string         `json:"user,omitempty"`
}

CompletionRequest represents a request structure for completion API.

type CompletionResponse

type CompletionResponse struct {
	ID      string             `json:"id"`
	Object  string             `json:"object"`
	Created int64              `json:"created"`
	Model   string             `json:"model"`
	Choices []CompletionChoice `json:"choices"`
	Usage   Usage              `json:"usage"`
	// contains filtered or unexported fields
}

CompletionResponse represents a response structure for completion API.

func (*CompletionResponse) GetRateLimitHeaders

func (h *CompletionResponse) GetRateLimitHeaders() RateLimitHeaders

func (*CompletionResponse) Header

func (h *CompletionResponse) Header() http.Header

func (*CompletionResponse) SetHeader

func (h *CompletionResponse) SetHeader(header http.Header)

type CompletionStream

type CompletionStream struct {
	// contains filtered or unexported fields
}

func (CompletionStream) Close

func (stream CompletionStream) Close()

func (CompletionStream) Recv

func (stream CompletionStream) Recv() (response T, err error)

type ContentFilterResults

type ContentFilterResults struct {
	Hate     Hate     `json:"hate,omitempty"`
	SelfHarm SelfHarm `json:"self_harm,omitempty"`
	Sexual   Sexual   `json:"sexual,omitempty"`
	Violence Violence `json:"violence,omitempty"`
}

type EditsChoice

type EditsChoice struct {
	Text  string `json:"text"`
	Index int    `json:"index"`
}

EditsChoice represents one of possible edits.

type EditsRequest

type EditsRequest struct {
	Model       *string `json:"model,omitempty"`
	Input       string  `json:"input,omitempty"`
	Instruction string  `json:"instruction,omitempty"`
	N           int     `json:"n,omitempty"`
	Temperature float32 `json:"temperature,omitempty"`
	TopP        float32 `json:"top_p,omitempty"`
}

EditsRequest represents a request structure for Edits API.

type EditsResponse

type EditsResponse struct {
	Object  string        `json:"object"`
	Created int64         `json:"created"`
	Usage   Usage         `json:"usage"`
	Choices []EditsChoice `json:"choices"`
	// contains filtered or unexported fields
}

EditsResponse represents a response structure for Edits API.

func (*EditsResponse) GetRateLimitHeaders

func (h *EditsResponse) GetRateLimitHeaders() RateLimitHeaders

func (*EditsResponse) Header

func (h *EditsResponse) Header() http.Header

func (*EditsResponse) SetHeader

func (h *EditsResponse) SetHeader(header http.Header)

type Embedding

type Embedding struct {
	Object    string    `json:"object"`
	Embedding []float32 `json:"embedding"`
	Index     int       `json:"index"`
}

Embedding is a special format of data representation that can be easily utilized by machine learning models and algorithms. The embedding is an information dense representation of the semantic meaning of a piece of text. Each embedding is a vector of floating point numbers, such that the distance between two embeddings in the vector space is correlated with semantic similarity between two inputs in the original format. For example, if two texts are similar, then their vector representations should also be similar.

func (*Embedding) DotProduct

func (e *Embedding) DotProduct(other *Embedding) (float32, error)

DotProduct calculates the dot product of the embedding vector with another embedding vector. Both vectors must have the same length; otherwise, an ErrVectorLengthMismatch is returned. The method returns the calculated dot product as a float32 value.

type EmbeddingEncodingFormat

type EmbeddingEncodingFormat string

EmbeddingEncodingFormat is the format of the embeddings data. Currently, only "float" and "base64" are supported, however, "base64" is not officially documented. If not specified OpenAI will use "float".

const (
	EmbeddingEncodingFormatFloat  EmbeddingEncodingFormat = "float"
	EmbeddingEncodingFormatBase64 EmbeddingEncodingFormat = "base64"
)

type EmbeddingModel

type EmbeddingModel int

EmbeddingModel enumerates the models which can be used to generate Embedding vectors.

const (
	Unknown EmbeddingModel = iota
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	AdaSimilarity
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	BabbageSimilarity
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	CurieSimilarity
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	DavinciSimilarity
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	AdaSearchDocument
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	AdaSearchQuery
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	BabbageSearchDocument
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	BabbageSearchQuery
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	CurieSearchDocument
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	CurieSearchQuery
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	DavinciSearchDocument
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	DavinciSearchQuery
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	AdaCodeSearchCode
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	AdaCodeSearchText
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	BabbageCodeSearchCode
	// Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
	BabbageCodeSearchText
	AdaEmbeddingV2
)

func (EmbeddingModel) MarshalText

func (e EmbeddingModel) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (EmbeddingModel) String

func (e EmbeddingModel) String() string

String implements the fmt.Stringer interface.

func (*EmbeddingModel) UnmarshalText

func (e *EmbeddingModel) UnmarshalText(b []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. On unrecognized value, it sets |e| to Unknown.

type EmbeddingRequest

type EmbeddingRequest struct {
	Input          any                     `json:"input"`
	Model          EmbeddingModel          `json:"model"`
	User           string                  `json:"user"`
	EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"`
}

func (EmbeddingRequest) Convert

func (r EmbeddingRequest) Convert() EmbeddingRequest

type EmbeddingRequestConverter

type EmbeddingRequestConverter interface {
	// Needs to be of type EmbeddingRequestStrings or EmbeddingRequestTokens
	Convert() EmbeddingRequest
}

type EmbeddingRequestStrings

type EmbeddingRequestStrings struct {
	// Input is a slice of strings for which you want to generate an Embedding vector.
	// Each input must not exceed 8192 tokens in length.
	// OpenAPI suggests replacing newlines (\n) in your input with a single space, as they
	// have observed inferior results when newlines are present.
	// E.g.
	//	"The food was delicious and the waiter..."
	Input []string `json:"input"`
	// ID of the model to use. You can use the List models API to see all of your available models,
	// or see our Model overview for descriptions of them.
	Model EmbeddingModel `json:"model"`
	// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
	User string `json:"user"`
	// EmbeddingEncodingFormat is the format of the embeddings data.
	// Currently, only "float" and "base64" are supported, however, "base64" is not officially documented.
	// If not specified OpenAI will use "float".
	EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"`
}

EmbeddingRequestStrings is the input to a create embeddings request with a slice of strings.

func (EmbeddingRequestStrings) Convert

type EmbeddingRequestTokens

type EmbeddingRequestTokens struct {
	// Input is a slice of slices of ints ([][]int) for which you want to generate an Embedding vector.
	// Each input must not exceed 8192 tokens in length.
	// OpenAPI suggests replacing newlines (\n) in your input with a single space, as they
	// have observed inferior results when newlines are present.
	// E.g.
	//	"The food was delicious and the waiter..."
	Input [][]int `json:"input"`
	// ID of the model to use. You can use the List models API to see all of your available models,
	// or see our Model overview for descriptions of them.
	Model EmbeddingModel `json:"model"`
	// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
	User string `json:"user"`
	// EmbeddingEncodingFormat is the format of the embeddings data.
	// Currently, only "float" and "base64" are supported, however, "base64" is not officially documented.
	// If not specified OpenAI will use "float".
	EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"`
}

func (EmbeddingRequestTokens) Convert

type EmbeddingResponse

type EmbeddingResponse struct {
	Object string         `json:"object"`
	Data   []Embedding    `json:"data"`
	Model  EmbeddingModel `json:"model"`
	Usage  Usage          `json:"usage"`
	// contains filtered or unexported fields
}

EmbeddingResponse is the response from a Create embeddings request.

func (*EmbeddingResponse) GetRateLimitHeaders

func (h *EmbeddingResponse) GetRateLimitHeaders() RateLimitHeaders

func (*EmbeddingResponse) Header

func (h *EmbeddingResponse) Header() http.Header

func (*EmbeddingResponse) SetHeader

func (h *EmbeddingResponse) SetHeader(header http.Header)

type EmbeddingResponseBase64

type EmbeddingResponseBase64 struct {
	Object string            `json:"object"`
	Data   []Base64Embedding `json:"data"`
	Model  EmbeddingModel    `json:"model"`
	Usage  Usage             `json:"usage"`
	// contains filtered or unexported fields
}

EmbeddingResponseBase64 is the response from a Create embeddings request with base64 encoding format.

func (*EmbeddingResponseBase64) GetRateLimitHeaders

func (h *EmbeddingResponseBase64) GetRateLimitHeaders() RateLimitHeaders

func (*EmbeddingResponseBase64) Header

func (h *EmbeddingResponseBase64) Header() http.Header

func (*EmbeddingResponseBase64) SetHeader

func (h *EmbeddingResponseBase64) SetHeader(header http.Header)

func (*EmbeddingResponseBase64) ToEmbeddingResponse

func (r *EmbeddingResponseBase64) ToEmbeddingResponse() (EmbeddingResponse, error)

ToEmbeddingResponse converts an embeddingResponseBase64 to an EmbeddingResponse.

type Engine

type Engine struct {
	ID     string `json:"id"`
	Object string `json:"object"`
	Owner  string `json:"owner"`
	Ready  bool   `json:"ready"`
	// contains filtered or unexported fields
}

Engine struct represents engine from OpenAPI API.

func (*Engine) GetRateLimitHeaders

func (h *Engine) GetRateLimitHeaders() RateLimitHeaders

func (*Engine) Header

func (h *Engine) Header() http.Header

func (*Engine) SetHeader

func (h *Engine) SetHeader(header http.Header)

type EnginesList

type EnginesList struct {
	Engines []Engine `json:"data"`
	// contains filtered or unexported fields
}

EnginesList is a list of engines.

func (*EnginesList) GetRateLimitHeaders

func (h *EnginesList) GetRateLimitHeaders() RateLimitHeaders

func (*EnginesList) Header

func (h *EnginesList) Header() http.Header

func (*EnginesList) SetHeader

func (h *EnginesList) SetHeader(header http.Header)

type ErrorResponse

type ErrorResponse struct {
	Error *APIError `json:"error,omitempty"`
}

type File

type File struct {
	Bytes         int    `json:"bytes"`
	CreatedAt     int64  `json:"created_at"`
	ID            string `json:"id"`
	FileName      string `json:"filename"`
	Object        string `json:"object"`
	Status        string `json:"status"`
	Purpose       string `json:"purpose"`
	StatusDetails string `json:"status_details"`
	// contains filtered or unexported fields
}

File struct represents an OpenAPI file.

func (*File) GetRateLimitHeaders

func (h *File) GetRateLimitHeaders() RateLimitHeaders

func (*File) Header

func (h *File) Header() http.Header

func (*File) SetHeader

func (h *File) SetHeader(header http.Header)

type FileRequest

type FileRequest struct {
	FileName string `json:"file"`
	FilePath string `json:"-"`
	Purpose  string `json:"purpose"`
}

type FilesList

type FilesList struct {
	Files []File `json:"data"`
	// contains filtered or unexported fields
}

FilesList is a list of files that belong to the user or organization.

func (*FilesList) GetRateLimitHeaders

func (h *FilesList) GetRateLimitHeaders() RateLimitHeaders

func (*FilesList) Header

func (h *FilesList) Header() http.Header

func (*FilesList) SetHeader

func (h *FilesList) SetHeader(header http.Header)

type FineTune deprecated

type FineTune struct {
	ID                string              `json:"id"`
	Object            string              `json:"object"`
	Model             string              `json:"model"`
	CreatedAt         int64               `json:"created_at"`
	FineTuneEventList []FineTuneEvent     `json:"events,omitempty"`
	FineTunedModel    string              `json:"fine_tuned_model"`
	HyperParams       FineTuneHyperParams `json:"hyperparams"`
	OrganizationID    string              `json:"organization_id"`
	ResultFiles       []File              `json:"result_files"`
	Status            string              `json:"status"`
	ValidationFiles   []File              `json:"validation_files"`
	TrainingFiles     []File              `json:"training_files"`
	UpdatedAt         int64               `json:"updated_at"`
	// contains filtered or unexported fields
}

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*FineTune) GetRateLimitHeaders

func (h *FineTune) GetRateLimitHeaders() RateLimitHeaders

func (*FineTune) Header

func (h *FineTune) Header() http.Header

func (*FineTune) SetHeader

func (h *FineTune) SetHeader(header http.Header)

type FineTuneDeleteResponse deprecated

type FineTuneDeleteResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Deleted bool   `json:"deleted"`
	// contains filtered or unexported fields
}

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*FineTuneDeleteResponse) GetRateLimitHeaders

func (h *FineTuneDeleteResponse) GetRateLimitHeaders() RateLimitHeaders

func (*FineTuneDeleteResponse) Header

func (h *FineTuneDeleteResponse) Header() http.Header

func (*FineTuneDeleteResponse) SetHeader

func (h *FineTuneDeleteResponse) SetHeader(header http.Header)

type FineTuneEvent deprecated

type FineTuneEvent struct {
	Object    string `json:"object"`
	CreatedAt int64  `json:"created_at"`
	Level     string `json:"level"`
	Message   string `json:"message"`
}

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

type FineTuneEventList deprecated

type FineTuneEventList struct {
	Object string          `json:"object"`
	Data   []FineTuneEvent `json:"data"`
	// contains filtered or unexported fields
}

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*FineTuneEventList) GetRateLimitHeaders

func (h *FineTuneEventList) GetRateLimitHeaders() RateLimitHeaders

func (*FineTuneEventList) Header

func (h *FineTuneEventList) Header() http.Header

func (*FineTuneEventList) SetHeader

func (h *FineTuneEventList) SetHeader(header http.Header)

type FineTuneHyperParams deprecated

type FineTuneHyperParams struct {
	BatchSize              int     `json:"batch_size"`
	LearningRateMultiplier float64 `json:"learning_rate_multiplier"`
	Epochs                 int     `json:"n_epochs"`
	PromptLossWeight       float64 `json:"prompt_loss_weight"`
}

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

type FineTuneList deprecated

type FineTuneList struct {
	Object string     `json:"object"`
	Data   []FineTune `json:"data"`
	// contains filtered or unexported fields
}

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

func (*FineTuneList) GetRateLimitHeaders

func (h *FineTuneList) GetRateLimitHeaders() RateLimitHeaders

func (*FineTuneList) Header

func (h *FineTuneList) Header() http.Header

func (*FineTuneList) SetHeader

func (h *FineTuneList) SetHeader(header http.Header)

type FineTuneModelDeleteResponse

type FineTuneModelDeleteResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Deleted bool   `json:"deleted"`
	// contains filtered or unexported fields
}

FineTuneModelDeleteResponse represents the deletion status of a fine-tuned model.

func (*FineTuneModelDeleteResponse) GetRateLimitHeaders

func (h *FineTuneModelDeleteResponse) GetRateLimitHeaders() RateLimitHeaders

func (*FineTuneModelDeleteResponse) Header

func (h *FineTuneModelDeleteResponse) Header() http.Header

func (*FineTuneModelDeleteResponse) SetHeader

func (h *FineTuneModelDeleteResponse) SetHeader(header http.Header)

type FineTuneRequest deprecated

type FineTuneRequest struct {
	TrainingFile                 string    `json:"training_file"`
	ValidationFile               string    `json:"validation_file,omitempty"`
	Model                        string    `json:"model,omitempty"`
	Epochs                       int       `json:"n_epochs,omitempty"`
	BatchSize                    int       `json:"batch_size,omitempty"`
	LearningRateMultiplier       float32   `json:"learning_rate_multiplier,omitempty"`
	PromptLossRate               float32   `json:"prompt_loss_rate,omitempty"`
	ComputeClassificationMetrics bool      `json:"compute_classification_metrics,omitempty"`
	ClassificationClasses        int       `json:"classification_n_classes,omitempty"`
	ClassificationPositiveClass  string    `json:"classification_positive_class,omitempty"`
	ClassificationBetas          []float32 `json:"classification_betas,omitempty"`
	Suffix                       string    `json:"suffix,omitempty"`
}

Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. This API will be officially deprecated on January 4th, 2024. OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.

type FineTuningJob

type FineTuningJob struct {
	ID              string          `json:"id"`
	Object          string          `json:"object"`
	CreatedAt       int64           `json:"created_at"`
	FinishedAt      int64           `json:"finished_at"`
	Model           string          `json:"model"`
	FineTunedModel  string          `json:"fine_tuned_model,omitempty"`
	OrganizationID  string          `json:"organization_id"`
	Status          string          `json:"status"`
	Hyperparameters Hyperparameters `json:"hyperparameters"`
	TrainingFile    string          `json:"training_file"`
	ValidationFile  string          `json:"validation_file,omitempty"`
	ResultFiles     []string        `json:"result_files"`
	TrainedTokens   int             `json:"trained_tokens"`
	// contains filtered or unexported fields
}

func (*FineTuningJob) GetRateLimitHeaders

func (h *FineTuningJob) GetRateLimitHeaders() RateLimitHeaders

func (*FineTuningJob) Header

func (h *FineTuningJob) Header() http.Header

func (*FineTuningJob) SetHeader

func (h *FineTuningJob) SetHeader(header http.Header)

type FineTuningJobEvent

type FineTuningJobEvent struct {
	Object    string `json:"object"`
	ID        string `json:"id"`
	CreatedAt int    `json:"created_at"`
	Level     string `json:"level"`
	Message   string `json:"message"`
	Data      any    `json:"data"`
	Type      string `json:"type"`
}

type FineTuningJobEventList

type FineTuningJobEventList struct {
	Object  string          `json:"object"`
	Data    []FineTuneEvent `json:"data"`
	HasMore bool            `json:"has_more"`
	// contains filtered or unexported fields
}

func (*FineTuningJobEventList) GetRateLimitHeaders

func (h *FineTuningJobEventList) GetRateLimitHeaders() RateLimitHeaders

func (*FineTuningJobEventList) Header

func (h *FineTuningJobEventList) Header() http.Header

func (*FineTuningJobEventList) SetHeader

func (h *FineTuningJobEventList) SetHeader(header http.Header)

type FineTuningJobRequest

type FineTuningJobRequest struct {
	TrainingFile    string           `json:"training_file"`
	ValidationFile  string           `json:"validation_file,omitempty"`
	Model           string           `json:"model,omitempty"`
	Hyperparameters *Hyperparameters `json:"hyperparameters,omitempty"`
	Suffix          string           `json:"suffix,omitempty"`
}

type FinishReason

type FinishReason string
const (
	FinishReasonStop          FinishReason = "stop"
	FinishReasonLength        FinishReason = "length"
	FinishReasonFunctionCall  FinishReason = "function_call"
	FinishReasonToolCalls     FinishReason = "tool_calls"
	FinishReasonContentFilter FinishReason = "content_filter"
	FinishReasonNull          FinishReason = "null"
)

func (FinishReason) MarshalJSON

func (r FinishReason) MarshalJSON() ([]byte, error)

type FunctionCall

type FunctionCall struct {
	Name string `json:"name,omitempty"`
	// call function with arguments in JSON format
	Arguments string `json:"arguments,omitempty"`
}

type FunctionDefine deprecated

type FunctionDefine = FunctionDefinition

Deprecated: use FunctionDefinition instead.

type FunctionDefinition

type FunctionDefinition struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	// Parameters is an object describing the function.
	// You can pass json.RawMessage to describe the schema,
	// or you can pass in a struct which serializes to the proper JSON schema.
	// The jsonschema package is provided for convenience, but you should
	// consider another specialized library if you require more complex schemas.
	Parameters any `json:"parameters"`
}

type Hate

type Hate struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

type Hyperparameters

type Hyperparameters struct {
	Epochs any `json:"n_epochs,omitempty"`
}

type ImageEditRequest

type ImageEditRequest struct {
	Image          *os.File `json:"image,omitempty"`
	Mask           *os.File `json:"mask,omitempty"`
	Prompt         string   `json:"prompt,omitempty"`
	N              int      `json:"n,omitempty"`
	Size           string   `json:"size,omitempty"`
	ResponseFormat string   `json:"response_format,omitempty"`
}

ImageEditRequest represents the request structure for the image API.

type ImageRequest

type ImageRequest struct {
	Prompt         string `json:"prompt,omitempty"`
	Model          string `json:"model,omitempty"`
	N              int    `json:"n,omitempty"`
	Quality        string `json:"quality,omitempty"`
	Size           string `json:"size,omitempty"`
	Style          string `json:"style,omitempty"`
	ResponseFormat string `json:"response_format,omitempty"`
	User           string `json:"user,omitempty"`
}

ImageRequest represents the request structure for the image API.

type ImageResponse

type ImageResponse struct {
	Created int64                    `json:"created,omitempty"`
	Data    []ImageResponseDataInner `json:"data,omitempty"`
	// contains filtered or unexported fields
}

ImageResponse represents a response structure for image API.

func (*ImageResponse) GetRateLimitHeaders

func (h *ImageResponse) GetRateLimitHeaders() RateLimitHeaders

func (*ImageResponse) Header

func (h *ImageResponse) Header() http.Header

func (*ImageResponse) SetHeader

func (h *ImageResponse) SetHeader(header http.Header)

type ImageResponseDataInner

type ImageResponseDataInner struct {
	URL           string `json:"url,omitempty"`
	B64JSON       string `json:"b64_json,omitempty"`
	RevisedPrompt string `json:"revised_prompt,omitempty"`
}

ImageResponseDataInner represents a response data structure for image API.

type ImageURLDetail

type ImageURLDetail string
const (
	ImageURLDetailLow  ImageURLDetail = "low"
	ImageURLDetailHigh ImageURLDetail = "high"
)

type ImageVariRequest

type ImageVariRequest struct {
	Image          *os.File `json:"image,omitempty"`
	N              int      `json:"n,omitempty"`
	Size           string   `json:"size,omitempty"`
	ResponseFormat string   `json:"response_format,omitempty"`
}

ImageVariRequest represents the request structure for the image API.

type InnerError

type InnerError struct {
	Code                 string               `json:"code,omitempty"`
	ContentFilterResults ContentFilterResults `json:"content_filter_result,omitempty"`
}

InnerError Azure Content filtering. Only valid for Azure OpenAI Service.

type ListFineTuningJobEventsParameter

type ListFineTuningJobEventsParameter func(*listFineTuningJobEventsParameters)

func ListFineTuningJobEventsWithAfter

func ListFineTuningJobEventsWithAfter(after string) ListFineTuningJobEventsParameter

func ListFineTuningJobEventsWithLimit

func ListFineTuningJobEventsWithLimit(limit int) ListFineTuningJobEventsParameter

type LogprobResult

type LogprobResult struct {
	Tokens        []string             `json:"tokens"`
	TokenLogprobs []float32            `json:"token_logprobs"`
	TopLogprobs   []map[string]float32 `json:"top_logprobs"`
	TextOffset    []int                `json:"text_offset"`
}

LogprobResult represents logprob result of Choice.

type Model

type Model struct {
	CreatedAt  int64        `json:"created"`
	ID         string       `json:"id"`
	Object     string       `json:"object"`
	OwnedBy    string       `json:"owned_by"`
	Permission []Permission `json:"permission"`
	Root       string       `json:"root"`
	Parent     string       `json:"parent"`
	// contains filtered or unexported fields
}

Model struct represents an OpenAPI model.

func (*Model) GetRateLimitHeaders

func (h *Model) GetRateLimitHeaders() RateLimitHeaders

func (*Model) Header

func (h *Model) Header() http.Header

func (*Model) SetHeader

func (h *Model) SetHeader(header http.Header)

type ModelsList

type ModelsList struct {
	Models []Model `json:"data"`
	// contains filtered or unexported fields
}

ModelsList is a list of models, including those that belong to the user or organization.

func (*ModelsList) GetRateLimitHeaders

func (h *ModelsList) GetRateLimitHeaders() RateLimitHeaders

func (*ModelsList) Header

func (h *ModelsList) Header() http.Header

func (*ModelsList) SetHeader

func (h *ModelsList) SetHeader(header http.Header)

type ModerationRequest

type ModerationRequest struct {
	Input string `json:"input,omitempty"`
	Model string `json:"model,omitempty"`
}

ModerationRequest represents a request structure for moderation API.

type ModerationResponse

type ModerationResponse struct {
	ID      string   `json:"id"`
	Model   string   `json:"model"`
	Results []Result `json:"results"`
	// contains filtered or unexported fields
}

ModerationResponse represents a response structure for moderation API.

func (*ModerationResponse) GetRateLimitHeaders

func (h *ModerationResponse) GetRateLimitHeaders() RateLimitHeaders

func (*ModerationResponse) Header

func (h *ModerationResponse) Header() http.Header

func (*ModerationResponse) SetHeader

func (h *ModerationResponse) SetHeader(header http.Header)

type Permission

type Permission struct {
	CreatedAt          int64       `json:"created"`
	ID                 string      `json:"id"`
	Object             string      `json:"object"`
	AllowCreateEngine  bool        `json:"allow_create_engine"`
	AllowSampling      bool        `json:"allow_sampling"`
	AllowLogprobs      bool        `json:"allow_logprobs"`
	AllowSearchIndices bool        `json:"allow_search_indices"`
	AllowView          bool        `json:"allow_view"`
	AllowFineTuning    bool        `json:"allow_fine_tuning"`
	Organization       string      `json:"organization"`
	Group              interface{} `json:"group"`
	IsBlocking         bool        `json:"is_blocking"`
}

Permission struct represents an OpenAPI permission.

type PromptAnnotation

type PromptAnnotation struct {
	PromptIndex          int                  `json:"prompt_index,omitempty"`
	ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"`
}

type RateLimitHeaders

type RateLimitHeaders struct {
	LimitRequests     int       `json:"x-ratelimit-limit-requests"`
	LimitTokens       int       `json:"x-ratelimit-limit-tokens"`
	RemainingRequests int       `json:"x-ratelimit-remaining-requests"`
	RemainingTokens   int       `json:"x-ratelimit-remaining-tokens"`
	ResetRequests     ResetTime `json:"x-ratelimit-reset-requests"`
	ResetTokens       ResetTime `json:"x-ratelimit-reset-tokens"`
}

RateLimitHeaders struct represents Openai rate limits headers.

type RequestError

type RequestError struct {
	HTTPStatusCode int
	Err            error
}

RequestError provides informations about generic request errors.

func (*RequestError) Error

func (e *RequestError) Error() string

func (*RequestError) Unwrap

func (e *RequestError) Unwrap() error

type ResetTime

type ResetTime string

func (ResetTime) String

func (r ResetTime) String() string

func (ResetTime) Time

func (r ResetTime) Time() time.Time

type Response

type Response interface {
	SetHeader(http.Header)
}

type Result

type Result struct {
	Categories     ResultCategories     `json:"categories"`
	CategoryScores ResultCategoryScores `json:"category_scores"`
	Flagged        bool                 `json:"flagged"`
}

Result represents one of possible moderation results.

type ResultCategories

type ResultCategories struct {
	Hate            bool `json:"hate"`
	HateThreatening bool `json:"hate/threatening"`
	SelfHarm        bool `json:"self-harm"`
	Sexual          bool `json:"sexual"`
	SexualMinors    bool `json:"sexual/minors"`
	Violence        bool `json:"violence"`
	ViolenceGraphic bool `json:"violence/graphic"`
}

ResultCategories represents Categories of Result.

type ResultCategoryScores

type ResultCategoryScores struct {
	Hate            float32 `json:"hate"`
	HateThreatening float32 `json:"hate/threatening"`
	SelfHarm        float32 `json:"self-harm"`
	Sexual          float32 `json:"sexual"`
	SexualMinors    float32 `json:"sexual/minors"`
	Violence        float32 `json:"violence"`
	ViolenceGraphic float32 `json:"violence/graphic"`
}

ResultCategoryScores represents CategoryScores of Result.

type SelfHarm

type SelfHarm struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

type Sexual

type Sexual struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

type Tool

type Tool struct {
	Type     ToolType           `json:"type"`
	Function FunctionDefinition `json:"function,omitempty"`
}

type ToolCall

type ToolCall struct {
	ID       string       `json:"id"`
	Function FunctionCall `json:"function"`
}

type ToolChoiche

type ToolChoiche struct {
	Type     ToolType     `json:"type"`
	Function ToolFunction `json:"function,omitempty"`
}

type ToolFunction

type ToolFunction struct {
	Name string `json:"name"`
}

type ToolType

type ToolType string
const (
	ToolTypeFunction ToolType = "function"
)

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage Represents the total token usage per request to OpenAI.

type Violence

type Violence struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

Directories

Path Synopsis
examples
Package jsonschema provides very simple functionality for representing a JSON schema as a (nested) struct.
Package jsonschema provides very simple functionality for representing a JSON schema as a (nested) struct.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL