| | |
| | | ], |
| | | "complex": [ |
| | | { |
| | | "id": "c703ad73-faed-4fab-b00c-717766ad71dd", |
| | | "id": "9992ba72fff111ef8d890242ac120004", |
| | | "name": "知识库专家", |
| | | "description": "知识库专家", |
| | | "icon": "intellFrame4", |
| | | "chat_mode": 3, |
| | | "parameters": { |
| | | }, |
| | | "dialogType": "4", |
| | | "dialogType": "1", |
| | | "mode": "complex-chat" |
| | | }, |
| | | { |
| | |
| | | from sqlalchemy import Column, Integer, String, BigInteger, ForeignKey, DateTime, Text, TEXT |
| | | from sqlalchemy.orm import Session |
| | | |
| | | from app.config.const import Dialog_STATSU_DELETE |
| | | from app.config.const import Dialog_STATSU_DELETE, Dialog_STATSU_ON |
| | | from app.models.base_model import Base |
| | | from app.utils.common import current_time |
| | | |
| | |
| | | return [i.id for i in session_list] |
| | | |
| | | async def get_complex_chat_by_mode(self, chat_mode: int) -> ComplexChatModel | None: |
| | | session = self.db.query(ComplexChatModel).filter(ComplexChatModel.chat_mode==chat_mode, ComplexChatModel.status!=Dialog_STATSU_DELETE).first() |
| | | session = self.db.query(ComplexChatModel).filter(ComplexChatModel.chat_mode==chat_mode, ComplexChatModel.status==Dialog_STATSU_ON).first() |
| | | return session |
| | | |
| | | |
| | |
| | | yield json_data |
| | | |
| | | except json.JSONDecodeError as e: |
| | | # print(e) |
| | | # print(complete_response) |
| | | logger.info("Invalid JSON data------------------") |
| | | # print(e) |
| | | |
| | |
| | | "session_id": session_id |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | @staticmethod |
| | | async def complex_request_data(question, dataset_ids, session_id=""): |
| | | return { |
| | | "question": question, |
| | | "stream": True, |
| | | "session_id": session_id, |
| | | "kb_ids": dataset_ids |
| | | } |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | |
| | | node_data = [] |
| | | if not query_data: |
| | | query_data = {} |
| | | # print(node_data) |
| | | # print("--------------------------------------------------------") |
| | | # print(query_data) |
| | | try: |
| | | complex_log = ComplexChatSessionDao(db) |
| | | if not conversation_id: |
| | |
| | | async def service_complex_chat(db, chat_id, mode, user_id, chat_request: ChatDataRequest): |
| | | answer_event = "" |
| | | answer_agent = "" |
| | | answer_dialog = "" |
| | | answer_workflow = "" |
| | | download_url = "" |
| | | message_id = "" |
| | | task_id = "" |
| | | error = "" |
| | | node_list = [] |
| | | reference= {} |
| | | conversation_id = "" |
| | | query_data = chat_request.to_dict() |
| | | new_message_id = str(uuid.uuid4()) |
| | | inputs = {"is_deep": chat_request.isDeep} |
| | | files = chat_request.files |
| | | if chat_request.chatMode == complex_knowledge_chat: |
| | | inputs["query_json"] = json.dumps({"query": chat_request.query, "dataset_ids": chat_request.knowledgeId}) |
| | | elif chat_request.chatMode == complex_content_optimization_chat: |
| | | if chat_request.chatMode == complex_content_optimization_chat: |
| | | inputs["type"] = chat_request.optimizeType |
| | | elif chat_request.chatMode == complex_dialog_chat: |
| | | if not files and chat_request.parentId: |
| | |
| | | "error": "\n**ERROR**: 创建会话失败!", "status": http_500}, |
| | | ensure_ascii=False) + "\n\n" |
| | | return |
| | | |
| | | query_data["parentId"] = new_message_id |
| | | try: |
| | | if chat_request.chatMode == complex_knowledge_chat: |
| | | if not conversation_id: |
| | | session = await service_chat_sessions(db, chat_id, chat_request.query) |
| | | # print(session) |
| | | if not session or session.get("code") != 0: |
| | | yield "data: " + json.dumps( |
| | | {"message": smart_message_error, "error": "\n**ERROR**: chat agent error", "status": http_500}) |
| | | return |
| | | conversation_id = session.get("data", {}).get("id") |
| | | token = await get_chat_token(db, rg_api_token) |
| | | url = settings.fwr_base_url + RG_CHAT_DIALOG.format(chat_id) |
| | | chat = ChatDialog() |
| | | try: |
| | | async for ans in chat.chat_completions(url, await chat.complex_request_data(chat_request.query, chat_request.knowledgeId, conversation_id), |
| | | await chat.get_headers(token)): |
| | | data = {} |
| | | error = "" |
| | | status = http_200 |
| | | if ans.get("code", None) == 102: |
| | | error = ans.get("message", "error!") |
| | | status = http_400 |
| | | event = smart_message_error |
| | | else: |
| | | if isinstance(ans.get("data"), bool) and ans.get("data") is True: |
| | | event = smart_message_end |
| | | else: |
| | | data = ans.get("data", {}) |
| | | # conversation_id = data.get("session_id", "") |
| | | if "session_id" in data: |
| | | del data["session_id"] |
| | | data["prompt"] = "" |
| | | if not message_id: |
| | | message_id = data.get("id", "") |
| | | answer_dialog = data.get("answer", "") |
| | | reference = data.get("reference", {}) |
| | | event = smart_message_cover |
| | | message_str = "data: " + json.dumps( |
| | | {"event": event, "data": data, "error": error, "status": status, "message_id":message_id, |
| | | "parent_id": new_message_id, |
| | | "session_id": chat_request.sessionId}, |
| | | ensure_ascii=False) + "\n\n" |
| | | for i in range(0, len(message_str), max_chunk_size): |
| | | chunk = message_str[i:i + max_chunk_size] |
| | | # print(chunk) |
| | | yield chunk # 发送分块消息 |
| | | except Exception as e: |
| | | |
| | | logger.error(e) |
| | | try: |
| | | yield "data: " + json.dumps({"message": smart_message_error, |
| | | "error": "\n**ERROR**: " + str(e), "status": http_500}, |
| | | ensure_ascii=False) + "\n\n" |
| | | except: |
| | | ... |
| | | else: |
| | | token = await get_chat_token(db, chat_id) |
| | | chat, url = await get_chat_object(mode) |
| | | async for ans in chat.chat_completions(url, |
| | |
| | | # "node_list": node_list, "task_id": task_id, "id": message_id, |
| | | # "error": error}, conversation_id) |
| | | if message_id: |
| | | await add_complex_log(db, message_id, chat_id, chat_request.sessionId, chat_request.chatMode, answer_event or answer_agent or answer_workflow or error, user_id, mode, DF_TYPE, 2, conversation_id, node_data=node_list, query_data=query_data) |
| | | await add_complex_log(db, message_id, chat_id, chat_request.sessionId, chat_request.chatMode, answer_event or answer_agent or answer_workflow or answer_dialog or error, user_id, mode, DF_TYPE, 2, conversation_id, node_data=node_list or reference, query_data=query_data) |
| | | |
| | | async def service_complex_upload(db, chat_id, file, user_id): |
| | | files = [] |
| | |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
| | | |
| | | now_complex_list = [] |
| | | for agent in complex_list: |
| | | now_complex_list.append(agent["id"]) |
| | | dialog = db.query(ComplexChatModel).filter(ComplexChatModel.id == agent["id"]).first() |
| | | if dialog: |
| | | try: |
| | |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
| | | |
| | | for i in db.query(ComplexChatModel).filter(ComplexChatModel.status == "1").all(): |
| | | if i.id not in now_complex_list: |
| | | try: |
| | | db.query(ComplexChatModel).filter(ComplexChatModel.id==i.id).update(({"status": "0"})) |
| | | db.commit() |
| | | except: |
| | | ... |
| | | |
| | | |
| | | |
| | | async def user_update_app(userid, db): |
| | |
| | | for i in complex_list: |
| | | user_token = db.query(ApiTokenModel).filter(ApiTokenModel.app_id == i.id).first() |
| | | if not user_token: |
| | | try: |
| | | chat = ChatBaseApply() |
| | | url = settings.dify_base_url + DF_CHAT_API_KEY.format(i.id) |
| | | access_token = await get_app_token(db, workflow_server) |
| | |
| | | db.add(ApiTokenModel(id=token_id, app_id=i.id, type="app", token=token)) |
| | | db.commit() |
| | | print("df_api_token: 更新成功!") |
| | | except Exception as e: |
| | | print(e) |
| | | |
| | | |
| | | except Exception as e: |
| | | print(e) |