|
|
|
@ -1,4 +1,6 @@
|
|
|
|
|
import logging
|
|
|
|
|
from typing import Optional, Dict
|
|
|
|
|
|
|
|
|
|
from aiomysql import create_pool
|
|
|
|
|
from WxMini.Milvus.Config.MulvusConfig import *
|
|
|
|
|
|
|
|
|
@ -24,12 +26,12 @@ async def init_mysql_pool():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 保存聊天记录到 MySQL
|
|
|
|
|
async def save_chat_to_mysql(mysql_pool, session_id, prompt, result, audio_url, duration):
|
|
|
|
|
async def save_chat_to_mysql(mysql_pool, person_id, prompt, result, audio_url, duration):
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"INSERT INTO t_chat_log (session_id, user_input, model_response,audio_url,duration,create_time) VALUES (%s, %s, %s, %s, %s,NOW())",
|
|
|
|
|
(session_id, prompt, result, audio_url, duration)
|
|
|
|
|
"INSERT INTO t_chat_log (person_id, user_input, model_response,audio_url,duration,create_time) VALUES (%s, %s, %s, %s, %s,NOW())",
|
|
|
|
|
(person_id, prompt, result, audio_url, duration)
|
|
|
|
|
)
|
|
|
|
|
await conn.commit()
|
|
|
|
|
|
|
|
|
@ -46,11 +48,11 @@ async def truncate_chat_log(mysql_pool):
|
|
|
|
|
from aiomysql import DictCursor
|
|
|
|
|
|
|
|
|
|
# 分页查询聊天记录
|
|
|
|
|
async def get_chat_log_by_session(mysql_pool, session_id, page=1, page_size=10):
|
|
|
|
|
async def get_chat_log_by_session(mysql_pool, person_id, page=1, page_size=10):
|
|
|
|
|
"""
|
|
|
|
|
根据 session_id 查询聊天记录,并按 id 降序分页
|
|
|
|
|
根据 person_id 查询聊天记录,并按 id 降序分页
|
|
|
|
|
:param mysql_pool: MySQL 连接池
|
|
|
|
|
:param session_id: 用户会话 ID
|
|
|
|
|
:param person_id: 用户会话 ID
|
|
|
|
|
:param page: 当前页码(默认值为 1,但会动态计算为最后一页)
|
|
|
|
|
:param page_size: 每页记录数
|
|
|
|
|
:return: 分页数据
|
|
|
|
@ -62,8 +64,8 @@ async def get_chat_log_by_session(mysql_pool, session_id, page=1, page_size=10):
|
|
|
|
|
async with conn.cursor(DictCursor) as cur: # 使用 DictCursor
|
|
|
|
|
# 查询总记录数
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"SELECT COUNT(*) FROM t_chat_log WHERE session_id = %s",
|
|
|
|
|
(session_id,)
|
|
|
|
|
"SELECT COUNT(*) FROM t_chat_log WHERE person_id = %s",
|
|
|
|
|
(person_id,)
|
|
|
|
|
)
|
|
|
|
|
total = (await cur.fetchone())['COUNT(*)']
|
|
|
|
|
|
|
|
|
@ -75,9 +77,9 @@ async def get_chat_log_by_session(mysql_pool, session_id, page=1, page_size=10):
|
|
|
|
|
|
|
|
|
|
# 查询分页数据,按 id 降序排列
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"SELECT id, session_id, user_input, model_response, audio_url, duration, create_time "
|
|
|
|
|
"FROM t_chat_log WHERE session_id = %s ORDER BY id DESC LIMIT %s OFFSET %s",
|
|
|
|
|
(session_id, page_size, offset)
|
|
|
|
|
"SELECT id, person_id, user_input, model_response, audio_url, duration, create_time "
|
|
|
|
|
"FROM t_chat_log WHERE person_id = %s ORDER BY id DESC LIMIT %s OFFSET %s",
|
|
|
|
|
(person_id, page_size, offset)
|
|
|
|
|
)
|
|
|
|
|
records = await cur.fetchall()
|
|
|
|
|
|
|
|
|
@ -88,7 +90,7 @@ async def get_chat_log_by_session(mysql_pool, session_id, page=1, page_size=10):
|
|
|
|
|
result = [
|
|
|
|
|
{
|
|
|
|
|
"id": record['id'],
|
|
|
|
|
"session_id": record['session_id'],
|
|
|
|
|
"person_id": record['person_id'],
|
|
|
|
|
"user_input": record['user_input'],
|
|
|
|
|
"model_response": record['model_response'],
|
|
|
|
|
"audio_url": record['audio_url'],
|
|
|
|
@ -108,7 +110,7 @@ async def get_chat_log_by_session(mysql_pool, session_id, page=1, page_size=10):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 获取指定会话的最后一条记录的 id
|
|
|
|
|
async def get_last_chat_log_id(mysql_pool, session_id):
|
|
|
|
|
async def get_last_chat_log_id(mysql_pool, person_id):
|
|
|
|
|
"""
|
|
|
|
|
获取指定会话的最后一条记录的 id
|
|
|
|
|
:param mysql_pool: MySQL 连接池
|
|
|
|
@ -118,19 +120,19 @@ async def get_last_chat_log_id(mysql_pool, session_id):
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"SELECT id FROM t_chat_log WHERE session_id = %s ORDER BY id DESC LIMIT 1",
|
|
|
|
|
(session_id,)
|
|
|
|
|
"SELECT id FROM t_chat_log WHERE person_id = %s ORDER BY id DESC LIMIT 1",
|
|
|
|
|
(person_id,)
|
|
|
|
|
)
|
|
|
|
|
result = await cur.fetchone()
|
|
|
|
|
return result[0] if result else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 更新为危险的记录
|
|
|
|
|
async def update_risk(mysql_pool, session_id, risk_memo):
|
|
|
|
|
async def update_risk(mysql_pool, person_id, risk_memo):
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
# 1. 获取此人员的最后一条记录 id
|
|
|
|
|
last_id = await get_last_chat_log_id(mysql_pool, session_id)
|
|
|
|
|
last_id = await get_last_chat_log_id(mysql_pool, person_id)
|
|
|
|
|
|
|
|
|
|
if last_id:
|
|
|
|
|
# 2. 更新 risk_flag 和 risk_memo
|
|
|
|
@ -139,9 +141,9 @@ async def update_risk(mysql_pool, session_id, risk_memo):
|
|
|
|
|
(risk_memo.replace('\n', '').replace("NO", ""), last_id)
|
|
|
|
|
)
|
|
|
|
|
await conn.commit()
|
|
|
|
|
logger.info(f"已更新 session_id={session_id} 的最后一条记录 (id={last_id}) 的 risk_flag 和 risk_memo。")
|
|
|
|
|
logger.info(f"已更新 person_id={person_id} 的最后一条记录 (id={last_id}) 的 risk_flag 和 risk_memo。")
|
|
|
|
|
else:
|
|
|
|
|
logger.warning(f"未找到 session_id={session_id} 的记录。")
|
|
|
|
|
logger.warning(f"未找到 person_id={person_id} 的记录。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 查询有风险的聊天记录
|
|
|
|
@ -166,7 +168,7 @@ async def get_risk_chat_log_page(mysql_pool, risk_flag, page=1, page_size=10):
|
|
|
|
|
|
|
|
|
|
# 查询分页数据
|
|
|
|
|
query = (
|
|
|
|
|
"SELECT id, session_id, user_input, model_response, audio_url, duration, create_time, risk_memo "
|
|
|
|
|
"SELECT id, person_id, user_input, model_response, audio_url, duration, create_time, risk_memo "
|
|
|
|
|
"FROM t_chat_log WHERE risk_flag = %s ORDER BY id DESC LIMIT %s OFFSET %s"
|
|
|
|
|
)
|
|
|
|
|
params = (risk_flag, page_size, offset)
|
|
|
|
@ -180,7 +182,7 @@ async def get_risk_chat_log_page(mysql_pool, risk_flag, page=1, page_size=10):
|
|
|
|
|
result = [
|
|
|
|
|
{
|
|
|
|
|
"id": record[0],
|
|
|
|
|
"session_id": record[1],
|
|
|
|
|
"person_id": record[1],
|
|
|
|
|
"user_input": record[2],
|
|
|
|
|
"model_response": record[3],
|
|
|
|
|
"audio_url": record[4],
|
|
|
|
@ -196,4 +198,23 @@ async def get_risk_chat_log_page(mysql_pool, risk_flag, page=1, page_size=10):
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"page_size": page_size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# 查询用户信息
|
|
|
|
|
async def get_user_by_login_name(mysql_pool, login_name: str) -> Optional[Dict]:
|
|
|
|
|
"""
|
|
|
|
|
根据用户名查询用户信息
|
|
|
|
|
:param pool: MySQL 连接池
|
|
|
|
|
:param login_name: 用户名
|
|
|
|
|
:return: 用户信息(字典形式)
|
|
|
|
|
"""
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
async with conn.cursor() as cursor:
|
|
|
|
|
sql = "SELECT * FROM t_base_person WHERE login_name = %s"
|
|
|
|
|
await cursor.execute(sql, (login_name,))
|
|
|
|
|
row = await cursor.fetchone()
|
|
|
|
|
if not row:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
# 将元组转换为字典
|
|
|
|
|
columns = [column[0] for column in cursor.description]
|
|
|
|
|
return dict(zip(columns, row))
|