|
|
|
@ -2,15 +2,14 @@ import json
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
import uvicorn # 导入 uvicorn
|
|
|
|
|
from fastapi import FastAPI, Depends, Form, Query
|
|
|
|
|
from fastapi import FastAPI, Form, Query
|
|
|
|
|
from openai import OpenAI
|
|
|
|
|
from starlette.responses import StreamingResponse
|
|
|
|
|
from starlette.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
|
|
from Config import MODEL_API_KEY, MODEL_API_URL, MODEL_NAME
|
|
|
|
|
from Config import MODEL_API_KEY, MODEL_API_URL, QWEN_MODEL_NAME
|
|
|
|
|
from Model.biModel import *
|
|
|
|
|
from Text2Sql.Util.MarkdownToDocxUtil import markdown_to_docx
|
|
|
|
|
from Text2Sql.Util.PostgreSQLUtil import get_db
|
|
|
|
|
from Text2Sql.Util.SaveToExcel import save_to_excel
|
|
|
|
|
from Text2Sql.Util.VannaUtil import VannaUtil
|
|
|
|
|
|
|
|
|
@ -31,7 +30,7 @@ def read_root():
|
|
|
|
|
# 通过语义生成Excel
|
|
|
|
|
# http://10.10.21.20:8000/questions/get_excel
|
|
|
|
|
@app.post("/questions/get_excel")
|
|
|
|
|
def get_excel(question_id: str = Form(...), question_str: str = Form(...), db: PostgreSQLUtil = Depends(get_db)):
|
|
|
|
|
def get_excel(question_id: str = Form(...), question_str: str = Form(...)):
|
|
|
|
|
# 只接受guid号
|
|
|
|
|
if len(question_id) != 36:
|
|
|
|
|
return {"success": False, "message": "question_id格式错误"}
|
|
|
|
@ -44,24 +43,25 @@ def get_excel(question_id: str = Form(...), question_str: str = Form(...), db: P
|
|
|
|
|
question = question_str + common_prompt
|
|
|
|
|
|
|
|
|
|
# 先删除后插入,防止重复插入
|
|
|
|
|
delete_question(db, question_id)
|
|
|
|
|
insert_question(db, question_id, question)
|
|
|
|
|
delete_question(question_id)
|
|
|
|
|
insert_question(question_id, question)
|
|
|
|
|
|
|
|
|
|
# 获取完整 SQL
|
|
|
|
|
sql = vn.generate_sql(question)
|
|
|
|
|
print("生成的查询 SQL:\n", sql)
|
|
|
|
|
|
|
|
|
|
# 更新question_id
|
|
|
|
|
update_question_by_id(db, question_id=question_id, sql=sql, state_id=1)
|
|
|
|
|
update_question_by_id(question_id=question_id, sql=sql, state_id=1)
|
|
|
|
|
|
|
|
|
|
# 执行SQL查询
|
|
|
|
|
_data = db.execute_query(sql)
|
|
|
|
|
with PostgreSQLUtil(postgresql_pool.getconn()) as db:
|
|
|
|
|
_data = db.execute_query(sql)
|
|
|
|
|
# 在static目录下,生成一个guid号的临时文件
|
|
|
|
|
uuid_str = str(uuid.uuid4())
|
|
|
|
|
filename = f"static/{uuid_str}.xlsx"
|
|
|
|
|
save_to_excel(_data, filename)
|
|
|
|
|
# 更新EXCEL文件名称
|
|
|
|
|
update_question_by_id(db, question_id, excel_file_name=filename)
|
|
|
|
|
update_question_by_id(question_id, excel_file_name=filename)
|
|
|
|
|
# 返回静态文件URL
|
|
|
|
|
return {"success": True, "message": "Excel文件生成成功", "download_url": f"/static/{uuid_str}.xlsx"}
|
|
|
|
|
|
|
|
|
@ -70,8 +70,7 @@ def get_excel(question_id: str = Form(...), question_str: str = Form(...), db: P
|
|
|
|
|
@app.api_route("/questions/get_docx_stream", methods=["POST", "GET"])
|
|
|
|
|
async def get_docx_stream(
|
|
|
|
|
question_id: str = Form(None, description="问题ID(POST请求)"), # POST 请求参数
|
|
|
|
|
question_id_get: str = Query(None, description="问题ID(GET请求)"), # GET 请求参数
|
|
|
|
|
db: PostgreSQLUtil = Depends(get_db)
|
|
|
|
|
question_id_get: str = Query(None, description="问题ID(GET请求)") # GET 请求参数
|
|
|
|
|
):
|
|
|
|
|
# 根据请求方式获取 question_id
|
|
|
|
|
if question_id is not None: # POST 请求
|
|
|
|
@ -82,7 +81,7 @@ async def get_docx_stream(
|
|
|
|
|
return {"success": False, "message": "缺少问题ID参数"}
|
|
|
|
|
|
|
|
|
|
# 根据问题ID获取查询sql
|
|
|
|
|
sql = get_question_by_id(db, question_id)[0]['sql']
|
|
|
|
|
sql = get_question_by_id(question_id)[0]['sql']
|
|
|
|
|
# 4、生成word报告
|
|
|
|
|
prompt = '''
|
|
|
|
|
请根据以下 JSON 数据,整理出2000字左右的话描述当前数据情况。要求:
|
|
|
|
@ -92,7 +91,8 @@ async def get_docx_stream(
|
|
|
|
|
4、尽量以条目列出,这样更清晰
|
|
|
|
|
5、数据:
|
|
|
|
|
'''
|
|
|
|
|
_data = db.execute_query(sql)
|
|
|
|
|
with PostgreSQLUtil(postgresql_pool.getconn()) as db:
|
|
|
|
|
_data = db.execute_query(sql)
|
|
|
|
|
prompt = prompt + json.dumps(_data, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
|
# 初始化 OpenAI 客户端
|
|
|
|
@ -103,7 +103,8 @@ async def get_docx_stream(
|
|
|
|
|
|
|
|
|
|
# 调用 OpenAI API 生成总结(流式输出)
|
|
|
|
|
response = client.chat.completions.create(
|
|
|
|
|
model=MODEL_NAME,
|
|
|
|
|
#model=MODEL_NAME,
|
|
|
|
|
model=QWEN_MODEL_NAME,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system", "content": "你是一个数据分析助手,擅长从 JSON 数据中提取关键信息并生成详细的总结。"},
|
|
|
|
|
{"role": "user", "content": prompt}
|
|
|
|
@ -126,6 +127,7 @@ async def get_docx_stream(
|
|
|
|
|
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 中
|
|
|
|
|
|
|
|
|
@ -133,7 +135,7 @@ async def get_docx_stream(
|
|
|
|
|
markdown_to_docx(summary, output_file=filename)
|
|
|
|
|
|
|
|
|
|
# 记录到数据库
|
|
|
|
|
update_question_by_id(db, question_id, docx_file_name=filename)
|
|
|
|
|
update_question_by_id(question_id, docx_file_name=filename)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# 如果发生异常,返回错误信息
|
|
|
|
@ -141,6 +143,7 @@ async def get_docx_stream(
|
|
|
|
|
"success": False,
|
|
|
|
|
"message": f"生成Word文件失败: {str(e)}"
|
|
|
|
|
})
|
|
|
|
|
print(error_response) # 输出错误信息到控制台
|
|
|
|
|
yield error_response.encode("utf-8") # 将错误信息编码为 UTF-8 字节
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
@ -168,7 +171,6 @@ async def get_docx_stream(
|
|
|
|
|
async def get_docx_file(
|
|
|
|
|
question_id: str = Form(None, description="问题ID(POST请求)"), # POST 请求参数
|
|
|
|
|
question_id_get: str = Query(None, description="问题ID(GET请求)"), # GET 请求参数
|
|
|
|
|
db: PostgreSQLUtil = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
# 根据请求方式获取 question_id
|
|
|
|
|
if question_id is not None: # POST 请求
|
|
|
|
@ -179,7 +181,7 @@ async def get_docx_file(
|
|
|
|
|
return {"success": False, "message": "缺少问题ID参数"}
|
|
|
|
|
|
|
|
|
|
# 根据问题ID获取查询docx_file_name
|
|
|
|
|
docx_file_name = get_question_by_id(db, question_id)[0]['docx_file_name']
|
|
|
|
|
docx_file_name = get_question_by_id(question_id)[0]['docx_file_name']
|
|
|
|
|
|
|
|
|
|
# 返回成功和静态文件的URL
|
|
|
|
|
return {"success": True, "message": "Word文件生成成功", "download_url": f"{docx_file_name}"}
|
|
|
|
@ -187,31 +189,31 @@ async def get_docx_file(
|
|
|
|
|
|
|
|
|
|
# 设置问题为系统推荐问题 ,0:取消,1:设置
|
|
|
|
|
@app.post("/questions/set_system_recommend")
|
|
|
|
|
def set_system_recommend(question_id: str = Form(...), flag: str = Form(...), db: PostgreSQLUtil = Depends(get_db)):
|
|
|
|
|
set_system_recommend_questions(db, question_id, flag)
|
|
|
|
|
def set_system_recommend(question_id: str = Form(...), flag: str = Form(...)):
|
|
|
|
|
set_system_recommend_questions(question_id, flag)
|
|
|
|
|
# 提示保存成功
|
|
|
|
|
return {"success": True, "message": "保存成功"}
|
|
|
|
|
|
|
|
|
|
# 设置问题为用户收藏问题 ,0:取消,1:设置
|
|
|
|
|
@app.post("/questions/set_user_collect")
|
|
|
|
|
def set_user_collect(question_id: str = Form(...), flag: str = Form(...), db: PostgreSQLUtil = Depends(get_db)):
|
|
|
|
|
set_user_collect_questions(db, question_id, flag)
|
|
|
|
|
def set_user_collect(question_id: str = Form(...), flag: str = Form(...)):
|
|
|
|
|
set_user_collect_questions(question_id, flag)
|
|
|
|
|
# 提示保存成功
|
|
|
|
|
return {"success": True, "message": "保存成功"}
|
|
|
|
|
|
|
|
|
|
# 查询有哪些系统推荐问题
|
|
|
|
|
@app.get("/questions/get_system_recommend")
|
|
|
|
|
def get_system_recommend(db: PostgreSQLUtil = Depends(get_db)):
|
|
|
|
|
def get_system_recommend():
|
|
|
|
|
# 查询所有系统推荐问题
|
|
|
|
|
system_recommend_questions = get_system_recommend_questions(db)
|
|
|
|
|
system_recommend_questions = get_system_recommend_questions()
|
|
|
|
|
# 返回查询结果
|
|
|
|
|
return {"success": True, "data": system_recommend_questions}
|
|
|
|
|
|
|
|
|
|
# 查询有哪些用户收藏问题
|
|
|
|
|
@app.get("/questions/get_user_collect")
|
|
|
|
|
def get_user_collect(db: PostgreSQLUtil = Depends(get_db)):
|
|
|
|
|
def get_user_collect():
|
|
|
|
|
# 查询所有用户收藏问题
|
|
|
|
|
user_collect_questions = get_user_collect_questions(db)
|
|
|
|
|
user_collect_questions = get_user_collect_questions()
|
|
|
|
|
# 返回查询结果
|
|
|
|
|
return {"success": True, "data": user_collect_questions}
|
|
|
|
|
|
|
|
|
|