diff --git a/README.md b/README.md index 6e342167c..95db45a48 100644 --- a/README.md +++ b/README.md @@ -265,6 +265,46 @@ This library throws exceptions in a single hierarchy for easy handling: - We failed to serialize the request body - We failed to parse the response body (has access to response code and body) +## Microsoft Azure OpenAI + +To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview), use the same +OpenAI client builder but with the Azure-specific configuration. + +```java +OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder(); + +/* Azure-specific code starts here */ +// You can either set 'endpoint' directly in the builder. +// or set the env var "AZURE_OPENAI_ENDPOINT" and use fromEnv() method instead +clientBuilder + .baseUrl(System.getenv("AZURE_OPENAI_ENDPOINT")) + .credential(BearerTokenCredential.create( + AuthenticationUtil.getBearerTokenSupplier( + new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default") + )); +/* Azure-specific code ends here */ + +OpenAIClient client = clientBuilder.build(); + +ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() + .addMessage(ChatCompletionMessageParam.ofChatCompletionUserMessageParam( + ChatCompletionUserMessageParam.builder() + .role(ChatCompletionUserMessageParam.Role.USER) + .content(ChatCompletionUserMessageParam.Content.ofTextContent("Who won the world series in 2020?")) + .build())) + .model("gpt-4o") + .build(); + +ChatCompletion chatCompletion = client.chat().completions().create(params); + +List choices = chatCompletion.choices(); +for (ChatCompletion.Choice choice : choices) { + System.out.println("Choice content: " + choice.message().content().get()); +} +``` + +See the complete Azure OpenAI examples in the [Azure OpenAI example](https://github.com/openai/openai-java/tree/next/openai-azure-java-example/src/main/java/com.openai.azure.examples). + ## Network options ### Retries