chores(knowledgebases): 清理相关冗余代码
- 删除了多余的 parse_document 函数 - 重命名 parse_document_async 为 parse_document,作为主要的文档解析接口
This commit is contained in:
parent
4262a2f829
commit
b5463870ff
|
@ -159,24 +159,6 @@ def delete_document(doc_id):
|
||||||
return error_response(str(e))
|
return error_response(str(e))
|
||||||
|
|
||||||
|
|
||||||
@knowledgebase_bp.route("/documents/<doc_id>/parse", methods=["POST"])
|
|
||||||
def parse_document(doc_id):
|
|
||||||
"""开始解析文档"""
|
|
||||||
# 处理 OPTIONS 预检请求
|
|
||||||
if request.method == "OPTIONS":
|
|
||||||
response = success_response({})
|
|
||||||
# 添加 CORS 相关头
|
|
||||||
response.headers.add("Access-Control-Allow-Methods", "POST")
|
|
||||||
response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
|
|
||||||
return response
|
|
||||||
|
|
||||||
try:
|
|
||||||
result = KnowledgebaseService.async_parse_document(doc_id)
|
|
||||||
return success_response(data=result)
|
|
||||||
except Exception as e:
|
|
||||||
return error_response(str(e), code=500)
|
|
||||||
|
|
||||||
|
|
||||||
@knowledgebase_bp.route("/documents/<doc_id>/parse/progress", methods=["GET"])
|
@knowledgebase_bp.route("/documents/<doc_id>/parse/progress", methods=["GET"])
|
||||||
def get_parse_progress(doc_id):
|
def get_parse_progress(doc_id):
|
||||||
"""获取文档解析进度"""
|
"""获取文档解析进度"""
|
||||||
|
@ -242,8 +224,8 @@ def set_system_embedding_config_route():
|
||||||
|
|
||||||
|
|
||||||
@knowledgebase_bp.route("/documents/<doc_id>/parse", methods=["POST"])
|
@knowledgebase_bp.route("/documents/<doc_id>/parse", methods=["POST"])
|
||||||
def parse_document_async(doc_id): # 函数名改为 async 以区分
|
def parse_document(doc_id):
|
||||||
"""开始异步解析单个文档"""
|
"""开始解析文档"""
|
||||||
if request.method == "OPTIONS":
|
if request.method == "OPTIONS":
|
||||||
response = success_response({})
|
response = success_response({})
|
||||||
response.headers.add("Access-Control-Allow-Methods", "POST")
|
response.headers.add("Access-Control-Allow-Methods", "POST")
|
||||||
|
@ -251,7 +233,7 @@ def parse_document_async(doc_id): # 函数名改为 async 以区分
|
||||||
return response
|
return response
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = KnowledgebaseService.parse_document(doc_id) # 调用同步版本
|
result = KnowledgebaseService.parse_document(doc_id)
|
||||||
if result.get("success"):
|
if result.get("success"):
|
||||||
return success_response(data={"message": f"文档 {doc_id} 同步解析完成。", "details": result})
|
return success_response(data={"message": f"文档 {doc_id} 同步解析完成。", "details": result})
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
from peewee import Model
|
from typing import Any, Dict, Type, TypeVar
|
||||||
from typing import Type, TypeVar, Dict, Any
|
|
||||||
|
from peewee import Model
|
||||||
|
|
||||||
|
T = TypeVar("T", bound=Model)
|
||||||
|
|
||||||
T = TypeVar('T', bound=Model)
|
|
||||||
|
|
||||||
class BaseService:
|
class BaseService:
|
||||||
model: Type[T]
|
model: Type[T]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_by_id(cls, id: str) -> T:
|
def get_by_id(cls, id: str) -> T:
|
||||||
return cls.model.get_by_id(id)
|
return cls.model.get_by_id(id)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def insert(cls, data: Dict[str, Any]) -> T:
|
def insert(cls, data: Dict[str, Any]) -> T:
|
||||||
return cls.model.create(**data)
|
return cls.model.create(**data)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def delete_by_id(cls, id: str) -> int:
|
def delete_by_id(cls, id: str) -> int:
|
||||||
return cls.model.delete().where(cls.model.id == id).execute()
|
return cls.model.delete().where(cls.model.id == id).execute()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def query(cls, **kwargs) -> list[T]:
|
def query(cls, **kwargs) -> list[T]:
|
||||||
return list(cls.model.select().where(*[
|
return list(cls.model.select().where(*[getattr(cls.model, k) == v for k, v in kwargs.items()]))
|
||||||
getattr(cls.model, k) == v for k, v in kwargs.items()
|
|
||||||
]))
|
|
||||||
|
|
|
@ -1,21 +1,20 @@
|
||||||
from peewee import *
|
from peewee import *
|
||||||
|
|
||||||
from .base_service import BaseService
|
from .base_service import BaseService
|
||||||
from .models import File2Document
|
from .models import File2Document
|
||||||
|
|
||||||
|
|
||||||
class File2DocumentService(BaseService):
|
class File2DocumentService(BaseService):
|
||||||
model = File2Document
|
model = File2Document
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_mapping(cls, file_id: str, document_id: str) -> File2Document:
|
def create_mapping(cls, file_id: str, document_id: str) -> File2Document:
|
||||||
return cls.insert({
|
return cls.insert({"file_id": file_id, "document_id": document_id})
|
||||||
'file_id': file_id,
|
|
||||||
'document_id': document_id
|
|
||||||
})
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_by_document_id(cls, document_id: str) -> list[File2Document]:
|
def get_by_document_id(cls, document_id: str) -> list[File2Document]:
|
||||||
return cls.query(document_id=document_id)
|
return cls.query(document_id=document_id)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_by_file_id(cls, file_id: str) -> list[File2Document]:
|
def get_by_file_id(cls, file_id: str) -> list[File2Document]:
|
||||||
return cls.query(file_id=file_id)
|
return cls.query(file_id=file_id)
|
||||||
|
|
|
@ -1041,7 +1041,7 @@ class KnowledgebaseService:
|
||||||
SELECT llm_name, api_key, api_base
|
SELECT llm_name, api_key, api_base
|
||||||
FROM tenant_llm
|
FROM tenant_llm
|
||||||
WHERE tenant_id = %s AND model_type = 'embedding'
|
WHERE tenant_id = %s AND model_type = 'embedding'
|
||||||
ORDER BY create_time DESC # 如果一个用户可能有多个embedding配置,取最早的
|
ORDER BY create_time DESC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
"""
|
"""
|
||||||
cursor.execute(query_embedding_config, (earliest_user_id,))
|
cursor.execute(query_embedding_config, (earliest_user_id,))
|
||||||
|
|
Loading…
Reference in New Issue