|
@@ -3,23 +3,67 @@ package eu.mjdev.desktop.provider
|
|
|
import dev.shreyaspatil.ai.client.generativeai.GenerativeModel
|
|
import dev.shreyaspatil.ai.client.generativeai.GenerativeModel
|
|
|
import dev.shreyaspatil.ai.client.generativeai.type.content
|
|
import dev.shreyaspatil.ai.client.generativeai.type.content
|
|
|
import eu.mjdev.desktop.extensions.Custom.loadKey
|
|
import eu.mjdev.desktop.extensions.Custom.loadKey
|
|
|
-import kotlinx.coroutines.runBlocking
|
|
|
|
|
|
|
+import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
+import kotlinx.coroutines.async
|
|
|
|
|
+import kotlinx.coroutines.launch
|
|
|
|
|
+import nl.marc_apps.tts.TextToSpeechFactory
|
|
|
|
|
+import nl.marc_apps.tts.experimental.ExperimentalDesktopTarget
|
|
|
|
|
|
|
|
-@Suppress("MemberVisibilityCanBePrivate", "unused")
|
|
|
|
|
|
|
+@Suppress("MemberVisibilityCanBePrivate", "unused", "UNUSED_PARAMETER", "RemoveEmptyClassBody")
|
|
|
class AIProvider(
|
|
class AIProvider(
|
|
|
- var plugin: IAIPlugin = AiPluginNull()
|
|
|
|
|
|
|
+ val scope: CoroutineScope,
|
|
|
|
|
+ var pluginAI: IAIPlugin = AiPluginNull(scope),
|
|
|
|
|
+ val pluginSTT: ISTTPlugin = STTPluginNull(scope),
|
|
|
|
|
+ val pluginTTS: TTSPlugin = TTSPluginMain(scope)
|
|
|
) {
|
|
) {
|
|
|
- fun ask(question: String): String = plugin.ask(question)
|
|
|
|
|
|
|
+ fun ask(
|
|
|
|
|
+ question: String,
|
|
|
|
|
+ block: AIProvider.(question: String, result: String) -> Unit
|
|
|
|
|
+ ) = scope.launch {
|
|
|
|
|
+ pluginAI.ask(question).also { result ->
|
|
|
|
|
+ block.invoke(this@AIProvider, question, result)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fun talk(text: String, clearQueue: Boolean = false) =
|
|
|
|
|
+ pluginTTS.talk(text, clearQueue)
|
|
|
|
|
|
|
|
interface IAIPlugin {
|
|
interface IAIPlugin {
|
|
|
- fun ask(question: String): String
|
|
|
|
|
|
|
+ suspend fun ask(question: String): String
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ interface ISTTPlugin {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ interface TTSPlugin {
|
|
|
|
|
+ fun talk(text: String, clearQueue: Boolean = false)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ class STTPluginNull(scope: CoroutineScope) : ISTTPlugin {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @OptIn(ExperimentalDesktopTarget::class)
|
|
|
|
|
+ class TTSPluginMain(
|
|
|
|
|
+ val scope: CoroutineScope
|
|
|
|
|
+ ) : TTSPlugin {
|
|
|
|
|
+ private val textToSpeech = scope.async {
|
|
|
|
|
+ TextToSpeechFactory().createOrNull()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun talk(text: String, clearQueue: Boolean) {
|
|
|
|
|
+ scope.launch {
|
|
|
|
|
+ textToSpeech.await()?.say(text, clearQueue)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- class AiPluginNull : IAIPlugin {
|
|
|
|
|
- override fun ask(question: String): String = ""
|
|
|
|
|
|
|
+ class AiPluginNull(scope: CoroutineScope) : IAIPlugin {
|
|
|
|
|
+ override suspend fun ask(question: String): String = ""
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- class AiPluginGemini : IAIPlugin {
|
|
|
|
|
|
|
+ class AiPluginGemini(
|
|
|
|
|
+ val scope: CoroutineScope
|
|
|
|
|
+ ) : IAIPlugin {
|
|
|
private val generativeModel: GenerativeModel by lazy {
|
|
private val generativeModel: GenerativeModel by lazy {
|
|
|
GenerativeModel(
|
|
GenerativeModel(
|
|
|
modelName = "gemini-1.5-pro-latest",
|
|
modelName = "gemini-1.5-pro-latest",
|
|
@@ -27,16 +71,17 @@ class AIProvider(
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- override fun ask(question: String): String = runBlocking {
|
|
|
|
|
|
|
+ override suspend fun ask(question: String): String = scope.async {
|
|
|
|
|
+ var error: Throwable? = null
|
|
|
runCatching {
|
|
runCatching {
|
|
|
generativeModel.generateContent(content {
|
|
generativeModel.generateContent(content {
|
|
|
text(question)
|
|
text(question)
|
|
|
}).text
|
|
}).text
|
|
|
- }.onFailure {
|
|
|
|
|
- println(it)
|
|
|
|
|
|
|
+ }.onFailure { e ->
|
|
|
|
|
+ error = e
|
|
|
}.getOrElse {
|
|
}.getOrElse {
|
|
|
- ""
|
|
|
|
|
- }
|
|
|
|
|
- }.orEmpty()
|
|
|
|
|
|
|
+ error?.message ?: ""
|
|
|
|
|
+ } ?: ""
|
|
|
|
|
+ }.await()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|