From 28f41fceef54144cf87eaedd18d09a5a8b9cd5e1 Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期三, 27 十一月 2024 09:40:58 +0800 Subject: [PATCH] dify 多次对话问题 --- app/images/cbaa6dd5-1408-4914-b73b-53fcaaa1dfd1.png | 0 app/api/chat.py | 44 ++++++++++++++++------ main.py | 2 + app/images/__init__.py | 0 app/service/basic.py | 6 ++ app/service/difyService.py | 22 ++++++++-- app/api/files.py | 19 ++++++++- 7 files changed, 73 insertions(+), 20 deletions(-) diff --git a/app/api/chat.py b/app/api/chat.py index cd5c060..7e85a8e 100644 --- a/app/api/chat.py +++ b/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: # 姝e父杈撳嚭 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) diff --git a/app/api/files.py b/app/api/files.py index 7df40e1..2189f94 100644 --- a/app/api/files.py +++ b/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锛屽瓨鍒癴iles 鍒楄〃涓� @@ -188,4 +188,19 @@ headers={"Content-Disposition": f"attachment; filename={filename}"} ) else: - return Response(code=400, msg="Unsupported file type") \ No newline at end of file + 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") \ No newline at end of file diff --git a/app/images/__init__.py b/app/images/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/images/__init__.py diff --git a/app/images/cbaa6dd5-1408-4914-b73b-53fcaaa1dfd1.png b/app/images/cbaa6dd5-1408-4914-b73b-53fcaaa1dfd1.png new file mode 100644 index 0000000..8455366 --- /dev/null +++ b/app/images/cbaa6dd5-1408-4914-b73b-53fcaaa1dfd1.png Binary files differ diff --git a/app/service/basic.py b/app/service/basic.py index 26c15c1..b3ad295 100644 --- a/app/service/basic.py +++ b/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: diff --git a/app/service/difyService.py b/app/service/difyService.py index 9aee918..78daa44 100644 --- a/app/service/difyService.py +++ b/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 diff --git a/main.py b/main.py index 2af3a01..f07896a 100644 --- a/main.py +++ b/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 -- Gitblit v1.8.0