Skip to content

Commit

Permalink
1.0.86 (#91)
Browse files Browse the repository at this point in the history
* 1.0.86

* wip

* wip

* Update HSQLUsageManager.kt
  • Loading branch information
acharneski authored Aug 13, 2024
1 parent d7ac324 commit 9ed443a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 43 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ Maven:
<dependency>
<groupId>com.simiacryptus</groupId>
<artifactId>skyenet-webui</artifactId>
<version>1.0.65</version>
<version>1.0.67</version>
</dependency>
```

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
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ open class ParsedActor<T : Any>(
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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)
)
"""
)
}

Expand All @@ -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<OpenAIModel, ApiModel.Usage> {
Expand Down Expand Up @@ -103,19 +107,19 @@ 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 ?: "")
statement.setString(3, usageKey.model.toString())
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))
)
}
Expand All @@ -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()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion webui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down

0 comments on commit 9ed443a

Please sign in to comment.