-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* using toml for dependency * Adding buttons * Adding story * Creating the sketch of the connection * Adding loading support * modules for Ollama connection * Call working * Streaming ollama response * Loading available models * adding ai answer drawer * Moving files * Adding error support * Update OllamaApi.kt * ktlint * Adding model selection * Printing models --------- Co-authored-by: CI Bot <[email protected]>
- Loading branch information
1 parent
4d0429f
commit b796a21
Showing
62 changed files
with
936 additions
and
219 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
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 @@ | ||
/build |
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,39 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
} | ||
|
||
kotlin { | ||
jvm {} | ||
|
||
js(IR) { | ||
browser() | ||
binaries.library() | ||
} | ||
|
||
listOf( | ||
iosX64(), | ||
iosArm64(), | ||
iosSimulatorArm64() | ||
).forEach { iosTarget -> | ||
iosTarget.binaries.framework { | ||
baseName = "WriteopiaCoreConnection" | ||
isStatic = true | ||
} | ||
} | ||
|
||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
implementation(libs.kotlinx.coroutines.core) | ||
implementation(libs.kotlinx.datetime) | ||
implementation(libs.ktor.client.core) | ||
implementation(libs.ktor.client.logging) | ||
|
||
implementation(libs.ktor.client.logging) | ||
implementation(libs.ktor.client.core) | ||
implementation(libs.ktor.client.content.negotiation) | ||
implementation(libs.ktor.serialization.json) | ||
} | ||
} | ||
} | ||
} |
Empty file.
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
48 changes: 48 additions & 0 deletions
48
application/core/connection/src/commonMain/kotlin/io/writeopia/di/AppConnectionInjection.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,48 @@ | ||
package io.writeopia.di | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.plugins.HttpTimeout | ||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation | ||
import io.ktor.client.plugins.logging.DEFAULT | ||
import io.ktor.client.plugins.logging.LogLevel | ||
import io.ktor.client.plugins.logging.Logger | ||
import io.ktor.client.plugins.logging.Logging | ||
import io.ktor.http.HttpHeaders | ||
import io.ktor.serialization.kotlinx.json.json | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.modules.SerializersModule | ||
|
||
class AppConnectionInjection( | ||
private val json: Json = Json { | ||
serializersModule = SerializersModule { | ||
ignoreUnknownKeys = true | ||
} | ||
}, | ||
private val apiLogger: Logger = Logger.Companion.DEFAULT | ||
) { | ||
fun provideJson() = json | ||
|
||
fun provideHttpClient(): HttpClient = ApiInjectorDefaults.httpClient(json, apiLogger) | ||
} | ||
|
||
object ApiInjectorDefaults { | ||
fun httpClient( | ||
json: Json, | ||
apiLogger: Logger, | ||
) = HttpClient { | ||
install(HttpTimeout) { | ||
requestTimeoutMillis = 30000 | ||
socketTimeoutMillis = 30000 | ||
} | ||
|
||
install(ContentNegotiation) { | ||
json(json = json) | ||
} | ||
|
||
install(Logging) { | ||
logger = apiLogger | ||
level = LogLevel.ALL | ||
sanitizeHeader { header -> header == HttpHeaders.Authorization } | ||
} | ||
} | ||
} |
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 @@ | ||
/build |
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,39 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
alias(libs.plugins.kotlinSerialization) | ||
alias(libs.plugins.ktlint) | ||
} | ||
|
||
kotlin { | ||
jvm {} | ||
|
||
js(IR) { | ||
browser() | ||
binaries.library() | ||
} | ||
|
||
listOf( | ||
iosX64(), | ||
iosArm64(), | ||
iosSimulatorArm64() | ||
).forEach { iosTarget -> | ||
iosTarget.binaries.framework { | ||
baseName = "WriteopiaCoreOllama" | ||
isStatic = true | ||
} | ||
} | ||
|
||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
implementation(libs.kotlinx.coroutines.core) | ||
implementation(libs.kotlinx.datetime) | ||
implementation(libs.ktor.client.core) | ||
implementation(project(":common:endpoints")) | ||
implementation(project(":application:core:connection")) | ||
implementation(project(":application:core:utils")) | ||
implementation(libs.kotlinx.serialization.json) | ||
} | ||
} | ||
} | ||
} |
Empty file.
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
18 changes: 18 additions & 0 deletions
18
application/core/ollama/src/commonMain/kotlin/io/writeopia/OllamaRepository.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,18 @@ | ||
package io.writeopia | ||
|
||
import io.writeopia.api.OllamaApi | ||
import io.writeopia.common.utils.ResultData | ||
import io.writeopia.requests.ModelsResponse | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
class OllamaRepository(private val ollamaApi: OllamaApi) { | ||
|
||
suspend fun generateReply(model: String, prompt: String): String { | ||
return ollamaApi.generateReply(model, prompt).response | ||
} | ||
|
||
fun streamReply(model: String, prompt: String): Flow<ResultData<String>> = | ||
ollamaApi.streamReply(model, prompt) | ||
|
||
fun getModels(): Flow<ResultData<ModelsResponse>> = ollamaApi.getModels() | ||
} |
92 changes: 92 additions & 0 deletions
92
application/core/ollama/src/commonMain/kotlin/io/writeopia/api/OllamaApi.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,92 @@ | ||
package io.writeopia.api | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.request.get | ||
import io.ktor.client.request.post | ||
import io.ktor.client.request.preparePost | ||
import io.ktor.client.request.setBody | ||
import io.ktor.client.request.url | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.contentType | ||
import io.ktor.utils.io.ByteReadChannel | ||
import io.ktor.utils.io.readUTF8Line | ||
import io.writeopia.app.endpoints.EndPoints | ||
import io.writeopia.common.utils.ResultData | ||
import io.writeopia.requests.ModelsResponse | ||
import io.writeopia.requests.OllamaGenerateRequest | ||
import io.writeopia.responses.OllamaResponse | ||
import kotlinx.coroutines.currentCoroutineContext | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.serialization.json.Json | ||
|
||
/** | ||
* API for calling Ollama | ||
*/ | ||
class OllamaApi( | ||
private val client: HttpClient, | ||
private val baseUrl: String = "http://localhost:11434/api", | ||
private val json: Json | ||
) { | ||
|
||
suspend fun generateReply( | ||
model: String, | ||
prompt: String, | ||
): OllamaResponse = | ||
client.post("$baseUrl/${EndPoints.ollamaGenerate()}") { | ||
contentType(ContentType.Application.Json) | ||
setBody(OllamaGenerateRequest(model, prompt, false)) | ||
}.body<OllamaResponse>() | ||
|
||
fun streamReply( | ||
model: String, | ||
prompt: String, | ||
): Flow<ResultData<String>> = | ||
flow { | ||
try { | ||
client.preparePost { | ||
url("$baseUrl/${EndPoints.ollamaGenerate()}") | ||
contentType(ContentType.Application.Json) | ||
setBody(OllamaGenerateRequest(model, prompt, true)) | ||
}.execute { response -> | ||
try { | ||
val stringBuilder = StringBuilder() | ||
val channel = response.body<ByteReadChannel>() | ||
|
||
while (currentCoroutineContext().isActive && !channel.isClosedForRead) { | ||
val line = | ||
channel.readUTF8Line()?.takeUnless { it.isEmpty() } ?: continue | ||
|
||
val value: OllamaResponse = json.decodeFromString(line) | ||
|
||
stringBuilder.append(" ${value.response}") | ||
|
||
emit(ResultData.Complete(stringBuilder.toString())) | ||
} | ||
} catch (e: Exception) { | ||
emit(ResultData.Error(e)) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
emit(ResultData.Error(e)) | ||
} | ||
} | ||
|
||
fun getModels(): Flow<ResultData<ModelsResponse>> { | ||
return flow { | ||
try { | ||
emit(ResultData.Loading()) | ||
|
||
val request = client.get("$baseUrl/${EndPoints.ollamaModels()}") { | ||
contentType(ContentType.Application.Json) | ||
} | ||
|
||
emit(ResultData.Complete(request.body())) | ||
} catch (e: Exception) { | ||
emit(ResultData.Error(e)) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.