openai/openai-java
Publicmirrored fromhttps://github.com/openai/openai-javaAvailable
openai-azure-java-example/src/main/java/com.openai.azure.examples/AzureEntraIDExampleAsync.java
59lines · modecode
| 1 | package com.openai.azure.examples; |
| 2 | |
| 3 | import com.azure.identity.AuthenticationUtil; |
| 4 | import com.azure.identity.DefaultAzureCredentialBuilder; |
| 5 | import com.openai.client.OpenAIClientAsync; |
| 6 | import com.openai.client.okhttp.OpenAIOkHttpClientAsync; |
| 7 | import com.openai.core.JsonValue; |
| 8 | import com.openai.credential.BearerTokenCredential; |
| 9 | import com.openai.models.ChatCompletion; |
| 10 | import com.openai.models.ChatCompletionCreateParams; |
| 11 | import com.openai.models.ChatCompletionMessageParam; |
| 12 | import com.openai.models.ChatCompletionUserMessageParam; |
| 13 | import java.util.List; |
| 14 | |
| 15 | /** |
| 16 | * This example demonstrates how to use the Azure Entra ID to authenticate with the OpenAI API, asynchronously. |
| 17 | */ |
| 18 | public final class AzureEntraIDExampleAsync { |
| 19 | |
| 20 | private AzureEntraIDExampleAsync() {} |
| 21 | |
| 22 | public static void main(String[] args) { |
| 23 | OpenAIOkHttpClientAsync.Builder asyncClientBuilder = OpenAIOkHttpClientAsync.builder(); |
| 24 | |
| 25 | /* Azure-specific code starts here */ |
| 26 | // You can either set 'endpoint' directly in the builder. |
| 27 | // or set the env var "AZURE_OPENAI_ENDPOINT" and use fromEnv() method instead |
| 28 | asyncClientBuilder |
| 29 | .baseUrl(System.getenv("AZURE_OPENAI_ENDPOINT")) |
| 30 | .credential(BearerTokenCredential.create( |
| 31 | AuthenticationUtil.getBearerTokenSupplier( |
| 32 | new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default") |
| 33 | )); |
| 34 | /* Azure-specific code ends here */ |
| 35 | |
| 36 | // All code from this line down is general-purpose OpenAI code |
| 37 | OpenAIClientAsync asyncClient = asyncClientBuilder.build(); |
| 38 | |
| 39 | ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() |
| 40 | .addMessage(ChatCompletionMessageParam.ofChatCompletionUserMessageParam( |
| 41 | ChatCompletionUserMessageParam.builder() |
| 42 | .role(ChatCompletionUserMessageParam.Role.USER) |
| 43 | .content(ChatCompletionUserMessageParam.Content.ofTextContent("Who won the world series in 2020?")) |
| 44 | .build())) |
| 45 | .model("gpt-4o") |
| 46 | .build(); |
| 47 | |
| 48 | ChatCompletion chatCompletion = |
| 49 | asyncClient.chat().completions().create(params).join(); |
| 50 | |
| 51 | List<ChatCompletion.Choice> choices = chatCompletion.choices(); |
| 52 | for (ChatCompletion.Choice choice : choices) { |
| 53 | System.out.println("Choice content: " + choice.message().content().get()); |
| 54 | } |
| 55 | |
| 56 | JsonValue filterResult = chatCompletion._additionalProperties().get("prompt_filter_results"); |
| 57 | System.out.println("Content filter results: " + filterResult); |
| 58 | } |
| 59 | } |
| 60 | |