初始化版本
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 .. import db
|
||||
from . import globals
|
||||
import json,ast
|
||||
# from .report_routes import consultation
|
||||
|
||||
message_routes = Blueprint('message', __name__)
|
||||
|
@ -41,7 +40,6 @@ def get_message(sessionId):
|
|||
def add_message():
|
||||
"""添加会话"""
|
||||
data = request.get_json()
|
||||
print("22222222request",request)
|
||||
result = filter_data(data)
|
||||
validation_result = validate_session_id(result["sessionId"]) # 修改为字典访问方式
|
||||
if validation_result:
|
||||
|
@ -58,7 +56,6 @@ def add_message():
|
|||
@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) # 修改为字典访问方式
|
||||
|
@ -73,56 +70,17 @@ def update_message(messageId, sessionId):
|
|||
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))
|
||||
@message_routes.route('/to-chat/<int:sessionId>', methods=['POST'])
|
||||
def to_chat(sessionId):
|
||||
data = request.get_json()
|
||||
print("33333request.json",request.json)
|
||||
print("对话内容", data)
|
||||
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':
|
||||
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.case_info import format_to_report
|
||||
import ast
|
||||
|
||||
# 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
|
||||
|
@ -13,21 +16,20 @@ class Consultation:
|
|||
load_dotenv()
|
||||
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]
|
||||
print(777777,session_state.__dict__)
|
||||
session_state.history = msgList
|
||||
print("5555555session_state.option",session_state.option)
|
||||
print("22222self.session_map",self.session_map)
|
||||
|
||||
if_overall = '总体评估' in session_state.option if session_state.option else False
|
||||
session_state.process_query_task(content, overall=if_overall)
|
||||
print(8888,session_state.__dict__)
|
||||
session_state.process_query_task(question, overall=if_overall)
|
||||
print("1111111session_state",vars(session_state))
|
||||
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':
|
||||
# update recorded information and update agent's knowledge
|
||||
session_state.update()
|
||||
|
||||
# choose agent and
|
||||
# choose agent and option
|
||||
session_state.choose_agent_and_option_task()
|
||||
if session_state.option == '无':
|
||||
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)
|
||||
|
||||
|
||||
# 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__':
|
||||
print(os.getenv('CASE'))
|
||||
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.utils import get_q_template, init_logger, process_stream
|
||||
from src.patient import Patient
|
||||
|
||||
# from agents import doctor_state, choose_agent, init_multi_agents
|
||||
# from utils import get_q_template, init_logger, process_stream
|
||||
# from patient import Patient
|
||||
|
@ -31,20 +30,18 @@ class SessionState:
|
|||
return self.option == '无' and self.agent_order == len(self.agent_names) - 1
|
||||
|
||||
def add_to_history(self, role, message):
|
||||
pass
|
||||
# self.history.append({"role": role, "content": message})
|
||||
# print("history:",self.history)
|
||||
self.history.append({"role": role, "content": message})
|
||||
|
||||
def process_query_task(self, cur_query, overall=False):
|
||||
|
||||
if cur_query:
|
||||
print(7777,cur_query)
|
||||
# self.add_to_history('user', cur_query)
|
||||
self.logger.info('query: ' + str(cur_query))
|
||||
self.add_to_history('user', cur_query)
|
||||
self.logger.info('query: ' + cur_query)
|
||||
else:
|
||||
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,
|
||||
self.logger, overall) if self.cur_agent else None
|
||||
print(888,info)
|
||||
self.query = cur_query
|
||||
self.info = info
|
||||
self.logger.info('information in the query: ' + str(info))
|
||||
|
@ -57,9 +54,7 @@ class SessionState:
|
|||
|
||||
# choose option
|
||||
if self.cur_agent:
|
||||
print("!!!!!!!!!!!",self.cur_agent)
|
||||
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.missing_info = None
|
||||
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)]
|
||||
else:
|
||||
state = state_list
|
||||
# self.add_to_history('assistant', ''.join(state))
|
||||
self.add_to_history('assistant', ''.join(state))
|
||||
self.missing_info = None
|
||||
self.logger.info('doctor statement: ' + ''.join(state))
|
||||
self.logger.info('HISTORY:\n' + str(self.history) + '\n\n')
|
||||
|
|
Loading…
Reference in New Issue
Block a user