From f1701d26fa4f72662c0a5207011ae5e90592817c Mon Sep 17 00:00:00 2001 From: franciscodr Date: Tue, 27 Aug 2024 11:08:05 +0200 Subject: [PATCH] Create an OpenAI config builder --- .../kotlin/com/xebia/functional/xef/Config.kt | 29 +++++++++++++++++++ .../xef/assistants/AssistantStreaming.kt | 2 +- .../services/LocalVectorStoreService.kt | 10 ++++++- .../services/PostgresVectorStoreService.kt | 9 +++++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/Config.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/Config.kt index d76ed4446..f15c7038c 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/Config.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/Config.kt @@ -85,6 +85,35 @@ data class HttpClientTimeoutPolicy( } } +class ConfigBuilder internal constructor(config: Config) { + var baseUrl: String = config.baseUrl + var httpClientRetryPolicy: HttpClientRetryPolicy = config.httpClientRetryPolicy + var httpClientTimeoutPolicy: HttpClientTimeoutPolicy = config.httpClientTimeoutPolicy + var apiToken: String? = config.apiToken + var organization: String? = config.organization + var json: Json = config.json + var streamingPrefix: String = config.streamingPrefix + var streamingDelimiter: String = config.streamingDelimiter + + fun build(): Config = + Config( + baseUrl = baseUrl, + httpClientRetryPolicy = httpClientRetryPolicy, + httpClientTimeoutPolicy = httpClientTimeoutPolicy, + apiToken = apiToken, + organization = organization, + json = json, + streamingPrefix = streamingPrefix, + streamingDelimiter = streamingDelimiter + ) +} + +fun Config(from: Config = Config.Default, builderAction: ConfigBuilder.() -> Unit): Config { + val builder = ConfigBuilder(from) + builder.builderAction() + return builder.build() +} + data class Config( val baseUrl: String, val httpClientRetryPolicy: HttpClientRetryPolicy, diff --git a/examples/src/main/kotlin/com/xebia/functional/xef/assistants/AssistantStreaming.kt b/examples/src/main/kotlin/com/xebia/functional/xef/assistants/AssistantStreaming.kt index 37bf3264e..85885f939 100644 --- a/examples/src/main/kotlin/com/xebia/functional/xef/assistants/AssistantStreaming.kt +++ b/examples/src/main/kotlin/com/xebia/functional/xef/assistants/AssistantStreaming.kt @@ -18,7 +18,7 @@ suspend fun main() { assistantId = "asst_BwQvmWIbGUMDvCuXOtAFH8B6", toolsConfig = listOf(Tool.toolOf(SumTool())) ) - val config = Config.Default.copy(organization = null) + val config = Config { organization = null } val api = OpenAI(config = config, logRequests = false).assistants val thread = AssistantThread(api = api, metric = metric) println("Welcome to the Math tutor, ask me anything about math:") diff --git a/server/src/main/kotlin/com/xebia/functional/xef/server/services/LocalVectorStoreService.kt b/server/src/main/kotlin/com/xebia/functional/xef/server/services/LocalVectorStoreService.kt index 068fe47d0..1964d0f3f 100644 --- a/server/src/main/kotlin/com/xebia/functional/xef/server/services/LocalVectorStoreService.kt +++ b/server/src/main/kotlin/com/xebia/functional/xef/server/services/LocalVectorStoreService.kt @@ -7,5 +7,13 @@ import com.xebia.functional.xef.store.VectorStore class LocalVectorStoreService : VectorStoreService() { override fun getVectorStore(token: String?, org: String?): VectorStore = - LocalVectorStore(OpenAI(Config.Default.copy(apiToken = token, organization = org)).embeddings) + LocalVectorStore( + OpenAI( + Config { + apiToken = token + organization = org + } + ) + .embeddings + ) } diff --git a/server/src/main/kotlin/com/xebia/functional/xef/server/services/PostgresVectorStoreService.kt b/server/src/main/kotlin/com/xebia/functional/xef/server/services/PostgresVectorStoreService.kt index c547a6614..25993aa51 100644 --- a/server/src/main/kotlin/com/xebia/functional/xef/server/services/PostgresVectorStoreService.kt +++ b/server/src/main/kotlin/com/xebia/functional/xef/server/services/PostgresVectorStoreService.kt @@ -38,7 +38,14 @@ class PostgresVectorStoreService( } override fun getVectorStore(token: String?, org: String?): VectorStore { - val embeddingsApi = OpenAI(Config.Default.copy(apiToken = token, organization = org)).embeddings + val embeddingsApi = + OpenAI( + Config { + apiToken = token + organization = org + } + ) + .embeddings return PGVectorStore( vectorSize = vectorSize,