diff --git a/README.md b/README.md index 76daa102..c7f19b7e 100644 --- a/README.md +++ b/README.md @@ -76,18 +76,18 @@ Maven: com.simiacryptus skyenet-webui - 1.0.65 + 1.0.67 ``` Gradle: ```groovy -implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.65' +implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.67' ``` ```kotlin -implementation("com.simiacryptus:skyenet:1.0.65") +implementation("com.simiacryptus:skyenet:1.0.67") ``` ### 🌟 To Use diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 553998d6..938f00f7 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -33,7 +33,7 @@ val hsqldb_version = "2.7.2" dependencies { - implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.0.65") + implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.0.67") implementation(group = "org.hsqldb", name = "hsqldb", version = hsqldb_version) implementation("org.apache.commons:commons-text:1.11.0") diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/core/actors/ParsedActor.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/core/actors/ParsedActor.kt index e4e8eb24..1eef612e 100644 --- a/core/src/main/kotlin/com/simiacryptus/skyenet/core/actors/ParsedActor.kt +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/core/actors/ParsedActor.kt @@ -79,7 +79,7 @@ open class ParsedActor( ApiModel.ChatRequest( messages = listOf( ApiModel.ChatMessage(role = ApiModel.Role.system, content = prompt.toContentList()), - ApiModel.ChatMessage(role = ApiModel.Role.user, content = input.toContentList()), + ApiModel.ChatMessage(role = ApiModel.Role.user, content = "The user message to parse:\n\n$input".toContentList()), ), temperature = temperature, model = model.modelName, diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/ApplicationServices.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/ApplicationServices.kt index d61d8e2d..22b7573d 100644 --- a/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/ApplicationServices.kt +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/ApplicationServices.kt @@ -16,6 +16,7 @@ import java.nio.ByteBuffer import java.util.* import java.util.concurrent.ThreadPoolExecutor import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong import kotlin.random.Random object ApplicationServicesConfig { @@ -258,8 +259,8 @@ interface UsageInterface { ) class UsageValues( - val inputTokens: AtomicInteger = AtomicInteger(), - val outputTokens: AtomicInteger = AtomicInteger(), + val inputTokens: AtomicLong = AtomicLong(), + val outputTokens: AtomicLong = AtomicLong(), val cost: AtomicDouble = AtomicDouble(), ) { fun addAndGet(tokens: ApiModel.Usage) { diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/HSQLUsageManager.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/HSQLUsageManager.kt index 1028cb50..f1aab0e8 100644 --- a/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/HSQLUsageManager.kt +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/HSQLUsageManager.kt @@ -11,7 +11,7 @@ import java.sql.Connection import java.sql.DriverManager import java.sql.ResultSet import java.sql.Timestamp -import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong class HSQLUsageManager(private val dbFile: File) : UsageInterface { @@ -29,17 +29,17 @@ class HSQLUsageManager(private val dbFile: File) : UsageInterface { logger.info("Creating database schema if not exists") connection.createStatement().executeUpdate( """ - CREATE TABLE IF NOT EXISTS usage ( - session_id VARCHAR(255), - api_key VARCHAR(255), - model VARCHAR(255), - prompt_tokens INT, - completion_tokens INT, - cost DOUBLE, - datetime TIMESTAMP, - PRIMARY KEY (session_id, api_key, model, prompt_tokens, completion_tokens, cost, datetime) - ) - """ + CREATE TABLE IF NOT EXISTS usage ( + session_id VARCHAR(255), + api_key VARCHAR(255), + model VARCHAR(255), + prompt_tokens BIGINT, + completion_tokens BIGINT, + cost DOUBLE, + datetime TIMESTAMP, + PRIMARY KEY (session_id, api_key, model, prompt_tokens, completion_tokens, cost, datetime) + ) + """ ) } @@ -56,12 +56,16 @@ class HSQLUsageManager(private val dbFile: File) : UsageInterface { } override fun incrementUsage(session: Session, apiKey: String?, model: OpenAIModel, tokens: ApiModel.Usage) { - logger.debug("Incrementing usage for session: ${session.sessionId}, apiKey: $apiKey, model: ${model.modelName}") - val usageKey = UsageInterface.UsageKey(session, apiKey, model) - val usageValues = getUsageValues(usageKey) - usageValues.addAndGet(tokens) - saveUsageValues(usageKey, usageValues) - logger.debug("Usage incremented for session: ${session.sessionId}, apiKey: $apiKey, model: ${model.modelName}") + try { + logger.debug("Incrementing usage for session: ${session.sessionId}, apiKey: $apiKey, model: ${model.modelName}") + val usageKey = UsageInterface.UsageKey(session, apiKey, model) + val usageValues = UsageInterface.UsageValues() //getUsageValues(usageKey) + usageValues.addAndGet(tokens) + saveUsageValues(usageKey, usageValues) + logger.debug("Usage incremented for session: ${session.sessionId}, apiKey: $apiKey, model: ${model.modelName}") + } catch (e: Exception) { + logger.error("Error incrementing usage", e) + } } override fun getUserUsageSummary(apiKey: String): Map { @@ -103,10 +107,10 @@ class HSQLUsageManager(private val dbFile: File) : UsageInterface { logger.debug("Getting usage values for session: ${usageKey.session.sessionId}, apiKey: ${usageKey.apiKey}, model: ${usageKey.model.modelName}") val statement = connection.prepareStatement( """ - SELECT COALESCE(SUM(prompt_tokens), 0), COALESCE(SUM(completion_tokens), 0), COALESCE(SUM(cost), 0) - FROM usage - WHERE session_id = ? AND api_key = ? AND model = ? - """ + SELECT COALESCE(SUM(prompt_tokens), 0), COALESCE(SUM(completion_tokens), 0), COALESCE(SUM(cost), 0) + FROM usage + WHERE session_id = ? AND api_key = ? AND model = ? + """ ) statement.setString(1, usageKey.session.sessionId) statement.setString(2, usageKey.apiKey ?: "") @@ -114,8 +118,8 @@ class HSQLUsageManager(private val dbFile: File) : UsageInterface { val resultSet = statement.executeQuery() resultSet.next() return UsageInterface.UsageValues( - AtomicInteger(resultSet.getInt(1)), - AtomicInteger(resultSet.getInt(2)), + AtomicLong(resultSet.getLong(1)), + AtomicLong(resultSet.getLong(2)), AtomicDouble(resultSet.getDouble(3)) ) } @@ -124,17 +128,17 @@ class HSQLUsageManager(private val dbFile: File) : UsageInterface { logger.debug("Saving usage values for session: ${usageKey.session.sessionId}, apiKey: ${usageKey.apiKey}, model: ${usageKey.model.modelName}") val statement = connection.prepareStatement( """ - INSERT INTO usage (session_id, api_key, model, prompt_tokens, completion_tokens, cost, datetime) - VALUES (?, ?, ?, ?, ?, ?, ?) - """ + INSERT INTO usage (session_id, api_key, model, prompt_tokens, completion_tokens, cost, datetime) + VALUES (?, ?, ?, ?, ?, ?, ?) + """ ) statement.setString(1, usageKey.session.sessionId) statement.setString(2, usageKey.apiKey ?: "") statement.setString(3, usageKey.model.modelName) - statement.setInt(4, usageValues.inputTokens.get()) - statement.setInt(5, usageValues.outputTokens.get()) + statement.setLong(4, usageValues.inputTokens.get()) + statement.setLong(5, usageValues.outputTokens.get()) statement.setDouble(6, usageValues.cost.get()) - statement.setTimestamp(7, Timestamp(System.currentTimeMillis())) + statement.setTimestamp(7, Timestamp(System.currentTimeMillis())) logger.debug("Executing statement: $statement") logger.debug("With parameters: ${usageKey.session.sessionId}, ${usageKey.apiKey}, ${usageKey.model.modelName}, ${usageValues.inputTokens.get()}, ${usageValues.outputTokens.get()}, ${usageValues.cost.get()}") statement.executeUpdate() @@ -147,8 +151,8 @@ class HSQLUsageManager(private val dbFile: File) : UsageInterface { val string = resultSet.getString(1) val model = openAIModel(string) ?: continue val usage = ApiModel.Usage( - prompt_tokens = resultSet.getInt(2), - completion_tokens = resultSet.getInt(3), + prompt_tokens = resultSet.getLong(2), + completion_tokens = resultSet.getLong(3), cost = resultSet.getDouble(4) ) summary[model] = usage diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/file/UsageManager.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/file/UsageManager.kt index 27a7f630..708ccf01 100644 --- a/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/file/UsageManager.kt +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/core/platform/file/UsageManager.kt @@ -49,14 +49,14 @@ open class UsageManager(val root: File) : UsageInterface { Session(sessionId), User(email = user), modelEnum, - com.simiacryptus.jopenai.ApiModel.Usage(prompt_tokens = value.toInt()) + com.simiacryptus.jopenai.ApiModel.Usage(prompt_tokens = value.toLong()) ) "output" -> incrementUsage( Session(sessionId), User(email = user), modelEnum, - com.simiacryptus.jopenai.ApiModel.Usage(completion_tokens = value.toInt()) + com.simiacryptus.jopenai.ApiModel.Usage(completion_tokens = value.toLong()) ) "cost" -> incrementUsage( diff --git a/gradle.properties b/gradle.properties index f6a4c021..4fcd6018 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Gradle Releases -> https://github.com/gradle/gradle/releases libraryGroup = com.simiacryptus.skyenet -libraryVersion = 1.0.85 +libraryVersion = 1.0.86 gradleVersion = 7.6.1 kotlin.daemon.jvmargs=-Xmx2g diff --git a/webui/build.gradle.kts b/webui/build.gradle.kts index 8f5d7d23..ddcbdd04 100644 --- a/webui/build.gradle.kts +++ b/webui/build.gradle.kts @@ -35,7 +35,7 @@ val jetty_version = "11.0.18" val jackson_version = "2.17.0" dependencies { - implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.0.65") { + implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.0.67") { exclude(group = "org.slf4j", module = "slf4j-api") }