Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down