diff --git a/AI/Text2Sql/__pycache__/app.cpython-310.pyc b/AI/Text2Sql/__pycache__/app.cpython-310.pyc index 840a68c3..1804e213 100644 Binary files a/AI/Text2Sql/__pycache__/app.cpython-310.pyc and b/AI/Text2Sql/__pycache__/app.cpython-310.pyc differ diff --git a/AI/Text2Sql/app.py b/AI/Text2Sql/app.py index 854f1581..67e5de37 100644 --- a/AI/Text2Sql/app.py +++ b/AI/Text2Sql/app.py @@ -149,37 +149,40 @@ async def get_docx_stream( async def generate_stream(): summary = "" try: - async for chunk in response: # 使用 async for 处理流式响应 - if chunk.choices[0].delta.content: # 检查是否有内容 + # 获取数据库连接 + db = await app.state.pool.acquire() + + async for chunk in response: + if chunk.choices[0].delta.content: chunk_content = chunk.choices[0].delta.content - # 逐字拆分并返回 for char in chunk_content: - # print(char, end="", flush=True) # 逐字输出到控制台 - yield char.encode("utf-8") # 将字符编码为 UTF-8 字节 - summary += char # 将内容拼接到 summary 中 + print(char, end="", flush=True) + yield char.encode("utf-8") + summary += char # 流式传输完成后,生成 Word 文档 markdown_to_docx(summary, output_file=filename) # 记录到数据库 await update_question_by_id(db, question_id, docx_file_name=filename) - #await db.execute("UPDATE t_bi_question SET docx_file_name = $1 WHERE id = $2", filename, question_id) + except asyncio.CancelledError: # 客户端提前断开连接,无需处理 print("客户端断开连接") except Exception as e: - # 如果发生异常,返回错误信息 error_response = json.dumps({ "success": False, "message": f"生成Word文件失败: {str(e)}" }) - # print(error_response) # 输出错误信息到控制台 - yield error_response.encode("utf-8") # 将错误信息编码为 UTF-8 字节 + print(error_response) + yield error_response.encode("utf-8") finally: # 确保资源释放 if "response" in locals(): await response.close() + if "db" in locals(): + await app.state.pool.release(db) # 释放连接回连接池 # 使用 StreamingResponse 返回流式结果 return StreamingResponse( diff --git a/AI/Text2Sql/static/5426c9e0-312c-4229-9af2-7044132298bf.docx b/AI/Text2Sql/static/5426c9e0-312c-4229-9af2-7044132298bf.docx new file mode 100644 index 00000000..b32bd1da Binary files /dev/null and b/AI/Text2Sql/static/5426c9e0-312c-4229-9af2-7044132298bf.docx differ diff --git a/AI/Text2Sql/static/9ca8d63a-387f-4aea-9d22-df80119c1031.docx b/AI/Text2Sql/static/9ca8d63a-387f-4aea-9d22-df80119c1031.docx new file mode 100644 index 00000000..97441637 Binary files /dev/null and b/AI/Text2Sql/static/9ca8d63a-387f-4aea-9d22-df80119c1031.docx differ diff --git a/AI/Text2Sql/static/ccacce2c-e8cd-448b-9579-c5e9df007d45.docx b/AI/Text2Sql/static/ccacce2c-e8cd-448b-9579-c5e9df007d45.docx new file mode 100644 index 00000000..3f893d54 Binary files /dev/null and b/AI/Text2Sql/static/ccacce2c-e8cd-448b-9579-c5e9df007d45.docx differ diff --git a/AI/Text2Sql/static/fc3774f8-05e4-45c0-a4e3-580a123f45c7.docx b/AI/Text2Sql/static/fc3774f8-05e4-45c0-a4e3-580a123f45c7.docx new file mode 100644 index 00000000..3eddd883 Binary files /dev/null and b/AI/Text2Sql/static/fc3774f8-05e4-45c0-a4e3-580a123f45c7.docx differ diff --git a/AI/Text2Sql/static/fd5bfe3f-688f-4f1f-bc57-4b951ad332aa.docx b/AI/Text2Sql/static/fd5bfe3f-688f-4f1f-bc57-4b951ad332aa.docx new file mode 100644 index 00000000..b466605e Binary files /dev/null and b/AI/Text2Sql/static/fd5bfe3f-688f-4f1f-bc57-4b951ad332aa.docx differ diff --git a/AI/Text2Sql/static/feff9540-946c-4bf8-89be-0ee2b8432fb2.docx b/AI/Text2Sql/static/feff9540-946c-4bf8-89be-0ee2b8432fb2.docx new file mode 100644 index 00000000..d6ba09ae Binary files /dev/null and b/AI/Text2Sql/static/feff9540-946c-4bf8-89be-0ee2b8432fb2.docx differ