parent
d7b4fc92cb
commit
570322904b
@ -0,0 +1,26 @@
|
|||||||
|
import logging
|
||||||
|
import jieba
|
||||||
|
from gensim.models import KeyedVectors
|
||||||
|
|
||||||
|
# 配置日志
|
||||||
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# 初始化 Word2Vec 模型
|
||||||
|
model_path = r"D:\Tencent_AILab_ChineseEmbedding\Tencent_AILab_ChineseEmbedding.txt"
|
||||||
|
model = KeyedVectors.load_word2vec_format(model_path, binary=False, limit=10000)
|
||||||
|
logger.info(f"模型加载成功,词向量维度: {model.vector_size}")
|
||||||
|
|
||||||
|
# 将文本转换为嵌入向量
|
||||||
|
def text_to_embedding(text):
|
||||||
|
words = jieba.lcut(text) # 使用 jieba 分词
|
||||||
|
logger.info(f"文本: {text}, 分词结果: {words}")
|
||||||
|
embeddings = [model[word] for word in words if word in model]
|
||||||
|
logger.info(f"有效词向量数量: {len(embeddings)}")
|
||||||
|
if embeddings:
|
||||||
|
avg_embedding = sum(embeddings) / len(embeddings)
|
||||||
|
logger.info(f"生成的平均向量: {avg_embedding[:5]}...") # 打印前 5 维
|
||||||
|
return avg_embedding
|
||||||
|
else:
|
||||||
|
logger.warning("未找到有效词,返回零向量")
|
||||||
|
return [0.0] * model.vector_size
|
@ -0,0 +1,36 @@
|
|||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
from aiomysql import create_pool
|
||||||
|
from WxMini.Milvus.Config.MulvusConfig 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,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# 初始化 MySQL 连接池
|
||||||
|
async def init_mysql_pool():
|
||||||
|
return await create_pool(**MYSQL_CONFIG)
|
||||||
|
|
||||||
|
|
||||||
|
# 保存聊天记录到 MySQL
|
||||||
|
async def save_chat_to_mysql(mysql_pool, session_id, prompt, result):
|
||||||
|
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, create_time) VALUES (%s, %s, %s, NOW())",
|
||||||
|
(session_id, prompt, result)
|
||||||
|
)
|
||||||
|
await conn.commit()
|
||||||
|
logger.info("用户输入和大模型反馈已记录到 MySQL 数据库。")
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue