Go OpenAI
This library provides unofficial Go clients for OpenAI API. We support:
- ChatGPT 4o, o1
- GPT-3, GPT-4
- DALL·E 2, DALL·E 3
- 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: “Hello!”, }, }, }, ) if err != nil { fmt.Printf(“ChatCompletion error: %v\n”, err) return } fmt.Println(resp.Choices[0].Message.Content) }
Getting an OpenAI API Key:
- Visit the OpenAI website at https://platform.openai.com/account/api-keys.
- If you don’t have an account, click on “Sign Up” to create one. If you do, click “Log In”.
- Once logged in, navigate to your API key management page.
- Click on “Create new secret key”.
- Enter a name for your new key, then click “Create secret key”.
- 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 completionGPT-3 completionGPT-3 streaming completionAudio Speech-To-TextAudio CaptionsDALL-E 2 image generationConfiguring proxyChatGPT support contextAzure OpenAI ChatGPTEmbedding Semantic SimilarityAzure OpenAI EmbeddingsJSON Schema for function calling
``
``
Error handling
Fine Tune ModelStructured OutputsSee 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.
- 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.
- 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:
- As of November 2023, use the new
seedparameter in conjunction with thesystem_fingerprintresponse field, alongside Temperature management. - Try using
math.SmallestNonzeroFloat32: By specifyingmath.SmallestNonzeroFloat32in the temperature field instead of 0, you can mimic the behavior of setting it to 0. - 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!

!