Skip to content

Commit 8aae2b4

Browse files
authored
Merge pull request #239065 from mrbullwinkle/mrb_05_23_2023_new_quickstarts
[Azure OpenAI] New Quickstarts
2 parents 304399e + 1530d7c commit 8aae2b4

File tree

7 files changed

+727
-2
lines changed

7 files changed

+727
-2
lines changed

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

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

3232
::: zone-end
3333

34+
::: zone pivot="programming-language-java"
35+
36+
[!INCLUDE [Java quickstart](includes/chatgpt-java.md)]
37+
38+
::: zone-end
39+
40+
::: zone pivot="programming-language-javascript"
41+
42+
[!INCLUDE [JavaScript quickstart](includes/chatgpt-javascript.md)]
43+
44+
::: zone-end
45+
3446
::: zone pivot="programming-language-python"
3547

3648
[!INCLUDE [Python SDK quickstart](includes/chatgpt-python.md)]
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
---
2+
title: 'Quickstart: Use Azure OpenAI Service with the Java SDK'
3+
titleSuffix: Azure OpenAI
4+
description: Walkthrough on how to get started with Azure OpenAI and make your first chat completions call with the Java 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: 05/22/2023
13+
keywords:
14+
---
15+
16+
[Source code](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/openai/azure-ai-openai) | [Artifact (Maven)](https://central.sonatype.com/artifact/com.azure/azure-ai-openai/1.0.0-beta.1) | [Samples](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/openai/azure-ai-openai/src/samples)
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+
- [Java Development Kit (JDK)](/java/azure/jdk/) with version 8 or above.
24+
- An Azure OpenAI Service resource with either the `gpt-35-turbo` or the `gpt-4`<sup>1</sup> models deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
25+
26+
<sup>1</sup> **GPT-4 models are currently only available by request.** Existing Azure OpenAI customers can [apply for access by filling out this form](https://aka.ms/oai/get-gpt4).
27+
28+
## Set up
29+
30+
1. Install [Apache Maven](https://maven.apache.org/install.html). Then run `mvn -v` to confirm successful installation. The `README.txt` file from the installation has instructions on adding the Maven bin directory to your PATH variable. If you don't set this the `mvn` command will instead need to run like `c:\apache-maven-3.9.2-bin\apache-maven-3.9.2\mvn -v`.
31+
32+
2. Create the directory structure for your project.
33+
34+
```console
35+
mkdir "quickstart/src/main/java/com/azure/ai/openai/usage"
36+
```
37+
38+
3. At the root of the quickstart directory, create a file named `pom.xml` with the following content:
39+
40+
```xml
41+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
42+
<modelVersion>4.0.0</modelVersion>
43+
<groupId>com.azure.ai.openai.usage.GetChatCompletionsSample</groupId>
44+
<artifactId>quickstart-eclipse</artifactId>
45+
<version>1.0.0-SNAPSHOT</version>
46+
<build>
47+
<sourceDirectory>src</sourceDirectory>
48+
<plugins>
49+
<plugin>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<version>3.7.0</version>
52+
<configuration>
53+
<source>1.8</source>
54+
<target>1.8</target>
55+
</configuration>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
<dependencies>
60+
<dependency>
61+
<groupId>com.azure</groupId>
62+
<artifactId>azure-ai-openai</artifactId>
63+
<version>1.0.0-beta.1</version>
64+
</dependency>
65+
</dependencies>
66+
</project>
67+
```
68+
69+
### Retrieve key and endpoint
70+
71+
To successfully make a call against Azure OpenAI, you need an **endpoint** and a **key**.
72+
73+
|Variable name | Value |
74+
|--------------------------|-------------|
75+
| `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/`.|
76+
| `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`.|
77+
78+
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.
79+
80+
:::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":::
81+
82+
Create and assign persistent environment variables for your key and endpoint.
83+
84+
### Environment variables
85+
86+
# [Command Line](#tab/command-line)
87+
88+
```CMD
89+
setx AZURE_OPENAI_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
90+
```
91+
92+
```CMD
93+
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
94+
```
95+
96+
# [PowerShell](#tab/powershell)
97+
98+
```powershell
99+
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
100+
```
101+
102+
```powershell
103+
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
104+
```
105+
106+
# [Bash](#tab/bash)
107+
108+
```Bash
109+
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
110+
```
111+
112+
```Bash
113+
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
114+
```
115+
---
116+
117+
## Create a sample application
118+
119+
Create a new file named `GetChatCompletionsSample.java` and place it in `quickstart/src/main/java/com/azure/ai/openai/usage` folder. Copy the following code into the file.
120+
121+
```java
122+
package com.azure.ai.openai.usage;
123+
124+
import com.azure.ai.openai.OpenAIClient;
125+
import com.azure.ai.openai.OpenAIClientBuilder;
126+
import com.azure.ai.openai.models.ChatChoice;
127+
import com.azure.ai.openai.models.ChatCompletions;
128+
import com.azure.ai.openai.models.ChatCompletionsOptions;
129+
import com.azure.ai.openai.models.ChatMessage;
130+
import com.azure.ai.openai.models.ChatRole;
131+
import com.azure.ai.openai.models.CompletionsUsage;
132+
import com.azure.core.credential.AzureKeyCredential;
133+
134+
import java.util.ArrayList;
135+
import java.util.List;
136+
137+
public class GetChatCompletionsSample {
138+
139+
public static void main(String[] args) {
140+
String azureOpenaiKey = System.getenv("AZURE_OPENAI_KEY");;
141+
String endpoint = System.getenv("AZURE_OPENAI_ENDPOINT");;
142+
String deploymentOrModelId = "gpt-35-turbo";
143+
144+
OpenAIClient client = new OpenAIClientBuilder()
145+
.endpoint(endpoint)
146+
.credential(new AzureKeyCredential(azureOpenaiKey))
147+
.buildClient();
148+
149+
List<ChatMessage> chatMessages = new ArrayList<>();
150+
chatMessages.add(new ChatMessage(ChatRole.SYSTEM).setContent("You are a helpful assistant."));
151+
chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Does Azure OpenAI support customer managed keys?"));
152+
chatMessages.add(new ChatMessage(ChatRole.ASSISTANT).setContent("Yes, customer managed keys are supported by Azure OpenAI?"));
153+
chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Do other Azure Cognitive Services support this too?"));
154+
155+
ChatCompletions chatCompletions = client.getChatCompletions(deploymentOrModelId, new ChatCompletionsOptions(chatMessages));
156+
157+
System.out.printf("Model ID=%s is created at %d.%n", chatCompletions.getId(), chatCompletions.getCreated());
158+
for (ChatChoice choice : chatCompletions.getChoices()) {
159+
ChatMessage message = choice.getMessage();
160+
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
161+
System.out.println("Message:");
162+
System.out.println(message.getContent());
163+
}
164+
165+
System.out.println();
166+
CompletionsUsage usage = chatCompletions.getUsage();
167+
System.out.printf("Usage: number of prompt token is %d, "
168+
+ "number of completion token is %d, and number of total tokens in request and response is %d.%n",
169+
usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
170+
}
171+
}
172+
```
173+
174+
> [!IMPORTANT]
175+
> 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 Cognitive Services [security](../../security-features.md) article.
176+
177+
From the top level quickstart directory where your `pom.xml` is located run:
178+
179+
```console
180+
mvn compile
181+
```
182+
183+
Now run the sample:
184+
185+
```console
186+
mvn exec:java -Dexec.mainClass="com.azure.ai.openai.usage.GetChatCompletionsSample"
187+
```
188+
189+
## Output
190+
191+
```output
192+
Model ID=chatcmpl-7JYnyE4zpd5gaIfTRH7hNpeVsvAw4 is created at 1684896378.
193+
Index: 0, Chat Role: assistant.
194+
Message:
195+
Yes, most of the Azure Cognitive Services support customer managed keys. However, there may be some exceptions, so it is best to check the documentation of each specific service to confirm.
196+
197+
Usage: number of prompt token is 59, number of completion token is 36, and number of total tokens in request and response is 95.
198+
```
199+
200+
## Clean up resources
201+
202+
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.
203+
204+
- [Portal](../../cognitive-services-apis-create-account.md#clean-up-resources)
205+
- [Azure CLI](../../cognitive-services-apis-create-account-cli.md#clean-up-resources)
206+
207+
## Next steps
208+
209+
* For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples)
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
title: 'Quickstart: Use Azure OpenAI Service with the JavaScript SDK'
3+
titleSuffix: Azure OpenAI
4+
description: Walkthrough on how to get started with Azure OpenAI and make your first chat completions call with the JavaScript 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: 05/22/2023
13+
keywords:
14+
---
15+
16+
[Source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai) | [Package (npm)](https://www.npmjs.com/package/@azure/openai) | [Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/tests/Samples)
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 either the `gpt-35-turbo` or the `gpt-4`<sup>1</sup> models deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
25+
26+
<sup>1</sup> **GPT-4 models are currently only available by request.** Existing Azure OpenAI customers can [apply for access by filling out this form](https://aka.ms/oai/get-gpt4).
27+
28+
## Set up
29+
30+
### Install the client library
31+
32+
Create a new directory and navigate into that directory.
33+
34+
Install the Azure OpenAI client library for JavaScript with npm:
35+
36+
```console
37+
npm install @azure/openai
38+
```
39+
40+
### Retrieve key and endpoint
41+
42+
To successfully make a call against Azure OpenAI, you need an **endpoint** and a **key**.
43+
44+
|Variable name | Value |
45+
|--------------------------|-------------|
46+
| `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/`.|
47+
| `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`.|
48+
49+
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.
50+
51+
:::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":::
52+
53+
Create and assign persistent environment variables for your key and endpoint.
54+
55+
### Environment variables
56+
57+
# [Command Line](#tab/command-line)
58+
59+
```CMD
60+
setx AZURE_OPENAI_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
61+
```
62+
63+
```CMD
64+
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
65+
```
66+
67+
# [PowerShell](#tab/powershell)
68+
69+
```powershell
70+
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
71+
```
72+
73+
```powershell
74+
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
75+
```
76+
77+
# [Bash](#tab/bash)
78+
79+
```Bash
80+
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
81+
```
82+
83+
```Bash
84+
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
85+
```
86+
---
87+
88+
## Create a sample application
89+
90+
Open a command prompt where you want the new project, and create a new file named ChatCompletion.js. Copy the following code into the ChatCompletion.js file.
91+
92+
```javascript
93+
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
94+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] ;
95+
const azureApiKey = process.env["AZURE_OPENAI_KEY"] ;
96+
97+
const messages = [
98+
{ role: "system", content: "You are a helpful assistant." },
99+
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
100+
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI" },
101+
{ role: "user", content: "Do other Azure Cognitive Services support this too" },
102+
];
103+
104+
async function main() {
105+
console.log("== Chat Completions Sample ==");
106+
107+
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
108+
const deploymentId = "gpt-35-turbo";
109+
const result = await client.getChatCompletions(deploymentId, messages);
110+
111+
for (const choice of result.choices) {
112+
console.log(choice.message);
113+
}
114+
}
115+
116+
main().catch((err) => {
117+
console.error("The sample encountered an error:", err);
118+
});
119+
120+
module.exports = { main };
121+
```
122+
123+
> [!IMPORTANT]
124+
> 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 Cognitive Services [security](../../security-features.md) article.
125+
126+
```cmd
127+
node.exe ChatCompletion.js
128+
```
129+
130+
## Output
131+
132+
```output
133+
== Chat Completions Sample ==
134+
{
135+
role: 'assistant',
136+
content: 'Yes, most Azure Cognitive Services support customer managed keys. It is always best to check the specific service documentation to confirm this.'
137+
}
138+
```
139+
140+
## Clean up resources
141+
142+
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.
143+
144+
- [Portal](../../cognitive-services-apis-create-account.md#clean-up-resources)
145+
- [Azure CLI](../../cognitive-services-apis-create-account-cli.md#clean-up-resources)
146+
147+
## Next steps
148+
149+
* For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples)

0 commit comments

Comments
 (0)