diff --git a/openai-java-core/src/main/kotlin/com/openai/azure/HttpRequestBuilderExtensions.kt b/openai-java-core/src/main/kotlin/com/openai/azure/HttpRequestBuilderExtensions.kt new file mode 100644 index 000000000..c6b7ff449 --- /dev/null +++ b/openai-java-core/src/main/kotlin/com/openai/azure/HttpRequestBuilderExtensions.kt @@ -0,0 +1,27 @@ +package com.openai.azure + +import com.openai.core.ClientOptions +import com.openai.core.http.HttpRequest +import com.openai.core.isAzureEndpoint +import com.openai.credential.BearerTokenCredential + +@JvmSynthetic +internal fun HttpRequest.Builder.addPathSegmentsForAzure( + clientOptions: ClientOptions, + deploymentModel: String +): HttpRequest.Builder = apply { + if (isAzureEndpoint(clientOptions.baseUrl)) { + addPathSegments("openai", "deployments", deploymentModel) + } +} + +@JvmSynthetic +internal fun HttpRequest.Builder.replaceBearerTokenForAzure( + clientOptions: ClientOptions +): HttpRequest.Builder = apply { + if ( + isAzureEndpoint(clientOptions.baseUrl) && clientOptions.credential is BearerTokenCredential + ) { + replaceHeaders("Authorization", "Bearer ${clientOptions.credential.token()}") + } +} diff --git a/openai-java-core/src/main/kotlin/com/openai/services/async/CompletionServiceAsyncImpl.kt b/openai-java-core/src/main/kotlin/com/openai/services/async/CompletionServiceAsyncImpl.kt index d9a137955..53c0e8f54 100644 --- a/openai-java-core/src/main/kotlin/com/openai/services/async/CompletionServiceAsyncImpl.kt +++ b/openai-java-core/src/main/kotlin/com/openai/services/async/CompletionServiceAsyncImpl.kt @@ -2,6 +2,8 @@ package com.openai.services.async +import com.openai.azure.addPathSegmentsForAzure +import com.openai.azure.replaceBearerTokenForAzure import com.openai.core.ClientOptions import com.openai.core.JsonValue import com.openai.core.RequestOptions @@ -41,10 +43,12 @@ constructor( val request = HttpRequest.builder() .method(HttpMethod.POST) + .addPathSegmentsForAzure(clientOptions, params.model().toString()) .addPathSegments("completions") .putAllQueryParams(clientOptions.queryParams) .replaceAllQueryParams(params.getQueryParams()) .putAllHeaders(clientOptions.headers) + .replaceBearerTokenForAzure(clientOptions) .replaceAllHeaders(params.getHeaders()) .body(json(clientOptions.jsonMapper, params.getBody())) .build() diff --git a/openai-java-core/src/main/kotlin/com/openai/services/async/EmbeddingServiceAsyncImpl.kt b/openai-java-core/src/main/kotlin/com/openai/services/async/EmbeddingServiceAsyncImpl.kt index 604d07d01..b47a66b9d 100644 --- a/openai-java-core/src/main/kotlin/com/openai/services/async/EmbeddingServiceAsyncImpl.kt +++ b/openai-java-core/src/main/kotlin/com/openai/services/async/EmbeddingServiceAsyncImpl.kt @@ -2,6 +2,8 @@ package com.openai.services.async +import com.openai.azure.addPathSegmentsForAzure +import com.openai.azure.replaceBearerTokenForAzure import com.openai.core.ClientOptions import com.openai.core.RequestOptions import com.openai.core.handlers.errorHandler @@ -35,10 +37,12 @@ constructor( val request = HttpRequest.builder() .method(HttpMethod.POST) + .addPathSegmentsForAzure(clientOptions, params.model().toString()) .addPathSegments("embeddings") .putAllQueryParams(clientOptions.queryParams) .replaceAllQueryParams(params.getQueryParams()) .putAllHeaders(clientOptions.headers) + .replaceBearerTokenForAzure(clientOptions) .replaceAllHeaders(params.getHeaders()) .body(json(clientOptions.jsonMapper, params.getBody())) .build() diff --git a/openai-java-core/src/main/kotlin/com/openai/services/async/ImageServiceAsyncImpl.kt b/openai-java-core/src/main/kotlin/com/openai/services/async/ImageServiceAsyncImpl.kt index 70f5d82e9..968cae2f8 100644 --- a/openai-java-core/src/main/kotlin/com/openai/services/async/ImageServiceAsyncImpl.kt +++ b/openai-java-core/src/main/kotlin/com/openai/services/async/ImageServiceAsyncImpl.kt @@ -2,6 +2,8 @@ package com.openai.services.async +import com.openai.azure.addPathSegmentsForAzure +import com.openai.azure.replaceBearerTokenForAzure import com.openai.core.ClientOptions import com.openai.core.RequestOptions import com.openai.core.handlers.errorHandler @@ -34,10 +36,12 @@ constructor( val request = HttpRequest.builder() .method(HttpMethod.POST) + .addPathSegmentsForAzure(clientOptions, params.model().get().toString()) .addPathSegments("images", "generations") .putAllQueryParams(clientOptions.queryParams) .replaceAllQueryParams(params.getQueryParams()) .putAllHeaders(clientOptions.headers) + .replaceBearerTokenForAzure(clientOptions) .replaceAllHeaders(params.getHeaders()) .body(json(clientOptions.jsonMapper, params.getBody())) .build() diff --git a/openai-java-core/src/main/kotlin/com/openai/services/async/chat/CompletionServiceAsyncImpl.kt b/openai-java-core/src/main/kotlin/com/openai/services/async/chat/CompletionServiceAsyncImpl.kt index 1dfe0da6a..e77f64327 100644 --- a/openai-java-core/src/main/kotlin/com/openai/services/async/chat/CompletionServiceAsyncImpl.kt +++ b/openai-java-core/src/main/kotlin/com/openai/services/async/chat/CompletionServiceAsyncImpl.kt @@ -2,6 +2,8 @@ package com.openai.services.async.chat +import com.openai.azure.addPathSegmentsForAzure +import com.openai.azure.replaceBearerTokenForAzure import com.openai.core.ClientOptions import com.openai.core.JsonValue import com.openai.core.RequestOptions @@ -17,9 +19,7 @@ import com.openai.core.http.HttpRequest import com.openai.core.http.HttpResponse.Handler import com.openai.core.http.StreamResponse import com.openai.core.http.toAsync -import com.openai.core.isAzureEndpoint import com.openai.core.json -import com.openai.credential.BearerTokenCredential import com.openai.errors.OpenAIError import com.openai.models.ChatCompletion import com.openai.models.ChatCompletionChunk @@ -49,23 +49,12 @@ constructor( val request = HttpRequest.builder() .method(HttpMethod.POST) - .apply { - if (isAzureEndpoint(clientOptions.baseUrl)) { - addPathSegments("openai", "deployments", params.model().toString()) - } - } + .addPathSegmentsForAzure(clientOptions, params.model().toString()) .addPathSegments("chat", "completions") .putAllQueryParams(clientOptions.queryParams) .replaceAllQueryParams(params.getQueryParams()) .putAllHeaders(clientOptions.headers) - .apply { - if ( - isAzureEndpoint(clientOptions.baseUrl) && - clientOptions.credential is BearerTokenCredential - ) { - putHeader("Authorization", "Bearer ${clientOptions.credential.token()}") - } - } + .replaceBearerTokenForAzure(clientOptions) .replaceAllHeaders(params.getHeaders()) .body(json(clientOptions.jsonMapper, params.getBody())) .build() diff --git a/openai-java-core/src/main/kotlin/com/openai/services/blocking/CompletionServiceImpl.kt b/openai-java-core/src/main/kotlin/com/openai/services/blocking/CompletionServiceImpl.kt index 6dea93341..a839a439d 100644 --- a/openai-java-core/src/main/kotlin/com/openai/services/blocking/CompletionServiceImpl.kt +++ b/openai-java-core/src/main/kotlin/com/openai/services/blocking/CompletionServiceImpl.kt @@ -2,6 +2,8 @@ package com.openai.services.blocking +import com.openai.azure.addPathSegmentsForAzure +import com.openai.azure.replaceBearerTokenForAzure import com.openai.core.ClientOptions import com.openai.core.JsonValue import com.openai.core.RequestOptions @@ -38,10 +40,12 @@ constructor( val request = HttpRequest.builder() .method(HttpMethod.POST) + .addPathSegmentsForAzure(clientOptions, params.model().toString()) .addPathSegments("completions") .putAllQueryParams(clientOptions.queryParams) .replaceAllQueryParams(params.getQueryParams()) .putAllHeaders(clientOptions.headers) + .replaceBearerTokenForAzure(clientOptions) .replaceAllHeaders(params.getHeaders()) .body(json(clientOptions.jsonMapper, params.getBody())) .build() diff --git a/openai-java-core/src/main/kotlin/com/openai/services/blocking/EmbeddingServiceImpl.kt b/openai-java-core/src/main/kotlin/com/openai/services/blocking/EmbeddingServiceImpl.kt index a06f7ddd9..8ae7f5fa0 100644 --- a/openai-java-core/src/main/kotlin/com/openai/services/blocking/EmbeddingServiceImpl.kt +++ b/openai-java-core/src/main/kotlin/com/openai/services/blocking/EmbeddingServiceImpl.kt @@ -2,6 +2,8 @@ package com.openai.services.blocking +import com.openai.azure.addPathSegmentsForAzure +import com.openai.azure.replaceBearerTokenForAzure import com.openai.core.ClientOptions import com.openai.core.RequestOptions import com.openai.core.handlers.errorHandler @@ -34,10 +36,12 @@ constructor( val request = HttpRequest.builder() .method(HttpMethod.POST) + .addPathSegmentsForAzure(clientOptions, params.model().toString()) .addPathSegments("embeddings") .putAllQueryParams(clientOptions.queryParams) .replaceAllQueryParams(params.getQueryParams()) .putAllHeaders(clientOptions.headers) + .replaceBearerTokenForAzure(clientOptions) .replaceAllHeaders(params.getHeaders()) .body(json(clientOptions.jsonMapper, params.getBody())) .build() diff --git a/openai-java-core/src/main/kotlin/com/openai/services/blocking/ImageServiceImpl.kt b/openai-java-core/src/main/kotlin/com/openai/services/blocking/ImageServiceImpl.kt index 31c03cfe7..8e4dcd1eb 100644 --- a/openai-java-core/src/main/kotlin/com/openai/services/blocking/ImageServiceImpl.kt +++ b/openai-java-core/src/main/kotlin/com/openai/services/blocking/ImageServiceImpl.kt @@ -2,6 +2,8 @@ package com.openai.services.blocking +import com.openai.azure.addPathSegmentsForAzure +import com.openai.azure.replaceBearerTokenForAzure import com.openai.core.ClientOptions import com.openai.core.RequestOptions import com.openai.core.handlers.errorHandler @@ -33,10 +35,12 @@ constructor( val request = HttpRequest.builder() .method(HttpMethod.POST) + .addPathSegmentsForAzure(clientOptions, params.model().get().toString()) .addPathSegments("images", "generations") .putAllQueryParams(clientOptions.queryParams) .replaceAllQueryParams(params.getQueryParams()) .putAllHeaders(clientOptions.headers) + .replaceBearerTokenForAzure(clientOptions) .replaceAllHeaders(params.getHeaders()) .body(json(clientOptions.jsonMapper, params.getBody())) .build() diff --git a/openai-java-core/src/main/kotlin/com/openai/services/blocking/chat/CompletionServiceImpl.kt b/openai-java-core/src/main/kotlin/com/openai/services/blocking/chat/CompletionServiceImpl.kt index f6ea7c91f..76a469169 100644 --- a/openai-java-core/src/main/kotlin/com/openai/services/blocking/chat/CompletionServiceImpl.kt +++ b/openai-java-core/src/main/kotlin/com/openai/services/blocking/chat/CompletionServiceImpl.kt @@ -2,6 +2,8 @@ package com.openai.services.blocking.chat +import com.openai.azure.addPathSegmentsForAzure +import com.openai.azure.replaceBearerTokenForAzure import com.openai.core.ClientOptions import com.openai.core.JsonValue import com.openai.core.RequestOptions @@ -15,9 +17,7 @@ import com.openai.core.http.HttpMethod import com.openai.core.http.HttpRequest import com.openai.core.http.HttpResponse.Handler import com.openai.core.http.StreamResponse -import com.openai.core.isAzureEndpoint import com.openai.core.json -import com.openai.credential.BearerTokenCredential import com.openai.errors.OpenAIError import com.openai.models.ChatCompletion import com.openai.models.ChatCompletionChunk @@ -46,23 +46,12 @@ constructor( val request = HttpRequest.builder() .method(HttpMethod.POST) - .apply { - if (isAzureEndpoint(clientOptions.baseUrl)) { - addPathSegments("openai", "deployments", params.model().toString()) - } - } + .addPathSegmentsForAzure(clientOptions, params.model().toString()) .addPathSegments("chat", "completions") .putAllQueryParams(clientOptions.queryParams) .replaceAllQueryParams(params.getQueryParams()) .putAllHeaders(clientOptions.headers) - .apply { - if ( - isAzureEndpoint(clientOptions.baseUrl) && - clientOptions.credential is BearerTokenCredential - ) { - putHeader("Authorization", "Bearer ${clientOptions.credential.token()}") - } - } + .replaceBearerTokenForAzure(clientOptions) .replaceAllHeaders(params.getHeaders()) .body(json(clientOptions.jsonMapper, params.getBody())) .build()