初始化版本
This commit is contained in:
parent
4911f3024f
commit
314e904b34
|
@ -2,7 +2,6 @@ from flask import Blueprint, jsonify, request
|
||||||
from ..models import Session, Message
|
from ..models import Session, Message
|
||||||
from .. import db
|
from .. import db
|
||||||
from . import globals
|
from . import globals
|
||||||
import json,ast
|
|
||||||
# from .report_routes import consultation
|
# from .report_routes import consultation
|
||||||
|
|
||||||
message_routes = Blueprint('message', __name__)
|
message_routes = Blueprint('message', __name__)
|
||||||
|
@ -41,7 +40,6 @@ def get_message(sessionId):
|
||||||
def add_message():
|
def add_message():
|
||||||
"""添加会话"""
|
"""添加会话"""
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
print("22222222request",request)
|
|
||||||
result = filter_data(data)
|
result = filter_data(data)
|
||||||
validation_result = validate_session_id(result["sessionId"]) # 修改为字典访问方式
|
validation_result = validate_session_id(result["sessionId"]) # 修改为字典访问方式
|
||||||
if validation_result:
|
if validation_result:
|
||||||
|
@ -58,7 +56,6 @@ def add_message():
|
||||||
@message_routes.route('/update-message/<int:messageId>/<int:sessionId>', methods=['PUT'])
|
@message_routes.route('/update-message/<int:messageId>/<int:sessionId>', methods=['PUT'])
|
||||||
def update_message(messageId, sessionId):
|
def update_message(messageId, sessionId):
|
||||||
"""更新会话"""
|
"""更新会话"""
|
||||||
print("1111111111111111111request",request)
|
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
remark = data.get("remark") # 修改为字典访问方式
|
remark = data.get("remark") # 修改为字典访问方式
|
||||||
validation_result = validate_session_id(sessionId) # 修改为字典访问方式
|
validation_result = validate_session_id(sessionId) # 修改为字典访问方式
|
||||||
|
@ -73,56 +70,17 @@ def update_message(messageId, sessionId):
|
||||||
return jsonify({'message': 'message updated successfully'})
|
return jsonify({'message': 'message updated successfully'})
|
||||||
|
|
||||||
|
|
||||||
@message_routes.route('/to-chat', methods=['POST'])
|
@message_routes.route('/to-chat/<int:sessionId>', methods=['POST'])
|
||||||
def to_chat():
|
def to_chat(sessionId):
|
||||||
print("请求头:", dict(request.headers)) # 检查Content-Type
|
data = request.get_json()
|
||||||
print("原始字节数据:", request.get_data()) # 检查数据是否合法
|
print("33333request.json",request.json)
|
||||||
print("1111111111111111111request",request)
|
print("对话内容", data)
|
||||||
# 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
|
value, option, system = None, None, None
|
||||||
|
print("55555sessionId",sessionId)
|
||||||
|
# globals.consultation.init_session(sessionId, case_data=data)
|
||||||
|
|
||||||
cur_card, answer = globals.consultation.qa_chat(patientId, content, msgList)
|
|
||||||
|
|
||||||
|
cur_card, answer = globals.consultation.qa_chat(sessionId, data['content'])
|
||||||
if cur_card and cur_card[0] and cur_card[0]['status'] == 'success':
|
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]
|
value, option, system = cur_card[0]['option_value'], cur_card[1], cur_card[2]
|
||||||
|
|
||||||
|
|
85
src/main.py
85
src/main.py
|
@ -3,7 +3,10 @@ from dotenv import load_dotenv
|
||||||
from src.session import SessionState
|
from src.session import SessionState
|
||||||
from src.case_info import format_to_report
|
from src.case_info import format_to_report
|
||||||
import ast
|
import ast
|
||||||
|
# import os
|
||||||
|
# from dotenv import load_dotenv
|
||||||
|
# from session import SessionState
|
||||||
|
# from case_info import format_to_report
|
||||||
class Consultation:
|
class Consultation:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# initialize one patient and session state
|
# initialize one patient and session state
|
||||||
|
@ -13,21 +16,20 @@ class Consultation:
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
self.session_map[session_id] = SessionState(case_data)
|
self.session_map[session_id] = SessionState(case_data)
|
||||||
|
|
||||||
def qa_chat(self, session_id, content, msgList, question=None):
|
def qa_chat(self, session_id, question=None):
|
||||||
session_state = self.session_map[session_id]
|
session_state = self.session_map[session_id]
|
||||||
print(777777,session_state.__dict__)
|
print("22222self.session_map",self.session_map)
|
||||||
session_state.history = msgList
|
|
||||||
print("5555555session_state.option",session_state.option)
|
|
||||||
if_overall = '总体评估' in session_state.option if session_state.option else False
|
if_overall = '总体评估' in session_state.option if session_state.option else False
|
||||||
session_state.process_query_task(content, overall=if_overall)
|
session_state.process_query_task(question, overall=if_overall)
|
||||||
print(8888,session_state.__dict__)
|
print("1111111session_state",vars(session_state))
|
||||||
cur_card = [session_state.info, session_state.option, session_state.agent_names[session_state.agent_order]]
|
cur_card = [session_state.info, session_state.option, session_state.agent_names[session_state.agent_order]]
|
||||||
print("66666cur_card",cur_card)
|
print("3333cur_card",cur_card)
|
||||||
if not session_state.info or session_state.info['status'] == 'success':
|
if not session_state.info or session_state.info['status'] == 'success':
|
||||||
# update recorded information and update agent's knowledge
|
# update recorded information and update agent's knowledge
|
||||||
session_state.update()
|
session_state.update()
|
||||||
|
|
||||||
# choose agent and
|
# choose agent and option
|
||||||
session_state.choose_agent_and_option_task()
|
session_state.choose_agent_and_option_task()
|
||||||
if session_state.option == '无':
|
if session_state.option == '无':
|
||||||
session_state.choose_agent_and_option_task()
|
session_state.choose_agent_and_option_task()
|
||||||
|
@ -50,70 +52,7 @@ class Consultation:
|
||||||
return format_to_report(self.session_map[session_id].patient.recorded_info)
|
return format_to_report(self.session_map[session_id].patient.recorded_info)
|
||||||
|
|
||||||
|
|
||||||
# if __name__ == '__main__':
|
|
||||||
# print(os.getenv('CASE'))
|
|
||||||
# consultation = Consultation()
|
|
||||||
# consultation.qa_chat()
|
|
||||||
|
|
||||||
"""import os
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
from src.session import SessionState
|
|
||||||
from src.case_info import format_to_report
|
|
||||||
|
|
||||||
# globals.consultation.init_session(case_data=data)
|
|
||||||
|
|
||||||
# import os
|
|
||||||
# from dotenv import load_dotenv
|
|
||||||
# from session import SessionState
|
|
||||||
# from case_info import format_to_report
|
|
||||||
|
|
||||||
class Consultation:
|
|
||||||
def __init__(self):
|
|
||||||
# initialize one patient and session state
|
|
||||||
self.session_map = {}
|
|
||||||
|
|
||||||
def init_session(self, case_data):
|
|
||||||
load_dotenv()
|
|
||||||
self.session_map = SessionState(case_data)
|
|
||||||
|
|
||||||
def qa_chat(self, question=None):
|
|
||||||
session_state = self.session_map
|
|
||||||
if_overall = '总体评估' in session_state.option if session_state.option else False
|
|
||||||
session_state.process_query_task(question, overall=if_overall)
|
|
||||||
cur_card = [session_state.info, session_state.option, session_state.agent_names[session_state.agent_order]]
|
|
||||||
if not session_state.info or session_state.info['status'] == 'success':
|
|
||||||
# update recorded information and update agent's knowledge
|
|
||||||
session_state.update()
|
|
||||||
|
|
||||||
# choose agent and option
|
|
||||||
session_state.choose_agent_and_option_task()
|
|
||||||
if session_state.option == '无':
|
|
||||||
session_state.choose_agent_and_option_task()
|
|
||||||
|
|
||||||
elif session_state.info['status'] == 'need_clarification':
|
|
||||||
session_state.missing_info = session_state.info['missing_info']
|
|
||||||
|
|
||||||
# doctor asks the question
|
|
||||||
stream_state = session_state.doctor_state_task()
|
|
||||||
if session_state.if_end():
|
|
||||||
pass
|
|
||||||
self.session_map = session_state
|
|
||||||
return cur_card, stream_state
|
|
||||||
|
|
||||||
def save_result(self):
|
|
||||||
return self.session_map.patient.recorded_info
|
|
||||||
|
|
||||||
def format_report(self):
|
|
||||||
return format_to_report(self.session_map.patient.recorded_info)
|
|
||||||
|
|
||||||
# def save_result(self):
|
|
||||||
# return self.session_state.patient.recorded_info
|
|
||||||
|
|
||||||
# def format_report(self):
|
|
||||||
# return format_to_report(self.session_state.patient.recorded_info)
|
|
||||||
"""
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(os.getenv('CASE'))
|
print(os.getenv('CASE'))
|
||||||
consultation = Consultation()
|
consultation = Consultation()
|
||||||
consultation.qa_chat()
|
consultation.qa_chat()
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
from src.agents import doctor_state, choose_agent, init_multi_agents
|
from src.agents import doctor_state, choose_agent, init_multi_agents
|
||||||
from src.utils import get_q_template, init_logger, process_stream
|
from src.utils import get_q_template, init_logger, process_stream
|
||||||
from src.patient import Patient
|
from src.patient import Patient
|
||||||
|
|
||||||
# from agents import doctor_state, choose_agent, init_multi_agents
|
# from agents import doctor_state, choose_agent, init_multi_agents
|
||||||
# from utils import get_q_template, init_logger, process_stream
|
# from utils import get_q_template, init_logger, process_stream
|
||||||
# from patient import Patient
|
# from patient import Patient
|
||||||
|
@ -31,20 +30,18 @@ class SessionState:
|
||||||
return self.option == '无' and self.agent_order == len(self.agent_names) - 1
|
return self.option == '无' and self.agent_order == len(self.agent_names) - 1
|
||||||
|
|
||||||
def add_to_history(self, role, message):
|
def add_to_history(self, role, message):
|
||||||
pass
|
self.history.append({"role": role, "content": message})
|
||||||
# self.history.append({"role": role, "content": message})
|
|
||||||
# print("history:",self.history)
|
|
||||||
|
|
||||||
def process_query_task(self, cur_query, overall=False):
|
def process_query_task(self, cur_query, overall=False):
|
||||||
|
|
||||||
if cur_query:
|
if cur_query:
|
||||||
print(7777,cur_query)
|
self.add_to_history('user', cur_query)
|
||||||
# self.add_to_history('user', cur_query)
|
self.logger.info('query: ' + cur_query)
|
||||||
self.logger.info('query: ' + str(cur_query))
|
|
||||||
else:
|
else:
|
||||||
self.logger.info('query: None')
|
self.logger.info('query: None')
|
||||||
|
print(6666,self.option, self.patient, self.history)
|
||||||
info = self.cur_agent.process_query(self.option, self.patient, self.history,
|
info = self.cur_agent.process_query(self.option, self.patient, self.history,
|
||||||
self.logger, overall) if self.cur_agent else None
|
self.logger, overall) if self.cur_agent else None
|
||||||
|
print(888,info)
|
||||||
self.query = cur_query
|
self.query = cur_query
|
||||||
self.info = info
|
self.info = info
|
||||||
self.logger.info('information in the query: ' + str(info))
|
self.logger.info('information in the query: ' + str(info))
|
||||||
|
@ -57,9 +54,7 @@ class SessionState:
|
||||||
|
|
||||||
# choose option
|
# choose option
|
||||||
if self.cur_agent:
|
if self.cur_agent:
|
||||||
print("!!!!!!!!!!!",self.cur_agent)
|
|
||||||
self.option = self.cur_agent.choose_option(self.history, self.patient, self.logger)
|
self.option = self.cur_agent.choose_option(self.history, self.patient, self.logger)
|
||||||
print("!!!!!!!!!!!self.option",self.option)
|
|
||||||
self.q_templates = get_q_template(self.get_agent_name(), self.option)
|
self.q_templates = get_q_template(self.get_agent_name(), self.option)
|
||||||
self.missing_info = None
|
self.missing_info = None
|
||||||
self.logger.info('current option and question templates:\n' + self.option + ' ' + str(self.q_templates))
|
self.logger.info('current option and question templates:\n' + self.option + ' ' + str(self.q_templates))
|
||||||
|
@ -78,7 +73,7 @@ class SessionState:
|
||||||
state = [s[i:i+3] for i in range(0, len(s), 3)]
|
state = [s[i:i+3] for i in range(0, len(s), 3)]
|
||||||
else:
|
else:
|
||||||
state = state_list
|
state = state_list
|
||||||
# self.add_to_history('assistant', ''.join(state))
|
self.add_to_history('assistant', ''.join(state))
|
||||||
self.missing_info = None
|
self.missing_info = None
|
||||||
self.logger.info('doctor statement: ' + ''.join(state))
|
self.logger.info('doctor statement: ' + ''.join(state))
|
||||||
self.logger.info('HISTORY:\n' + str(self.history) + '\n\n')
|
self.logger.info('HISTORY:\n' + str(self.history) + '\n\n')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user