|
| 1 | +--- |
| 2 | +title: 'Quickstart: Use Azure OpenAI Service with the JavaScript SDK and the completions API' |
| 3 | +titleSuffix: Azure OpenAI |
| 4 | +description: Walkthrough on how to get started with Azure OpenAI and make your first completions call with the Go SDK. |
| 5 | +services: cognitive-services |
| 6 | +manager: nitinme |
| 7 | +ms.service: cognitive-services |
| 8 | +ms.subservice: openai |
| 9 | +ms.topic: include |
| 10 | +author: mrbullwinkle |
| 11 | +ms.author: mbullwin |
| 12 | +ms.date: 08/30/2023 |
| 13 | +keywords: |
| 14 | +--- |
| 15 | + |
| 16 | +[Source code](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/ai/azopenai) | [Package (Go)](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai)| [Samples](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai#pkg-examples) |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true) |
| 21 | +- Access granted to the Azure OpenAI service in the desired Azure subscription. |
| 22 | + Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at [https://aka.ms/oai/access](https://aka.ms/oai/access?azure-portal=true). |
| 23 | +- [Go 1.21.0](https://go.dev/dl/) or higher installed locally. |
| 24 | +- An Azure OpenAI Service resource with the `gpt-35-turbo` model deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md). |
| 25 | + |
| 26 | +## Set up |
| 27 | + |
| 28 | +[!INCLUDE [get-key-endpoint](get-key-endpoint.md)] |
| 29 | + |
| 30 | +[!INCLUDE [environment-variables](environment-variables.md)] |
| 31 | + |
| 32 | +## Create a sample application |
| 33 | + |
| 34 | + Create a new file named chat_completions.go. Copy the following code into the chat_completions.go file. |
| 35 | + |
| 36 | +```go |
| 37 | +package main |
| 38 | + |
| 39 | +import ( |
| 40 | + "context" |
| 41 | + "fmt" |
| 42 | + "log" |
| 43 | + "os" |
| 44 | + |
| 45 | + "github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai" |
| 46 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" |
| 47 | +) |
| 48 | + |
| 49 | +func main() { |
| 50 | + azureOpenAIKey := os.Getenv("AZURE_OPENAI_KEY") |
| 51 | + //modelDeploymentID = deployment name, if model name and deployment name do not match change this value to name chosen when you deployed the model. |
| 52 | + modelDeploymentID := "gpt-35-turbo" |
| 53 | + |
| 54 | + // Ex: "https://<your-azure-openai-host>.openai.azure.com" |
| 55 | + azureOpenAIEndpoint := os.Getenv("AZURE_OPENAI_ENDPOINT") |
| 56 | + |
| 57 | + if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" { |
| 58 | + fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n") |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey) |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + // TODO: Update the following line with your application specific error handling logic |
| 66 | + log.Fatalf("ERROR: %s", err) |
| 67 | + } |
| 68 | + |
| 69 | + client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil) |
| 70 | + |
| 71 | + if err != nil { |
| 72 | + // TODO: Update the following line with your application specific error handling logic |
| 73 | + log.Fatalf("ERROR: %s", err) |
| 74 | + } |
| 75 | + |
| 76 | + // This is a conversation in progress. |
| 77 | + // NOTE: all messages, regardless of role, count against token usage for this API. |
| 78 | + messages := []azopenai.ChatMessage{ |
| 79 | + // You set the tone and rules of the conversation with a prompt as the system role. |
| 80 | + {Role: to.Ptr(azopenai.ChatRoleSystem), Content: to.Ptr("You are a helpful assistant.")}, |
| 81 | + |
| 82 | + // The user asks a question |
| 83 | + {Role: to.Ptr(azopenai.ChatRoleUser), Content: to.Ptr("Does Azure OpenAI support customer managed keys?")}, |
| 84 | + |
| 85 | + // The reply would come back from the Azure OpenAI model. You'd add it to the conversation so we can maintain context. |
| 86 | + {Role: to.Ptr(azopenai.ChatRoleAssistant), Content: to.Ptr("Yes, customer managed keys are supported by Azure OpenAI")}, |
| 87 | + |
| 88 | + // The user answers the question based on the latest reply. |
| 89 | + {Role: to.Ptr(azopenai.ChatRoleUser), Content: to.Ptr("Do other Azure AI services support this too?")}, |
| 90 | + |
| 91 | + // from here you'd keep iterating, sending responses back from the chat completions API |
| 92 | + } |
| 93 | + |
| 94 | + resp, err := client.GetChatCompletions(context.TODO(), azopenai.ChatCompletionsOptions{ |
| 95 | + // This is a conversation in progress. |
| 96 | + // NOTE: all messages count against token usage for this API. |
| 97 | + Messages: messages, |
| 98 | + Deployment: modelDeploymentID, |
| 99 | + }, nil) |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + // TODO: Update the following line with your application specific error handling logic |
| 103 | + log.Fatalf("ERROR: %s", err) |
| 104 | + } |
| 105 | + |
| 106 | + for _, choice := range resp.Choices { |
| 107 | + fmt.Fprintf(os.Stderr, "Content[%d]: %s\n", *choice.Index, *choice.Message.Content) |
| 108 | + } |
| 109 | + |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +> [!IMPORTANT] |
| 114 | +> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](../../../key-vault/general/overview.md). For more information about credential security, see the Azure AI services [security](../../security-features.md) article. |
| 115 | +
|
| 116 | +Now open a command prompt and run: |
| 117 | + |
| 118 | +```cmd |
| 119 | +go mod init chat_completions.go |
| 120 | +``` |
| 121 | + |
| 122 | +Next run: |
| 123 | + |
| 124 | +```cmd |
| 125 | +go mod tidy |
| 126 | +``` |
| 127 | + |
| 128 | +```cmd |
| 129 | +go run chat_completions.go |
| 130 | +``` |
| 131 | + |
| 132 | +## Output |
| 133 | + |
| 134 | +```output |
| 135 | +Content[0]: Yes, many Azure AI services also support customer managed keys. These services enable you to bring your own encryption keys for data at rest, which provides you with more control over the security of your data. |
| 136 | +``` |
| 137 | + |
| 138 | +## Clean up resources |
| 139 | + |
| 140 | +If you want to clean up and remove an Azure OpenAI resource, you can delete the resource. Before deleting the resource, you must first delete any deployed models. |
| 141 | + |
| 142 | +- [Portal](../../multi-service-resource.md?pivots=azportal#clean-up-resources) |
| 143 | +- [Azure CLI](../../multi-service-resource.md?pivots=azcli#clean-up-resources) |
| 144 | + |
| 145 | +## Next steps |
| 146 | + |
| 147 | +* For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples) |
0 commit comments