# README
openai
Zero dependency (Unofficial) Go Client for OpenAI API endpoints. Built upon the great work done here.
Goals
Why did we bother to refactor the original library? We have 5 main goals:
- Use more idiomatic Go style.
- Better documentation.
- Make request parameters whose Go default value is a valid parameter value (and differs from OpenAI's defaults) pointers (See here for more).
- Have a consistent style throughout.
- Implement all endpoints.
We hope that by doing the above, future maintenance should also be trivial. Read more on the original refactoring PR here.
Installation
go get github.com/fabiustech/openai
Example Usage
package main
import (
"context"
"fmt"
"os"
"github.com/fabiustech/openai"
"github.com/fabiustech/openai/models"
"github.com/fabiustech/openai/params"
)
func main() {
var key, ok = os.LookupEnv("OPENAI_API_KEY")
if !ok {
panic("env variable OPENAI_API_KEY not set")
}
var c = openai.NewClient(key)
var resp, err = c.CreateCompletion(context.Background(), &openai.CompletionRequest[models.Completion]{
Model: models.TextDavinci003,
MaxTokens: 100,
Prompt: "Lorem ipsum",
Temperature: params.Optional(0.0),
})
if err != nil {
return
}
fmt.Println(resp.Choices[0].Text)
}
Contributing
Contributions are welcome and encouraged! Feel free to report any bugs / feature requests as issues.
# Packages
Package audio contains the enum values which represent the output formats returned by the OpenAI transcription endpoint.
Package images contains the enum values which represent the various image formats and sizes returned by the OpenAI image endpoints.
Package models contains the enum values which represent the various models used by all OpenAI endpoints.
Package objects contains the enum values which represent the various objects returned by all OpenAI endpoints.
Package params provides a helper function to simplify setting optional parameters in struct literals.
Package routes contains constants for all OpenAI endpoint routes.
# Functions
FunctionCallAuto returns a FunctionCall which will specify that the model should pick between an end-user or calling a function.
FunctionCallNone returns a FunctionCall which will specify that no function should be called.
NewClient creates new OpenAI API client.
NewClientWithOrg creates new OpenAI API client for specified Organization ID.
NewFineTuneFileRequest returns a |*FileRequest| with File opened from |path| and Purpose set to "fine-tuned".
# Constants
Assistant represents a message from the assistant.
RoleFunction represents a call to a function.
System represents a system message, which helps set the behavior of the assistant.
User represents a message from a user, which helps instruct the assistant.
# Variables
ErrBadPrefix is returned if we attempt to parse a Read from the response Body that doesn't begin with "data: ".
# Structs
AudioTranscriptionRequest is the request body for the audio/transcriptions endpoint.
ChatCompletionChoice represents one of possible chat completions.
ChatCompletionRequest contains all relevant fields for requests to the chat completions endpoint.
ChatCompletionResponse is the response from the chat completions endpoint.
ChatMessage represents a message in a chat completion.
Client is OpenAI API client.
CompletionChoice represents one of possible completions.
CompletionRequest contains all relevant fields for requests to the completions endpoint.
CompletionResponse is the response from the completions endpoint.
CreateImageRequest contains all relevant fields for requests to the images/generations endpoint.
EditImageRequest contains all relevant fields for requests to the images/edits endpoint.
EditsChoice represents one of possible edits.
EditsRequest contains all relevant fields for requests to the edits endpoint.
EditsResponse represents a response structure for Edits API.
Embedding is a special format of data representation that can be easily utilized by machine learning models and algorithms.
EmbeddingRequest contains all relevant fields for requests to the embeddings endpoint.
EmbeddingResponse is the response from a Create embeddings request.
Engine contains all relevant fields for requests to the engines endpoint.
Error represents an error response from the API.
Event represents an event related to a fine-tune request.
File represents an OpenAPI file.
FileRequest contains all relevant data for upload requests to the files endpoint.
FineTuneDeletionResponse is the response from the fine-tunes/delete endpoint.
FineTuneRequest contains all relevant fields for requests to the fine-tunes endpoints.
FineTuneResponse is the response from fine-tunes endpoints.
Function is a list of functions the model may generate JSON inputs for.
FunctionCall specifies that the model should explicitly call the |Name|d function.
FunctionCallResponse represents a response from a function call.
ImageData represents a response data structure for image API.
ImageResponse represents a response structure for image API.
List represents a generic form of list of objects returned from many get endpoints.
LogprobResult represents logprob result of Choice.
ModerationRequest contains all relevant fields for requests to the moderations endpoint.
ModerationResponse represents a response structure for moderation API.
Result represents one of possible moderation results.
ResultCategories represents Categories of Result.
ResultCategoryScores represents CategoryScores of Result.
Usage Represents the total token usage per request to OpenAI.
VariationImageRequest contains all relevant fields for requests to the images/variations endpoint.
# Type aliases
ChatRole is an enum of the various message roles.