Skip to content

Commit a1d169a

Browse files
authored
adding openai sample (#45344)
1 parent ee4876b commit a1d169a

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

sdk/ai/azure-ai-projects/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ Code generated by Microsoft (R) TypeSpec Code Generator.
7878
<artifactId>azure-ai-inference</artifactId>
7979
<version>1.0.0-beta.4</version> <!-- {x-version-update;com.azure:azure-ai-inference;external_dependency} -->
8080
</dependency>
81+
<dependency>
82+
<groupId>com.azure</groupId>
83+
<artifactId>azure-ai-openai</artifactId>
84+
<version>1.0.0-beta.16</version> <!-- {x-version-update;com.azure:azure-ai-openai;dependency} -->
85+
</dependency>
8186

8287
<!-- test Dependencies -->
8388

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure.ai.projects;
4+
5+
import com.azure.ai.openai.OpenAIAsyncClient;
6+
import com.azure.ai.openai.OpenAIClientBuilder;
7+
import com.azure.ai.openai.models.ChatChoice;
8+
import com.azure.ai.openai.models.ChatCompletionsOptions;
9+
import com.azure.ai.openai.models.ChatRequestMessage;
10+
import com.azure.ai.openai.models.ChatRequestSystemMessage;
11+
import com.azure.ai.openai.models.ChatRequestUserMessage;
12+
import com.azure.ai.projects.models.ApiKeyCredentials;
13+
import com.azure.ai.projects.models.BaseCredentials;
14+
import com.azure.ai.projects.models.ConnectionType;
15+
import com.azure.ai.projects.models.CredentialType;
16+
import com.azure.core.credential.KeyCredential;
17+
import com.azure.core.util.Configuration;
18+
import com.azure.identity.DefaultAzureCredentialBuilder;
19+
import reactor.core.publisher.Mono;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
23+
public class InferenceOpenAIAsyncSample {
24+
25+
public static void main(String[] args) {
26+
openAIConnectedAsyncSample().block();
27+
}
28+
29+
public static Mono<Void> openAIConnectedAsyncSample() {
30+
String endpoint = Configuration.getGlobalConfiguration().get("ENDPOINT", "endpoint");
31+
32+
ConnectionsAsyncClient connectionsAsyncClient = new AIProjectClientBuilder().endpoint(endpoint)
33+
.credential(new DefaultAzureCredentialBuilder().build())
34+
.buildConnectionsAsyncClient();
35+
36+
String openAIConnectionName = Configuration.getGlobalConfiguration().get("OPENAI_CONNECTION_NAME", "");
37+
if (openAIConnectionName.isEmpty()) {
38+
return Mono.error(new IllegalArgumentException("OPENAI_CONNECTION_NAME is not set."));
39+
}
40+
41+
return connectionsAsyncClient.getConnection(openAIConnectionName, true)
42+
.flatMap(connection -> {
43+
if (connection.getType() != ConnectionType.AZURE_OPEN_AI) {
44+
return Mono.error(new IllegalArgumentException("The connection is not of type OPENAI."));
45+
}
46+
47+
String azureOpenAIEndpoint = connection.getTarget();
48+
if (azureOpenAIEndpoint.endsWith("/")) {
49+
azureOpenAIEndpoint = azureOpenAIEndpoint.substring(0, azureOpenAIEndpoint.length() - 1);
50+
}
51+
52+
OpenAIAsyncClient openAIAsyncClient;
53+
54+
BaseCredentials credentials = connection.getCredentials();
55+
if (credentials.getType() == CredentialType.API_KEY && credentials instanceof ApiKeyCredentials) {
56+
String apiKey = ((ApiKeyCredentials) credentials).getApiKey();
57+
openAIAsyncClient = new OpenAIClientBuilder().endpoint(azureOpenAIEndpoint)
58+
.credential(new KeyCredential(apiKey))
59+
.buildAsyncClient();
60+
} else if (credentials.getType() == CredentialType.ENTRA_ID) {
61+
openAIAsyncClient = new OpenAIClientBuilder().endpoint(azureOpenAIEndpoint)
62+
.credential(new DefaultAzureCredentialBuilder().build())
63+
.buildAsyncClient();
64+
} else {
65+
return Mono.error(new IllegalArgumentException("Unsupported credential type."));
66+
}
67+
68+
List<ChatRequestMessage> chatMessages = Arrays.asList(
69+
new ChatRequestSystemMessage("You are a helpful assistant."),
70+
new ChatRequestUserMessage("I am going to Paris, what should I see?")
71+
);
72+
ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions(chatMessages);
73+
74+
return openAIAsyncClient.getChatCompletions("gpt-4o", chatCompletionsOptions)
75+
.flatMap(chatCompletions -> {
76+
System.out.printf("Model ID=%s is created at %s.%n", chatCompletions.getId(), chatCompletions.getCreatedAt());
77+
for (ChatChoice choice : chatCompletions.getChoices()) {
78+
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), choice.getMessage().getRole());
79+
System.out.println("Message:");
80+
System.out.println(choice.getMessage().getContent());
81+
}
82+
return Mono.empty();
83+
});
84+
});
85+
}
86+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure.ai.projects;
4+
5+
import com.azure.ai.openai.OpenAIClient;
6+
import com.azure.ai.openai.OpenAIClientBuilder;
7+
import com.azure.ai.openai.models.ChatChoice;
8+
import com.azure.ai.openai.models.ChatCompletions;
9+
import com.azure.ai.openai.models.ChatCompletionsOptions;
10+
import com.azure.ai.openai.models.ChatRequestMessage;
11+
import com.azure.ai.openai.models.ChatRequestSystemMessage;
12+
import com.azure.ai.openai.models.ChatRequestUserMessage;
13+
import com.azure.ai.openai.models.ChatResponseMessage;
14+
import com.azure.ai.projects.models.ApiKeyCredentials;
15+
import com.azure.ai.projects.models.BaseCredentials;
16+
import com.azure.ai.projects.models.Connection;
17+
import com.azure.ai.projects.models.ConnectionType;
18+
import com.azure.ai.projects.models.CredentialType;
19+
import com.azure.core.credential.KeyCredential;
20+
import com.azure.core.util.Configuration;
21+
import com.azure.identity.DefaultAzureCredentialBuilder;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
public class InferenceOpenAISample {
26+
27+
public static void main(String[] args) {
28+
openAIConnectedSample();
29+
}
30+
31+
public static void openAIConnectedSample() {
32+
// BEGIN: com.azure.ai.projects.InferenceOpenAISample.openAIConnectedSample
33+
34+
String endpoint = Configuration.getGlobalConfiguration().get("ENDPOINT", "endpoint");
35+
36+
ConnectionsClient connectionsClient = new AIProjectClientBuilder().endpoint(endpoint)
37+
.credential(new DefaultAzureCredentialBuilder().build())
38+
.buildConnectionsClient();
39+
40+
String openAIConnectionName = Configuration.getGlobalConfiguration().get("OPENAI_CONNECTION_NAME", "");
41+
if (openAIConnectionName.isEmpty()) {
42+
throw new IllegalArgumentException("OPENAI_CONNECTION_NAME is not set.");
43+
}
44+
45+
Connection connection = connectionsClient.getConnection(openAIConnectionName, true);
46+
if (connection.getType() != ConnectionType.AZURE_OPEN_AI) {
47+
throw new IllegalArgumentException("The connection is not of type OPENAI.");
48+
}
49+
50+
String azureOpenAIEndpoint = connection.getTarget();
51+
if (azureOpenAIEndpoint.endsWith("/")) {
52+
azureOpenAIEndpoint = azureOpenAIEndpoint.substring(0, azureOpenAIEndpoint.length() - 1);
53+
}
54+
55+
OpenAIClient openAIClient;
56+
57+
BaseCredentials credentials = connection.getCredentials();
58+
if (credentials.getType() == CredentialType.API_KEY && credentials instanceof ApiKeyCredentials) {
59+
String apiKey = ((ApiKeyCredentials) credentials).getApiKey();
60+
openAIClient = new OpenAIClientBuilder().endpoint(azureOpenAIEndpoint)
61+
.credential(new KeyCredential(apiKey))
62+
.buildClient();
63+
} else if (credentials.getType() == CredentialType.ENTRA_ID) {
64+
openAIClient = new OpenAIClientBuilder().endpoint(azureOpenAIEndpoint)
65+
.credential(new DefaultAzureCredentialBuilder().build())
66+
.buildClient();
67+
} else {
68+
throw new IllegalArgumentException("Unsupported credential type.");
69+
}
70+
71+
List<ChatRequestMessage> chatMessages = Arrays.asList(
72+
new ChatRequestSystemMessage("You are a helpful assistant."),
73+
new ChatRequestUserMessage("I am going to Paris, what should I see?")
74+
);
75+
ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions(chatMessages);
76+
77+
ChatCompletions chatCompletions = openAIClient.getChatCompletions("gpt-4o", chatCompletionsOptions);
78+
79+
System.out.printf("Model ID=%s is created at %s.%n", chatCompletions.getId(), chatCompletions.getCreatedAt());
80+
for (ChatChoice choice : chatCompletions.getChoices()) {
81+
ChatResponseMessage message = choice.getMessage();
82+
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
83+
System.out.println("Message:");
84+
System.out.println(message.getContent());
85+
}
86+
87+
// END: com.azure.ai.projects.InferenceOpenAISample.openAIConnectedSample
88+
}
89+
}

0 commit comments

Comments
 (0)