Skip to content

Commit 2e5c5dc

Browse files
committed
update
1 parent cb62a86 commit 2e5c5dc

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

articles/ai-services/openai/includes/chatgpt-java.md

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ ms.service: azure-ai-openai
88
ms.topic: include
99
author: mrbullwinkle
1010
ms.author: mbullwin
11-
ms.date: 07/26/2023
11+
ms.date: 07/03/2024
1212
---
1313

14-
[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.3) | [Samples](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/openai/azure-ai-openai/src/samples) | [Retrieval Augmented Generation (RAG) enterprise chat template](/azure/developer/java/quickstarts/get-started-app-chat-template) | [IntelliJ IDEA](/azure/developer/java/toolkit-for-intellij/chatgpt-intellij)
14+
[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.10) | [Samples](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/openai/azure-ai-openai/src/samples) | [Retrieval Augmented Generation (RAG) enterprise chat template](/azure/developer/java/quickstarts/get-started-app-chat-template) | [IntelliJ IDEA](/azure/developer/java/toolkit-for-intellij/chatgpt-intellij)
1515

1616
## Prerequisites
1717

@@ -55,7 +55,7 @@ When prompted to choose a **DSL**, select **Kotlin**.
5555
5656
## Install the Java SDK
5757

58-
This quickstart uses the Gradle dependency manager. You can find the client library and information for other dependency managers on the [Maven Central Repository](https://search.maven.org/artifact/com.microsoft.azure.cognitiveservices/azure-cognitiveservices-computervision).
58+
This quickstart uses the Gradle dependency manager. You can find the client library and information for other dependency managers on the [Maven Central Repository](https://central.sonatype.com/search?q=azure-ai-openai).
5959

6060
Locate *build.gradle.kts* and open it with your preferred IDE or text editor. Then copy in the following build configuration. This configuration defines the project as a Java application whose entry point is the class **OpenAIQuickstart**. It imports the Azure AI Vision library.
6161

@@ -71,7 +71,7 @@ repositories {
7171
mavenCentral()
7272
}
7373
dependencies {
74-
implementation(group = "com.azure", name = "azure-ai-openai", version = "1.0.0-beta.3")
74+
implementation(group = "com.azure", name = "azure-ai-openai", version = "1.0.0-beta.10")
7575
implementation("org.slf4j:slf4j-simple:1.7.9")
7676
}
7777
```
@@ -105,7 +105,7 @@ dependencies {
105105
import java.util.ArrayList;
106106
import java.util.List;
107107
108-
public class GetChatCompletionsSample {
108+
public class OpenAIQuickstart {
109109
110110
public static void main(String[] args) {
111111
String azureOpenaiKey = System.getenv("AZURE_OPENAI_API_KEY");;
@@ -142,6 +142,75 @@ dependencies {
142142
}
143143
```
144144

145+
```java
146+
package com.azure.ai.openai.usage;
147+
148+
import com.azure.ai.openai.OpenAIClient;
149+
import com.azure.ai.openai.OpenAIClientBuilder;
150+
import com.azure.ai.openai.models.ChatChoice;
151+
import com.azure.ai.openai.models.ChatCompletions;
152+
import com.azure.ai.openai.models.ChatCompletionsOptions;
153+
import com.azure.ai.openai.models.ChatRequestAssistantMessage;
154+
import com.azure.ai.openai.models.ChatRequestMessage;
155+
import com.azure.ai.openai.models.ChatRequestSystemMessage;
156+
import com.azure.ai.openai.models.ChatRequestUserMessage;
157+
import com.azure.ai.openai.models.ChatResponseMessage;
158+
import com.azure.ai.openai.models.CompletionsUsage;
159+
import com.azure.core.credential.AzureKeyCredential;
160+
import com.azure.core.util.Configuration;
161+
162+
import java.util.ArrayList;
163+
import java.util.List;
164+
165+
/**
166+
* Sample demonstrates how to get chat completions for the provided chat messages.
167+
* Completions support a wide variety of tasks and generate text that continues from or "completes" provided
168+
* prompt data.
169+
*/
170+
public class GetChatCompletionsSample {
171+
/**
172+
* Runs the sample algorithm and demonstrates how to get chat completions for the provided chat messages.
173+
* Completions support a wide variety of tasks and generate text that continues from or "completes" provided
174+
* prompt data.
175+
*
176+
* @param args Unused. Arguments to the program.
177+
*/
178+
public static void main(String[] args) {
179+
String azureOpenaiKey = Configuration.getGlobalConfiguration().get("AZURE_OPENAI_API_KEY");
180+
String endpoint = Configuration.getGlobalConfiguration().get("AZURE_OPENAI_ENDPOINT");
181+
String deploymentOrModelId = "{azure-open-ai-deployment-model-id}";
182+
183+
OpenAIClient client = new OpenAIClientBuilder()
184+
.endpoint(endpoint)
185+
.credential(new AzureKeyCredential(azureOpenaiKey))
186+
.buildClient();
187+
188+
List<ChatRequestMessage> chatMessages = new ArrayList<>();
189+
chatMessages.add(new ChatRequestSystemMessage("You are a helpful assistant."));
190+
chatMessages.add(new ChatRequestUserMessage("Does Azure OpenAI support customer managed keys?"));
191+
chatMessages.add(new ChatRequestAssistantMessage("Yes, customer managed keys are supported by Azure OpenAI?"));
192+
chatMessages.add(new ChatRequestUserMessage("Do other Azure AI services support this too?"));
193+
194+
ChatCompletions chatCompletions = client.getChatCompletions(deploymentOrModelId, new ChatCompletionsOptions(chatMessages));
195+
196+
System.out.printf("Model ID=%s is created at %s.%n", chatCompletions.getId(), chatCompletions.getCreatedAt());
197+
for (ChatChoice choice : chatCompletions.getChoices()) {
198+
ChatResponseMessage message = choice.getMessage();
199+
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
200+
System.out.println("Message:");
201+
System.out.println(message.getContent());
202+
}
203+
204+
System.out.println();
205+
CompletionsUsage usage = chatCompletions.getUsage();
206+
System.out.printf("Usage: number of prompt token is %d, "
207+
+ "number of completion token is %d, and number of total tokens in request and response is %d.%n",
208+
usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
209+
}
210+
}
211+
```
212+
213+
145214
> [!IMPORTANT]
146215
> 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.
147216

0 commit comments

Comments
 (0)