From 103272b9c8ed090950a6bfdf678f0c8463f027b6 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Sat, 16 Sep 2023 21:34:24 +0800 Subject: [PATCH] fix: fix relevant code issue --- .../domains/semantic/CodeSemanticWorkflow.kt | 2 +- .../semantic/flow/SemanticSolutionExecutor.kt | 26 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/cc/unitmesh/cf/domains/semantic/CodeSemanticWorkflow.kt b/src/main/kotlin/cc/unitmesh/cf/domains/semantic/CodeSemanticWorkflow.kt index 37bf7e4e..b1d07c4a 100644 --- a/src/main/kotlin/cc/unitmesh/cf/domains/semantic/CodeSemanticWorkflow.kt +++ b/src/main/kotlin/cc/unitmesh/cf/domains/semantic/CodeSemanticWorkflow.kt @@ -88,7 +88,7 @@ class CodeSemanticWorkflow : Workflow() { | - Keep number of quoted lines of code to a minimum when possible | - Basic markdown is allowed | - If you have enough information, try your best to answer more details. - | - You muse use PlantUML to provide key process of your thinking, + | - You MUST provide key process of your thinking, | |相关的代码: |${'$'}{relevantCode} diff --git a/src/main/kotlin/cc/unitmesh/cf/domains/semantic/flow/SemanticSolutionExecutor.kt b/src/main/kotlin/cc/unitmesh/cf/domains/semantic/flow/SemanticSolutionExecutor.kt index c7ce900d..d35ca8ba 100644 --- a/src/main/kotlin/cc/unitmesh/cf/domains/semantic/flow/SemanticSolutionExecutor.kt +++ b/src/main/kotlin/cc/unitmesh/cf/domains/semantic/flow/SemanticSolutionExecutor.kt @@ -5,6 +5,9 @@ import cc.unitmesh.cf.core.flow.SolutionExecutor import cc.unitmesh.cf.core.flow.model.Answer import cc.unitmesh.cf.core.llms.LlmMsg import cc.unitmesh.cf.core.llms.LlmProvider +import cc.unitmesh.cf.core.parser.MarkdownCode +import cc.unitmesh.cf.domains.interpreter.flow.CodeInput +import cc.unitmesh.cf.domains.interpreter.flow.CodeSolutionExecutor import cc.unitmesh.cf.domains.semantic.CodeSemanticWorkflow import cc.unitmesh.cf.domains.semantic.context.SemanticVariableResolver import cc.unitmesh.cf.domains.semantic.model.ExplainQuery @@ -14,7 +17,10 @@ import cc.unitmesh.nlp.embedding.OpenAiEncoding import cc.unitmesh.rag.document.Document import cc.unitmesh.rag.document.DocumentOrder import cc.unitmesh.rag.store.EmbeddingStore +import io.reactivex.rxjava3.core.BackpressureStrategy import io.reactivex.rxjava3.core.Flowable +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json class SemanticSolutionExecutor( val completion: LlmProvider, @@ -52,14 +58,13 @@ class SemanticSolutionExecutor( codes.add(it.score to it.embedded.text) variables.putCode("", codes.map { it.second }) val testPrompt = variables.compile(basePrompt) - // todo: make 3072 configurable + // todo: make 2048 configurable if (encodingTokenizer.encode(testPrompt).size >= 2048) { codes.removeAt(codes.size - 1) return@forEach } } - val reorderCodes = DocumentOrder.lostInMiddleReorder(codes) variables.putCode("", reorderCodes.map { it.second }) val finalPrompt = variables.compile(basePrompt) @@ -89,7 +94,20 @@ class SemanticSolutionExecutor( |``` |""".trimMargin() - return Flowable.just(Answer(this.javaClass.name, debugInfo)) - .concatWith(completion.map { Answer(this.javaClass.name, it) }) + return Flowable.create({ emitter -> + emitter.onNext(Answer(this.javaClass.name, debugInfo)) + completion + .subscribe( + { result -> + val answer = Answer(this.javaClass.name, result) + emitter.onNext(answer) + }, + { throwable: Throwable -> + emitter.tryOnError(throwable) + }) + { + emitter.onComplete() + } + }, BackpressureStrategy.BUFFER) } }