Skip to content

Commit 9fca322

Browse files
Merge pull request #280014 from mrbullwinkle/mrb_07_03_2024_java_fix
[Azure OpenAI] Chat Completions Java updates
2 parents f7a26fd + e6a33cd commit 9fca322

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

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

Lines changed: 28 additions & 20 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
```
@@ -91,43 +91,50 @@ dependencies {
9191

9292
1. Open *OpenAIQuickstart.java* in your preferred editor or IDE and paste in the following code.
9393

94-
```java
94+
```java
95+
package com.azure.ai.openai.usage;
96+
9597
import com.azure.ai.openai.OpenAIClient;
9698
import com.azure.ai.openai.OpenAIClientBuilder;
9799
import com.azure.ai.openai.models.ChatChoice;
98100
import com.azure.ai.openai.models.ChatCompletions;
99101
import com.azure.ai.openai.models.ChatCompletionsOptions;
100-
import com.azure.ai.openai.models.ChatMessage;
101-
import com.azure.ai.openai.models.ChatRole;
102+
import com.azure.ai.openai.models.ChatRequestAssistantMessage;
103+
import com.azure.ai.openai.models.ChatRequestMessage;
104+
import com.azure.ai.openai.models.ChatRequestSystemMessage;
105+
import com.azure.ai.openai.models.ChatRequestUserMessage;
106+
import com.azure.ai.openai.models.ChatResponseMessage;
102107
import com.azure.ai.openai.models.CompletionsUsage;
103108
import com.azure.core.credential.AzureKeyCredential;
109+
import com.azure.core.util.Configuration;
104110
105111
import java.util.ArrayList;
106112
import java.util.List;
107113
108-
public class GetChatCompletionsSample {
109-
114+
115+
public class OpenAIQuickstart {
116+
110117
public static void main(String[] args) {
111-
String azureOpenaiKey = System.getenv("AZURE_OPENAI_API_KEY");;
112-
String endpoint = System.getenv("AZURE_OPENAI_ENDPOINT");;
113-
String deploymentOrModelId = "gpt-35-turbo"; //Change to match your deployment name
118+
String azureOpenaiKey = Configuration.getGlobalConfiguration().get("AZURE_OPENAI_API_KEY");
119+
String endpoint = Configuration.getGlobalConfiguration().get("AZURE_OPENAI_ENDPOINT");
120+
String deploymentOrModelId = "{azure-open-ai-deployment-model-id}";
114121
115-
OpenAIClient client = new OpenAIClientBuilder()
122+
OpenAIClient client = new OpenAIClientBuilder()
116123
.endpoint(endpoint)
117124
.credential(new AzureKeyCredential(azureOpenaiKey))
118125
.buildClient();
119126
120-
List<ChatMessage> chatMessages = new ArrayList<>();
121-
chatMessages.add(new ChatMessage(ChatRole.SYSTEM, "You are a helpful assistant"));
122-
chatMessages.add(new ChatMessage(ChatRole.USER, "Does Azure OpenAI support customer managed keys?"));
123-
chatMessages.add(new ChatMessage(ChatRole.ASSISTANT, "Yes, customer managed keys are supported by Azure OpenAI?"));
124-
chatMessages.add(new ChatMessage(ChatRole.USER, "Do other Azure AI services support this too?"));
127+
List<ChatRequestMessage> chatMessages = new ArrayList<>();
128+
chatMessages.add(new ChatRequestSystemMessage("You are a helpful assistant."));
129+
chatMessages.add(new ChatRequestUserMessage("Does Azure OpenAI support customer managed keys?"));
130+
chatMessages.add(new ChatRequestAssistantMessage("Yes, customer managed keys are supported by Azure OpenAI?"));
131+
chatMessages.add(new ChatRequestUserMessage("Do other Azure AI services support this too?"));
125132
126133
ChatCompletions chatCompletions = client.getChatCompletions(deploymentOrModelId, new ChatCompletionsOptions(chatMessages));
127134
128135
System.out.printf("Model ID=%s is created at %s.%n", chatCompletions.getId(), chatCompletions.getCreatedAt());
129136
for (ChatChoice choice : chatCompletions.getChoices()) {
130-
ChatMessage message = choice.getMessage();
137+
ChatResponseMessage message = choice.getMessage();
131138
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
132139
System.out.println("Message:");
133140
System.out.println(message.getContent());
@@ -139,9 +146,10 @@ dependencies {
139146
+ "number of completion token is %d, and number of total tokens in request and response is %d.%n",
140147
usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
141148
}
142-
}
149+
}
143150
```
144151

152+
145153
> [!IMPORTANT]
146154
> 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.
147155

0 commit comments

Comments
 (0)