Skip to content

Commit 8ec229b

Browse files
TomerAberbachstainless-app[bot]
authored andcommitted
docs: more examples and cleanup (#159)
* docs: more examples and cleanup * chore: change mainClass * docs: async example updates * docs: add some todos * docs: model list example * docs: structured outputs * docs: add small comment * chore: delete not needed dir
1 parent dabb60c commit 8ec229b

23 files changed

+667
-308
lines changed

README.md

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ This library requires Java 8 or later.
4646

4747
## Usage
4848

49+
See the [`openai-java-example`](openai-java-example/src/main/java/com/openai/example) directory for complete and runnable examples.
50+
4951
### Configure the client
5052

5153
Use `OpenAIOkHttpClient.builder()` to configure the client. At a minimum you need to set `.apiKey()`:
@@ -290,41 +292,19 @@ This library throws exceptions in a single hierarchy for easy handling:
290292
## Microsoft Azure OpenAI
291293

292294
To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview), use the same
293-
OpenAI client builder but with the Azure-specific configuration.
295+
OpenAI client builder but with the Azure-specific configuration.
294296

295297
```java
296-
OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder();
297-
298-
/* Azure-specific code starts here */
299-
// You can either set 'endpoint' directly in the builder.
300-
// or set the env var "AZURE_OPENAI_ENDPOINT" and use fromEnv() method instead
301-
clientBuilder
302-
.baseUrl(System.getenv("AZURE_OPENAI_ENDPOINT"))
303-
.credential(BearerTokenCredential.create(
304-
AuthenticationUtil.getBearerTokenSupplier(
305-
new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")
306-
));
307-
/* Azure-specific code ends here */
308-
309-
OpenAIClient client = clientBuilder.build();
310-
311-
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
312-
.addMessage(ChatCompletionMessageParam.ofChatCompletionUserMessageParam(
313-
ChatCompletionUserMessageParam.builder()
314-
.content(ChatCompletionUserMessageParam.Content.ofTextContent("Who won the world series in 2020?"))
315-
.build()))
316-
.model("gpt-4o")
317-
.build();
318-
319-
ChatCompletion chatCompletion = client.chat().completions().create(params);
320-
321-
List<ChatCompletion.Choice> choices = chatCompletion.choices();
322-
for (ChatCompletion.Choice choice : choices) {
323-
System.out.println("Choice content: " + choice.message().content().get());
324-
}
298+
OpenAIClient client = OpenAIOkHttpClient.builder()
299+
// Gets the API key from the `AZURE_OPENAI_KEY` environment variable
300+
.fromEnv()
301+
// Set the Azure Entra ID
302+
.credential(BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier(
303+
new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")))
304+
.build();
325305
```
326306

327-
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+
See the complete Azure OpenAI example in the [`openai-java-example`](openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java) directory. The other examples in the directory also work with Azure as long as the client is configured to use it.
328308

329309
## Network options
330310

examples/.keep

Lines changed: 0 additions & 4 deletions
This file was deleted.

openai-azure-java-example/build.gradle.kts

Lines changed: 0 additions & 12 deletions
This file was deleted.

openai-azure-java-example/src/main/java/com.openai.azure.examples/AzureApiKeyExample.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

openai-azure-java-example/src/main/java/com.openai.azure.examples/AzureApiKeyExampleAsync.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

openai-azure-java-example/src/main/java/com.openai.azure.examples/AzureEntraIDExample.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

openai-azure-java-example/src/main/java/com.openai.azure.examples/AzureEntraIDExampleAsync.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

openai-java-example/build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ plugins {
66

77
dependencies {
88
implementation(project(":openai-java"))
9+
api("com.azure:azure-identity:1.15.0")
910
}
1011

1112
tasks.withType<JavaCompile>().configureEach {
1213
// Allow using more modern APIs, like `List.of` and `Map.of`, in examples.
13-
options.release.set(9)
14+
options.release.set(11)
1415
}
1516

1617
application {
17-
mainClass = "com.openai.example.Main"
18+
mainClass = "com.openai.example.CompletionsExample"
1819
}

0 commit comments

Comments
 (0)