Skip to content

Commit fd9e9dc

Browse files
authored
docs(readme): add Microsoft Azure section (#17)
1 parent 6ddc67e commit fd9e9dc

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,46 @@ This library throws exceptions in a single hierarchy for easy handling:
265265
- We failed to serialize the request body
266266
- We failed to parse the response body (has access to response code and body)
267267

268+
## Microsoft Azure OpenAI
269+
270+
To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview), use the same
271+
OpenAI client builder but with the Azure-specific configuration.
272+
273+
```java
274+
OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder();
275+
276+
/* Azure-specific code starts here */
277+
// You can either set 'endpoint' directly in the builder.
278+
// or set the env var "AZURE_OPENAI_ENDPOINT" and use fromEnv() method instead
279+
clientBuilder
280+
.baseUrl(System.getenv("AZURE_OPENAI_ENDPOINT"))
281+
.credential(BearerTokenCredential.create(
282+
AuthenticationUtil.getBearerTokenSupplier(
283+
new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")
284+
));
285+
/* Azure-specific code ends here */
286+
287+
OpenAIClient client = clientBuilder.build();
288+
289+
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
290+
.addMessage(ChatCompletionMessageParam.ofChatCompletionUserMessageParam(
291+
ChatCompletionUserMessageParam.builder()
292+
.role(ChatCompletionUserMessageParam.Role.USER)
293+
.content(ChatCompletionUserMessageParam.Content.ofTextContent("Who won the world series in 2020?"))
294+
.build()))
295+
.model("gpt-4o")
296+
.build();
297+
298+
ChatCompletion chatCompletion = client.chat().completions().create(params);
299+
300+
List<ChatCompletion.Choice> choices = chatCompletion.choices();
301+
for (ChatCompletion.Choice choice : choices) {
302+
System.out.println("Choice content: " + choice.message().content().get());
303+
}
304+
```
305+
306+
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).
307+
268308
## Network options
269309

270310
### Retries

0 commit comments

Comments
 (0)