-
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.
- Loading branch information
Showing
10 changed files
with
152 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.xebia.functional.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 io.ktor.http.Url as KUrl | ||
import arrow.resilience.Schedule | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
data class InvalidConfig(val message: String) | ||
|
||
data class Env(val openAI: OpenAIConfig, val huggingFace: HuggingFaceConfig) | ||
|
||
data class OpenAIConfig(val token: String, val chunkSize: Int, val retryConfig: RetryConfig) | ||
|
||
data class RetryConfig(val backoff: Duration, val maxRetries: Long) { | ||
fun schedule(): Schedule<Throwable, Unit> = | ||
Schedule.recurs<Throwable>(maxRetries) | ||
.and(Schedule.exponential(backoff)) | ||
.jittered(0.75, 1.25) | ||
.map { } | ||
} | ||
|
||
data class HuggingFaceConfig(val token: String, val baseUrl: KUrl) | ||
|
||
fun Raise<InvalidConfig>.Env(): Env = | ||
recover({ | ||
zipOrAccumulate( | ||
{ OpenAIConfig() }, | ||
{ HuggingFaceConfig() } | ||
) { openAI, huggingFace -> Env(openAI, huggingFace) } | ||
}) { nel -> raise(InvalidConfig(nel.joinToString(separator = "\n"))) } | ||
|
||
fun Raise<NonEmptyList<String>>.OpenAIConfig(token: String? = null) = | ||
zipOrAccumulate( | ||
{ token ?: env("OPENAI_TOKEN") }, | ||
{ env("OPENAI_CHUNK_SIZE", default = 1000) { it.toIntOrNull() } }, | ||
{ env("OPENAI_BACKOFF", default = 5.seconds) { it.toIntOrNull()?.seconds } }, | ||
{ env("OPENAI_MAX_RETRIES", default = 5) { it.toLongOrNull() } }, | ||
) { token2, chunkSize, backoff, maxRetries -> OpenAIConfig(token2, chunkSize, RetryConfig(backoff, maxRetries)) } | ||
|
||
fun Raise<NonEmptyList<String>>.HuggingFaceConfig(token: String? = null) = | ||
zipOrAccumulate( | ||
{ token ?: env("HF_TOKEN") }, | ||
{ env("HF_BASE_URI", default = Url("https://api-inference.huggingface.co")) { Url(it) } } | ||
) { token2, baseUrl -> HuggingFaceConfig(token2, baseUrl) } | ||
|
||
fun Raise<String>.Url(urlString: String): KUrl = | ||
catch({ KUrl(urlString) }) { raise(it.message ?: "Invalid url: $it") } |
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,48 @@ | ||
package com.xebia.functional.env | ||
|
||
import arrow.core.raise.Raise | ||
import arrow.core.raise.ensureNotNull | ||
|
||
/** | ||
* Get an env variable by [name], or fallback to an _optional_ default value. | ||
* In case no env value is found or no default value is provided then it raises an error message. | ||
*/ | ||
fun Raise<String>.env( | ||
name: String, | ||
default: String? = null | ||
): String = | ||
ensureNotNull(getenv(name) ?: default) { "\"$name\" configuration missing" } | ||
|
||
/** | ||
* Get an env variable by [name] and [transform] it. | ||
* If no env variable is found, it raises an error message. | ||
* The [transform] function can also raise a custom error message. | ||
*/ | ||
fun <A : Any> Raise<String>.env( | ||
name: String, | ||
transform: Raise<String>.(String) -> A? | ||
): A { | ||
val string = getenv(name)?.let { transform(it) } | ||
return ensureNotNull(string) { "\"$name\" configuration found with $string" } | ||
} | ||
|
||
/** | ||
* Get an env variable by [name] and [transform] it, or fallback to a default value. | ||
* The [transform] function can raise a custom error message. | ||
*/ | ||
fun <A : Any> Raise<String>.env( | ||
name: String, | ||
default: A, | ||
transform: Raise<String>.(String) -> A? | ||
): A = getenv(name)?.let { transform(it) } ?: default | ||
|
||
/** | ||
* 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? |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.xebia.functional.env | ||
|
||
external val process: dynamic | ||
|
||
/** | ||
* We wrap it in runCatching because this only works in NodeJS. | ||
* In the browser, we get a ReferenceError: "process" is not defined. | ||
*/ | ||
actual fun getenv(name: String): String? = runCatching { | ||
process.env[name] as String? | ||
}.getOrNull() |
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,4 @@ | ||
package com.xebia.functional.env | ||
|
||
actual fun getenv(name: String): String? = | ||
System.getenv(name) |
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,6 @@ | ||
package com.xebia.functional.env | ||
|
||
import kotlinx.cinterop.toKString | ||
|
||
actual fun getenv(name: String): String? = | ||
platform.posix.getenv(name)?.toKString() |