Skip to content

Commit

Permalink
Create an OpenAI config builder
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscodr committed Aug 27, 2024
1 parent fe6c46d commit f1701d2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
29 changes: 29 additions & 0 deletions core/src/commonMain/kotlin/com/xebia/functional/xef/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f1701d2

Please sign in to comment.