From 21c3e453e256565361a49d2df3b4480bffea2499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Monta=C3=B1ez?= Date: Tue, 7 May 2024 12:18:03 +0200 Subject: [PATCH] Revert "Merge branch 'main' into dependabot/gradle/io.opentelemetry-opentelemetry-semconv-1.31.0-alpha-SNAPSHOT" This reverts commit 66dac7c24164906c99540e7123ea53ce38597d85, reversing changes made to b06a8072cfeb137b5e5c06a80996ff8393f1a168. --- .../functional/xef/llm/MetricManagement.kt | 41 ------------------- .../xef/llm/assistants/AssistantThread.kt | 7 ++-- .../functional/xef/metrics/LogsMetric.kt | 15 ------- .../xebia/functional/xef/metrics/Metric.kt | 4 -- .../xebia/functional/xef/assistants/DSL.kt | 1 + .../OpenTelemetryAssistantState.kt | 18 -------- .../xef/opentelemetry/OpenTelemetryMetric.kt | 3 -- 7 files changed, 4 insertions(+), 85 deletions(-) diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/MetricManagement.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/MetricManagement.kt index c98eb8ae6..a7c123567 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/MetricManagement.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/MetricManagement.kt @@ -2,9 +2,7 @@ package com.xebia.functional.xef.llm import com.xebia.functional.openai.generated.model.CreateChatCompletionResponse import com.xebia.functional.openai.generated.model.RunObject -import com.xebia.functional.openai.generated.model.RunStepObject import com.xebia.functional.xef.conversation.Conversation -import com.xebia.functional.xef.llm.assistants.RunDelta import com.xebia.functional.xef.metrics.Metric import com.xebia.functional.xef.prompt.Prompt import com.xebia.functional.xef.prompt.completionRole @@ -22,18 +20,6 @@ suspend fun CreateChatCompletionResponse.addMetrics( ) conversation.metric.parameter("openai.chat_completion.token.count", "${it.totalTokens}") } - choices.forEach { choice -> - choice.message.content?.let { - conversation.metric.parameter("openai.chat_completion.choice.${choice.index}.content", it) - } - choice.message.toolCalls?.zip(choice.message.toolCalls!!.indices)?.forEach { - (toolCall, toolCallIndex) -> - conversation.metric.parameter( - "openai.chat_completion.choice.${choice.index}.tool_call.$toolCallIndex", - toolCall.function.arguments - ) - } - } return this } @@ -64,30 +50,3 @@ suspend fun RunObject.addMetrics(metric: Metric): RunObject { metric.assistantCreateRun(this) return this } - -suspend fun RunStepObject.addMetrics(metric: Metric): RunStepObject { - metric.assistantCreateRunStep(this) - return this -} - -suspend fun RunDelta.addMetrics(metric: Metric): RunDelta { - when (this) { - is RunDelta.RunCancelled -> run.addMetrics(metric) - is RunDelta.RunCancelling -> run.addMetrics(metric) - is RunDelta.RunCompleted -> run.addMetrics(metric) - is RunDelta.RunCreated -> run.addMetrics(metric) - is RunDelta.RunExpired -> run.addMetrics(metric) - is RunDelta.RunFailed -> run.addMetrics(metric) - is RunDelta.RunInProgress -> run.addMetrics(metric) - is RunDelta.RunQueued -> run.addMetrics(metric) - is RunDelta.RunRequiresAction -> run.addMetrics(metric) - is RunDelta.RunStepCancelled -> runStep.addMetrics(metric) - is RunDelta.RunStepCompleted -> runStep.addMetrics(metric) - is RunDelta.RunStepCreated -> runStep.addMetrics(metric) - is RunDelta.RunStepExpired -> runStep.addMetrics(metric) - is RunDelta.RunStepFailed -> runStep.addMetrics(metric) - is RunDelta.RunStepInProgress -> runStep.addMetrics(metric) - else -> {} // ignore other cases - } - return this -} diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/AssistantThread.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/AssistantThread.kt index 9f6851d49..ed5be6179 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/AssistantThread.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/AssistantThread.kt @@ -67,9 +67,6 @@ class AssistantThread( ) .data - suspend fun createRun(assistant: Assistant): RunObject = - createRun(CreateRunRequest(assistantId = assistant.assistantId)) - suspend fun createRun(request: CreateRunRequest): RunObject = api.createRun(threadId, request, configure = ::defaultConfig).addMetrics(metric) @@ -77,7 +74,6 @@ class AssistantThread( api .createRunStream(threadId, request, configure = ::defaultConfig) .map { RunDelta.fromServerSentEvent(it) } - .map { it.addMetrics(metric) } .collect { event -> when (event) { // submit tool outputs and join streams @@ -188,6 +184,9 @@ class AssistantThread( suspend fun getRun(runId: String): RunObject = api.getRun(threadId, runId, configure = ::defaultConfig) + suspend fun createRun(assistant: Assistant): RunObject = + createRun(CreateRunRequest(assistantId = assistant.assistantId)) + fun run(assistant: Assistant): Flow = createRunStream(assistant, CreateRunRequest(assistantId = assistant.assistantId)) 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 b5bd1562f..a0bdecd1a 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 @@ -67,21 +67,6 @@ class LogsMetric(private val level: Level = Level.INFO) : Metric { return output } - override suspend fun assistantCreateRunStep(runObject: RunStepObject) { - logger.at(level) { - this.message = "${writeIndent(numberOfBlocks.get())}|-- AssistantId: ${runObject.assistantId}" - } - logger.at(level) { - this.message = "${writeIndent(numberOfBlocks.get())}|-- ThreadId: ${runObject.threadId}" - } - logger.at(level) { - this.message = "${writeIndent(numberOfBlocks.get())}|-- RunId: ${runObject.id}" - } - logger.at(level) { - this.message = "${writeIndent(numberOfBlocks.get())}|-- Status: ${runObject.status.name}" - } - } - override suspend fun assistantCreatedMessage( runId: String, block: suspend Metric.() -> List 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 62661f070..b03621930 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 @@ -20,8 +20,6 @@ interface Metric { suspend fun assistantCreateRun(runId: String, block: suspend Metric.() -> RunObject): RunObject - suspend fun assistantCreateRunStep(runObject: RunStepObject) - suspend fun assistantCreatedMessage( runId: String, block: suspend Metric.() -> List @@ -53,8 +51,6 @@ interface Metric { block: suspend Metric.() -> RunObject ): RunObject = block() - override suspend fun assistantCreateRunStep(runObject: RunStepObject) {} - override suspend fun assistantCreatedMessage( runId: String, block: suspend Metric.() -> List diff --git a/examples/src/main/kotlin/com/xebia/functional/xef/assistants/DSL.kt b/examples/src/main/kotlin/com/xebia/functional/xef/assistants/DSL.kt index ff00ac381..76472cc92 100644 --- a/examples/src/main/kotlin/com/xebia/functional/xef/assistants/DSL.kt +++ b/examples/src/main/kotlin/com/xebia/functional/xef/assistants/DSL.kt @@ -1,5 +1,6 @@ package com.xebia.functional.xef.assistants +import com.xebia.functional.openai.generated.model.* import com.xebia.functional.xef.OpenAI import com.xebia.functional.xef.llm.assistants.Assistant import com.xebia.functional.xef.llm.assistants.AssistantThread diff --git a/integrations/opentelemetry/src/main/kotlin/com/xebia/functional/xef/opentelemetry/OpenTelemetryAssistantState.kt b/integrations/opentelemetry/src/main/kotlin/com/xebia/functional/xef/opentelemetry/OpenTelemetryAssistantState.kt index ad2d09f36..11554ff4a 100644 --- a/integrations/opentelemetry/src/main/kotlin/com/xebia/functional/xef/opentelemetry/OpenTelemetryAssistantState.kt +++ b/integrations/opentelemetry/src/main/kotlin/com/xebia/functional/xef/opentelemetry/OpenTelemetryAssistantState.kt @@ -74,24 +74,6 @@ class OpenTelemetryAssistantState(private val tracer: Tracer) { } } - fun runStepSpan(runObject: RunStepObject) { - - val parentOrRoot: Context = runObject.runId.getOrCreateContext() - - val currentSpan = - tracer - .spanBuilder("step ${runObject.status.name} ${runObject.id}") - .setParent(parentOrRoot) - .setSpanKind(SpanKind.CLIENT) - .startSpan() - - try { - currentSpan.makeCurrent().use { runObject.setParameters(currentSpan) } - } finally { - currentSpan.end() - } - } - suspend fun runStepSpan(runId: String, block: suspend () -> RunStepObject): RunStepObject { val parentOrRoot: Context = runId.getOrCreateContext() diff --git a/integrations/opentelemetry/src/main/kotlin/com/xebia/functional/xef/opentelemetry/OpenTelemetryMetric.kt b/integrations/opentelemetry/src/main/kotlin/com/xebia/functional/xef/opentelemetry/OpenTelemetryMetric.kt index 3ee6c4728..4a0cc8c56 100644 --- a/integrations/opentelemetry/src/main/kotlin/com/xebia/functional/xef/opentelemetry/OpenTelemetryMetric.kt +++ b/integrations/opentelemetry/src/main/kotlin/com/xebia/functional/xef/opentelemetry/OpenTelemetryMetric.kt @@ -43,9 +43,6 @@ class OpenTelemetryMetric( block: suspend Metric.() -> RunObject ): RunObject = assistantState.runSpan(runId) { block() } - override suspend fun assistantCreateRunStep(runObject: RunStepObject) = - assistantState.runStepSpan(runObject) - override suspend fun assistantCreatedMessage( runId: String, block: suspend Metric.() -> List