Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -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()}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Loading