| | |
| | | from pydantic import BaseModel |
| | | from sqlalchemy.orm import Session |
| | | |
| | | from app.api import Response, get_current_user, ResponseList |
| | | from app.api import Response, get_current_user, ResponseList, process_files |
| | | from app.api.user import reset_user_pwd |
| | | from app.config.config import settings |
| | | from app.models.agent_model import AgentType, AgentModel |
| | |
| | | except Exception as e: |
| | | raise HTTPException(status_code=500, detail=str(e)) |
| | | elif agent.agent_type == AgentType.BISHENG: |
| | | is_join = False |
| | | if agent.name == "报告生成": |
| | | is_join = True |
| | | bisheng_service = BishengService(base_url=settings.sgb_base_url) |
| | | try: |
| | | token = get_bisheng_token(db, current_user.id) |
| | | result = await bisheng_service.get_session_log(token, agent_id, conversation_id) |
| | | combined_logs = [] |
| | | last_question = None |
| | | |
| | | answer_str = "" |
| | | files = [] |
| | | for session in result: |
| | | print(session) |
| | | # print(session) |
| | | # 检查 session 是否为 None |
| | | if session is None: |
| | | continue |
| | |
| | | message = message_json['query'] |
| | | elif 'report_name' in message_json: |
| | | message = message_json['report_name'] |
| | | except json.JSONDecodeError: |
| | | |
| | | except Exception as e: |
| | | pass # 非 JSON 字符串,继续使用原始 message |
| | | if session.get('files') and isinstance(session.get('files'), str): |
| | | try: |
| | | files = json.loads(session.get('files')) |
| | | process_files(files, agent_id) |
| | | |
| | | except Exception as e: |
| | | pass # 非 JSON 字符串,继续使用原始 message |
| | | |
| | | # 检查 message 是否为 None |
| | | if message is None: |
| | | continue |
| | | if is_join: |
| | | ... |
| | | if session.get('role') == 'question': |
| | | last_question = message |
| | | elif session.get('role') == 'answer': |
| | | answer_str += message |
| | | |
| | | if session.get('role') == 'question': |
| | | last_question = message |
| | | elif session.get('role') == 'answer' and last_question: |
| | | combined_logs.append(last_question + "\n" + message) |
| | | last_question = None |
| | | return JSONResponse(status_code=200, content={"code": 200, "data": {"question":"", "answer": "\n".join(combined_logs)}}) |
| | | else: |
| | | if session.get('role') == 'question': |
| | | last_question = message |
| | | elif session.get('role') == 'answer' and last_question: |
| | | combined_logs.append({ |
| | | 'question': last_question, |
| | | 'answer': message |
| | | }) |
| | | last_question = None |
| | | return JSONResponse(status_code=200, content={"code": 200, "data": combined_logs if combined_logs else [{'question': last_question, |
| | | 'answer': answer_str, 'files': files}]}) |
| | | except Exception as e: |
| | | raise HTTPException(status_code=500, detail=str(e)) |
| | | elif agent.agent_type == AgentType.BASIC: |
| | |
| | | for i in session.log_to_json().get("message", []): |
| | | if i.get("role") == "user": |
| | | tmp_data["question"] = i.get("content") |
| | | if "type" in i.get("content"): |
| | | tmp_data["type"] = i.get("content")["type"] |
| | | elif i.get("role") == "assistant": |
| | | if isinstance(i.get("content"), dict): |
| | | tmp_data["answer"] = i.get("content", {}).get("answer") |
| | | if "file_name" in i.get("content", {}): |
| | | tmp_data["files"] = [{"file_name": i.get("content", {}).get("file_name"), |
| | | "file_url": i.get("content", {}).get("file_url")}] |
| | | content = i.get("content", {}) |
| | | tmp_data["answer"] = content.get("answer") |
| | | if "file_name" in content: |
| | | tmp_data["files"] = [{"file_name": content.get("file_name"), |
| | | "file_url": content.get("file_url")}] |
| | | if "images" in i.get("content", {}): |
| | | tmp_data["images"] = i.get("content", {}).get("images") |
| | | tmp_data["images"] = content.get("images") |
| | | |
| | | if "download_url" in content: |
| | | tmp_data["download_url"] = content.get("download_url") |
| | | |
| | | if "node_list" in content: |
| | | node_dict = { |
| | | "node_data": [], # {"title": "去除冗余", # 节点名称 "status": "succeeded", # 节点状态"created_at": 1735817337, # 开始时间"finished_at": 1735817337, # 结束时间"error": "" # 错误日志} |
| | | "total_tokens": 0, # 花费token数 |
| | | "created_at": 0, # 开始时间 |
| | | "finished_at": 0, # 结束时间 |
| | | "status": "succeeded", # 工作流状态 |
| | | "error": "", # 错误日志 |
| | | } |
| | | for node in content["node_list"]: |
| | | if node.get("event") == "node_finished": |
| | | node_dict["node_data"].append({ |
| | | "title": node.get("data", {}).get("title", ""), |
| | | "status": node.get("data", {}).get("status", ""), |
| | | "created_at":node.get("data", {}).get("created_at", 0), |
| | | "finished_at":node.get("data", {}).get("finished_at", 0), |
| | | "node_type":node.get("data", {}).get("node_type", 0), |
| | | "elapsed_time":node.get("data", {}).get("elapsed_time", 0), |
| | | "error":node.get("data", {}).get("error", ""), |
| | | }) |
| | | elif node.get("event") == "workflow_finished": |
| | | node_dict["total_tokens"] = node.get("data", {}).get("total_tokens", 0) |
| | | node_dict["created_at"] = node.get("data", {}).get("created_at", 0) |
| | | node_dict["finished_at"] = node.get("data", {}).get("finished_at", 0) |
| | | node_dict["status"] = node.get("data", {}).get("status", "") |
| | | node_dict["error"] = node.get("data", {}).get("error", "") |
| | | node_dict["elapsed_time"] = node.get("data", {}).get("elapsed_time", 0) |
| | | tmp_data["workflow"] = node_dict |
| | | |
| | | else: |
| | | tmp_data["answer"] = i.get("content") |
| | | data.append(tmp_data) |
| | |
| | | return Response(code=404, msg="Agent not found") |
| | | |
| | | return Response(code=200, msg="", data={"chat_id": uuid.uuid4().hex}) |
| | | |
| | | |