From 0c6a820c48d75880398dc3e393ade43d9ebbb0a6 Mon Sep 17 00:00:00 2001 From: Shawn Fang Date: Tue, 19 Nov 2024 17:08:05 -0800 Subject: [PATCH 1/2] Added HttpRequest.Builder extension methods --- .../azure/HttpRequestBuilderExtensions.kt | 27 +++++++++++++++++++ .../async/CompletionServiceAsyncImpl.kt | 4 +++ .../async/EmbeddingServiceAsyncImpl.kt | 4 +++ .../services/async/ImageServiceAsyncImpl.kt | 4 +++ .../async/chat/CompletionServiceAsyncImpl.kt | 19 +++---------- .../blocking/CompletionServiceImpl.kt | 4 +++ .../services/blocking/EmbeddingServiceImpl.kt | 4 +++ .../services/blocking/ImageServiceImpl.kt | 4 +++ .../blocking/chat/CompletionServiceImpl.kt | 19 +++---------- 9 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 openai-java-core/src/main/kotlin/com/openai/azure/HttpRequestBuilderExtensions.kt 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 4c5ed7df3..9801b1a6c 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.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("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 17ce15ddf..17f3472ae 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.RequestOptions import com.openai.core.handlers.errorHandler @@ -10,9 +12,7 @@ import com.openai.core.handlers.withErrorHandler import com.openai.core.http.HttpMethod import com.openai.core.http.HttpRequest import com.openai.core.http.HttpResponse.Handler -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.ChatCompletionCreateParams @@ -41,23 +41,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() From 2bb4e6fcdd09cfab23c82149c391eaffd60c4fc6 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Thu, 21 Nov 2024 12:00:58 +0000 Subject: [PATCH 2/2] remove unused imports --- .../openai/services/async/chat/CompletionServiceAsyncImpl.kt | 2 -- .../com/openai/services/blocking/chat/CompletionServiceImpl.kt | 2 -- 2 files changed, 4 deletions(-) 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 344647a41..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 @@ -19,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 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 87c53563c..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 @@ -17,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