feat(knowledge): 实现外部直接插入知识库

This commit is contained in:
v-zhangjc9
2025-06-05 15:20:33 +08:00
parent 8b4827b164
commit e359bed97c
5 changed files with 145 additions and 1 deletions

View File

@@ -94,7 +94,7 @@ public class KnowledgeBaseController {
@PostMapping("submit_text")
public void submitText(
@RequestParam(value = "id") Long id,
@RequestParam("id") Long id,
@RequestParam(value = "mode", defaultValue = "NORMAL") String mode,
@RequestParam(value = "type", defaultValue = "text") String type,
@RequestParam(value = "content", required = false) String content,
@@ -109,6 +109,16 @@ public class KnowledgeBaseController {
}
}
@PostMapping("submit_text_directly")
public void submitDirectly(
@RequestParam("id") Long id,
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "split_key", defaultValue = "\n\n") String splitKey,
@RequestBody String content
) {
embeddingService.submitDirectly(id, name, Lists.immutable.of(content.split(splitKey)));
}
@PostMapping("query")
public ImmutableList<String> query(
@RequestParam("id") Long id,

View File

@@ -9,6 +9,7 @@ import com.lanyuanxiaoyao.service.ai.knowledge.entity.Knowledge;
import com.lanyuanxiaoyao.service.ai.knowledge.entity.vo.DataFileVO;
import com.yomahub.liteflow.core.FlowExecutor;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -104,4 +105,32 @@ public class EmbeddingService {
}
});
}
public void submitDirectly(Long id, String name, ImmutableList<String> contents) {
executors.submit(() -> {
Knowledge knowledge = knowledgeBaseService.get(id);
String groupName = name;
if (StrUtil.isBlank(groupName)) {
groupName = StrUtil.format("外部-{}", IdUtil.nanoId(10));
}
Long groupId = groupService.add(knowledge.getId(), groupName);
EmbeddingContext context = EmbeddingContext.builder()
.vectorSourceId(knowledge.getVectorSourceId())
.groupId(groupId)
.build();
context.setDocuments(
contents
.collect(StrUtil::trim)
.collect(content ->
Document.builder()
.text(content)
.metadata(new HashMap<>())
.build()
)
.toList()
);
executor.execute2Resp("embedding_submit_directly", null, context);
groupService.finish(groupId);
});
}
}

View File

@@ -195,6 +195,7 @@ public class EmbeddingNodes {
.call()
.content();
Assert.notBlank(response, "LLM response is empty");
logger.info("LLM response: \n{}", response);
// noinspection DataFlowIssue
return Arrays.stream(StrUtil.trim(response).split("---"))
.map(text -> text.replaceAll("(?!^.+) +$", ""))
@@ -209,6 +210,8 @@ public class EmbeddingNodes {
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "import_vector_source", nodeName = "导入向量库", nodeType = NodeTypeEnum.COMMON)
public void importVectorSource(NodeComponent node) {
EmbeddingContext context = node.getContextBean(EmbeddingContext.class);
Assert.notNull(context.getVectorSourceId(), "VectorSourceId is null");
Assert.notNull(context.getGroupId(), "GroupId is null");
if (ObjectUtil.isNotEmpty(context.getDocuments())) {
VectorStore vs = QdrantVectorStore.builder(qdrantClient, embeddingModel)
.collectionName(String.valueOf(context.getVectorSourceId()))

View File

@@ -20,4 +20,7 @@
<chain id="embedding_submit">
SER(embedding_preview, import_vector_source)
</chain>
<chain id="embedding_submit_directly">
SER(import_vector_source)
</chain>
</flow>