This commit is contained in:
hailin 2025-05-09 23:53:52 +08:00
parent 7243bc3e7a
commit ce38e21219
1 changed files with 8 additions and 3 deletions

View File

@ -1,8 +1,13 @@
from fastapi import APIRouter, UploadFile, File, Form, HTTPException
import os
import shutil
import logging
from scripts.rag_build_query import build_user_index
# 设置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
router = APIRouter()
ALLOWED_SUFFIXES = {".txt", ".md", ".pdf", ".docx"}
@ -29,7 +34,7 @@ def upload_user_file(user_id: str = Form(...), file: UploadFile = File(...)):
try:
with open(file_path, "wb") as f:
shutil.copyfileobj(file.file, f)
print(f"[UPLOAD] 文件已保存至 {file_path}")
logger.info(f"文件已保存至 {file_path}")
# 创建索引并保存 docstore.json
docstore_path = os.path.join(index_data_dir, "docstore.json")
@ -39,10 +44,10 @@ def upload_user_file(user_id: str = Form(...), file: UploadFile = File(...)):
# 重新构建用户索引
build_user_index(user_id)
print(f"[UPLOAD] 用户 {user_id} 的索引已重建")
logger.info(f"用户 {user_id} 的索引已重建")
except Exception as e:
print(f"[UPLOAD ERROR] {e}")
logger.error(f"[UPLOAD ERROR] {e}")
raise HTTPException(status_code=500, detail="索引构建失败")
return {"status": "ok", "filename": filename}