Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding ktor engine config to support Kotlin Scripting #261

Merged
merged 1 commit into from
Nov 16, 2023
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
Expand Up @@ -4,6 +4,7 @@ import com.aallam.openai.api.http.Timeout
import com.aallam.openai.api.logging.LogLevel
import com.aallam.openai.api.logging.Logger
import io.ktor.client.HttpClientConfig
import io.ktor.client.engine.HttpClientEngine
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

Expand All @@ -19,6 +20,7 @@ import kotlin.time.Duration.Companion.seconds
* @param proxy HTTP proxy url
* @param host OpenAI host configuration.
* @param retry rate limit retry configuration
* @param engine explicit ktor engine for http requests.
* @param httpClientConfig additional custom client configuration
*/
public class OpenAIConfig(
Expand All @@ -30,6 +32,7 @@ public class OpenAIConfig(
public val host: OpenAIHost = OpenAIHost.OpenAI,
public val proxy: ProxyConfig? = null,
public val retry: RetryStrategy = RetryStrategy(),
public val engine: HttpClientEngine? = null,
public val httpClientConfig: HttpClientConfig<*>.() -> Unit = {}
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.time.DurationUnit
* Default Http Client.
*/
internal fun createHttpClient(config: OpenAIConfig): HttpClient {
return HttpClient {
val configuration: HttpClientConfig<*>.() -> Unit = {
engine {
config.proxy?.let { proxyConfig ->
proxy = when (proxyConfig) {
Expand Down Expand Up @@ -82,6 +82,12 @@ internal fun createHttpClient(config: OpenAIConfig): HttpClient {

config.httpClientConfig(this)
}

return if(config.engine != null) {
HttpClient(config.engine, configuration)
} else {
HttpClient(configuration)
}
}

/**
Expand Down