From 1e4f06164802a026e4f44e1def83700b6b9dff07 Mon Sep 17 00:00:00 2001 From: Shawn Fang Date: Wed, 5 Feb 2025 15:24:27 -0800 Subject: [PATCH 1/2] add more new Azure service version and update azure samples --- .../openai/azure/AzureOpenAIServiceVersion.kt | 4 ++ .../kotlin/com/openai/core/ClientOptions.kt | 4 +- .../openai/example/AzureApiKeyExample.java | 37 +++++++++++++++++++ .../openai/example/AzureEntraIdExample.java | 18 ++++++--- 4 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 openai-java-example/src/main/java/com/openai/example/AzureApiKeyExample.java diff --git a/openai-java-core/src/main/kotlin/com/openai/azure/AzureOpenAIServiceVersion.kt b/openai-java-core/src/main/kotlin/com/openai/azure/AzureOpenAIServiceVersion.kt index 1188d2fbd..d12665c40 100644 --- a/openai-java-core/src/main/kotlin/com/openai/azure/AzureOpenAIServiceVersion.kt +++ b/openai-java-core/src/main/kotlin/com/openai/azure/AzureOpenAIServiceVersion.kt @@ -16,6 +16,7 @@ class AzureOpenAIServiceVersion private constructor(@get:JvmName("value") val va @JvmStatic val V2023_05_15 = fromString("2023-05-15") @JvmStatic val V2024_02_01 = fromString("2024-02-01") @JvmStatic val V2024_06_01 = fromString("2024-06-01") + @JvmStatic val V2024_10_21 = fromString("2024-10-21") @JvmStatic val V2023_06_01_PREVIEW = fromString("2023-06-01-preview") @JvmStatic val V2023_07_01_PREVIEW = fromString("2023-07-01-preview") @JvmStatic val V2024_02_15_PREVIEW = fromString("2024-02-15-preview") @@ -25,6 +26,9 @@ class AzureOpenAIServiceVersion private constructor(@get:JvmName("value") val va @JvmStatic val V2024_07_01_PREVIEW = fromString("2024-07-01-preview") @JvmStatic val V2024_08_01_PREVIEW = fromString("2024-08-01-preview") @JvmStatic val V2024_09_01_PREVIEW = fromString("2024-09-01-preview") + @JvmStatic val V2024_10_01_PREVIEW = fromString("2024-10-01-preview") + @JvmStatic val V2024_12_01_PREVIEW = fromString("2024-12-01-preview") + @JvmStatic val V2025_01_01_PREVIEW = fromString("2025-01-01-preview") } override fun equals(other: Any?): Boolean = diff --git a/openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt b/openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt index d23b8222f..4aff18a91 100644 --- a/openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt +++ b/openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt @@ -4,7 +4,7 @@ package com.openai.core import com.fasterxml.jackson.databind.json.JsonMapper import com.openai.azure.AzureOpenAIServiceVersion -import com.openai.azure.AzureOpenAIServiceVersion.Companion.V2024_06_01 +import com.openai.azure.AzureOpenAIServiceVersion.Companion.V2024_10_21 import com.openai.azure.credential.AzureApiKeyCredential import com.openai.core.http.Headers import com.openai.core.http.HttpClient @@ -262,7 +262,7 @@ private constructor( // Default Azure OpenAI version is used if Azure user doesn't // specific a service API version in 'queryParams'. // We can update the default value every major announcement if needed. - replaceQueryParams("api-version", (azureServiceVersion ?: V2024_06_01).value) + replaceQueryParams("api-version", (azureServiceVersion ?: V2024_10_21).value) } headers.replaceAll(this.headers.build()) diff --git a/openai-java-example/src/main/java/com/openai/example/AzureApiKeyExample.java b/openai-java-example/src/main/java/com/openai/example/AzureApiKeyExample.java new file mode 100644 index 000000000..1be76b13c --- /dev/null +++ b/openai-java-example/src/main/java/com/openai/example/AzureApiKeyExample.java @@ -0,0 +1,37 @@ +package com.openai.example; + +import com.openai.azure.credential.AzureApiKeyCredential; +import com.openai.client.OpenAIClient; +import com.openai.client.okhttp.OpenAIOkHttpClient; +import com.openai.models.ChatCompletionCreateParams; +import com.openai.models.ChatModel; + +public final class AzureApiKeyExample { + private AzureApiKeyExample() {} + + public static void main(String[] args) { + OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder(); + + /* Azure-specific code starts here */ + // You can either set 'endpoint' or 'apiKey' directly in the builder. + // or set same two env vars and use fromEnv() method instead + clientBuilder + .baseUrl(System.getenv("AZURE_OPENAI_ENDPOINT")) + .credential(AzureApiKeyCredential.create(System.getenv("AZURE_OPENAI_KEY"))); + /* Azure-specific code ends here */ + + // All code from this line down is general-purpose OpenAI code + OpenAIClient client = clientBuilder.build(); + + ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder() + .model(ChatModel.GPT_4O) + .maxCompletionTokens(2048) + .addDeveloperMessage("Make sure you mention Stainless!") + .addUserMessage("Tell me a story about building the best SDK!") + .build(); + + client.chat().completions().create(createParams).choices().stream() + .flatMap(choice -> choice.message().content().stream()) + .forEach(System.out::println); + } +} diff --git a/openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java b/openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java index a964e1e1c..c9db3fb3b 100644 --- a/openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java +++ b/openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java @@ -12,16 +12,22 @@ public final class AzureEntraIdExample { private AzureEntraIdExample() {} public static void main(String[] args) { - OpenAIClient client = OpenAIOkHttpClient.builder() - // Gets the API key from the `AZURE_OPENAI_KEY` environment variable - .fromEnv() + 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")) // Set the Azure Entra ID .credential(BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( - new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default"))) - .build(); + new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default"))); + /* Azure-specific code ends here */ + + OpenAIClient client = clientBuilder.build(); ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder() - .model(ChatModel.GPT_3_5_TURBO) + .model(ChatModel.GPT_4O) .maxCompletionTokens(2048) .addDeveloperMessage("Make sure you mention Stainless!") .addUserMessage("Tell me a story about building the best SDK!") From c9c8c269eac3faa296b1676692cf37f10c25ac0a Mon Sep 17 00:00:00 2001 From: Shawn Fang Date: Thu, 6 Feb 2025 09:53:57 -0800 Subject: [PATCH 2/2] revert samples changes --- .../openai/example/AzureApiKeyExample.java | 37 ------------------- .../openai/example/AzureEntraIdExample.java | 18 +++------ 2 files changed, 6 insertions(+), 49 deletions(-) delete mode 100644 openai-java-example/src/main/java/com/openai/example/AzureApiKeyExample.java diff --git a/openai-java-example/src/main/java/com/openai/example/AzureApiKeyExample.java b/openai-java-example/src/main/java/com/openai/example/AzureApiKeyExample.java deleted file mode 100644 index 1be76b13c..000000000 --- a/openai-java-example/src/main/java/com/openai/example/AzureApiKeyExample.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.openai.example; - -import com.openai.azure.credential.AzureApiKeyCredential; -import com.openai.client.OpenAIClient; -import com.openai.client.okhttp.OpenAIOkHttpClient; -import com.openai.models.ChatCompletionCreateParams; -import com.openai.models.ChatModel; - -public final class AzureApiKeyExample { - private AzureApiKeyExample() {} - - public static void main(String[] args) { - OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder(); - - /* Azure-specific code starts here */ - // You can either set 'endpoint' or 'apiKey' directly in the builder. - // or set same two env vars and use fromEnv() method instead - clientBuilder - .baseUrl(System.getenv("AZURE_OPENAI_ENDPOINT")) - .credential(AzureApiKeyCredential.create(System.getenv("AZURE_OPENAI_KEY"))); - /* Azure-specific code ends here */ - - // All code from this line down is general-purpose OpenAI code - OpenAIClient client = clientBuilder.build(); - - ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder() - .model(ChatModel.GPT_4O) - .maxCompletionTokens(2048) - .addDeveloperMessage("Make sure you mention Stainless!") - .addUserMessage("Tell me a story about building the best SDK!") - .build(); - - client.chat().completions().create(createParams).choices().stream() - .flatMap(choice -> choice.message().content().stream()) - .forEach(System.out::println); - } -} diff --git a/openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java b/openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java index c9db3fb3b..a964e1e1c 100644 --- a/openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java +++ b/openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java @@ -12,22 +12,16 @@ public final class AzureEntraIdExample { private AzureEntraIdExample() {} public static void main(String[] args) { - 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")) + OpenAIClient client = OpenAIOkHttpClient.builder() + // Gets the API key from the `AZURE_OPENAI_KEY` environment variable + .fromEnv() // Set the Azure Entra ID .credential(BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( - new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default"))); - /* Azure-specific code ends here */ - - OpenAIClient client = clientBuilder.build(); + new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default"))) + .build(); ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder() - .model(ChatModel.GPT_4O) + .model(ChatModel.GPT_3_5_TURBO) .maxCompletionTokens(2048) .addDeveloperMessage("Make sure you mention Stainless!") .addUserMessage("Tell me a story about building the best SDK!")