Documentation
¶
Overview ¶
Package openai provides a Go SDK for the OpenAI API.this package supports several models, including GPT-4, GPT-3.5, GPT-3, DALL-E, and audio models. You can specify the desired model using the `Model` field in the request object.
Usage ¶
To use this SDK, you will first need to obtain an API key from the OpenAI website. You can then create a client object using the `New` function:
client := client.New(apiKey)
The client object provides methods for making requests to the various endpoints of the OpenAI API. For example, to generate text using the GPT-3.5 or GPT-4 model, you can use the `CreateChatCompletion` method:
c := openai.NewChat(cli) resp, err := c.CreateChatCompletion(context.Background(), entity.ChatRequest{ Model: models.GPT35_TURBO, Messages: []entity.ChatMessage{ { Role: entity.USER, Content: "Hello", }, }, })
In addition to generating text and images, this package also supports fine-tuning models and generating embeddings. For example, to fine-tune a GPT-3 model, you can use the `CreateFineTune` method:
c := openai.NewFineTune(cli) resp, err := c.CreateFineTune(context.Background(), entity.FineTuneRequest{})
For more information about the available methods and request/response objects, see the documentation for the `Client` type and the various endpoint types.
Authentication ¶
Requests to the OpenAI API must include an API key in the `Authorization` header. You can pass this key to the client constructor, or you can set the `OPENAI_API_KEY` environment variable to automatically use it:
os.Setenv("OPENAI_API_KEY", apiKey) client := client.New(os.GetEnv("OPENAI_API_KEY"))
Concurrency ¶
The client methods are safe to use concurrently from multiple goroutines.
Errors ¶
Any errors returned by the client methods will be of type `openai.Error`. This type provides access to the raw HTTP response, as well as any JSON error response that was returned by the API. For more information, see the documentation for the `Error` type.
Endpoint Types ¶
The package defines types for each of the endpoints in the OpenAI API. These types provide a convenient way to construct requests and parse responses for each endpoint. For more information, see the documentation for each endpoint type.
Examples ¶
The `_examples` directory in the package source contains several examples of how to use the SDK to perform various tasks with the OpenAI API. These examples can serve as a starting point for your own usage of the SDK.
Index ¶
- type Audio
- type ChatCompletion
- type Completion
- type Embedding
- type File
- type FineTune
- func (f *FineTune) CancelFineTune(ctx context.Context, fineTuneID types.ID) (*entity.FineTuneResponse, error)
- func (f *FineTune) CreateFineTune(ctx context.Context, req entity.FineTuneRequest) (*entity.FineTuneResponse, error)
- func (f *FineTune) DeleteFineTuneModel(ctx context.Context, model string) (*entity.FineTuneDeleteResponse, error)
- func (f *FineTune) ListFineTuneEvent(ctx context.Context, fineTuneID types.ID) (*entity.FineTuneEventList, error)
- func (f *FineTune) ListFineTunes(ctx context.Context) (*entity.FineTuneList, error)
- func (f *FineTune) RetrieveFineTune(ctx context.Context, fineTuneID types.ID) (*entity.FineTuneResponse, error)
- type Image
- func (i *Image) CreateImage(ctx context.Context, req entity.ImageRequest) (*entity.ImageResponse, error)
- func (i *Image) CreateImageVariation(ctx context.Context, req entity.ImageVariationRequest) (*entity.ImageResponse, error)
- func (i *Image) ImageEdit(ctx context.Context, req entity.ImageEditRequest) (*entity.ImageResponse, error)
- type Moderation
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Audio ¶
type Audio struct {
// contains filtered or unexported fields
}
func NewAudio ¶
func NewAudio(client client.Transporter) *Audio
NewAudio create audio object to transcription and translation
Example ¶
apiKey := os.Getenv("OPENAI_API_KEY") cli, err := client.New([]string{apiKey}) if err != nil { log.Fatalln(err) } f, err := os.Open("./testdata/file.mp3") if err != nil { log.Fatalln(err) } c := NewAudio(cli) resp, err := c.CreateTranscription(context.Background(), entity.AudioRequest{ Model: models.WHISPER_1, File: f, Language: "fa", }) if err != nil { log.Fatalln(err) } log.Println(resp)
Output:
func (*Audio) CreateTranscription ¶
func (a *Audio) CreateTranscription(ctx context.Context, req entity.AudioRequest) (*entity.AudioResponse, error)
CreateTranscription Transcribes audio into the input language
func (*Audio) CreateTranslation ¶
func (a *Audio) CreateTranslation(ctx context.Context, req entity.AudioRequest) (*entity.AudioResponse, error)
CreateTranslation Translates audio into English
type ChatCompletion ¶
type ChatCompletion struct {
// contains filtered or unexported fields
}
func NewChat ¶
func NewChat(client client.Transporter) *ChatCompletion
NewChat create chat completion object to create chat with chatgpt
Example ¶
apiKey := os.Getenv("OPENAI_API_KEY") cli, err := client.New([]string{apiKey}) if err != nil { log.Fatalln(err) } c := NewChat(cli) resp, err := c.CreateChatCompletion(context.Background(), entity.ChatRequest{ Model: models.GPT35_TURBO, Messages: []entity.ChatMessage{ { Role: entity.USER, Content: "Hello!!", }, }, }) if err != nil { log.Fatalln(err) } log.Println(resp)
Output:
func (*ChatCompletion) CreateChatCompletion ¶
func (c *ChatCompletion) CreateChatCompletion(ctx context.Context, req entity.ChatRequest) (*entity.ChatResponse, error)
CreateChatCompletion Creates a completion for the provided prompt and parameters
type Completion ¶
type Completion struct {
// contains filtered or unexported fields
}
func NewCompletion ¶
func NewCompletion(client client.Transporter) *Completion
NewCompletion create Completion object to text completion using davinci
Example ¶
apiKey := os.Getenv("OPENAI_API_KEY") cli, err := client.New([]string{apiKey}) if err != nil { log.Fatalln(err) } c := NewCompletion(cli) resp, err := c.CreateCompletion(context.Background(), entity.CompletionRequest{ Model: models.TEXT_DAVINCI_002, Prompt: "Golang history", }) if err != nil { log.Fatalln(err) } log.Println(resp)
Output:
func (*Completion) CreateCompletion ¶
func (c *Completion) CreateCompletion(ctx context.Context, req entity.CompletionRequest) (*entity.CompletionResponse, error)
CreateCompletion Creates a job that fine-tunes a specified model from a given dataset. Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
func (*Completion) CreateCompletionFromPattern ¶
func (c *Completion) CreateCompletionFromPattern(ctx context.Context, pattern completion.CompletionPattern) (*entity.CompletionResponse, error)
CreateCompletionFromPattern create a completion using specific patterns
type Embedding ¶
type Embedding struct {
// contains filtered or unexported fields
}
func NewEmbedding ¶
func NewEmbedding(client client.Transporter) *Embedding
NewEmbedding create embedding object to create embeddings
Example ¶
apiKey := os.Getenv("OPENAI_API_KEY") cli, err := client.New([]string{apiKey}) if err != nil { log.Fatalln(err) } c := NewEmbedding(cli) resp, err := c.CreateEmbedding(context.Background(), entity.EmbeddingRequest{ Model: models.TEXT_EMBEDDING_ADA_002, Input: []string{"example input"}, }) if err != nil { log.Fatalln(err) } log.Println(resp)
Output:
func (*Embedding) CreateEmbedding ¶
func (e *Embedding) CreateEmbedding(ctx context.Context, req entity.EmbeddingRequest) (*entity.EmbeddingResponse, error)
CreateEmbedding Creates an embedding vector representing the input text
type File ¶
type File struct {
// contains filtered or unexported fields
}
func NewFile ¶
func NewFile(client client.Transporter) *File
NewFile create File object to manage file (upload, retrieve, list)
Example ¶
apiKey := os.Getenv("OPENAI_API_KEY") cli, err := client.New([]string{apiKey}) if err != nil { log.Fatalln(err) } f, err := os.Open("./testdata/file.jsonl") if err != nil { log.Fatalln(err) } c := NewFile(cli) resp, err := c.UploadFile(context.Background(), entity.FileUploadRequest{ File: f, Purpose: "fine-tune", }) if err != nil { log.Fatalln(err) } log.Println(resp)
Output:
func (*File) RetrieveFile ¶
func (f *File) RetrieveFile(ctx context.Context, fileID types.ID, content bool) (*entity.FileResponse, error)
RetrieveFile Returns information about a specific file or file content
func (*File) UploadFile ¶
func (f *File) UploadFile(ctx context.Context, req entity.FileUploadRequest) (*entity.FileResponse, error)
UploadFile Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit
type FineTune ¶
type FineTune struct {
// contains filtered or unexported fields
}
func NewFineTune ¶
func NewFineTune(client client.Transporter) *FineTune
Example ¶
apiKey := os.Getenv("OPENAI_API_KEY") cli, err := client.New([]string{apiKey}) if err != nil { log.Fatalln(err) } c := NewFineTune(cli) resp, err := c.CreateFineTune(context.Background(), entity.FineTuneRequest{ Model: models.DAVINCI, TrainingFile: "file-xyz", }) if err != nil { log.Fatalln(err) } log.Println(resp)
Output:
func (*FineTune) CancelFineTune ¶
func (f *FineTune) CancelFineTune(ctx context.Context, fineTuneID types.ID) (*entity.FineTuneResponse, error)
CancelFineTune Immediately cancel a fine-tune job
func (*FineTune) CreateFineTune ¶
func (f *FineTune) CreateFineTune(ctx context.Context, req entity.FineTuneRequest) (*entity.FineTuneResponse, error)
CreateFineTune Creates a job that fine-tunes a specified model from a given dataset
func (*FineTune) DeleteFineTuneModel ¶
func (f *FineTune) DeleteFineTuneModel(ctx context.Context, model string) (*entity.FineTuneDeleteResponse, error)
DeleteFineTuneModel Delete a fine-tuned model. You must have the Owner role in your organization
func (*FineTune) ListFineTuneEvent ¶
func (f *FineTune) ListFineTuneEvent(ctx context.Context, fineTuneID types.ID) (*entity.FineTuneEventList, error)
ListFineTuneEvent Get fine-grained status updates for a fine-tune job
func (*FineTune) ListFineTunes ¶
ListFineTunes List your organization's fine-tuning jobs
func (*FineTune) RetrieveFineTune ¶
func (f *FineTune) RetrieveFineTune(ctx context.Context, fineTuneID types.ID) (*entity.FineTuneResponse, error)
RetrieveFineTune Gets info about the fine-tune job
type Image ¶
type Image struct {
// contains filtered or unexported fields
}
func NewImage ¶
func NewImage(client client.Transporter) *Image
NewImage create Image object to create, edit image or image variation using DALL·E 2
Example ¶
apiKey := os.Getenv("OPENAI_API_KEY") cli, err := client.New([]string{apiKey}) if err != nil { log.Fatalln(err) } c := NewImage(cli) resp, err := c.CreateImage(context.Background(), entity.ImageRequest{ Prompt: "Create a gopher baby", }) if err != nil { log.Fatalln(err) } log.Println(resp)
Output:
func (*Image) CreateImage ¶
func (i *Image) CreateImage(ctx context.Context, req entity.ImageRequest) (*entity.ImageResponse, error)
CreateImage Creates an image given a prompt
func (*Image) CreateImageVariation ¶
func (i *Image) CreateImageVariation(ctx context.Context, req entity.ImageVariationRequest) (*entity.ImageResponse, error)
CreateImageVariation Creates a variation of a given image
func (*Image) ImageEdit ¶
func (i *Image) ImageEdit(ctx context.Context, req entity.ImageEditRequest) (*entity.ImageResponse, error)
ImageEdit Creates an edited or extended image given an original image and a prompt
type Moderation ¶
type Moderation struct {
// contains filtered or unexported fields
}
func NewModeration ¶
func NewModeration(client client.Transporter) *Moderation
Example ¶
apiKey := os.Getenv("OPENAI_API_KEY") cli, err := client.New([]string{apiKey}) if err != nil { log.Fatalln(err) } c := NewModeration(cli) resp, err := c.CreateModeration(context.Background(), entity.ModerationRequest{ Input: "I want to kill them.", }) if err != nil { log.Fatalln(err) } log.Println(resp)
Output:
func (*Moderation) CreateModeration ¶
func (m *Moderation) CreateModeration(ctx context.Context, req entity.ModerationRequest) (*entity.ModerationResponse, error)
CreateModeration Classifies if text violates OpenAI's Content Policy