135 lines
4.9 KiB
Python
135 lines
4.9 KiB
Python
from flask import Blueprint, jsonify, request
|
||
from ..models import Session, Message
|
||
from .. import db
|
||
from . import globals
|
||
import json,ast
|
||
# from .report_routes import consultation
|
||
|
||
message_routes = Blueprint('message', __name__)
|
||
|
||
|
||
def filter_data(data):
|
||
return {
|
||
"content": data.get("content"),
|
||
"remark": data.get("remark"),
|
||
"role": data.get("role"),
|
||
"sessionId": data.get("sessionId")
|
||
}
|
||
|
||
|
||
def validate_session_id(sessionId):
|
||
"""验证sessionId是否提供且存在于Session表中"""
|
||
if not sessionId:
|
||
return jsonify({"error": "sessionId is required"}), 400
|
||
session = Session.query.filter_by(id=sessionId).first()
|
||
if not session:
|
||
return jsonify({"error": "Session not found"}), 404
|
||
return None
|
||
|
||
|
||
@message_routes.route('/get-message/<int:sessionId>', methods=['GET'])
|
||
def get_message(sessionId):
|
||
"""获取当前用户的所有会话"""
|
||
validation_result = validate_session_id(sessionId)
|
||
if validation_result:
|
||
return validation_result
|
||
messages = Message.query.filter_by(sessionId=sessionId).all()
|
||
return jsonify(messages=[message.to_dict() for message in messages]), 200
|
||
|
||
|
||
@message_routes.route('/add-message', methods=['POST'])
|
||
def add_message():
|
||
"""添加会话"""
|
||
data = request.get_json()
|
||
print("22222222request",request)
|
||
result = filter_data(data)
|
||
validation_result = validate_session_id(result["sessionId"]) # 修改为字典访问方式
|
||
if validation_result:
|
||
return validation_result
|
||
if not result["role"]: # 修改为字典访问方式
|
||
return jsonify({"error": "role is required"}), 400
|
||
|
||
new_message = Message(**result)
|
||
db.session.add(new_message)
|
||
db.session.commit()
|
||
return jsonify({'message': 'message added successfully', 'data': new_message.to_dict()}), 201
|
||
|
||
|
||
@message_routes.route('/update-message/<int:messageId>/<int:sessionId>', methods=['PUT'])
|
||
def update_message(messageId, sessionId):
|
||
"""更新会话"""
|
||
print("1111111111111111111request",request)
|
||
data = request.get_json()
|
||
remark = data.get("remark") # 修改为字典访问方式
|
||
validation_result = validate_session_id(sessionId) # 修改为字典访问方式
|
||
if validation_result:
|
||
return validation_result # 修改为字典访问方式
|
||
message = Message.query.filter_by(id=messageId).first() # 修改为字典访问方式
|
||
if not message: # 修改为字典访问方式
|
||
return jsonify({"error": "message not found"}), 404 # 修改为字典访问方式
|
||
if remark:
|
||
message.remark = remark # 修改为字典访问方式
|
||
db.session.commit() # 修改为字典访问方式
|
||
return jsonify({'message': 'message updated successfully'})
|
||
|
||
|
||
@message_routes.route('/to-chat', methods=['POST'])
|
||
def to_chat():
|
||
print("请求头:", dict(request.headers)) # 检查Content-Type
|
||
print("原始字节数据:", request.get_data()) # 检查数据是否合法
|
||
print("1111111111111111111request",request)
|
||
# if not request.is_json:
|
||
# return jsonify({"error": "Content-Type must be application/json"}), 401
|
||
|
||
# # 2. 获取外层JSON数据
|
||
# try:
|
||
# data = request.json
|
||
# print("外层JSON数据:", data)
|
||
# except Exception as e:
|
||
# return jsonify({"error": f"外层JSON解析失败: {str(e)}"}), 402
|
||
|
||
# # 3. 解析嵌套的JSON字符串(msgList和patientInfo)
|
||
# try:
|
||
# # 解析msgList(字符串 -> 列表)
|
||
# msg_list = json.loads(data["msgList"])
|
||
# print("解析后的msgList:", msg_list)
|
||
|
||
# # 解析patientInfo(字符串 -> 字典)
|
||
# patient_info = json.loads(data["patientInfo"])
|
||
# print("解析后的patientInfo:", patient_info)
|
||
|
||
# except json.JSONDecodeError as e:
|
||
# return jsonify({"error": f"嵌套JSON解析失败: {str(e)}"}), 403
|
||
# except KeyError as e:
|
||
# return jsonify({"error": f"缺少必要字段: {str(e)}"}), 404
|
||
|
||
|
||
|
||
|
||
|
||
data = request.json
|
||
print("1111111111111111111data",data)
|
||
patientId = data['patientId']
|
||
msgList = data['msgList']
|
||
patient_info = data['patientInfo']
|
||
print("222222222patient_info",patient_info)
|
||
if len(msgList) == 0:
|
||
globals.consultation.init_session(patientId,case_data=patient_info)
|
||
content = None
|
||
else:
|
||
content = data['msgList'][-1]['content']
|
||
print("333333333content",type(content))
|
||
value, option, system = None, None, None
|
||
|
||
cur_card, answer = globals.consultation.qa_chat(patientId, content, msgList)
|
||
|
||
if cur_card and cur_card[0] and cur_card[0]['status'] == 'success':
|
||
value, option, system = cur_card[0]['option_value'], cur_card[1], cur_card[2]
|
||
|
||
return jsonify({
|
||
'answer': answer,
|
||
'analysis': {
|
||
'value': value, 'option': option, 'system': system
|
||
}
|
||
}), 201
|