71 lines
2.6 KiB
Python
71 lines
2.6 KiB
Python
import asyncio
|
|
from http.client import HTTPException
|
|
from typing import List
|
|
|
|
from fastapi import UploadFile, File, Form
|
|
|
|
from app.utils.db_utils import AsyncSessionDep
|
|
from app.utils.knowledge_utils import knowledge_service
|
|
|
|
|
|
def add_knowledge_route(app):
|
|
# @app.get("/knowledge/search")
|
|
# async def knowledge_search(text: str = Param()):
|
|
# print("text", text)
|
|
# query_result = await milvus_service.async_search(text or "hello")
|
|
# return query_result
|
|
#
|
|
# @app.post("/knowledge/embed_text")
|
|
# async def knowledge_embed_text(text_list: List[str]):
|
|
# id_list = await next_id(len(text_list))
|
|
# document_list = [
|
|
# Document(
|
|
# text=text,
|
|
# id=id_list[index],
|
|
# metadata={"kb_id": "cde"}
|
|
# )
|
|
# for index, text in enumerate(text_list)
|
|
# ]
|
|
# await milvus_service.async_create_index_from_documents(document_list)
|
|
# search_result = await milvus_service.async_search("hello")
|
|
# return {
|
|
# "result": "嵌入成功:",
|
|
# "origin_documents": [document.to_dict() for document in document_list],
|
|
# "search_documents": search_result,
|
|
# }
|
|
#
|
|
# @app.post("/knowledge/delete")
|
|
# async def knowledge_embed_text(body: dict):
|
|
# await milvus_service.async_delete(body.get('id'))
|
|
# return {
|
|
# "result": "删除成功",
|
|
@app.post("/knowledge/upload_files")
|
|
async def knowledge_search(
|
|
session: AsyncSessionDep,
|
|
files: List[UploadFile] = File(...),
|
|
kb_code: str = Form(..., description="所属知识库的编码"),
|
|
):
|
|
"""
|
|
批量上传文档并嵌入到向量数据库(同步响应版本)
|
|
"""
|
|
if not files:
|
|
raise HTTPException(status_code=400, detail="No files provided")
|
|
|
|
# 限制文件数量
|
|
if len(files) > 20:
|
|
raise HTTPException(status_code=400, detail="Maximum 20 files allowed per upload")
|
|
|
|
# 先将所有文件直接保存到本地
|
|
task_list = [asyncio.create_task(knowledge_service.save_file_with_new_session(file=file)) for file in files]
|
|
task_result_list = await asyncio.gather(*task_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)
|
|
|
|
# 异步处理嵌入文档,不再等待
|
|
[asyncio.create_task(knowledge_service.process_file_dict(file_dict=file_dict, kb_code=kb_code)) for file_dict in file_dict_list]
|
|
|
|
# 直接返回文档对象
|
|
return {"message": f"正在处理 {len(doc_cls_list)} 个文档,请刷新列表查看文档状态。", "result": [item.model_dump() for item in doc_cls_list]}
|