main
HuangHai 4 months ago
parent b370f7a57f
commit f9c0417ccf

@ -70,10 +70,9 @@ def update_question_by_id(db: PostgreSQLUtil, question_id: str, **kwargs):
return False
# 根据 问题id 查询 sql
def get_question_sql_by_id(db, question_id: str):
def get_question_by_id(db, question_id: str):
select_sql = """
select * from t_bi_question where id=%s
"""
_data = db.execute_query(select_sql, (question_id,))
sql = _data[0]['sql']
return sql
return _data

@ -66,15 +66,13 @@ def get_excel(question_id: str = Form(...), question_str: str = Form(...), db: P
return {"success": True, "message": "Excel文件生成成功", "download_url": f"/static/{uuid_str}.xlsx"}
# http://10.10.21.20:8000/questions/get_docx?question_id_get=af15d834-e7f5-46b4-a0f6-15f1f888f443
# http://10.10.21.20:8000/questions/get_docx_stream?question_id_get=af15d834-e7f5-46b4-a0f6-15f1f888f443
@app.api_route("/questions/get_docx", methods=["POST", "GET"])
async def get_docx(
question_id: str = Form(None, description="问题IDPOST请求"), # POST 请求参数
question_id_get: str = Query(None, description="问题IDGET请求"), # GET 请求参数
db: PostgreSQLUtil = Depends(get_db)
@app.api_route("/questions/get_docx_stream", methods=["POST", "GET"])
async def get_docx_stream(
question_id: str = Form(None, description="问题IDPOST请求"), # POST 请求参数
question_id_get: str = Query(None, description="问题IDGET请求"), # GET 请求参数
db: PostgreSQLUtil = Depends(get_db)
):
# 根据请求方式获取 question_id
if question_id is not None: # POST 请求
@ -85,7 +83,7 @@ async def get_docx(
return {"success": False, "message": "缺少问题ID参数"}
# 根据问题ID获取查询sql
sql = get_question_sql_by_id(db, question_id)
sql = get_question_by_id(db, question_id)[0]['sql']
# 4、生成word报告
prompt = '''
请根据以下 JSON 数据整理出2000字左右的话描述当前数据情况要求
@ -129,28 +127,22 @@ async def get_docx(
chunk_content = chunk.choices[0].delta.content
# 逐字拆分并返回
for char in chunk_content:
print(char, end="", flush=True) # 逐字输出到控制台
# print(char, end="", flush=True) # 逐字输出到控制台
yield char.encode("utf-8") # 将字符编码为 UTF-8 字节
summary += char # 将内容拼接到 summary 中
# 流式传输完成后,生成 Word 文档
markdown_to_docx(summary, output_file=filename)
# 返回最终的 JSON 数据
final_response = json.dumps({
"success": True,
"message": "Word文件生成成功",
"download_url": f"/static/{uuid_str}.docx"
})
print(final_response) # 输出最终 JSON 到控制台
yield final_response.encode("utf-8") # 将 JSON 数据编码为 UTF-8 字节
# 记录到数据库
update_question_by_id(db, question_id, docx_file_name=filename)
except Exception as e:
# 如果发生异常,返回错误信息
error_response = json.dumps({
"success": False,
"message": f"生成Word文件失败: {str(e)}"
})
print(error_response) # 输出错误信息到控制台
# print(error_response) # 输出错误信息到控制台
yield error_response.encode("utf-8") # 将错误信息编码为 UTF-8 字节
finally:
# 确保资源释放
@ -167,6 +159,29 @@ async def get_docx(
}
)
# 返回生成的Word文件下载地址
# http://10.10.21.20:8000/questions/get_docx_file?question_id_get=af15d834-e7f5-46b4-a0f6-15f1f888f443
@app.api_route("/questions/get_docx_file", methods=["POST", "GET"])
async def get_docx_file(
question_id: str = Form(None, description="问题IDPOST请求"), # POST 请求参数
question_id_get: str = Query(None, description="问题IDGET请求"), # GET 请求参数
db: PostgreSQLUtil = Depends(get_db)
):
# 根据请求方式获取 question_id
if question_id is not None: # POST 请求
question_id = question_id
elif question_id_get is not None: # GET 请求
question_id = question_id_get
else:
return {"success": False, "message": "缺少问题ID参数"}
# 根据问题ID获取查询docx_file_name
docx_file_name = get_question_by_id(db, question_id)[0]['docx_file_name']
# 返回成功和静态文件的URL
return {"success": True, "message": "Word文件生成成功", "download_url": f"{docx_file_name}"}
# 确保直接运行脚本时启动 FastAPI 应用
if __name__ == "__main__":
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)

Loading…
Cancel
Save