From ab54df2fafaaccd12a3805c9692b7f9220e575d3 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 8 Sep 2026 21:18:44 +0300 Subject: [PATCH] use ultra model --- apps/vbox/backend/build.gradle.kts | 2 +- .../vbox/backend/data/ClientIdProviderImpl.kt | 9 +++++++++ .../shadowsparky/chat/backend/data/GigaChatService.kt | 10 +++++++--- .../ru/shadowsparky/chat/backend/domain/ChatService.kt | 4 ++++ .../chat/backend/domain/ProcessUserMessageUseCase.kt | 8 +++++--- gradle/libs.versions.toml | 2 ++ 6 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 apps/vbox/backend/src/main/kotlin/ru/shadowsparky/vbox/backend/data/ClientIdProviderImpl.kt diff --git a/apps/vbox/backend/build.gradle.kts b/apps/vbox/backend/build.gradle.kts index daf007a..55fd485 100644 --- a/apps/vbox/backend/build.gradle.kts +++ b/apps/vbox/backend/build.gradle.kts @@ -24,7 +24,7 @@ dependencies { implementation(project(":feature:chat:chat-common")) implementation(project(":feature:chat:chat-backend")) - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:1.8.1") + implementation(libs.kotlinx.coroutines.reactive) implementation(libs.lettuce.lettuce.core) implementation(libs.ktor.server.core.jvm) implementation(libs.ktor.server.host.common.jvm) diff --git a/apps/vbox/backend/src/main/kotlin/ru/shadowsparky/vbox/backend/data/ClientIdProviderImpl.kt b/apps/vbox/backend/src/main/kotlin/ru/shadowsparky/vbox/backend/data/ClientIdProviderImpl.kt new file mode 100644 index 0000000..dfec162 --- /dev/null +++ b/apps/vbox/backend/src/main/kotlin/ru/shadowsparky/vbox/backend/data/ClientIdProviderImpl.kt @@ -0,0 +1,9 @@ +package ru.shadowsparky.vbox.backend.data + +import org.koin.core.annotation.Factory +import ru.shadowsparky.chat.backend.domain.ChatService + +@Factory +class ClientIdProviderImpl : ChatService.ClientIdProvider { + override suspend fun provide(userId: Long): String = "vbox-$userId" +} diff --git a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/data/GigaChatService.kt b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/data/GigaChatService.kt index 0b8a279..2ea26d8 100644 --- a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/data/GigaChatService.kt +++ b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/data/GigaChatService.kt @@ -13,6 +13,7 @@ import kotlinx.coroutines.sync.withLock import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import org.koin.core.annotation.Single +import ru.shadowsparky.backend.data.EnvFetcher import ru.shadowsparky.chat.backend.domain.ChatService import ru.shadowsparky.chat.domain.Message import ru.shadowsparky.http.domain.HttpClientFactory @@ -21,7 +22,9 @@ import java.util.UUID @Single class GigaChatService( private val tokenManager: GigaChatTokenManager, - private val httpClientFactory: HttpClientFactory + private val httpClientFactory: HttpClientFactory, + private val clientIdProvider: ChatService.ClientIdProvider, + private val envFetcher: EnvFetcher ) : ChatService { private val mutex = Mutex() private val httpClient by lazy { @@ -40,9 +43,10 @@ class GigaChatService( } override suspend fun getCompletion(userId: Long, messages: List): String = mutex.withLock { - val clientId = "vbox-$userId" + val clientId = clientIdProvider.provide(userId) val staticSessionId = UUID.nameUUIDFromBytes(clientId.toByteArray()).toString() val requestBody = GigaChatRequest( + model = envFetcher.get("GIGA_CHAT_MODEL", "GigaChat-3-Ultra"), messages = messages.map { GigaChatMessageDto(role = it.role, content = it.content) } ) val response: GigaChatResponse = httpClient.post("https://api.giga.chat/v1/chat/completions") { @@ -58,7 +62,7 @@ class GigaChatService( @Serializable private data class GigaChatRequest( - val model: String = "GigaChat-2", + val model: String, val stream: Boolean = false, @SerialName("update_interval") val updateInterval: Int = 0, diff --git a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ChatService.kt b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ChatService.kt index 443d5ee..9da1bb0 100644 --- a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ChatService.kt +++ b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ChatService.kt @@ -4,4 +4,8 @@ import ru.shadowsparky.chat.domain.Message interface ChatService { suspend fun getCompletion(userId: Long, messages: List): String + + interface ClientIdProvider { + suspend fun provide(userId: Long): String + } } diff --git a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ProcessUserMessageUseCase.kt b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ProcessUserMessageUseCase.kt index 597dac8..7374711 100644 --- a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ProcessUserMessageUseCase.kt +++ b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ProcessUserMessageUseCase.kt @@ -11,13 +11,15 @@ class ProcessUserMessageUseCase( private val chatService: ChatService, private val envFetcher: EnvFetcher ) { - suspend fun execute(userId: Long): Message { + suspend fun execute( + userId: Long, + systemPrompt: String = envFetcher.get("CHAT_SYSTEM_PROMPT").trim('"') + ): Message { val history = storage.query(userId, null, limit = 10).toList() val sortedHistory = history.sortedBy { it.timestamp } val systemMessage = Message( role = ChatRoles.SYSTEM, - content = envFetcher.get("CHAT_SYSTEM_PROMPT").trim('"') - .ifEmpty { error("System prompt not set") }, + content = systemPrompt.ifEmpty { error("System prompt not set") }, timestamp = System.currentTimeMillis() ) val fullContext = listOf(systemMessage) + sortedHistory diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c7331a5..86bc611 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,6 @@ [versions] activity-compose = "1.13.0" +kotlinxCoroutinesReactive = "1.11.0" lettuceCoreVersion = "7.7.0.RELEASE" material3-compose = "1.11.0-alpha03" adaptiveLayout = "1.3.0-beta02" @@ -75,6 +76,7 @@ haze-materials = { module = "dev.chrisbanes.haze:haze-materials", version.ref = java-jwt = { module = "com.auth0:java-jwt", version.ref = "java-jwt" } jdbc-driver = { module = "app.cash.sqldelight:jdbc-driver", version.ref = "sqlite-driver" } koin-annotations = { module = "io.insert-koin:koin-annotations", version.ref = "koin-core" } +kotlinx-coroutines-reactive = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-reactive", version.ref = "kotlinxCoroutinesReactive" } ktor-client-js = { module = "io.ktor:ktor-client-js", version.ref = "ktor-plugin" } ktor-server-cors = { module = "io.ktor:ktor-server-cors" } ktor-server-auth-jwt = { module = "io.ktor:ktor-server-auth-jwt" }