-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First approach for adding metrics to Conversations * Spotless apply * Spotless apply * LogsMetrics improved * Spotless apply
- Loading branch information
1 parent
bf8955d
commit 3d216a3
Showing
33 changed files
with
432 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
core/src/commonMain/kotlin/com/xebia/functional/xef/llm/MetricManagement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.xebia.functional.xef.llm | ||
|
||
import com.xebia.functional.xef.conversation.Conversation | ||
import com.xebia.functional.xef.llm.models.chat.ChatCompletionResponse | ||
import com.xebia.functional.xef.llm.models.chat.ChatCompletionResponseWithFunctions | ||
|
||
fun ChatCompletionResponseWithFunctions.addMetrics( | ||
conversation: Conversation | ||
): ChatCompletionResponseWithFunctions { | ||
conversation.metric.log(conversation, "Model: ${`object`}") | ||
conversation.metric.log( | ||
conversation, | ||
"Tokens: ${usage.promptTokens} (prompt) + ${usage.completionTokens} (completion) = ${usage.totalTokens}" | ||
) | ||
return this | ||
} | ||
|
||
fun ChatCompletionResponse.addMetrics(conversation: Conversation): ChatCompletionResponse { | ||
conversation.metric.log(conversation, "Model: ${`object`}") | ||
conversation.metric.log( | ||
conversation, | ||
"Tokens: ${usage.promptTokens} (prompt) + ${usage.completionTokens} (completion) = ${usage.totalTokens}" | ||
) | ||
return this | ||
} |
29 changes: 29 additions & 0 deletions
29
core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.xebia.functional.xef.metrics | ||
|
||
import com.xebia.functional.xef.conversation.Conversation | ||
import com.xebia.functional.xef.prompt.Prompt | ||
import io.ktor.util.date.* | ||
|
||
class LogsMetric : Metric { | ||
|
||
private val identSize = 4 | ||
|
||
override suspend fun <A> promptSpan( | ||
conversation: Conversation, | ||
prompt: Prompt, | ||
block: suspend Metric.() -> A | ||
): A { | ||
val milis = getTimeMillis() | ||
val name = prompt.messages.lastOrNull()?.content ?: "empty" | ||
println("Prompt-Span: $name") | ||
val output = block() | ||
println("${writeIdent()}|-- Finished in ${getTimeMillis()-milis} ms") | ||
return output | ||
} | ||
|
||
override fun log(conversation: Conversation, message: String) { | ||
println("${writeIdent()}|-- $message".padStart(identSize, ' ')) | ||
} | ||
|
||
private fun writeIdent(times: Int = 1) = (1..identSize * times).fold("") { a, b -> "$a " } | ||
} |
14 changes: 14 additions & 0 deletions
14
core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/Metric.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.xebia.functional.xef.metrics | ||
|
||
import com.xebia.functional.xef.conversation.Conversation | ||
import com.xebia.functional.xef.prompt.Prompt | ||
|
||
interface Metric { | ||
suspend fun <A> promptSpan( | ||
conversation: Conversation, | ||
prompt: Prompt, | ||
block: suspend Metric.() -> A | ||
): A | ||
|
||
fun log(conversation: Conversation, message: String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
core/src/jsMain/kotlin/com/xebia/functional/xef/conversation/JSConversation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
core/src/jsMain/kotlin/com/xebia/functional/xef/conversation/PlatformConversation.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
package com.xebia.functional.xef.conversation | ||
|
||
import com.xebia.functional.xef.metrics.Metric | ||
import com.xebia.functional.xef.store.ConversationId | ||
import com.xebia.functional.xef.store.VectorStore | ||
|
||
actual abstract class PlatformConversation | ||
actual constructor(store: VectorStore, conversationId: ConversationId?) : Conversation, AutoClose { | ||
actual companion object { | ||
actual fun create(store: VectorStore, conversationId: ConversationId?): PlatformConversation { | ||
actual fun create( | ||
store: VectorStore, | ||
metric: Metric, | ||
conversationId: ConversationId? | ||
): PlatformConversation { | ||
conversationId?.let { store.updateIndexByConversationId(conversationId) } | ||
return JSConversation(store, conversationId) | ||
return JSConversation(store, metric, conversationId) | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
core/src/jvmMain/kotlin/com/xebia/functional/xef/conversation/JVMConversation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.