app/api/chat.py | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/api/files.py | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/images/__init__.py | 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/images/cbaa6dd5-1408-4914-b73b-53fcaaa1dfd1.png | 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/service/basic.py | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/service/difyService.py | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
main.py | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
app/api/chat.py
@@ -1,4 +1,5 @@ import json import re import uuid from fastapi import WebSocket, WebSocketDisconnect, APIRouter, Depends @@ -250,7 +251,9 @@ await websocket.send_json(result) else: logger.error("---------------------excel_talk-----------------------------") async for data in service.excel_talk(question, chat_id): logger.error(data) output = data.get("output", "") excel_name = data.get("excel_name", "") image_name = data.get("image_name", "") @@ -293,7 +296,11 @@ token = settings.dify_api_token try: async def forward_to_dify(): while True: image_list = [] is_image = False conversation_id = "" receive_message = await websocket.receive_json() print(f"Received from client {chat_id}: {receive_message}") @@ -339,8 +346,14 @@ else: # 正常输出 answer = data.get("answer", "") if isinstance(answer, str): answer_str += answer if "]+\)' url_image = image_list.pop() new_answer = re.sub(pattern, url_image, answer) answer_str += new_answer else: answer_str += answer elif isinstance(answer, dict): logger.error("未知数据体:0---------------------------------") @@ -349,22 +362,29 @@ result = {"message": answer_str, "type": "message"} elif data.get("event") == "message_end": message_files = [] res_msg = await dify_service.get_session_history(token, data.get("conversation_id"), str(current_user.id)) if len(res_msg) > 0: message_files = res_msg[0].get("message_files") result = {"message": answer_str, "type": "close", "message_files": message_files} images_url = [] # res_msg = await dify_service.get_session_history(token, data.get("conversation_id"), str(current_user.id)) # if len(res_msg) > 0: # message_files = res_msg[-1].get("message_files") # for msg_file in message_files: # await dify_service.save_images(msg_file.get("url"), msg_file.get("id")+".png") # images_url.append(msg_file.get("id")) # result = {"message": answer_str, "type": "close"} # , "message_files": images_url if image_list and not is_image: answer_str += image_list[-1] result = {"message": answer_str, "type": "close"} # , "message_files": images_url try: SessionService(db).update_session(chat_id, message={"role": "assistant", "content": {"answer":answer_str, "images":[i.get("url") for i in message_files]}},conversation_id=data.get("conversation_id")) message={"role": "assistant", "content": {"answer":answer_str, "images":images_url}},conversation_id=data.get("conversation_id")) 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"} await dify_service.save_images(data.get("url"), data.get("id") + ".png") image_list.append(f"})") # result = {"message": answer_str, "type": "message"} continue else: continue await websocket.send_json(result) app/api/files.py
@@ -75,7 +75,7 @@ if agent_id == "basic_excel_talk": # 处理单个文件的情况 file_list = file if len(file) == 1 and agent.agent_type != AgentType.BASIC: if len(file) == 1: # and agent.agent_type != AgentType.BASIC file_list = [file[0]] # 如果只有一个文件,确保它是一个列表 service = BasicService(base_url=settings.basic_base_url) # 遍历file_list,存到files 列表中 @@ -188,4 +188,19 @@ headers={"Content-Disposition": f"attachment; filename={filename}"} ) else: return Response(code=400, msg="Unsupported file type") return Response(code=400, msg="Unsupported file type") @router.get("/image/{imageId}", response_model=Response) async def download_image_file(imageId: str, db=Depends(get_db)): file_path = f"app/images/{imageId}.png" def generate(): with open(file_path, "rb") as file: while True: data = file.read(1048576) # 读取1MB if not data: break yield data return StreamingResponse(generate(), media_type="application/octet-stream") app/images/__init__.py
app/images/cbaa6dd5-1408-4914-b73b-53fcaaa1dfd1.png
app/service/basic.py
@@ -13,7 +13,9 @@ def _check_response(self, response: httpx.Response): """检查响应并处理错误""" if response.status_code not in [200, 201]: raise Exception(f"Failed to fetch data from API: {response.text}") # raise Exception(f"Failed to fetch data from API: {response.status_code}") logger.error(f"Failed to fetch data from API:") logger.error(response.status_code) response_data = response.json() return response_data @@ -73,6 +75,8 @@ answer = json.loads(decoded_line) yield answer except GeneratorExit as e: logger.error("------------except GeneratorExit as e:---------------------") logger.error(e) print(e) yield {"message": "内部错误", "type": "close"} finally: app/service/difyService.py
@@ -6,7 +6,6 @@ from fastapi import HTTPException from starlette import status from Log import logger from app.config.config import settings from app.utils.rsa_crypto import RagflowCrypto @@ -83,7 +82,7 @@ "inputs": {}, "query": message, "response_mode": "streaming", "conversation_id": "", "conversation_id": conversation_id, "user": str(user_id), "files": files } @@ -141,14 +140,27 @@ return data async def save_images(self, url: str, filename: str): url = f"{self.base_url}{url}" async with httpx.AsyncClient() as client: response = await client.get(url) response.raise_for_status() # 打开一个文件用于写入 with open(f"app/images/{filename}", 'wb') as f: # 写入请求的内容 f.write(response.content) if __name__ == "__main__": async def a(): a = DifyService("http://192.168.20.119:11080") b = await a.get_knowledge_list("ImY3ZTZlZWQwYTY2NTExZWY5ZmFiMDI0MmFjMTMwMDA2Ig.Zzxwmw.uI_HAWzOkipQuga1aeQtoeIc3IM", 1, 10) a = DifyService("http://192.168.20.116") b = await a.get_session_history("app-YmOAMDsPpDDlqryMHnc9TzTO", "f94c6328-8ff0-4713-af3f-e823d547682d", "63") print(b) import asyncio main.py
@@ -2,6 +2,7 @@ # from apscheduler.schedulers.background import BackgroundScheduler from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from starlette.staticfiles import StaticFiles from app.api.auth import router as auth_router from app.api.canvas import canvas_router @@ -71,6 +72,7 @@ app.include_router(dialog_router, prefix='/api/dialog', tags=["dialog"]) app.include_router(canvas_router, prefix='/api/canvas', tags=["canvas"]) app.include_router(sync_router, prefix='/api/sync', tags=["sync"]) app.mount("/static", StaticFiles(directory="app/images"), name="static") if __name__ == "__main__": import uvicorn