gopenai is an unofficial package that provides bindings for the OpenAI API. It exposes a collection of interfaces for interacting with models, completions, edits, images, embeddings, files, fine tunes, and moderations.
To use the package, you'll need to create a Config
struct with your OpenAI API key and organization ID. You can also set a custom request timeout.
cfg := Config{
APIKey: os.Getenv("OPENAI_API_KEY"),
OrganizationID: "YOUR_ORGANIZATION_ID",
RequestTimeout: time.Second * 30
}
You can create a new client that uses the config you created with New()
.
c := gopenai.New(cfg)
The client provides access to the different APIs.
modelsAPI := c.Models()
completionsAPI := c.Completions()
editsAPI := c.Edits()
imagesAPI := c.Images()
embeddingsAPI := c.Embeddings()
filesAPI := c.Files()
fineTunesAPI := c.FineTunes()
moderationsAPI := c.Moderations()
The Models API allows you to get a list of all models, get a model by ID, and delete a model by ID.
models, err := modelsAPI.GetAll()
model, err := modelsAPI.GetByID(id)
deletedModel, err := modelsAPI.DeleteByID(id)
The Completions API allows you to create a completion.
params := CompletionParams{
Model: "YOUR_MODEL",
Prompt: "YOUR_PROMPT",
Suffix: "YOUR_SUFFIX",
MaxTokens: 30,
Temperature: 1.0,
TopP: 0.9,
N: 10,
Logprobs: 1,
Echo: true,
Stop: ".",
PresencePenalty: 0.0,
FrequencyPenalty: 0.0,
BestOf: 3,
User: "YOUR_USER"
}
completion, err := completionsAPI.Create(params)
The Chat Completions API allows you to create a chat completion.
params := ChatCompletionParams{
Model: "gpt-3.5-turbo",
Messages: []ChatCompletionMessage{
{Role: ChatCompletionMessageRoleSystem, Content: "SYSTEM_INSTRUCTION"},
{Role: ChatCompletionMessageRoleUser, Content: "USER_MESSAGE"},
},
Temperature: 0.8,
TopP: 0.9,
N: 10,
Stop: ".",
PresencePenalty: 0.0,
FrequencyPenalty: 0.0,
LogitBias: map[string]interface{}{
"2435": -100,
"640": -100,
},
User: "YOUR_USER",
}
completion, err := chatCompletionsAPI.Create(params)
The Edits API provides methods for creating edits.
params := EditParams{
Model: "model_name",
Input: "input_text",
Instruction: "edit_instruction",
N: 10,
Temperature: 0.5,
TopP: 0.8,
}
edit, err := editsAPI.Create(params)
The Images API provides methods for creating, editing, and creating variations of images.
The Create
method creates a new image based on the given input.
params := gopenai.ImageGenerationParams{
Prompt: "This is a test prompt",
N: 10,
Size: gopenai.ImageSize1024x1024,
ResponseFormat: gopenai.ImageResponseFormatURL,
User: "username",
}
images, err := imagesAPI.Create(params)
The Edit
method edits an existing image based on the given input.
params := gopenai.ImageEditParams{
Image: "path/to/image.png",
Mask: "path/to/mask.png",
Prompt: "This is a test prompt",
N: 10,
Size: gopenai.ImageSize1024x1024,
ResponseFormat: gopenai.ImageResponseFormatURL,
User: "username",
}
images, err := imagesAPI.Edit(params)
The CreateVariations
method creates variations of an existing image based on the given input.
params := gopenai.ImageVariationParams{
Image: "path/to/image.png",
N: 10,
Size: gopenai.ImageSize1024x1024,
ResponseFormat: gopenai.ImageResponseFormatURL,
User: "username",
}
images, err := imagesAPI.CreateVariations(params)
The Embeddings API provides methods to create embeddings from the OpenAI models.
The Create
method takes an EmbeddingParams
struct as a parameter and returns an Embedding
struct.
params := gopenai.EmbeddingParams{
Model: "text-embedding-ada-002",
Input: "This is a string to be embedded",
User: "[email protected]",
}
embedding, err := embeddingsAPI.Create(params)
The Files API provides methods to manage files, such as creating, deleting and downloading files.
The GetAll
method returns a list of File
structs.
files, err := filesAPI.GetAll()
The GetByID
method takes an ID as a parameter and returns a File
struct.
file, err := filesAPI.GetByID("<file_id>")
The Create
method takes a FileParams
struct as a parameter and returns a File
struct.
params := gopenai.FileParams{
File: "<path_to_file>",
Purpose: "fine-tune",
}
file, err := filesAPI.Create(params)
The DeleteByID
method takes an ID as a parameter and returns a DeletedFile
struct.
deleted, err := filesAPI.DeleteByID("<file_id>")
The DownloadByID
method takes an ID and a destination io.Writer
as parameters and returns an error.
err := filesAPI.DownloadByID("<file_id>", os.Stdout)
The FineTunes API provides methods to manage fine tunes, such as creating and cancelling fine tunes.
The GetAll
method returns a list of FineTune
structs.
fineTunes, err := fineTunesAPI.GetAll()
The GetByID
method takes an ID as a parameter and returns a FineTune
struct.
fineTune, err := fineTunesAPI.GetByID("<fine_tune_id>")
The Create
method takes a FineTuneParams
struct as a parameter and returns a FineTune
struct.
params := gopenai.FineTuneParams{
TrainingFile: "<training_file_id>",
ValidationFile: "<validation_file_id>",
Model: "curie",
NEpochs: 3,
BatchSize: 16,
LearningRateMultiplier: 0.1,
PromptLossWeight: 0.5,
ComputeClassificationMetrics: true,
ClassificationNClasses: 2,
ClassificationPositiveClass: "positive",
ClassificationBetas: []float64{0.5, 0.5},
Suffix: "my_suffix",
}
fineTune, err := fineTunesAPI.Create(params)
The Cancel
method takes an ID as a parameter and returns a FineTune
struct.
fineTune, err := fineTunesAPI.Cancel("<fine_tune_id>")
The GetEvents
method takes a fine tune ID as a parameter and returns a list of FineTuneEvent
structs.
events, err := fineTunesAPI.GetEvents("<fine_tune_id>")
The Moderations API provides methods to manage moderations, such as creating moderations.
The Create
method takes a ModerationParams
struct as a parameter and returns a Moderation
struct.
params := gopenai.ModerationParams{
Input: "This is a string to be moderated",
Model: "text-moderation-stable",
}
moderation, err := moderationsAPI.Create(params)
- add more tests