| | |
| | | from app.api import get_current_user_websocket |
| | | from app.config.config import settings |
| | | from app.config.const import IMAGE_TO_TEXT, DOCUMENT_TO_REPORT, DOCUMENT_TO_CLEANING, DOCUMENT_IA_QUESTIONS, \ |
| | | DOCUMENT_TO_REPORT_TITLE, DOCUMENT_TO_TITLE, DOCUMENT_TO_PAPER, DOCUMENT_IA_QUESTIONS_DS |
| | | DOCUMENT_TO_REPORT_TITLE, DOCUMENT_TO_TITLE, DOCUMENT_TO_PAPER, DOCUMENT_IA_QUESTIONS_DS, \ |
| | | DOCUMENT_IA_QUESTIONS_EQUIPMENT |
| | | from app.models import MenuCapacityModel |
| | | from app.models.agent_model import AgentModel, AgentType |
| | | from app.models.base_model import get_db |
| | |
| | | print(f"Client {agent_id} connected") |
| | | agent = db.query(MenuCapacityModel).filter(MenuCapacityModel.chat_id == agent_id).first() |
| | | if not agent: |
| | | print("Agent not found") |
| | | agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first() |
| | | agent_type = agent.agent_type |
| | | chat_type = agent.type |
| | |
| | | result = {"message": f"内部错误: {e2}", "type": "close"} |
| | | await websocket.send_json(result) |
| | | print(f"Error process message of ragflow: {e2}") |
| | | elif chat_type == "documentIa" or chat_type == "documentIaDs": |
| | | elif chat_type == "documentIa" or chat_type == "documentIaDs" or chat_type == "documentIaEq": |
| | | token_dict = { |
| | | "documentIa": DOCUMENT_IA_QUESTIONS, |
| | | "documentIaDs": DOCUMENT_IA_QUESTIONS_DS, |
| | | "documentIaEq": DOCUMENT_IA_QUESTIONS_EQUIPMENT, |
| | | } |
| | | token = DfTokenDao(db).get_token_by_id(token_dict[chat_type]) |
| | | # print(token) |
| | | node_list = [] |
| | | workflow_list = [] |
| | | workflow_dict = {} |
| | | if not token: |
| | | await websocket.send_json({"message": "Invalid token", "type": "error"}) |
| | | |
| | |
| | | complete_response += rag_response |
| | | try: |
| | | data = json.loads(complete_response) |
| | | if data.get("event") == "node_started" or data.get( |
| | | "event") == "node_finished": # "event": "message_end" |
| | | node_data = deepcopy(data) |
| | | if "data" in node_data: |
| | | if "outputs" in node_data["data"]: |
| | | node_data["data"]["outputs"] = {} |
| | | if "inputs" in node_data["data"]: |
| | | node_data["data"]["inputs"] = {} |
| | | # print(node_data) |
| | | node_list.append(node_data) |
| | | if data.get("event") == "node_started": |
| | | if "data" not in data or not data["data"]: # 信息过滤 |
| | | logger.error("非法数据--------------------") |
| | | logger.error(data) |
| | |
| | | else: |
| | | message = answer.get("title", "") |
| | | |
| | | result = {"message": message, "type": "system"} |
| | | # continue |
| | | result = {"message": message, "type": "system", "workflow": {"node_data": workflow_list}} |
| | | elif data.get("event") == "node_finished": |
| | | workflow_list.append({ |
| | | "title": data.get("data", {}).get("title", ""), |
| | | "status": data.get("data", {}).get("status", ""), |
| | | "created_at": data.get("data", {}).get("created_at", 0), |
| | | "finished_at": data.get("data", {}).get("finished_at", 0), |
| | | "node_type": data.get("data", {}).get("node_type", 0), |
| | | "elapsed_time": data.get("data", {}).get("elapsed_time", 0), |
| | | "error": data.get("data", {}).get("error", ""), |
| | | }) |
| | | if "data" not in data or not data["data"]: # 信息过滤 |
| | | logger.error("非法数据--------------------") |
| | | logger.error(data) |
| | | continue |
| | | else: # 正常输出 |
| | | answer = data.get("data", "") |
| | | if isinstance(answer, str): |
| | | logger.error("----------------未知数据--------------------") |
| | | logger.error(data) |
| | | continue |
| | | elif isinstance(answer, dict): |
| | | if answer.get("status") == "failed": |
| | | message = answer.get("error", "") |
| | | else: |
| | | message = answer.get("title", "") |
| | | |
| | | result = {"message": message, "type": "system", |
| | | "workflow": {"node_data": workflow_list}} |
| | | elif data.get("event") == "message": # "event": "message_end" |
| | | # 正常输出 |
| | | answer = data.get("answer", "") |
| | | result = {"message": answer, "type": "stream"} |
| | | result = {"message": answer, "type": "stream", "workflow": {"node_data": workflow_list}} |
| | | elif data.get("event") == "error": # "event": "message_end" |
| | | # 正常输出 |
| | | answer = data.get("message", "") |
| | | result = {"message": answer, "type": "system", "workflow": {"node_data": workflow_list}} |
| | | elif data.get("event") == "workflow_finished": |
| | | answer = data.get("data", "") |
| | | if isinstance(answer, str): |
| | |
| | | message={"role": "assistant", |
| | | "content": { |
| | | "answer": message, |
| | | "node_list": node_list, |
| | | "download_url": download_url}}, |
| | | conversation_id=data.get( |
| | | "conversation_id")) |
| | | node_list = [] |
| | | except Exception as e: |
| | | logger.error("保存dify的会话异常!") |
| | | logger.error(e) |
| | | # await websocket.send_json(result) |
| | | workflow_dict = { |
| | | "node_data": workflow_list, |
| | | "total_tokens": data.get("data", {}).get("total_tokens", 0), |
| | | "created_at": data.get("data", {}).get("created_at", 0), |
| | | "finished_at": data.get("data", {}).get("finished_at", 0), |
| | | "status": data.get("data", {}).get("status", ""), |
| | | "error": data.get("data", {}).get("error", ""), |
| | | "elapsed_time": data.get("data", {}).get("elapsed_time", 0) |
| | | } |
| | | continue |
| | | elif data.get("event") == "message_end": |
| | | result = {"message": "", "type": "close"} |
| | | result = {"message": "", "type": "close","workflow": workflow_dict} |
| | | |
| | | else: |
| | | continue |
| | |
| | | from app.api import Response, get_current_user, ResponseList |
| | | from app.config.config import settings |
| | | from app.config.const import DOCUMENT_TO_REPORT, IMAGE_TO_TEXT, DOCUMENT_TO_REPORT_TITLE, DOCUMENT_IA_QUESTIONS, \ |
| | | DOCUMENT_TO_PAPER, DOCUMENT_IA_QUESTIONS_DS |
| | | DOCUMENT_TO_PAPER, DOCUMENT_IA_QUESTIONS_DS, DOCUMENT_IA_QUESTIONS_EQUIPMENT |
| | | from app.models import MenuCapacityModel |
| | | from app.models.agent_model import AgentType, AgentModel |
| | | from app.models.base_model import get_db |
| | |
| | | data = await dify_service.upload(token, file.filename, file_content, current_user.id) |
| | | except Exception as e: |
| | | raise HTTPException(status_code=500, detail=str(e)) |
| | | elif agent.chat_type == "reportWorkflow" or agent.chat_type == "documentIa" or agent.chat_type == "paperTalk" or agent.chat_type == "documentIaDs": |
| | | elif agent.chat_type == "reportWorkflow" or agent.chat_type == "documentIa" or agent.chat_type == "paperTalk" or agent.chat_type == "documentIaDs" or agent.chat_type == "documentIaEq": |
| | | token_dict = { |
| | | "reportWorkflow": DOCUMENT_TO_REPORT_TITLE, |
| | | "documentIa": DOCUMENT_IA_QUESTIONS, |
| | | "paperTalk": DOCUMENT_TO_PAPER, |
| | | "documentIaDs": DOCUMENT_IA_QUESTIONS_DS, |
| | | "documentIaEq": DOCUMENT_IA_QUESTIONS_EQUIPMENT, |
| | | } |
| | | token = DfTokenDao(db).get_token_by_id(token_dict[agent.chat_type]) |
| | | if not token: |
| | |
| | | return Response(code=400, msg="The password must be at least 8 and contain both numbers and letters") |
| | | db_user = db.query(UserModel).filter(UserModel.username == user.userName, UserModel.status != USER_STATSU_DELETE).first() |
| | | if db_user: |
| | | return Response(code=200, msg="user already created") |
| | | return Response(code=400, msg="user already created") |
| | | pwd = user.pwd |
| | | if not pwd: |
| | | pwd = "basic123456" |
| | |
| | | from fastapi.responses import JSONResponse |
| | | from Log import logger |
| | | from app.api import Response |
| | | from app.config.const import IMAGE_TO_TEXT, DOCUMENT_TO_CLEANING, \ |
| | | DOCUMENT_IA_QUESTIONS, DOCUMENT_TO_REPORT_TITLE, DOCUMENT_TO_TITLE, DOCUMENT_TO_PAPER |
| | | |
| | | from app.config.const import IMAGE_TO_TEXT, DOCUMENT_TO_CLEANING, DOCUMENT_TO_REPORT, DIFY, BISHENG, RAGFLOW, \ |
| | | DOCUMENT_IA_QUESTIONS, DOCUMENT_TO_REPORT_TITLE, DOCUMENT_TO_TITLE, DOCUMENT_TO_PAPER, \ |
| | | DOCUMENT_IA_QUESTIONS_EQUIPMENT |
| | | |
| | | from app.models.base_model import get_db |
| | | from app.models.public_api_model import DfToken |
| | | from app.service.v2.api_token import DfTokenDao |
| | |
| | | |
| | | @public_api.post("/sync/df_token", response_model=Response) |
| | | async def sync_df_token(df: DfToken, db=Depends(get_db)): |
| | | token_dict = {IMAGE_TO_TEXT: df.image, DOCUMENT_TO_CLEANING: df.clean, |
| | | token_dict = {IMAGE_TO_TEXT: df.image, DOCUMENT_TO_CLEANING: df.clean,DOCUMENT_IA_QUESTIONS_EQUIPMENT: df.equipment, |
| | | DOCUMENT_IA_QUESTIONS: df.doc, DOCUMENT_TO_REPORT_TITLE: df.report,DOCUMENT_TO_TITLE: df.title,DOCUMENT_TO_PAPER: df.paper} |
| | | |
| | | try: |
| | |
| | | IMAGE_TO_TEXT = "image_and_text_conversion" |
| | | DOCUMENT_IA_QUESTIONS = "document_ia_questions" |
| | | DOCUMENT_IA_QUESTIONS_DS = "document_ia_questions_ds" |
| | | DOCUMENT_IA_QUESTIONS_EQUIPMENT = "document_ia_questions_equipment" |
| | | DOCUMENT_TO_REPORT_TITLE = "document_to_report_title" |
| | | DOCUMENT_TO_TITLE = "document_to_title" |
| | | DOCUMENT_TO_PAPER = "document_to_paper" |
| | |
| | | "rank": 97, |
| | | "dialog": [ |
| | | { |
| | | "id": "7638f006-38a2-4c21-a68e-c6c49b304a35", |
| | | "chat_id": "7638f006-38a2-4c21-a68e-c6c49b304a35", |
| | | "id": "e138dd77-34d9-40f8-88f8-4d0ba7842b0b", |
| | | "chat_id": "e138dd77-34d9-40f8-88f8-4d0ba7842b0b", |
| | | "chat_type": "documentIa", |
| | | "agentType": 4 |
| | | } |
| | |
| | | "rank": 90, |
| | | "dialog": [ |
| | | { |
| | | "id": "e138dd77-34d9-40f8-88f8-4d0ba7842b0b", |
| | | "chat_id": "e138dd77-34d9-40f8-88f8-4d0ba7842b0b", |
| | | "id": "7638f006-38a2-4c21-a68e-c6c49b304a35", |
| | | "chat_id": "7638f006-38a2-4c21-a68e-c6c49b304a35", |
| | | "chat_type": "documentIaDs", |
| | | "agentType": 4 |
| | | } |
| | | ] |
| | | }, |
| | | { |
| | | "id": 11, |
| | | "title": "设备分析", |
| | | "icon": "7", |
| | | "img": "/src/assets/index/7.png", |
| | | "desc": "设备分析助手,基于您上传的设备数据进行问答,支持多设备", |
| | | "describe": "设备分析助手,基于您上传的文档进行问答,支持多文档,大小在30M以内。", |
| | | "rank": 89, |
| | | "dialog": [ |
| | | { |
| | | "id": "6912d21c-ede2-4c53-ac58-80192bc53878", |
| | | "chat_id": "6912d21c-ede2-4c53-ac58-80192bc53878", |
| | | "chat_type": "documentIaEq", |
| | | "agentType": 4 |
| | | } |
| | | ] |
| | | } |
| | | ] |
| | | } |
| | |
| | | image: Optional[str] = "" |
| | | clean: Optional[str] = "" |
| | | report: Optional[str] = "" |
| | | doc: Optional[str] = "" |
| | | title: Optional[str] = "" |
| | | paper: Optional[str] = "" |
| | | equipment: Optional[str] = "" |
| | | |
| | | |
| | | class AppRegister(BaseModel): |
| | |
| | | |
| | | |
| | | def encrypted_password(self, password): |
| | | return cipher_suite.encrypt(password.encode("utf-8")).decode("utf-8") |
| | | return cipher_suite.encrypt(str(password).encode("utf-8")).decode("utf-8") |
| | | |
| | | def decrypted_password(self): |
| | | return cipher_suite.decrypt(self.password).decode("utf-8") |
| | |
| | | message = {"role": "user", "content": name} |
| | | existing_session = self.get_session_by_id(session_id) |
| | | if existing_session: |
| | | print("update success") |
| | | # existing_session.add_message({"role": "user", "content": name}) |
| | | existing_session.add_message(message) |
| | | existing_session.update_date = current_time() |
| | |
| | | |
| | | async def chat_ping(self, url, params, headers): |
| | | res = await self.http_get(url, params, headers) |
| | | print(res.text) |
| | | # print(res.text) |
| | | if res.status_code != 200: |
| | | return 0 |
| | | if res.json().get("code") == "unauthorized" or res.json().get("code") == 401: |