AzOpenAI is a Go SDK for interfacing with Azure OpenAI Service. It provides a simple and easy-to-use interface for requesting various Azure OpenAI Service operations, including Chat Completions, Completions and Embeddings.
Installation | Usage | License | Documentation | Azure OpenAI Service
To use AzOpenAI in your Go project, add the following import statement.
import "github.com/element-of-surprise/azopenai"
Then, run go get
(after initializing your module with go mod init), to download and install the package.
go get github.com/element-of-surprise/azopenai
Here is an example of how to use AzOpenAI to generate text using the OpenAI completions API endpoint.
See samples/main.go for full Completions, Chat and Embeddings samples.
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/element-of-surprise/azopenai"
"github.com/element-of-surprise/azopenai/auth"
)
func main() {
ctx := context.Background()
// apiKey is the a generated key located in the Azure Portal under "keys and endpoint".
// It is also possible to use azidentity instead of an API key.
apiKey := os.Getenv("API_KEY")
// resourceName is the name of your OpenAI cluster. This is unique in Azure.
// You can find this both in the upper left corner of the Portal or under the overview
// tab there will be an "Endpoint" label with something like "https://openai230322.openai.azure.com/" displayed. "openai230300" would be the resource name in this case.
resourceName := os.Getenv("RESOURCE_ID")
// deploymentID is the name of a model you deployed. This is YOUR NAME for the model, not the
// actual model name (as you could deploy 2 text-davinci-003 models with different names). This
// can be found under the "Model deployments" tab under the column "Model deployments name".
deploymentID := os.Getenv("DEPLOYMENT_ID")
client, err := azopenai.New(resourceName, auth.Authorizer{ApiKey: apiKey})
if err != nil {
log.Fatal(err)
}
completions := client.Completions(deploymentID)
resp, err := completions.Call(ctx, []string{"The capital of California is"})
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Text[0])
}
In this example, azopenai.New()
is used to create a new AzOpenAI client with your Azure OpenAI Service API Key. This SDK also supports azidentity for authentication.
Then the returned client aggregator client
has client.Completions()
called to get a client for the completions endpoint. You pass the deploymentID
here so that you can point at the right model for the sub-client. Deployments only have support for some API calls.
Next, completions.Call()
is invoked on the client with the prompt(s) and any additional options specified.
Finally, response returned by the Completions endpoint is printed to the console.
AzOpenAI Go SDK is licensed under the MIT License. See LICENSE for more information.
For more detailed information on the SDK, please see the which includes more authentication examples, using other API endpoints, details on options, and more.