zhangqian
2024-10-17 c570e362ed163412658d1c1648e938a027529fed
app/api/files.py
@@ -1,4 +1,4 @@
from fastapi import Depends, APIRouter, HTTPException, UploadFile, File, requests
from fastapi import Depends, APIRouter, HTTPException, UploadFile, File, requests, Query
from sqlalchemy.orm import Session
from app.api import Response, get_current_user, ResponseList
@@ -16,6 +16,7 @@
@router.post("/upload/{agent_id}", response_model=Response)
async def upload_file(agent_id: str,
                      file: UploadFile = File(...),
                      chat_id: str = Query(..., description="The ID of the chat"),
                      db: Session = Depends(get_db),
                      current_user: UserModel = Depends(get_current_user)
                      ):
@@ -29,7 +30,18 @@
        return Response(code=400, msg=str(e))
    if agent.agent_type == AgentType.RAGFLOW:
        pass
        token = get_ragflow_token(db, current_user.id)
        ragflow_service = RagflowService(base_url=settings.ragflow_base_url)
        # 查询会话是否存在,不存在先创建会话
        history = await ragflow_service.get_session_history(token, chat_id)
        if len(history) == 0:
            message = {"role": "user", "message": file.filename}
            await ragflow_service.set_session(token, agent_id, message, chat_id, True)
        ragflow_service = RagflowService(base_url=settings.ragflow_base_url)
        token = get_ragflow_token(db, current_user.id)
        doc_ids = await ragflow_service.upload_and_parse(token, chat_id, file.filename, file_content)
        return Response(code=200, msg="", data={"doc_ids": doc_ids, "file_name": file.filename})
    elif agent.agent_type == AgentType.BISHENG:
        bisheng_service = BishengService(base_url=settings.bisheng_base_url)
@@ -38,6 +50,7 @@
            result = await bisheng_service.upload(token, file.filename, file_content)
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))
        result["file_name"] = file.filename
        return Response(code=200, msg="", data=result)
    else: