From 5580958d49e5aab48908000614e47ecb75ff4797 Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期四, 28 十一月 2024 19:14:26 +0800 Subject: [PATCH] 智能数据问题优化 --- app/api/chat.py | 118 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 85 insertions(+), 33 deletions(-) diff --git a/app/api/chat.py b/app/api/chat.py index 483a69a..f21f07c 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,12 @@ await websocket.send_json(result) else: + message_data = {} + logger.error("---------------------excel_talk-----------------------------") + excel_url = "" + image_url = "" 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", "") @@ -260,27 +266,31 @@ return None return (f"/api/files/download/?agent_id={agent_id}&file_id={name}" f"&file_type={file_type}") - excel_url = build_file_url(excel_name, 'excel') - image_url = build_file_url(image_name, 'image') - if excel_url or data.get("e", ""): - try: - SessionService(db).update_session(chat_id, - message={ - "content": output, - "excel_url": excel_url, - "image_url": image_url, - "sql": data.get("sql", ""), - "code": data.get("code", ""), - "e": data.get("e", ""), - "role": "assistant"}) - except Exception as e: - logger.error(f"Unexpected error when update_session: {e}") + if excel_name: + excel_url = build_file_url(excel_name, 'excel') + if image_name: + image_url = build_file_url(image_name, 'image') + if data["type"] == "message": + message_data = { + "content": output, + "excel_url": excel_url, + "image_url": image_url, + "sql": data.get("sql", ""), + "code": data.get("code", ""), + "e": data.get("e", ""), + "role": "assistant"} + # 鍙戦�佺粨鏋滅粰瀹㈡埛绔� - data["type"] = "message" + # data["type"] = "message" data["message"] = output data["excel_url"] = excel_url data["image_url"] = image_url await websocket.send_json(data) + if message_data: + try: + SessionService(db).update_session(chat_id,message=message_data) + except Exception as e: + logger.error(f"Unexpected error when update_session: {e}") except Exception as e: logger.error(e) await websocket.send_json({"message": "鍑虹幇閿欒锛�", "type": "error"}) @@ -293,11 +303,15 @@ 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}") - 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"}) @@ -313,30 +327,73 @@ conversation_id = session.conversation_id except Exception as e: logger.error(e) - complete_response = "" + # 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) try: if rag_response[:5] == "data:": # 濡傛灉鏄紝鍒欐埅鍙栨帀鍓�5涓瓧绗︼紝骞跺幓闄ら灏剧┖鐧界 - text = rag_response[5:].strip() + complete_response = rag_response[5:].strip() else: # 鍚﹀垯锛屼繚鎸佸師鏍� - text = rag_response - complete_response += text + complete_response = rag_response + # complete_response += text try: data = json.loads(complete_response) + complete_response = "" # data = json_data.get("data") - if "answer" not in data or not isinstance(data["answer"], dict): # 淇℃伅杩囨护 - continue - else: # 姝e父杈撳嚭 - answer = data.get("answer", "") + if data.get("event") == "agent_message":# "event": "message_end" + if "answer" not in data or not data["answer"]: # 淇℃伅杩囨护 + logger.error("闈炴硶鏁版嵁--------------------") + # logger.error(data) - result = {"message": answer, "type": "message"} + continue + else: # 姝e父杈撳嚭 + answer = data.get("answer", "") + if isinstance(answer, str): + 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---------------------------------") + logger.error(answer) + answer_str += answer.get("action_input", "") + + result = {"message": answer_str, "type": "message"} + elif data.get("event") == "message_end": + 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": data, "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": + 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) complete_response = "" except json.JSONDecodeError as e: @@ -346,12 +403,7 @@ result = {"message": f"鍐呴儴閿欒锛� {e2}", "type": "close"} await websocket.send_json(result) print(f"Error process message of ragflow: {e2}") - try: - dialog_chat_history = await ragflow_service.get_session_history(token, chat_id, 1) - await update_session_history(db, dialog_chat_history, current_user.id) - except Exception as e: - logger.error(e) - logger.error("-----------------淇濆瓨ragflow鐨勫巻鍙蹭細璇濆紓甯�-----------------") + # 鍚姩浠诲姟澶勭悊瀹㈡埛绔秷鎭� tasks = [ -- Gitblit v1.8.0