diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt index de0f825b2..63cda5b9d 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt @@ -6,25 +6,25 @@ import io.ktor.util.date.* class LogsMetric : Metric { - private val identSize = 4 + private val indentSize = 4 override suspend fun 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 " } } diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/Metric.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/Metric.kt index f8b9f4e62..b025e5e74 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/Metric.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/Metric.kt @@ -11,4 +11,17 @@ interface Metric { ): A fun log(conversation: Conversation, message: String) + + companion object { + val EMPTY: Metric = + object : Metric { + override suspend fun promptSpan( + conversation: Conversation, + prompt: Prompt, + block: suspend Metric.() -> A + ): A = block() + + override fun log(conversation: Conversation, message: String) {} + } + } } diff --git a/examples/kotlin/src/main/kotlin/com/xebia/functional/xef/conversation/conversations/Animal.kt b/examples/kotlin/src/main/kotlin/com/xebia/functional/xef/conversation/conversations/Animal.kt index 8caa1c8ce..d9dec7804 100644 --- a/examples/kotlin/src/main/kotlin/com/xebia/functional/xef/conversation/conversations/Animal.kt +++ b/examples/kotlin/src/main/kotlin/com/xebia/functional/xef/conversation/conversations/Animal.kt @@ -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 @@ -29,7 +30,7 @@ suspend fun main() { messagePolicy { addMessagesFromConversation = MessagesFromHistory.NONE } } - OpenAI.conversation { + OpenAI.conversation(metric = LogsMetric()) { val animal: Animal = prompt( Prompt { +user("A unique animal species.") } @@ -42,10 +43,8 @@ suspend fun main() { .copy(configuration = configNoneFromConversation) ) - println() - println("Animal: $animal") + println("\nAnimal: $animal") println("Invention: $invention") - println() val storyPrompt = Prompt { @@ -61,12 +60,7 @@ 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") @@ -74,9 +68,6 @@ suspend fun main() { val story2: String = promptMessage(storyPrompt2) - println() - println("Story 2:") - println() - println(story2) + println("\nStory 2:\n$story2\n") } } diff --git a/gpt4all-kotlin/src/jvmMain/kotlin/com/xebia/functional/gpt4all/Conversation.kt b/gpt4all-kotlin/src/jvmMain/kotlin/com/xebia/functional/gpt4all/Conversation.kt index d4fe96f01..fc9b74165 100644 --- a/gpt4all-kotlin/src/jvmMain/kotlin/com/xebia/functional/gpt4all/Conversation.kt +++ b/gpt4all-kotlin/src/jvmMain/kotlin/com/xebia/functional/gpt4all/Conversation.kt @@ -8,6 +8,6 @@ import com.xebia.functional.xef.store.VectorStore suspend inline fun conversation( store: VectorStore = LocalVectorStore(HuggingFaceLocalEmbeddings.DEFAULT), - metric: Metric = LogsMetric(), + metric: Metric = Metric.EMPTY, noinline block: suspend Conversation.() -> A ): A = block(Conversation(store, metric)) diff --git a/gpt4all-kotlin/src/jvmMain/kotlin/com/xebia/functional/gpt4all/GPT4All.kt b/gpt4all-kotlin/src/jvmMain/kotlin/com/xebia/functional/gpt4all/GPT4All.kt index c935bfcdd..84e97316f 100644 --- a/gpt4all-kotlin/src/jvmMain/kotlin/com/xebia/functional/gpt4all/GPT4All.kt +++ b/gpt4all-kotlin/src/jvmMain/kotlin/com/xebia/functional/gpt4all/GPT4All.kt @@ -40,7 +40,8 @@ interface GPT4All : AutoCloseable, Chat, Completion { @JvmSynthetic suspend inline fun conversation( - store: VectorStore, + store: VectorStore = LocalVectorStore(HuggingFaceLocalEmbeddings.DEFAULT), + metric: Metric = Metric.EMPTY, noinline block: suspend Conversation.() -> A ): A = block(conversation(store)) @@ -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( diff --git a/integrations/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/GCP.kt b/integrations/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/GCP.kt index 817b04f60..baf7e986d 100644 --- a/integrations/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/GCP.kt +++ b/integrations/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/GCP.kt @@ -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 @@ -63,8 +62,8 @@ class GCP(projectId: String? = null, location: VertexAIRegion? = null, token: St @JvmSynthetic suspend inline fun 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)) @@ -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 GCP.conversation(noinline block: suspend Conversation.() -> A): A = - block(Conversation(LocalVectorStore(DEFAULT_EMBEDDING), LogsMetric())) diff --git a/kotlin/src/commonMain/kotlin/com/xebia/functional/xef/conversation/DSLExtensions.kt b/kotlin/src/commonMain/kotlin/com/xebia/functional/xef/conversation/DSLExtensions.kt index c7312d0e7..d0a8ebcc0 100644 --- a/kotlin/src/commonMain/kotlin/com/xebia/functional/xef/conversation/DSLExtensions.kt +++ b/kotlin/src/commonMain/kotlin/com/xebia/functional/xef/conversation/DSLExtensions.kt @@ -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 @@ -17,6 +16,6 @@ import com.xebia.functional.xef.store.VectorStore suspend inline fun conversation( embeddings: Embeddings, store: VectorStore = LocalVectorStore(embeddings), - metric: Metric = LogsMetric(), + metric: Metric = Metric.EMPTY, noinline block: suspend Conversation.() -> A ): A = block(Conversation(store, metric)) diff --git a/openai/src/commonMain/kotlin/com/xebia/functional/xef/conversation/llm/openai/OpenAI.kt b/openai/src/commonMain/kotlin/com/xebia/functional/xef/conversation/llm/openai/OpenAI.kt index d5d08d4d7..8daa88ccc 100644 --- a/openai/src/commonMain/kotlin/com/xebia/functional/xef/conversation/llm/openai/OpenAI.kt +++ b/openai/src/commonMain/kotlin/com/xebia/functional/xef/conversation/llm/openai/OpenAI.kt @@ -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 @@ -215,8 +214,8 @@ class OpenAI( @JvmSynthetic suspend inline fun 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)) @@ -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) } }