Skip to content

Commit

Permalink
Added empty metrics by default (#508)
Browse files Browse the repository at this point in the history
* added empty metrics

* removed logger
  • Loading branch information
Montagon authored Oct 26, 2023
1 parent 3986df1 commit d164266
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import io.ktor.util.date.*

class LogsMetric : Metric {

private val identSize = 4
private val indentSize = 4

override suspend fun <A> promptSpan(
conversation: Conversation,
prompt: Prompt,
block: suspend Metric.() -> A
): A {
val milis = getTimeMillis()
val millis = getTimeMillis()
val name = prompt.messages.lastOrNull()?.content ?: "empty"
println("Prompt-Span: $name")
println("${writeIdent()}|-- Conversation Id: ${conversation.conversationId?.value ?: "empty"}")
println("${writeIndent()}|-- Conversation Id: ${conversation.conversationId?.value ?: "empty"}")
val output = block()
println("${writeIdent()}|-- Finished in ${getTimeMillis()-milis} ms")
println("${writeIndent()}|-- Finished in ${getTimeMillis() - millis} ms")
return output
}

override fun log(conversation: Conversation, message: String) {
println("${writeIdent()}|-- $message")
println("${writeIndent()}|-- $message")
}

private fun writeIdent(times: Int = 1) = (1..identSize * times).fold("") { a, b -> "$a " }
private fun writeIndent(times: Int = 1) = (1..indentSize * times).fold("") { a, b -> "$a " }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@ interface Metric {
): A

fun log(conversation: Conversation, message: String)

companion object {
val EMPTY: Metric =
object : Metric {
override suspend fun <A> promptSpan(
conversation: Conversation,
prompt: Prompt,
block: suspend Metric.() -> A
): A = block()

override fun log(conversation: Conversation, message: String) {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.xebia.functional.xef.conversation.MessagesToHistory
import com.xebia.functional.xef.conversation.llm.openai.OpenAI
import com.xebia.functional.xef.conversation.llm.openai.prompt
import com.xebia.functional.xef.conversation.llm.openai.promptMessage
import com.xebia.functional.xef.metrics.LogsMetric
import com.xebia.functional.xef.prompt.Prompt
import com.xebia.functional.xef.prompt.configuration.PromptConfiguration
import com.xebia.functional.xef.prompt.templates.system
Expand All @@ -29,7 +30,7 @@ suspend fun main() {
messagePolicy { addMessagesFromConversation = MessagesFromHistory.NONE }
}

OpenAI.conversation {
OpenAI.conversation(metric = LogsMetric()) {
val animal: Animal =
prompt<Animal>(
Prompt { +user("A unique animal species.") }
Expand All @@ -42,10 +43,8 @@ suspend fun main() {
.copy(configuration = configNoneFromConversation)
)

println()
println("Animal: $animal")
println("\nAnimal: $animal")
println("Invention: $invention")
println()

val storyPrompt =
Prompt {
Expand All @@ -61,22 +60,14 @@ suspend fun main() {

val story: String = promptMessage(storyPrompt)

println()
println("Story 1:")
println()
println(story)
println()
println()
println("\nStory 1:\n$story\n")

val storyPrompt2 = Prompt {
+user("Write a short story of 100 words that involves the animal in a city called Cadiz")
}

val story2: String = promptMessage(storyPrompt2)

println()
println("Story 2:")
println()
println(story2)
println("\nStory 2:\n$story2\n")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import com.xebia.functional.xef.store.VectorStore

suspend inline fun <A> conversation(
store: VectorStore = LocalVectorStore(HuggingFaceLocalEmbeddings.DEFAULT),
metric: Metric = LogsMetric(),
metric: Metric = Metric.EMPTY,
noinline block: suspend Conversation.() -> A
): A = block(Conversation(store, metric))
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ interface GPT4All : AutoCloseable, Chat, Completion {

@JvmSynthetic
suspend inline fun <A> conversation(
store: VectorStore,
store: VectorStore = LocalVectorStore(HuggingFaceLocalEmbeddings.DEFAULT),
metric: Metric = Metric.EMPTY,
noinline block: suspend Conversation.() -> A
): A = block(conversation(store))

Expand All @@ -53,7 +54,7 @@ interface GPT4All : AutoCloseable, Chat, Completion {
@JvmOverloads
fun conversation(
store: VectorStore = LocalVectorStore(HuggingFaceLocalEmbeddings.DEFAULT),
metric: Metric = LogsMetric()
metric: Metric = Metric.EMPTY
): PlatformConversation = Conversation(store, metric)

operator fun invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.xebia.functional.xef.env.getenv
import com.xebia.functional.xef.gcp.models.GcpChat
import com.xebia.functional.xef.gcp.models.GcpEmbeddings
import com.xebia.functional.xef.llm.LLM
import com.xebia.functional.xef.metrics.LogsMetric
import com.xebia.functional.xef.metrics.Metric
import com.xebia.functional.xef.store.LocalVectorStore
import com.xebia.functional.xef.store.VectorStore
Expand Down Expand Up @@ -63,8 +62,8 @@ class GCP(projectId: String? = null, location: VertexAIRegion? = null, token: St

@JvmSynthetic
suspend inline fun <A> conversation(
store: VectorStore,
metric: Metric,
store: VectorStore = LocalVectorStore(FromEnvironment.DEFAULT_EMBEDDING),
metric: Metric = Metric.EMPTY,
noinline block: suspend Conversation.() -> A
): A = block(conversation(store, metric))

Expand All @@ -76,10 +75,7 @@ class GCP(projectId: String? = null, location: VertexAIRegion? = null, token: St
@JvmOverloads
fun conversation(
store: VectorStore = LocalVectorStore(FromEnvironment.DEFAULT_EMBEDDING),
metric: Metric = LogsMetric(),
metric: Metric = Metric.EMPTY,
): PlatformConversation = Conversation(store, metric)
}
}

suspend inline fun <A> GCP.conversation(noinline block: suspend Conversation.() -> A): A =
block(Conversation(LocalVectorStore(DEFAULT_EMBEDDING), LogsMetric()))
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.xebia.functional.xef.conversation

import com.xebia.functional.xef.llm.Embeddings
import com.xebia.functional.xef.metrics.LogsMetric
import com.xebia.functional.xef.metrics.Metric
import com.xebia.functional.xef.store.LocalVectorStore
import com.xebia.functional.xef.store.VectorStore
Expand All @@ -17,6 +16,6 @@ import com.xebia.functional.xef.store.VectorStore
suspend inline fun <A> conversation(
embeddings: Embeddings,
store: VectorStore = LocalVectorStore(embeddings),
metric: Metric = LogsMetric(),
metric: Metric = Metric.EMPTY,
noinline block: suspend Conversation.() -> A
): A = block(Conversation(store, metric))
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.xebia.functional.xef.conversation.autoClose
import com.xebia.functional.xef.conversation.llm.openai.models.*
import com.xebia.functional.xef.env.getenv
import com.xebia.functional.xef.llm.LLM
import com.xebia.functional.xef.metrics.LogsMetric
import com.xebia.functional.xef.metrics.Metric
import com.xebia.functional.xef.store.LocalVectorStore
import com.xebia.functional.xef.store.VectorStore
Expand Down Expand Up @@ -215,8 +214,8 @@ class OpenAI(

@JvmSynthetic
suspend inline fun <A> conversation(
store: VectorStore,
metric: Metric,
store: VectorStore = LocalVectorStore(FromEnvironment.DEFAULT_EMBEDDING),
metric: Metric = Metric.EMPTY,
noinline block: suspend Conversation.() -> A
): A = block(conversation(store, metric))

Expand All @@ -227,7 +226,7 @@ class OpenAI(
@JvmOverloads
fun conversation(
store: VectorStore = LocalVectorStore(FromEnvironment.DEFAULT_EMBEDDING),
metric: Metric = LogsMetric()
metric: Metric = Metric.EMPTY
): PlatformConversation = Conversation(store, metric)
}
}

0 comments on commit d164266

Please sign in to comment.