|  |  |  | 
|---|
|  |  |  | conversation_id = "" | 
|---|
|  |  |  | receive_message = await websocket.receive_json() | 
|---|
|  |  |  | print(f"Received from client {chat_id}: {receive_message}") | 
|---|
|  |  |  | upload_file_id = receive_message.get('upload_file_id', []) | 
|---|
|  |  |  | upload_file_id = receive_message.get('upload_file_id', "") | 
|---|
|  |  |  | question = receive_message.get('message', "") | 
|---|
|  |  |  | if not question and not image_url: | 
|---|
|  |  |  | await websocket.send_json({"message": "Invalid request", "type": "error"}) | 
|---|
|  |  |  | 
|---|
|  |  |  | # complete_response = "" | 
|---|
|  |  |  | answer_str = "" | 
|---|
|  |  |  | async for rag_response in dify_service.chat(token, current_user.id, question, upload_file_id, conversation_id): | 
|---|
|  |  |  | print("=============================================") | 
|---|
|  |  |  | print(rag_response) | 
|---|
|  |  |  | # print("=============================================") | 
|---|
|  |  |  | # print(rag_response) | 
|---|
|  |  |  | try: | 
|---|
|  |  |  | if rag_response[:5] == "data:": | 
|---|
|  |  |  | # 如果是,则截取掉前5个字符,并去除首尾空白符 | 
|---|
|  |  |  | 
|---|
|  |  |  | if data.get("event") == "agent_message":# "event": "message_end" | 
|---|
|  |  |  | if "answer" not in  data or not data["answer"]:  # 信息过滤 | 
|---|
|  |  |  | logger.error("非法数据--------------------") | 
|---|
|  |  |  | logger.error(data) | 
|---|
|  |  |  | # logger.error(data) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | continue | 
|---|
|  |  |  | else:  # 正常输出 | 
|---|
|  |  |  | 
|---|
|  |  |  | logger.error("未知数据体:0---------------------------------") | 
|---|
|  |  |  | logger.error(answer) | 
|---|
|  |  |  | answer_str += answer.get("action_input", "") | 
|---|
|  |  |  | continue | 
|---|
|  |  |  |  | 
|---|
|  |  |  | result = {"message": answer_str, "type": "message"} | 
|---|
|  |  |  | elif data.get("event") == "message_end": | 
|---|
|  |  |  | result = {"message": answer_str, "type": "close"} | 
|---|
|  |  |  | try: | 
|---|
|  |  |  | 
|---|
|  |  |  | except Exception as e: | 
|---|
|  |  |  | logger.error("保存dify的会话异常!") | 
|---|
|  |  |  | logger.error(e) | 
|---|
|  |  |  | elif data.get("event") == "message_file": | 
|---|
|  |  |  | url = data.get("url", "") | 
|---|
|  |  |  | result = {"message": url, "type": "image"} | 
|---|
|  |  |  | else: | 
|---|
|  |  |  | continue | 
|---|
|  |  |  | await websocket.send_json(result) | 
|---|
|  |  |  | 
|---|
|  |  |  | 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) | 
|---|
|  |  |  | token = settings.dify_api_token | 
|---|
|  |  |  | 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 | 
|---|
|  |  |  | # result["file_name"] = file.filename | 
|---|
|  |  |  | return Response(code=200, msg="", data=result) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return Response(code=200, msg="", data=result) | 
|---|
|  |  |  | 
|---|
|  |  |  | return { | 
|---|
|  |  |  | 'id': self.id, | 
|---|
|  |  |  | 'updated_time': self.update_time, | 
|---|
|  |  |  | 'update_date': self.update_date.strftime('%Y-%m-%d %H:%M:%S'), | 
|---|
|  |  |  | 'update_date': datetime.fromtimestamp(self.update_time / 1000.0).strftime('%Y-%m-%d %H:%M:%S'), | 
|---|
|  |  |  | 'name': self.name, | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | { | 
|---|
|  |  |  | "id": item["chat_id"], | 
|---|
|  |  |  | "name": process_name(item), | 
|---|
|  |  |  | "update_date": item["update_time"], | 
|---|
|  |  |  | "update_date": item["update_time"].replace("T", " "), | 
|---|
|  |  |  | "updated_time": int(datetime.strptime(item["update_time"], "%Y-%m-%dT%H:%M:%S").timestamp() * 1000) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for item in data | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def _handle_response(self, response: httpx.Response) -> Union[Dict, List]: | 
|---|
|  |  |  | if response.status_code != 200: | 
|---|
|  |  |  | if response.status_code == 201: | 
|---|
|  |  |  | return response.json() | 
|---|
|  |  |  | return {} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | data = response.json() | 
|---|
|  |  |  | 
|---|
|  |  |  | elif isinstance(data.get("data"), list): | 
|---|
|  |  |  | return data.get("data", []) | 
|---|
|  |  |  | else: | 
|---|
|  |  |  | return {} | 
|---|
|  |  |  | return data | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async def register(self, username: str, password: str): | 
|---|
|  |  |  | password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password) | 
|---|
|  |  |  | 
|---|
|  |  |  | async with httpx.AsyncClient() as client: | 
|---|
|  |  |  | response = await client.post(url, headers=headers, files=files, data=data) | 
|---|
|  |  |  | data = self._handle_response(response) | 
|---|
|  |  |  | logger.error("----------------------------ffff-------------------------------") | 
|---|
|  |  |  | logger.error(response.status_code) | 
|---|
|  |  |  | logger.error(response.text) | 
|---|
|  |  |  | # file_path = data.get("file_path", "") | 
|---|
|  |  |  | result = { | 
|---|
|  |  |  | "file_path": data | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return result | 
|---|
|  |  |  | return data | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | "id": item["id"], | 
|---|
|  |  |  | "name": item["name"], | 
|---|
|  |  |  | "updated_time": item["update_time"], | 
|---|
|  |  |  | "update_date": datetime.utcfromtimestamp(item["update_time"] / 1000.0).strftime('%Y-%m-%d %H:%M:%S') | 
|---|
|  |  |  | "update_date": datetime.fromtimestamp(item["update_time"] / 1000.0).strftime('%Y-%m-%d %H:%M:%S') | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for item in data | 
|---|
|  |  |  | if "id" in item and "name" in item and item["name"] | 
|---|