You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.4 KiB
44 lines
1.4 KiB
"""
|
|
pip install aiomysql
|
|
"""
|
|
import logging
|
|
|
|
from aiomysql import create_pool
|
|
|
|
from Config.Config import *
|
|
|
|
# 配置日志
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# MySQL 配置
|
|
MYSQL_CONFIG = {
|
|
"host": MYSQL_HOST,
|
|
"port": MYSQL_PORT,
|
|
"user": MYSQL_USER,
|
|
"password": MYSQL_PASSWORD,
|
|
"db": MYSQL_DB_NAME,
|
|
"minsize": 1,
|
|
"maxsize": 20,
|
|
"charset": "utf8mb4"
|
|
}
|
|
|
|
|
|
# 初始化 MySQL 连接池
|
|
async def init_mysql_pool():
|
|
return await create_pool(**MYSQL_CONFIG)
|
|
|
|
|
|
# 保存聊天记录到 MySQL
|
|
async def save_chat_to_mysql(mysql_pool, person_id, prompt, result, audio_url, duration, input_type=1, output_type=1,
|
|
input_image_type=0, image_width=0, image_height=0):
|
|
async with mysql_pool.acquire() as conn:
|
|
await conn.ping() # 重置连接
|
|
async with conn.cursor() as cur:
|
|
await cur.execute(
|
|
"INSERT INTO t_chat_log (person_id, user_input, model_response,audio_url,duration,input_type,output_type,input_image_type,image_width,image_height,create_time) VALUES (%s, %s, %s, %s, %s, %s, %s,%s,%s,%s,NOW())",
|
|
(person_id, prompt, result, audio_url, duration, input_type, output_type, input_image_type, image_width,
|
|
image_height)
|
|
)
|
|
await conn.commit()
|