main
HuangHai 4 months ago
parent 23422db861
commit 363a9f876d

@ -36,11 +36,12 @@ MYSQL_PASSWORD="DsideaL147258369"
MYSQL_DB_NAME="ai_db"
# ----------------下面的配置需要根据情况进行修改-------------------------
# 驿来特账号的AK,SK
######################### 驿来特 #########################
ACCESS_KEY_ID = 'LTAI5t5jxkgJtRK8wew8fnbq'
ACCESS_KEY_SECRET = 'b8HXNGz7IkI3Dhv7BZx9BNBEZy1uku'
BUCKET_NAME = 'ylt'
ENDPOINT = 'https://oss-cn-hangzhou.aliyuncs.com'
OSS_PREFIX = "https://ylt.oss-cn-hangzhou.aliyuncs.com/"
# 阿里云中用来调用 deepseek v3 的密钥
MODEL_API_KEY = "sk-01d13a39e09844038322108ecdbd1bbc"
@ -49,4 +50,20 @@ MODEL_NAME = "deepseek-v3"
# TTS的APPKEY
APPKEY = "90RJcqjlN4ZqymGd" # 获取Appkey请前往控制台https://nls-portal.console.aliyun.com/applist
######################### 绘智 #########################
#ACCESS_KEY_ID = 'LTAI5tE4tgpGcKWhbZg6C4bh'
#ACCESS_KEY_SECRET = 'oizcTOZ8izbGUouboC00RcmGE8vBQ1'
#BUCKET_NAME = 'hzkc'
#ENDPOINT = 'https://oss-cn-beijing.aliyuncs.com'
#OSS_PREFIX = "https://hzkc.oss-cn-beijing.aliyuncs.com/"
# 阿里云中用来调用 deepseek v3 的密钥
#MODEL_API_KEY = "sk-01d13a39e09844038322108ecdbd1bbc"
##MODEL_NAME = "qwen-plus"
#MODEL_NAME = "deepseek-v3"
# TTS的APPKEY
#APPKEY = "90RJcqjlN4ZqymGd" # 获取Appkey请前往控制台https://nls-portal.console.aliyun.com/applist
#----------------------------------------------------------------------

@ -211,7 +211,7 @@ async def reply(session_id: str = Form(...), prompt: str = Form(...)):
logger.info(f"TTS 文件已直接上传到 OSS: {tts_file}")
# 完整的 URL
url = 'https://ylt.oss-cn-hangzhou.aliyuncs.com/' + tts_file
url = OSS_PREFIX + tts_file
# 记录聊天数据到 MySQL
await save_chat_to_mysql(app.state.mysql_pool, session_id, prompt, result, url, duration)
@ -239,18 +239,19 @@ async def reply(session_id: str = Form(...), prompt: str = Form(...)):
# 获取聊天记录
from fastapi import Query
# 获取聊天记录
@app.get("/aichat/get_chat_log")
async def get_chat_log(
session_id: str,
page: int = Query(default=None, ge=1, description="当前页码(如果为 None则默认跳转到最后一页)"),
page: int = Query(default=1, ge=1, description="当前页码(默认值为 1但会动态计算为最后一页)"),
page_size: int = Query(default=10, ge=1, le=100, description="每页记录数")
):
"""
获取指定会话的聊天记录默认返回最新的记录最后一页
:param session_id: 用户会话 ID
:param page: 当前页码如果为 None则默认跳转到最后一页
:param page: 当前页码默认值为 1但会动态计算为最后一页
:param page_size: 每页记录数
:return: 分页数据
"""

@ -43,13 +43,15 @@ async def truncate_chat_log(mysql_pool):
logger.info("表 t_chat_log 已清空。")
from aiomysql import DictCursor
# 分页查询聊天记录
async def get_chat_log_by_session(mysql_pool, session_id, page=None, page_size=10):
async def get_chat_log_by_session(mysql_pool, session_id, page=1, page_size=10):
"""
根据 session_id 查询聊天记录并按 id 序分页
根据 session_id 查询聊天记录并按 id 序分页
:param mysql_pool: MySQL 连接池
:param session_id: 用户会话 ID
:param page: 当前页码如果为 None则默认跳转到最后一页
:param page: 当前页码默认值为 1但会动态计算为最后一页
:param page_size: 每页记录数
:return: 分页数据
"""
@ -57,42 +59,41 @@ async def get_chat_log_by_session(mysql_pool, session_id, page=None, page_size=1
raise ValueError("MySQL 连接池未初始化")
async with mysql_pool.acquire() as conn:
async with conn.cursor() as cur:
async with conn.cursor(DictCursor) as cur: # 使用 DictCursor
# 查询总记录数
await cur.execute(
"SELECT COUNT(*) FROM t_chat_log WHERE session_id = %s",
(session_id,)
)
total = (await cur.fetchone())[0]
total = (await cur.fetchone())['COUNT(*)']
# 计算总页数
total_pages = (total + page_size - 1) // page_size
# 如果未指定页码,则默认跳转到最后一页
if page is None:
page = total_pages
# 计算偏移量
offset = (page - 1) * page_size
# 查询分页数据,按 id 序排列
# 查询分页数据,按 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 ASC LIMIT %s OFFSET %s",
"FROM t_chat_log WHERE session_id = %s ORDER BY id DESC LIMIT %s OFFSET %s",
(session_id, page_size, offset)
)
records = await cur.fetchall()
# 将查询结果反转,确保最新消息显示在最后
records.reverse()
# 将查询结果转换为字典列表
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")
"id": record['id'],
"session_id": record['session_id'],
"user_input": record['user_input'],
"model_response": record['model_response'],
"audio_url": record['audio_url'],
"duration": record['duration'],
"create_time": record['create_time'].strftime("%Y-%m-%d %H:%M:%S")
}
for record in records
]

Loading…
Cancel
Save