-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Raise with exception hierarchy (#171)
- Loading branch information
Showing
13 changed files
with
79 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 18 additions & 53 deletions
71
core/src/commonMain/kotlin/com/xebia/functional/xef/env/config.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,31 @@ | ||
package com.xebia.functional.xef.env | ||
|
||
import arrow.core.NonEmptyList | ||
import arrow.core.raise.Raise | ||
import arrow.core.raise.catch | ||
import arrow.core.raise.recover | ||
import arrow.core.raise.zipOrAccumulate | ||
import arrow.resilience.Schedule | ||
import com.xebia.functional.xef.AIError | ||
import io.ktor.http.Url as KUrl | ||
import kotlin.jvm.JvmOverloads | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
data class Env(val openAI: OpenAIConfig, val huggingFace: HuggingFaceConfig) | ||
|
||
data class OpenAIConfig( | ||
val token: String, | ||
val baseUrl: KUrl, | ||
val chunkSize: Int, | ||
val retryConfig: RetryConfig, | ||
val requestTimeout: Duration | ||
data class OpenAIConfig | ||
@JvmOverloads | ||
constructor( | ||
val token: String = | ||
requireNotNull(getenv("OPENAI_TOKEN")) { "OpenAI Token missing from environment." }, | ||
val baseUrl: String = "https://api.openai.com/v1/", | ||
val chunkSize: Int = 300, | ||
val retryConfig: RetryConfig = RetryConfig(), | ||
val requestTimeoutMillis: Long = 30_000 | ||
) | ||
|
||
data class RetryConfig(val backoff: Duration, val maxRetries: Long) { | ||
data class RetryConfig(val backoff: Duration = 5.seconds, val maxRetries: Long = 5) { | ||
fun schedule(): Schedule<Throwable, Long> = | ||
Schedule.recurs<Throwable>(maxRetries) | ||
.zipLeft(Schedule.exponential<Throwable>(backoff).jittered(0.75, 1.25)) | ||
} | ||
|
||
data class HuggingFaceConfig(val token: String, val baseUrl: KUrl) | ||
|
||
fun Raise<NonEmptyList<AIError.Env>>.Env(): Env = | ||
zipOrAccumulate({ OpenAIConfig() }, { withNel { HuggingFaceConfig() } }) { openAI, huggingFace -> | ||
Env(openAI, huggingFace) | ||
} | ||
|
||
fun Raise<AIError.Env.OpenAI>.OpenAIConfig(token: String? = null) = | ||
recover({ | ||
zipOrAccumulate( | ||
{ token ?: env("OPENAI_TOKEN") }, | ||
{ env("OPENAI_BASE_URI", default = Url("https://api.openai.com/v1/")) { Url(it) } }, | ||
{ env("OPENAI_CHUNK_SIZE", default = 300) { it.toIntOrNull() } }, | ||
{ env("OPENAI_BACKOFF", default = 5.seconds) { it.toIntOrNull()?.seconds } }, | ||
{ env("OPENAI_MAX_RETRIES", default = 5) { it.toLongOrNull() } }, | ||
{ env("OPENAI_REQUEST_TIMEOUT", default = 30.seconds) { it.toIntOrNull()?.seconds } }, | ||
) { token2, baseUrl, chunkSize, backoff, maxRetries, requestTimeout -> | ||
OpenAIConfig(token2, baseUrl, chunkSize, RetryConfig(backoff, maxRetries), requestTimeout) | ||
} | ||
}) { e: NonEmptyList<String> -> | ||
raise(AIError.Env.OpenAI(e)) | ||
} | ||
|
||
fun Raise<AIError.Env.HuggingFace>.HuggingFaceConfig(token: String? = null) = | ||
recover({ | ||
zipOrAccumulate( | ||
{ token ?: env("HF_TOKEN") }, | ||
{ env("HF_BASE_URI", default = Url("https://api-inference.huggingface.co/")) { Url(it) } } | ||
) { token2, baseUrl -> | ||
HuggingFaceConfig(token2, baseUrl) | ||
} | ||
}) { e: NonEmptyList<String> -> | ||
raise(AIError.Env.HuggingFace(e)) | ||
} | ||
|
||
fun Raise<String>.Url(urlString: String): KUrl = | ||
catch({ KUrl(urlString) }) { raise(it.message ?: "Invalid url: $it") } | ||
data class HuggingFaceConfig | ||
@JvmOverloads | ||
constructor( | ||
val token: String = | ||
requireNotNull(getenv("OPENAI_TOKEN")) { "OpenAI Token missing from environment." }, | ||
val baseUrl: String = "https://api-inference.huggingface.co/" | ||
) |
41 changes: 0 additions & 41 deletions
41
core/src/commonMain/kotlin/com/xebia/functional/xef/env/env.kt
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
core/src/commonMain/kotlin/com/xebia/functional/xef/env/getenv.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.xebia.functional.xef.env | ||
|
||
/** | ||
* A function that reads the configuration from the environment. This only works on JVM, Native and | ||
* NodeJS. | ||
* | ||
* In the browser, we default to `null` so either rely on the default values, or provide construct | ||
* the values explicitly. | ||
* | ||
* We might be able to support browser through webpack. | ||
*/ | ||
expect fun getenv(name: String): String? |
Oops, something went wrong.