This package provides Go Client for Azure OpenAI
Just type this script on your project.
$ go get github.com/anaregdesign/go-aoai
package main
import (
"context"
"encoding/json"
"fmt"
aoai "github.com/piroyoung/go-aoai"
"os"
)
func main() {
ctx := context.Background()
resourceName := "example-aoai-02"
deploymentName := "gpt-35-turbo-0301"
apiVersion := "2023-03-15-preview"
accessToken := os.Getenv("AZURE_OPENAI_API_KEY")
client := aoai.New(resourceName, deploymentName, apiVersion, accessToken)
request := aoai.CompletionRequest{
Prompts: []string{"I have a dream that one day on"},
MaxTokens: 100,
Stream: false,
}
response, err := client.Completion(ctx, request)
if err != nil {
fmt.Println(err)
return
}
if jsonString, err := json.MarshalIndent(response, "", "\t"); err == nil {
fmt.Println(string(jsonString))
}
}
Then we got belows.
{
"id": "cmpl-6zN8ZDK3V6fxT4jyKYGCPwyhroWKK",
"object": "text_completion",
"created": 1680084967,
"model": "gpt-35-turbo",
"choices": [
{
"text": " the red hills of Georgia, the sons of former slaves and the sons of former slave owners will be able to sit together at the table of brotherhood.\n\nI have a dream today.\n\nI have a dream that one day, even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice.\n\nI have a dream today.\n\nI have a dream that my four little children will one day",
"logprobs": {},
"finish_reason": "length"
}
]
}
func (c *AzureOpenAI) Completion(ctx context.Context, request CompletionRequest) (*CompletionResponse, error)
Simple text completion api. If you pass some incomplete text, it will guess the rest of the text.
request := CompletionRequest{
Prompts: []string{"I have a dream that one day on"},
MaxTokens: 100,
Stream: false,
}
response, err := client.Completion(ctx, request)
func (a *AzureOpenAI) CompletionStream(ctx context.Context, request CompletionRequest, consumer func(CompletionResponse) error) error
CompletionStream
is a streaming api of Completion
If a field CompletionRequest.Stream
is true
, it will return a stream of responses with response header Content-Type: text/event-stream'
.
We can process each chunk of response with consumer
function.
request := CompletionRequest{
Prompts: []string{"I have a dream that one day on"},
MaxTokens: 100,
Stream: true,
}
response, err := client.CompletionStream(ctx, request, func(chunk CompletionResponse) error {
fmt.Println(chunk)
return nil
})
func (c *AzureOpenAI) Embedding(ctx context.Context, request EmbeddingRequest) (*EmbeddingResponse, error)
Embedding
api returns a vector representation of the input text, and the vector is used to calculate the similarity between texts.
request := EmbeddingRequest{
Prompts: []string{"I have a dream that one day on"},
}
response, err := client.Embedding(ctx, request)
func (a *AzureOpenAI) ChatCompletion(ctx context.Context, request ChatRequest) (*ChatResponse, error)
ChatCompletion
api is a chatbot api. It can be used to generate a response to a given prompt.
request := ChatRequest{
Messages: []ChatMessage{
{
Role: "user",
Content: "What is Azure OpenAI?",
},
},
MaxTokens: 100,
}
response, err := client.ChatCompletion(ctx, request)
func (a *AzureOpenAI) ChatCompletionStream(ctx context.Context, request ChatRequest, consumer func(ChatResponse) error) error
ChatCompletionStream
is a streaming api of ChatCompletion
If a field ChatRequest.Stream
is true
, it will return a stream of responses with response header Content-Type: text/event-stream'
.
We can process each chunk of response with consumer
function as same as CompletionStream
.
request := ChatRequest{
Messages: []ChatMessage{
{
Role: "user",
Content: "What is Azure OpenAI?",
},
},
MaxTokens: 100,
Stream: true,
}
response, err := client.ChatCompletionStream(ctx, request, func(chunk ChatResponse) error {
fmt.Println(chunk)
return nil
})
This SDK requires some parameters to identify your project and deployment.
name | description |
---|---|
resourceName |
Name of Azure OpenAI resource |
deploymentName |
Name of deployment in your Azure OpenAI resource |
apiVersion |
Check here. |
accessToken |
Authentication key. We can use Azure Active Directory Authentication(TBD). |
Models of Request/Response body are defined in model.go
,
check here.
And you can also
see Swagger API Reference.