Skip to content

Commit 5f89796

Browse files
committed
update
1 parent bd01498 commit 5f89796

File tree

3 files changed

+155
-18
lines changed

3 files changed

+155
-18
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 ChatGPT. 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)

articles/zone-pivot-groups.yml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,23 +1731,6 @@ groups:
17311731
title: REST
17321732
# Owner: mbullwin
17331733
- id: openai-quickstart-new
1734-
title: Programming languages
1735-
prompt: Choose your preferred usage method
1736-
pivots:
1737-
- id: programming-language-studio
1738-
title: Studio
1739-
- id: programming-language-csharp
1740-
title: C#
1741-
- id: programming-language-java
1742-
title: Java
1743-
- id: programming-language-javascript
1744-
title: JavaScript
1745-
- id: programming-language-python
1746-
title: Python
1747-
- id: rest-api
1748-
title: REST
1749-
# Owner: mbullwin
1750-
- id: openai-quickstart-new2
17511734
title: Programming languages
17521735
prompt: Choose your preferred usage method
17531736
pivots:

0 commit comments

Comments
 (0)