use ultra model
This commit is contained in:
@@ -24,7 +24,7 @@ dependencies {
|
|||||||
implementation(project(":feature:chat:chat-common"))
|
implementation(project(":feature:chat:chat-common"))
|
||||||
implementation(project(":feature:chat:chat-backend"))
|
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.lettuce.lettuce.core)
|
||||||
implementation(libs.ktor.server.core.jvm)
|
implementation(libs.ktor.server.core.jvm)
|
||||||
implementation(libs.ktor.server.host.common.jvm)
|
implementation(libs.ktor.server.host.common.jvm)
|
||||||
|
|||||||
+9
@@ -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"
|
||||||
|
}
|
||||||
+7
-3
@@ -13,6 +13,7 @@ import kotlinx.coroutines.sync.withLock
|
|||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import org.koin.core.annotation.Single
|
import org.koin.core.annotation.Single
|
||||||
|
import ru.shadowsparky.backend.data.EnvFetcher
|
||||||
import ru.shadowsparky.chat.backend.domain.ChatService
|
import ru.shadowsparky.chat.backend.domain.ChatService
|
||||||
import ru.shadowsparky.chat.domain.Message
|
import ru.shadowsparky.chat.domain.Message
|
||||||
import ru.shadowsparky.http.domain.HttpClientFactory
|
import ru.shadowsparky.http.domain.HttpClientFactory
|
||||||
@@ -21,7 +22,9 @@ import java.util.UUID
|
|||||||
@Single
|
@Single
|
||||||
class GigaChatService(
|
class GigaChatService(
|
||||||
private val tokenManager: GigaChatTokenManager,
|
private val tokenManager: GigaChatTokenManager,
|
||||||
private val httpClientFactory: HttpClientFactory
|
private val httpClientFactory: HttpClientFactory,
|
||||||
|
private val clientIdProvider: ChatService.ClientIdProvider,
|
||||||
|
private val envFetcher: EnvFetcher
|
||||||
) : ChatService {
|
) : ChatService {
|
||||||
private val mutex = Mutex()
|
private val mutex = Mutex()
|
||||||
private val httpClient by lazy {
|
private val httpClient by lazy {
|
||||||
@@ -40,9 +43,10 @@ class GigaChatService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getCompletion(userId: Long, messages: List<Message>): String = mutex.withLock {
|
override suspend fun getCompletion(userId: Long, messages: List<Message>): String = mutex.withLock {
|
||||||
val clientId = "vbox-$userId"
|
val clientId = clientIdProvider.provide(userId)
|
||||||
val staticSessionId = UUID.nameUUIDFromBytes(clientId.toByteArray()).toString()
|
val staticSessionId = UUID.nameUUIDFromBytes(clientId.toByteArray()).toString()
|
||||||
val requestBody = GigaChatRequest(
|
val requestBody = GigaChatRequest(
|
||||||
|
model = envFetcher.get("GIGA_CHAT_MODEL", "GigaChat-3-Ultra"),
|
||||||
messages = messages.map { GigaChatMessageDto(role = it.role, content = it.content) }
|
messages = messages.map { GigaChatMessageDto(role = it.role, content = it.content) }
|
||||||
)
|
)
|
||||||
val response: GigaChatResponse = httpClient.post("https://api.giga.chat/v1/chat/completions") {
|
val response: GigaChatResponse = httpClient.post("https://api.giga.chat/v1/chat/completions") {
|
||||||
@@ -58,7 +62,7 @@ class GigaChatService(
|
|||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
private data class GigaChatRequest(
|
private data class GigaChatRequest(
|
||||||
val model: String = "GigaChat-2",
|
val model: String,
|
||||||
val stream: Boolean = false,
|
val stream: Boolean = false,
|
||||||
@SerialName("update_interval")
|
@SerialName("update_interval")
|
||||||
val updateInterval: Int = 0,
|
val updateInterval: Int = 0,
|
||||||
|
|||||||
+4
@@ -4,4 +4,8 @@ import ru.shadowsparky.chat.domain.Message
|
|||||||
|
|
||||||
interface ChatService {
|
interface ChatService {
|
||||||
suspend fun getCompletion(userId: Long, messages: List<Message>): String
|
suspend fun getCompletion(userId: Long, messages: List<Message>): String
|
||||||
|
|
||||||
|
interface ClientIdProvider {
|
||||||
|
suspend fun provide(userId: Long): String
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -11,13 +11,15 @@ class ProcessUserMessageUseCase(
|
|||||||
private val chatService: ChatService,
|
private val chatService: ChatService,
|
||||||
private val envFetcher: EnvFetcher
|
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 history = storage.query(userId, null, limit = 10).toList()
|
||||||
val sortedHistory = history.sortedBy { it.timestamp }
|
val sortedHistory = history.sortedBy { it.timestamp }
|
||||||
val systemMessage = Message(
|
val systemMessage = Message(
|
||||||
role = ChatRoles.SYSTEM,
|
role = ChatRoles.SYSTEM,
|
||||||
content = envFetcher.get("CHAT_SYSTEM_PROMPT").trim('"')
|
content = systemPrompt.ifEmpty { error("System prompt not set") },
|
||||||
.ifEmpty { error("System prompt not set") },
|
|
||||||
timestamp = System.currentTimeMillis()
|
timestamp = System.currentTimeMillis()
|
||||||
)
|
)
|
||||||
val fullContext = listOf(systemMessage) + sortedHistory
|
val fullContext = listOf(systemMessage) + sortedHistory
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
[versions]
|
[versions]
|
||||||
activity-compose = "1.13.0"
|
activity-compose = "1.13.0"
|
||||||
|
kotlinxCoroutinesReactive = "1.11.0"
|
||||||
lettuceCoreVersion = "7.7.0.RELEASE"
|
lettuceCoreVersion = "7.7.0.RELEASE"
|
||||||
material3-compose = "1.11.0-alpha03"
|
material3-compose = "1.11.0-alpha03"
|
||||||
adaptiveLayout = "1.3.0-beta02"
|
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" }
|
java-jwt = { module = "com.auth0:java-jwt", version.ref = "java-jwt" }
|
||||||
jdbc-driver = { module = "app.cash.sqldelight:jdbc-driver", version.ref = "sqlite-driver" }
|
jdbc-driver = { module = "app.cash.sqldelight:jdbc-driver", version.ref = "sqlite-driver" }
|
||||||
koin-annotations = { module = "io.insert-koin:koin-annotations", version.ref = "koin-core" }
|
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-client-js = { module = "io.ktor:ktor-client-js", version.ref = "ktor-plugin" }
|
||||||
ktor-server-cors = { module = "io.ktor:ktor-server-cors" }
|
ktor-server-cors = { module = "io.ktor:ktor-server-cors" }
|
||||||
ktor-server-auth-jwt = { module = "io.ktor:ktor-server-auth-jwt" }
|
ktor-server-auth-jwt = { module = "io.ktor:ktor-server-auth-jwt" }
|
||||||
|
|||||||
Reference in New Issue
Block a user