zhaoqingang
2024-12-02 f249dda1376dd5a525e74617900351c5bbc61705
app/api/agent.py
@@ -7,6 +7,7 @@
from sqlalchemy.orm import Session
from app.api import Response, get_current_user, ResponseList
from app.api.user import reset_user_pwd
from app.config.config import settings
from app.models.agent_model import AgentType, AgentModel
from app.models.base_model import get_db
@@ -120,29 +121,36 @@
            last_question = None
            for session in result:
                message = session['message']
                print(session)
                # 检查 session 是否为 None
                if session is None:
                    continue
                # 判断message是字符串还是json 对象,如果是json取其中的question字段,或者report_name字段赋值给message
                try:
                    message_json = json.loads(message)
                    if 'question' in message_json:
                        message = message_json['question']
                    elif 'query' in message_json:
                        message = message_json['query']
                    elif 'report_name' in message_json:
                        message = message_json['report_name']
                except json.JSONDecodeError:
                    pass
                message = session.get('message')
                if session['role'] == 'question':
                # 判断 message 是否是字符串,然后尝试解析为 JSON 对象
                if isinstance(message, str):
                    try:
                        message_json = json.loads(message)
                        if 'question' in message_json:
                            message = message_json['question']
                        elif 'query' in message_json:
                            message = message_json['query']
                        elif 'report_name' in message_json:
                            message = message_json['report_name']
                    except json.JSONDecodeError:
                        pass  # 非 JSON 字符串,继续使用原始 message
                # 检查 message 是否为 None
                if message is None:
                    continue
                if session.get('role') == 'question':
                    last_question = message
                elif session['role'] == 'answer' and last_question:
                    combined_logs.append({
                        'question': last_question,
                        'answer': 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": combined_logs})
            return JSONResponse(status_code=200, content={"code": 200, "data": {"question":"", "answer": "\n".join(combined_logs)}})
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))
    elif agent.agent_type == AgentType.BASIC:
@@ -172,6 +180,11 @@
                        tmp_data["code"] = i.get("code")
                    if "e" in i:
                        tmp_data["e"] = i.get("e")
                    if "image_name" in i:
                        tmp_data["image_name"] = i.get("image_name")
                    if "excel_name" in i:
                        tmp_data["excel_name"] = i.get("excel_name")
                    data.append(tmp_data)
                    tmp_data = {}