feat: 上传文档块的时候,文档块保留用户的名称
This commit is contained in:
@@ -3,6 +3,7 @@ from http.client import HTTPException
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from fastapi import UploadFile, File, Form
|
from fastapi import UploadFile, File, Form
|
||||||
|
from starlette.requests import Request
|
||||||
|
|
||||||
from app.utils.db_utils import AsyncSessionDep
|
from app.utils.db_utils import AsyncSessionDep
|
||||||
from app.utils.knowledge_utils import knowledge_service
|
from app.utils.knowledge_utils import knowledge_service
|
||||||
@@ -45,6 +46,7 @@ def add_knowledge_route(app):
|
|||||||
@app.post("/knowledge/upload_files")
|
@app.post("/knowledge/upload_files")
|
||||||
async def knowledge_search(
|
async def knowledge_search(
|
||||||
session: AsyncSessionDep,
|
session: AsyncSessionDep,
|
||||||
|
request: Request,
|
||||||
files: List[UploadFile] = File(...),
|
files: List[UploadFile] = File(...),
|
||||||
kb_code: str = Form(..., description="所属知识库的编码"),
|
kb_code: str = Form(..., description="所属知识库的编码"),
|
||||||
):
|
):
|
||||||
@@ -64,7 +66,12 @@ def add_knowledge_route(app):
|
|||||||
file_dict_list = [item["result"] for item in task_result_list]
|
file_dict_list = [item["result"] for item in task_result_list]
|
||||||
|
|
||||||
# 插入对应的文档对象记录
|
# 插入对应的文档对象记录
|
||||||
doc_cls_list = await knowledge_service.save_knowledge_doc_list(session=session, file_dict_list=file_dict_list, kb_code=kb_code)
|
doc_cls_list = await knowledge_service.save_knowledge_doc_list(
|
||||||
|
session=session,
|
||||||
|
file_dict_list=file_dict_list,
|
||||||
|
kb_code=kb_code,
|
||||||
|
user=request.state.user,
|
||||||
|
)
|
||||||
|
|
||||||
# 异步处理嵌入文档,不再等待
|
# 异步处理嵌入文档,不再等待
|
||||||
[asyncio.create_task(knowledge_service.process_doc_cls(doc_cls=doc_cls)) for doc_cls in doc_cls_list]
|
[asyncio.create_task(knowledge_service.process_doc_cls(doc_cls=doc_cls)) for doc_cls in doc_cls_list]
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from llama_index.core import SimpleDirectoryReader, Document
|
|||||||
from app.config.env import env
|
from app.config.env import env
|
||||||
from app.model.FileModel import FileSaveService
|
from app.model.FileModel import FileSaveService
|
||||||
from app.model.KnowledgeDoc import KnowledgeDocModel, KnowledgeDocService
|
from app.model.KnowledgeDoc import KnowledgeDocModel, KnowledgeDocService
|
||||||
|
from app.model.UserModel import PublicUser
|
||||||
from app.utils.db_utils import AsyncSessionDep, async_session
|
from app.utils.db_utils import AsyncSessionDep, async_session
|
||||||
from app.utils.milvus_utils import milvus_service
|
from app.utils.milvus_utils import milvus_service
|
||||||
|
|
||||||
@@ -24,11 +25,24 @@ class KnowledgeService:
|
|||||||
return await FileSaveService.saveFile(session=session, file=file, filename=file.filename, file_record={})
|
return await FileSaveService.saveFile(session=session, file=file, filename=file.filename, file_record={})
|
||||||
|
|
||||||
# 将 file_dict_list 保存为 doc_cls_list
|
# 将 file_dict_list 保存为 doc_cls_list
|
||||||
async def save_knowledge_doc_list(self, session: AsyncSessionDep, file_dict_list: List[dict], kb_code: str):
|
async def save_knowledge_doc_list(
|
||||||
|
self,
|
||||||
|
session: AsyncSessionDep,
|
||||||
|
file_dict_list: List[dict],
|
||||||
|
kb_code: str,
|
||||||
|
user: PublicUser,
|
||||||
|
):
|
||||||
# 先插入文档对象
|
# 先插入文档对象
|
||||||
|
|
||||||
tobe_insert_kd_list = [
|
tobe_insert_kd_list = [
|
||||||
KnowledgeDocModel(id=file_dict["id"], name=file_dict["name"], path=file_dict["path"], parent_code=kb_code, status="process").model_dump()
|
KnowledgeDocModel(
|
||||||
|
id=file_dict["id"],
|
||||||
|
created_by=user.id,
|
||||||
|
name=file_dict["name"],
|
||||||
|
path=file_dict["path"],
|
||||||
|
parent_code=kb_code,
|
||||||
|
status="process"
|
||||||
|
).model_dump()
|
||||||
for file_dict in file_dict_list
|
for file_dict in file_dict_list
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user