|
| 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) |
0 commit comments