|
| 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