Skip to content

Commit

Permalink
add custom host config (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam authored Mar 2, 2023
1 parent 0345e53 commit 7326a24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlin.time.Duration.Companion.seconds
* @param timeout http client timeout
* @param headers extra http headers
* @param organization OpenAI organization ID
* @param host OpenAI host configuration.
*/
public class OpenAIConfig(
public val token: String,
Expand All @@ -22,4 +23,19 @@ public class OpenAIConfig(
public val timeout: Timeout = Timeout(socket = 30.seconds),
public val organization: String? = null,
public val headers: Map<String, String> = emptyMap(),
public val host: OpenAIHost = OpenAIHost.OpenAI,
)

/**
* OpenAI host configuration.
*/
public class OpenAIHost(
/** Base URL configuration.*/
public val baseUrl: String,
/** Additional query parameters */
public val queryParams: Map<String, String> = emptyMap()
) {
public companion object {
public val OpenAI: OpenAIHost = OpenAIHost("https://api.openai.com")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package com.aallam.openai.client.internal
import com.aallam.openai.client.OpenAIConfig
import com.aallam.openai.client.internal.extension.toKtorLogLevel
import com.aallam.openai.client.internal.extension.toKtorLogger
import io.ktor.client.HttpClient
import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.plugins.auth.Auth
import io.ktor.client.plugins.auth.providers.BearerTokens
import io.ktor.client.plugins.auth.providers.bearer
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.plugins.logging.Logging
import io.ktor.http.ContentType
import io.ktor.http.URLProtocol
import io.ktor.serialization.kotlinx.KotlinxSerializationConverter
import io.ktor.util.appendIfNameAbsent
import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.auth.*
import io.ktor.client.plugins.auth.providers.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.*
import io.ktor.util.*
import kotlinx.serialization.json.Json
import kotlin.time.DurationUnit

Expand Down Expand Up @@ -51,10 +48,8 @@ internal fun createHttpClient(config: OpenAIConfig): HttpClient {
}

defaultRequest {
url {
protocol = URLProtocol.HTTPS
host = "api.openai.com"
}
url(config.host.baseUrl)
config.host.queryParams.onEach { (key, value) -> url.parameters.appendIfNameAbsent(key, value) }
config.organization?.let { organization -> headers.append("OpenAI-Organization", organization) }
config.headers.onEach { (key, value) -> headers.appendIfNameAbsent(key, value) }
}
Expand Down

0 comments on commit 7326a24

Please sign in to comment.