feat: KnowledgeDocServiceWithCreator, optimize
This commit is contained in:
+16
-13
@@ -87,25 +87,28 @@ KnowledgeDocService = create_model_service(
|
|||||||
|
|
||||||
# /*---------------------------------------KnowledgeDocServiceWithCreator-------------------------------------------*/
|
# /*---------------------------------------KnowledgeDocServiceWithCreator-------------------------------------------*/
|
||||||
|
|
||||||
|
def _create_document(cls: KnowledgeDocModel):
|
||||||
|
metadata = cls.model_dump()
|
||||||
|
# 删除metadata中的content属性值,避免将原文内容保存到元信息中
|
||||||
|
del metadata['content']
|
||||||
|
return Document(
|
||||||
|
doc_id=cls.id,
|
||||||
|
text=cls.content,
|
||||||
|
metadata=metadata
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# 新建文档之后,要给对应的文档创建向量索引
|
# 新建文档之后,要给对应的文档创建向量索引
|
||||||
async def handle_after_insert(insert_cls: KnowledgeDocModel, row_dict: dict, session: AsyncSessionDep):
|
async def handle_after_insert(insert_cls: KnowledgeDocModel, row_dict: dict, session: AsyncSessionDep):
|
||||||
await milvus_service.async_create_index_from_documents([Document(
|
metadata = insert_cls.model_dump()
|
||||||
doc_id=insert_cls.id,
|
# 删除metadata中的content属性值,避免将原文内容保存到元信息中
|
||||||
text=insert_cls.content,
|
del metadata['content']
|
||||||
metadata=insert_cls.model_dump()
|
await milvus_service.async_create_index_from_documents([_create_document(insert_cls)])
|
||||||
)])
|
|
||||||
|
|
||||||
|
|
||||||
# 批量新建之后,给所有对应的文档创建向量索引
|
# 批量新建之后,给所有对应的文档创建向量索引
|
||||||
async def handle_after_batch_insert(insert_cls_list: list[KnowledgeDocModel], row_dict_list: list[dict], session: AsyncSessionDep):
|
async def handle_after_batch_insert(insert_cls_list: list[KnowledgeDocModel], row_dict_list: list[dict], session: AsyncSessionDep):
|
||||||
document_list = [
|
document_list = [_create_document(insert_cls) for insert_cls in insert_cls_list]
|
||||||
Document(
|
|
||||||
doc_id=insert_cls.id,
|
|
||||||
text=insert_cls.content,
|
|
||||||
metadata=insert_cls.model_dump()
|
|
||||||
)
|
|
||||||
for insert_cls in insert_cls_list
|
|
||||||
]
|
|
||||||
await milvus_service.async_create_index_from_documents(document_list)
|
await milvus_service.async_create_index_from_documents(document_list)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user