From 1ab90de913405b45050e0f732a03004087134fda Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期一, 25 十一月 2024 11:11:04 +0800 Subject: [PATCH] 上传dify文件 --- app/api/chat.py | 4 ++-- app/service/difyService.py | 16 +++++++++------- app/api/agent.py | 6 ++++++ app/api/files.py | 30 +++++++++++++++--------------- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/app/api/agent.py b/app/api/agent.py index 5e72c15..b8c1b9d 100644 --- a/app/api/agent.py +++ b/app/api/agent.py @@ -64,6 +64,12 @@ result = [item.to_dict() for item in records] return ResponseList(code=200, msg="", data=result) + elif agent.agent_type == AgentType.DIFY: + offset = (page - 1) * limit + records = db.query(SessionModel).filter(SessionModel.agent_id == agent_id, SessionModel.tenant_id==current_user.id).order_by(SessionModel.create_date.desc()).offset(offset).limit(limit).all() + result = [item.to_dict() for item in records] + return ResponseList(code=200, msg="", data=result) + else: return ResponseList(code=200, msg="Unsupported agent type") diff --git a/app/api/chat.py b/app/api/chat.py index 2932d45..483a69a 100644 --- a/app/api/chat.py +++ b/app/api/chat.py @@ -314,7 +314,7 @@ except Exception as e: logger.error(e) complete_response = "" - async for rag_response in dify_service.chat(token, chat_id, question, upload_file_id, conversation_id): + async for rag_response in dify_service.chat(token, current_user.id, question, upload_file_id, conversation_id): try: if rag_response[:5] == "data:": # 濡傛灉鏄紝鍒欐埅鍙栨帀鍓�5涓瓧绗︼紝骞跺幓闄ら灏剧┖鐧界 @@ -326,7 +326,7 @@ try: data = json.loads(complete_response) # data = json_data.get("data") - if "answer" not in data or isinstance(data["answer"], dict): # 淇℃伅杩囨护 + if "answer" not in data or not isinstance(data["answer"], dict): # 淇℃伅杩囨护 continue else: # 姝e父杈撳嚭 answer = data.get("answer", "") diff --git a/app/api/files.py b/app/api/files.py index dd9166a..22c2216 100644 --- a/app/api/files.py +++ b/app/api/files.py @@ -94,21 +94,21 @@ service = BasicService(base_url=settings.basic_paper_url) result = await service.paper_file_upload(chat_id, file.filename, file_content) - elif agent.agent_type == AgentType.DIFY: - file = file[0] - # 璇诲彇涓婁紶鐨勬枃浠跺唴瀹� - try: - file_content = await file.read() - except Exception as e: - return Response(code=400, msg=str(e)) - dify_service = DifyService(base_url=settings.dify_base_url) - try: - token = get_bisheng_token(db, current_user.id) - result = await dify_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) + elif agent.agent_type == AgentType.DIFY: + file = file[0] + # 璇诲彇涓婁紶鐨勬枃浠跺唴瀹� + try: + file_content = await file.read() + except Exception as e: + return Response(code=400, msg=str(e)) + dify_service = DifyService(base_url=settings.dify_base_url) + try: + token = get_bisheng_token(db, current_user.id) + result = await dify_service.upload(token, file.filename, file_content, current_user.id) + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + result["file_name"] = file.filename + return Response(code=200, msg="", data=result) return Response(code=200, msg="", data=result) diff --git a/app/service/difyService.py b/app/service/difyService.py index 3e588cf..0444387 100644 --- a/app/service/difyService.py +++ b/app/service/difyService.py @@ -62,7 +62,7 @@ raise Exception("Authorization header not found in response") return authorization - async def chat(self, token: str, chat_id: str, message: str, upload_file_id: str, conversation_id: str): + async def chat(self, token: str, user_id: int, message: str, upload_file_id: str, conversation_id: str): target_url = f"{self.base_url}/v1/chat-messages" files = [] @@ -80,7 +80,7 @@ "query": message, "response_mode": "streaming", "conversation_id": conversation_id, - "user": chat_id, + "user": str(user_id), "files": files } @@ -114,17 +114,19 @@ return data return data.get("message", []) - async def upload(self, token: str, filename: str, file: bytes) -> dict: - url = f"{self.base_url}/console/api/files/upload" + async def upload(self, token: str, filename: str, file: bytes, user_id) -> dict: + url = f"{self.base_url}/v1/files/upload" headers = { - 'Content-Type': 'application/json', + # 'Content-Type': 'application/json', 'Authorization': f'Bearer {token}' } - + data = { + 'user': str(user_id) + } # 鍒涘缓琛ㄥ崟鏁版嵁锛屽寘鍚枃浠� files = {"file": (filename, file)} async with httpx.AsyncClient() as client: - response = await client.post(url, headers=headers, files=files) + response = await client.post(url, headers=headers, files=files, data=data) data = self._handle_response(response) # file_path = data.get("file_path", "") result = { -- Gitblit v1.8.0