Skip to content

Commit be25d35

Browse files
authored
Merge pull request #249934 from mrbullwinkle/mrb_08_23_2023_go_quickstart_completion
[Azure OpenAI] Go Completions + Chat Completions
2 parents 6d769bc + 3366ded commit be25d35

File tree

5 files changed

+289
-2
lines changed

5 files changed

+289
-2
lines changed

articles/ai-services/openai/chatgpt-quickstart.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ms.custom: build-2023, build-2023-dataai, devx-track-python, devx-track-dotnet,
1010
ms.topic: quickstart
1111
author: mrbullwinkle
1212
ms.author: mbullwin
13-
ms.date: 05/23/2023
13+
ms.date: 08/31/2023
1414
zone_pivot_groups: openai-quickstart-new
1515
recommendations: false
1616
---
@@ -31,6 +31,13 @@ Use this article to get started using Azure OpenAI.
3131

3232
::: zone-end
3333

34+
::: zone pivot="programming-language-go"
35+
36+
[!INCLUDE [Go quickstart](includes/chat-go.md)]
37+
38+
::: zone-end
39+
40+
3441
::: zone pivot="programming-language-java"
3542

3643
[!INCLUDE [Java quickstart](includes/chatgpt-java.md)]
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 text-davinci-003 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 completions.go. Copy the following code into the completions.go file.
35+
36+
```go
37+
package main
38+
39+
import (
40+
"context"
41+
"fmt"
42+
"os"
43+
44+
"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
45+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
46+
)
47+
48+
func main() {
49+
azureOpenAIKey := os.Getenv("AZURE_OPENAI_KEY")
50+
modelDeploymentID := "text-davinci-003"
51+
52+
azureOpenAIEndpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
53+
54+
if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" {
55+
fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
56+
return
57+
}
58+
59+
keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)
60+
61+
if err != nil {
62+
// TODO: handle error
63+
}
64+
65+
client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)
66+
67+
if err != nil {
68+
// TODO: handle error
69+
}
70+
71+
resp, err := client.GetCompletions(context.TODO(), azopenai.CompletionsOptions{
72+
Prompt: []string{"What is Azure OpenAI, in 20 words or less"},
73+
MaxTokens: to.Ptr(int32(2048)),
74+
Temperature: to.Ptr(float32(0.0)),
75+
DeploymentID: modelDeploymentID,
76+
}, nil)
77+
78+
if err != nil {
79+
// TODO: handle error
80+
}
81+
82+
for _, choice := range resp.Choices {
83+
fmt.Fprintf(os.Stderr, "Result: %s\n", *choice.Text)
84+
}
85+
86+
}
87+
```
88+
89+
> [!IMPORTANT]
90+
> 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.
91+
92+
Now open a command prompt and run:
93+
94+
```cmd
95+
go mod init completions.go
96+
```
97+
98+
Next run:
99+
100+
```cmd
101+
go mod tidy
102+
```
103+
104+
```cmd
105+
go run completions.go
106+
```
107+
108+
## Output
109+
110+
```output
111+
== Get completions Sample ==
112+
113+
Microsoft was founded on April 4, 1975.
114+
```
115+
116+
## Clean up resources
117+
118+
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.
119+
120+
- [Portal](../../multi-service-resource.md?pivots=azportal#clean-up-resources)
121+
- [Azure CLI](../../multi-service-resource.md?pivots=azcli#clean-up-resources)
122+
123+
## Next steps
124+
125+
* For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples)

articles/ai-services/openai/quickstart.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ms.custom: devx-track-dotnet, devx-track-python, devx-track-extended-java, devx-
1010
ms.topic: quickstart
1111
author: mrbullwinkle
1212
ms.author: mbullwin
13-
ms.date: 05/23/2023
13+
ms.date: 08/23/2023
1414
zone_pivot_groups: openai-quickstart-new
1515
recommendations: false
1616
---
@@ -31,6 +31,12 @@ Use this article to get started making your first calls to Azure OpenAI.
3131

3232
::: zone-end
3333

34+
::: zone pivot="programming-language-go"
35+
36+
[!INCLUDE [Go quickstart](includes/go.md)]
37+
38+
::: zone-end
39+
3440
::: zone pivot="programming-language-java"
3541

3642
[!INCLUDE [Java quickstart](includes/java.md)]

articles/zone-pivot-groups.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,8 @@ groups:
17381738
title: Studio
17391739
- id: programming-language-csharp
17401740
title: C#
1741+
- id: programming-language-go
1742+
title: Go
17411743
- id: programming-language-java
17421744
title: Java
17431745
- id: programming-language-javascript

0 commit comments

Comments
 (0)