From f3caf4e663a00080ec09733bec1dd9bdfb2515f5 Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Mon, 24 Mar 2025 13:39:05 +0800 Subject: [PATCH] 'commit' --- AI/WxMini/X_Start.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/AI/WxMini/X_Start.py b/AI/WxMini/X_Start.py index e866bd85..55df224c 100644 --- a/AI/WxMini/X_Start.py +++ b/AI/WxMini/X_Start.py @@ -59,7 +59,6 @@ milvus_pool = MilvusConnectionPool(host=MS_HOST, port=MS_PORT, max_connections=M collection_name = MS_COLLECTION_NAME collection_manager = MilvusCollectionManager(collection_name) - # 将文本转换为嵌入向量 def text_to_embedding(text): words = jieba.lcut(text) # 使用 jieba 分词 @@ -74,7 +73,6 @@ def text_to_embedding(text): print("未找到有效词,返回零向量") return [0.0] * model.vector_size - # 使用 Lifespan Events 处理应用启动和关闭逻辑 @asynccontextmanager async def lifespan(app: FastAPI): @@ -86,7 +84,6 @@ async def lifespan(app: FastAPI): milvus_pool.close() print("Milvus 连接池已关闭。") - # 初始化 FastAPI 应用 app = FastAPI(lifespan=lifespan) @@ -99,10 +96,6 @@ client = OpenAI( # 设置相似度阈值 SIMILARITY_THRESHOLD = 0.5 # 距离小于 0.5 的结果被认为是高相似度 -# 在 /reply 接口中优化查询逻辑和提示词构建方式 -# 在 /reply 接口中优化提示词传递和系统提示词 -# 在 /reply 接口中优化提示词传递和系统提示词 -# 在 /reply 接口中优化查询逻辑和提示词构建方式 @app.post("/reply") async def reply(session_id: str = Form(...), prompt: str = Form(...)): """ @@ -123,7 +116,7 @@ async def reply(session_id: str = Form(...), prompt: str = Form(...)): print(f"提取的关键词: {keywords}") # 构建查询条件 - expr = f"session_id == '{session_id}'" # 只过滤 session_id + expr = build_query_expr(session_id, keywords) print(f"查询条件: {expr}") # 查询与当前对话最相关的五条历史交互 @@ -140,9 +133,6 @@ async def reply(session_id: str = Form(...), prompt: str = Form(...)): ) end_time = time.time() - # 调试:输出查询结果 - print(f"查询结果: {results}") - # 构建历史交互提示词 history_prompt = "" if results: @@ -192,12 +182,12 @@ async def reply(session_id: str = Form(...), prompt: str = Form(...)): collection_manager.insert_data(entities) print("用户输入和大模型反馈已记录到向量数据库。") - # 调用tts进行生成mp3 + # 调用 TTS 生成 MP3 uuid_str = str(uuid.uuid4()) tts_file = "audio/" + uuid_str + ".mp3" t = TTS(tts_file) t.start(result) - # 文件上传到oss + # 文件上传到 OSS upload_mp3_to_oss(tts_file, tts_file) # 删除临时文件 try: @@ -205,7 +195,7 @@ async def reply(session_id: str = Form(...), prompt: str = Form(...)): print(f"临时文件 {tts_file} 已删除") except Exception as e: print(f"删除临时文件失败: {e}") - # 完整的url + # 完整的 URL url = 'https://ylt.oss-cn-hangzhou.aliyuncs.com/' + tts_file return { "success": True, @@ -221,9 +211,8 @@ async def reply(session_id: str = Form(...), prompt: str = Form(...)): # 释放连接 milvus_pool.release_connection(connection) - # 运行 FastAPI 应用 if __name__ == "__main__": import uvicorn - uvicorn.run(app, host="0.0.0.0", port=5600) + uvicorn.run(app, host="0.0.0.0", port=5600) \ No newline at end of file