| | |
| | | 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 |
| | |
| | | @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) |
| | | ): |
| | |
| | | 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) |
| | |
| | | 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: |