|
|
|
@ -1,4 +1,3 @@
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
from aiomysql import create_pool
|
|
|
|
|
from WxMini.Milvus.Config.MulvusConfig import *
|
|
|
|
@ -25,16 +24,17 @@ 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, session_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)
|
|
|
|
|
(session_id, prompt, result, audio_url, duration)
|
|
|
|
|
)
|
|
|
|
|
await conn.commit()
|
|
|
|
|
logger.info("用户输入和大模型反馈已记录到 MySQL 数据库。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 清空表
|
|
|
|
|
async def truncate_chat_log(mysql_pool):
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
@ -43,8 +43,9 @@ async def truncate_chat_log(mysql_pool):
|
|
|
|
|
await conn.commit()
|
|
|
|
|
logger.info("表 t_chat_log 已清空。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 分页查询聊天记录
|
|
|
|
|
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, session_id, page=1, page_size=10):
|
|
|
|
|
"""
|
|
|
|
|
根据 session_id 查询聊天记录,并按 id 降序分页
|
|
|
|
|
:param session_id: 用户会话 ID
|
|
|
|
@ -92,4 +93,76 @@ async def get_chat_log_by_session(mysql_pool,session_id, page=1, page_size=10):
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"page_size": page_size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 更新为危险的记录
|
|
|
|
|
async def update_risk(mysql_pool, session_id, risk_memo):
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
# 1. 获取此人员的最后一条记录 id
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"SELECT id FROM t_chat_log WHERE session_id = %s ORDER BY id DESC LIMIT 1",
|
|
|
|
|
(session_id,)
|
|
|
|
|
)
|
|
|
|
|
result = await cur.fetchone()
|
|
|
|
|
|
|
|
|
|
if result:
|
|
|
|
|
last_id = result[0]
|
|
|
|
|
# 2. 更新 risk_flag 和 risk_memo
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"UPDATE t_chat_log SET risk_flag = 1, risk_memo = %s WHERE id = %s",
|
|
|
|
|
(risk_memo.replace('\n', '').replace("NO", ""), last_id)
|
|
|
|
|
)
|
|
|
|
|
await conn.commit()
|
|
|
|
|
print(f"已更新 session_id={session_id} 的最后一条记录 (id={last_id}) 的 risk_flag 和 risk_memo。")
|
|
|
|
|
else:
|
|
|
|
|
print(f"未找到 session_id={session_id} 的记录。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_risk_chat_log_page(mysql_pool, risk_flag, page=1, page_size=10):
|
|
|
|
|
"""
|
|
|
|
|
查询有风险的聊天记录,并按 id 降序分页
|
|
|
|
|
:param mysql_pool: MySQL 连接池
|
|
|
|
|
:param page: 当前页码
|
|
|
|
|
:param page_size: 每页记录数
|
|
|
|
|
:return: 分页数据
|
|
|
|
|
"""
|
|
|
|
|
offset = (page - 1) * page_size
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
# 查询总记录数
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"SELECT COUNT(*) FROM t_chat_log WHERE risk_flag = %s", (risk_flag)
|
|
|
|
|
)
|
|
|
|
|
total = (await cur.fetchone())[0]
|
|
|
|
|
|
|
|
|
|
# 查询分页数据
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"SELECT id, session_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",
|
|
|
|
|
(risk_flag, page_size, offset)
|
|
|
|
|
)
|
|
|
|
|
records = await cur.fetchall()
|
|
|
|
|
|
|
|
|
|
# 将查询结果转换为字典列表
|
|
|
|
|
result = [
|
|
|
|
|
{
|
|
|
|
|
"id": record[0],
|
|
|
|
|
"session_id": record[1],
|
|
|
|
|
"user_input": record[2],
|
|
|
|
|
"model_response": record[3],
|
|
|
|
|
"audio_url": record[4],
|
|
|
|
|
"duration": record[5],
|
|
|
|
|
"create_time": record[6].strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
|
"risk_memo": record[7]
|
|
|
|
|
}
|
|
|
|
|
for record in records
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"data": result,
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"page_size": page_size
|
|
|
|
|
}
|
|
|
|
|