Skip to content

Commit 2438622

Browse files
committed
update
1 parent 3aa38e9 commit 2438622

File tree

2 files changed

+185
-1
lines changed

2 files changed

+185
-1
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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/23/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)
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+
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
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+
> [!div class="nextstepaction"]
27+
> [I ran into an issue with the prerequisites.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=JAVASCRIPT&Pillar=AOAI&&Product=gpt&Page=quickstart&Section=Prerequisites)
28+
29+
## Set up
30+
31+
### Retrieve key and endpoint
32+
33+
To successfully make a call against Azure OpenAI, you need an **endpoint** and a **key**.
34+
35+
|Variable name | Value |
36+
|--------------------------|-------------|
37+
| `ENDPOINT` | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. Alternatively, you can find the value in the **Azure OpenAI Studio** > **Playground** > **Code View**. An example endpoint is: `https://docs-test-001.openai.azure.com/`.|
38+
| `API-KEY` | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. You can use either `KEY1` or `KEY2`.|
39+
40+
Go to your resource in the Azure portal. The **Endpoint and Keys** can be found in the **Resource Management** section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either `KEY1` or `KEY2`. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
41+
42+
:::image type="content" source="../media/quickstarts/endpoint.png" alt-text="Screenshot of the overview UI for an OpenAI Resource in the Azure portal with the endpoint and access keys location circled in red." lightbox="../media/quickstarts/endpoint.png":::
43+
44+
Create and assign persistent environment variables for your key and endpoint.
45+
46+
### Environment variables
47+
48+
# [Command Line](#tab/command-line)
49+
50+
```CMD
51+
setx AZURE_OPENAI_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
52+
```
53+
54+
```CMD
55+
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
56+
```
57+
58+
# [PowerShell](#tab/powershell)
59+
60+
```powershell
61+
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
62+
```
63+
64+
```powershell
65+
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
66+
```
67+
68+
# [Bash](#tab/bash)
69+
70+
```Bash
71+
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
72+
```
73+
74+
```Bash
75+
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
76+
```
77+
---
78+
79+
> [!div class="nextstepaction"]
80+
> [I ran into an issue with the setup.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=JAVASCRIPT&Pillar=AOAI&&Product=gpt&Page=quickstart&Section=Set-up-the-environment)
81+
82+
## Create a sample application
83+
84+
Create a new file named completion.go. Copy the following code into the completion.go file.
85+
86+
```go
87+
package main
88+
89+
import (
90+
"context"
91+
"fmt"
92+
"os"
93+
94+
"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
95+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
96+
)
97+
98+
func main() {
99+
azureOpenAIKey := os.Getenv("AZURE_OPENAI_KEY")
100+
modelDeploymentID := "text-davinci-003"
101+
102+
azureOpenAIEndpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
103+
104+
if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" {
105+
fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
106+
return
107+
}
108+
109+
keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)
110+
111+
if err != nil {
112+
// TODO: handle error
113+
}
114+
115+
client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)
116+
117+
if err != nil {
118+
// TODO: handle error
119+
}
120+
121+
resp, err := client.GetCompletions(context.TODO(), azopenai.CompletionsOptions{
122+
Prompt: []string{"What is Azure OpenAI, in 20 words or less"},
123+
MaxTokens: to.Ptr(int32(2048)),
124+
Temperature: to.Ptr(float32(0.0)),
125+
DeploymentID: modelDeploymentID,
126+
}, nil)
127+
128+
if err != nil {
129+
// TODO: handle error
130+
}
131+
132+
for _, choice := range resp.Choices {
133+
fmt.Fprintf(os.Stderr, "Result: %s\n", *choice.Text)
134+
}
135+
136+
}
137+
```
138+
139+
> [!IMPORTANT]
140+
> 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.
141+
142+
Now open a command prompt and run:
143+
144+
```cmd
145+
go mod init completions.go
146+
```
147+
148+
Next run:
149+
150+
```cmd
151+
go mod tidy
152+
```
153+
154+
```cmd
155+
go run completions.go
156+
```
157+
158+
## Output
159+
160+
```output
161+
== Get completions Sample ==
162+
163+
Microsoft was founded on April 4, 1975.
164+
```
165+
166+
> [!div class="nextstepaction"]
167+
> [I ran into an issue when running the code sample.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=JAVASCRIPT&Pillar=AOAI&&Product=gpt&Page=quickstart&Section=Create-application)
168+
169+
## Clean up resources
170+
171+
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.
172+
173+
- [Portal](../../multi-service-resource.md?pivots=azportal#clean-up-resources)
174+
- [Azure CLI](../../multi-service-resource.md?pivots=azcli#clean-up-resources)
175+
176+
## Next steps
177+
178+
* 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)]

0 commit comments

Comments
 (0)