Skip to content

Commit 1e4f061

Browse files
committed
add more new Azure service version and update azure samples
1 parent 054403d commit 1e4f061

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

openai-java-core/src/main/kotlin/com/openai/azure/AzureOpenAIServiceVersion.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class AzureOpenAIServiceVersion private constructor(@get:JvmName("value") val va
1616
@JvmStatic val V2023_05_15 = fromString("2023-05-15")
1717
@JvmStatic val V2024_02_01 = fromString("2024-02-01")
1818
@JvmStatic val V2024_06_01 = fromString("2024-06-01")
19+
@JvmStatic val V2024_10_21 = fromString("2024-10-21")
1920
@JvmStatic val V2023_06_01_PREVIEW = fromString("2023-06-01-preview")
2021
@JvmStatic val V2023_07_01_PREVIEW = fromString("2023-07-01-preview")
2122
@JvmStatic val V2024_02_15_PREVIEW = fromString("2024-02-15-preview")
@@ -25,6 +26,9 @@ class AzureOpenAIServiceVersion private constructor(@get:JvmName("value") val va
2526
@JvmStatic val V2024_07_01_PREVIEW = fromString("2024-07-01-preview")
2627
@JvmStatic val V2024_08_01_PREVIEW = fromString("2024-08-01-preview")
2728
@JvmStatic val V2024_09_01_PREVIEW = fromString("2024-09-01-preview")
29+
@JvmStatic val V2024_10_01_PREVIEW = fromString("2024-10-01-preview")
30+
@JvmStatic val V2024_12_01_PREVIEW = fromString("2024-12-01-preview")
31+
@JvmStatic val V2025_01_01_PREVIEW = fromString("2025-01-01-preview")
2832
}
2933

3034
override fun equals(other: Any?): Boolean =

openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package com.openai.core
44

55
import com.fasterxml.jackson.databind.json.JsonMapper
66
import com.openai.azure.AzureOpenAIServiceVersion
7-
import com.openai.azure.AzureOpenAIServiceVersion.Companion.V2024_06_01
7+
import com.openai.azure.AzureOpenAIServiceVersion.Companion.V2024_10_21
88
import com.openai.azure.credential.AzureApiKeyCredential
99
import com.openai.core.http.Headers
1010
import com.openai.core.http.HttpClient
@@ -262,7 +262,7 @@ private constructor(
262262
// Default Azure OpenAI version is used if Azure user doesn't
263263
// specific a service API version in 'queryParams'.
264264
// We can update the default value every major announcement if needed.
265-
replaceQueryParams("api-version", (azureServiceVersion ?: V2024_06_01).value)
265+
replaceQueryParams("api-version", (azureServiceVersion ?: V2024_10_21).value)
266266
}
267267

268268
headers.replaceAll(this.headers.build())
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.openai.example;
2+
3+
import com.openai.azure.credential.AzureApiKeyCredential;
4+
import com.openai.client.OpenAIClient;
5+
import com.openai.client.okhttp.OpenAIOkHttpClient;
6+
import com.openai.models.ChatCompletionCreateParams;
7+
import com.openai.models.ChatModel;
8+
9+
public final class AzureApiKeyExample {
10+
private AzureApiKeyExample() {}
11+
12+
public static void main(String[] args) {
13+
OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder();
14+
15+
/* Azure-specific code starts here */
16+
// You can either set 'endpoint' or 'apiKey' directly in the builder.
17+
// or set same two env vars and use fromEnv() method instead
18+
clientBuilder
19+
.baseUrl(System.getenv("AZURE_OPENAI_ENDPOINT"))
20+
.credential(AzureApiKeyCredential.create(System.getenv("AZURE_OPENAI_KEY")));
21+
/* Azure-specific code ends here */
22+
23+
// All code from this line down is general-purpose OpenAI code
24+
OpenAIClient client = clientBuilder.build();
25+
26+
ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
27+
.model(ChatModel.GPT_4O)
28+
.maxCompletionTokens(2048)
29+
.addDeveloperMessage("Make sure you mention Stainless!")
30+
.addUserMessage("Tell me a story about building the best SDK!")
31+
.build();
32+
33+
client.chat().completions().create(createParams).choices().stream()
34+
.flatMap(choice -> choice.message().content().stream())
35+
.forEach(System.out::println);
36+
}
37+
}

openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,22 @@ public final class AzureEntraIdExample {
1212
private AzureEntraIdExample() {}
1313

1414
public static void main(String[] args) {
15-
OpenAIClient client = OpenAIOkHttpClient.builder()
16-
// Gets the API key from the `AZURE_OPENAI_KEY` environment variable
17-
.fromEnv()
15+
OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder();
16+
17+
/* Azure-specific code starts here */
18+
// You can either set 'endpoint' directly in the builder.
19+
// or set the env var "AZURE_OPENAI_ENDPOINT" and use fromEnv() method instead
20+
clientBuilder
21+
.baseUrl(System.getenv("AZURE_OPENAI_ENDPOINT"))
1822
// Set the Azure Entra ID
1923
.credential(BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier(
20-
new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")))
21-
.build();
24+
new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")));
25+
/* Azure-specific code ends here */
26+
27+
OpenAIClient client = clientBuilder.build();
2228

2329
ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
24-
.model(ChatModel.GPT_3_5_TURBO)
30+
.model(ChatModel.GPT_4O)
2531
.maxCompletionTokens(2048)
2632
.addDeveloperMessage("Make sure you mention Stainless!")
2733
.addUserMessage("Tell me a story about building the best SDK!")

0 commit comments

Comments
 (0)