The openai
package is a Go client library for interacting with OpenAI's GPT models. It provides a convenient way to make chat completion requests to different versions of the GPT model, including GPT-3.5 Turbo and GPT-4.
- Supports multiple GPT models (GPT-3.5 Turbo, GPT-4, Babbage, etc.)
- Allows for conversation-based interactions using
ChatSession
- Provides retry strategies for API requests
- Customizable HTTP client
- Environment variable support for API key
To install the package, run:
import "go.llib.dev/openai"
To set up local development:
export OPENAI_API_KEY="your-openai-api-key-goes-here"
. .envrc # or direnv allow
client := &openai.Client{
APIKey: "your-openai-api-key", // or leave it and OPENAI_API_KEY from the env will be used
}
session := openai.ChatSession{Model: openai.GPT3}.
WithSystemMessage("You are a helpful assistant.")
session, err := client.ChatSession(ctx, session)
if err != nil {
log.Fatal(err)
}
lastReply := newSession.LastAssistantContent()
The package defines custom errors like ErrContextLengthExceeded
for better error handling.
It also uses a generic http error retry strategy, in attempt to handle common networking issues.
Feel free to open issues or submit pull requests.