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

Cu 861mwhnjx split kotlinx serialization #170

Merged
merged 9 commits into from
Jun 13, 2023
Prev Previous commit
Next Next commit
Add technical documentation
nomisRev committed Jun 7, 2023
commit 7ade11ed9796732f26bbb18449c996f9c8c19d56
28 changes: 28 additions & 0 deletions core/TECHNICAL.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Technical

This document tracks technical decisions for the Core module,
such that we can document and backtrack our decisions.

There is a trade-off between building a lean and mean _core_ library,
and writing as much as common code in Kotlin Multiplatform as possible.

To achieve this we try to keep the dependencies on external dependencies as small as possible,
but we have a need for a couple _base_ dependencies which we document below.
The breakdown is only in terms of JVM, since that's where we _mostly_ care about this.

## Kotlin's dependency breakdown (JVM - common)

We include the following dependencies in our _core_ module to implement a _common_ layer to interact with LLMs.
The dependency on Kotlin Stdlib is unavoidable, since we use Kotlin as our main language.
We also require KotlinX Coroutines such that we can leverage the `suspend` keyword in our API and expose `Future` to the
Java/Scala API.
Additionally, we also have a need for a HTTP client, and a serialization framework. Here we use Ktor and KotlinX
Serialization respectively,
and Xef relies on the CIO engine for Ktor, which avoids any additional dependencies.

- kotlin-stdlib (1810 Kb = 1598 Kb + 212 Kb)
- kotlinx-coroutines-core (1608 Kb = 1442 Kb + 166 Kb)
- Ktor Client (288Kb)
- KotlinX Serialization (791 Kb)

Total: 4497 Kb
4 changes: 1 addition & 3 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -62,10 +62,8 @@ kotlin {

api(libs.bundles.ktor.client)
api(projects.xefTokenizer)
implementation(libs.arrow.fx.stm)

// TODO split to a separate module
implementation(libs.kotlinx.serialization.json)
implementation(libs.arrow.fx.stm)

implementation(libs.uuid)
implementation(libs.klogging)
Original file line number Diff line number Diff line change
@@ -8,16 +8,15 @@ import com.xebia.functional.xef.httpClient
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.plugins.timeout
import io.ktor.client.request.post
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpStatusCode
import io.ktor.http.path
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

interface OpenAIClient {
suspend fun createCompletion(request: CompletionRequest): CompletionResult
@@ -124,8 +123,7 @@ private class KtorOpenAIClient(
}

private suspend inline fun <reified T> HttpResponse.bodyOrError(): T =
if (status == HttpStatusCode.OK) Json.decodeFromString(bodyAsText())
else throw OpenAIClientException(status, Json.decodeFromString(bodyAsText()))
if (status == HttpStatusCode.OK) body() else throw OpenAIClientException(status, body())

class OpenAIClientException(val httpStatusCode: HttpStatusCode, val error: Error) :
IllegalStateException(