|
|
|
@ -2,7 +2,9 @@ import asyncio
|
|
|
|
|
import json
|
|
|
|
|
import uuid
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
import asyncpg
|
|
|
|
|
import uvicorn
|
|
|
|
|
from fastapi import FastAPI, Depends
|
|
|
|
|
from fastapi import Form, Query
|
|
|
|
@ -125,7 +127,6 @@ async def get_docx_stream(
|
|
|
|
|
# print(_data)
|
|
|
|
|
# 将 asyncpg.Record 转换为 JSON 格式
|
|
|
|
|
json_data = json.dumps([dict(record) for record in _data], ensure_ascii=False)
|
|
|
|
|
print(json_data) # 打印 JSON 数据
|
|
|
|
|
prompt = prompt + json.dumps(json_data, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
|
# 调用 OpenAI API 生成总结(流式输出)
|
|
|
|
@ -153,7 +154,7 @@ async def get_docx_stream(
|
|
|
|
|
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 中
|
|
|
|
|
|
|
|
|
@ -171,7 +172,7 @@ async def get_docx_stream(
|
|
|
|
|
"success": False,
|
|
|
|
|
"message": f"生成Word文件失败: {str(e)}"
|
|
|
|
|
})
|
|
|
|
|
print(error_response) # 输出错误信息到控制台
|
|
|
|
|
#print(error_response) # 输出错误信息到控制台
|
|
|
|
|
yield error_response.encode("utf-8") # 将错误信息编码为 UTF-8 字节
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
@ -199,6 +200,7 @@ 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: asyncpg.Connection = Depends(get_db) # 添加 db 参数
|
|
|
|
|
):
|
|
|
|
|
# 根据请求方式获取 question_id
|
|
|
|
|
if question_id is not None: # POST 请求
|
|
|
|
@ -209,7 +211,7 @@ async def get_docx_file(
|
|
|
|
|
return {"success": False, "message": "缺少问题ID参数"}
|
|
|
|
|
|
|
|
|
|
# 根据问题ID获取查询docx_file_name
|
|
|
|
|
docx_file_name = get_question_by_id(question_id)[0]['docx_file_name']
|
|
|
|
|
docx_file_name = (await get_question_by_id(db, question_id))[0]['docx_file_name']
|
|
|
|
|
|
|
|
|
|
# 返回成功和静态文件的URL
|
|
|
|
|
return {"success": True, "message": "Word文件生成成功", "download_url": f"{docx_file_name}"}
|
|
|
|
@ -217,38 +219,39 @@ async def get_docx_file(
|
|
|
|
|
|
|
|
|
|
# 设置问题为系统推荐问题 ,0:取消,1:设置
|
|
|
|
|
@app.post("/questions/set_system_recommend")
|
|
|
|
|
def set_system_recommend(question_id: str = Form(...), flag: str = Form(...)):
|
|
|
|
|
set_system_recommend_questions(question_id, flag)
|
|
|
|
|
async def set_system_recommend(
|
|
|
|
|
question_id: str = Form(...),
|
|
|
|
|
flag: str = Form(...),
|
|
|
|
|
db: asyncpg.Connection = Depends(get_db) # 添加 db 参数
|
|
|
|
|
):
|
|
|
|
|
await set_system_recommend_questions(db, 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(...)):
|
|
|
|
|
set_user_collect_questions(question_id, flag)
|
|
|
|
|
async def set_user_collect(
|
|
|
|
|
question_id: str = Form(...),
|
|
|
|
|
flag: str = Form(...),
|
|
|
|
|
db: asyncpg.Connection = Depends(get_db) # 添加 db 参数
|
|
|
|
|
):
|
|
|
|
|
await set_user_collect_questions(db, question_id, flag)
|
|
|
|
|
# 提示保存成功
|
|
|
|
|
return {"success": True, "message": "保存成功"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 查询有哪些系统推荐问题
|
|
|
|
|
@app.get("/questions/get_system_recommend")
|
|
|
|
|
def get_system_recommend():
|
|
|
|
|
async def get_system_recommend(db: asyncpg.Connection = Depends(get_db)): # 添加 db 参数
|
|
|
|
|
# 查询所有系统推荐问题
|
|
|
|
|
system_recommend_questions = get_system_recommend_questions()
|
|
|
|
|
system_recommend_questions = await get_system_recommend_questions(db)
|
|
|
|
|
# 返回查询结果
|
|
|
|
|
return {"success": True, "data": system_recommend_questions}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 查询有哪些用户收藏问题
|
|
|
|
|
@app.get("/questions/get_user_collect")
|
|
|
|
|
def get_user_collect():
|
|
|
|
|
async def get_user_collect(db: asyncpg.Connection = Depends(get_db)): # 添加 db 参数
|
|
|
|
|
# 查询所有用户收藏问题
|
|
|
|
|
user_collect_questions = get_user_collect_questions()
|
|
|
|
|
user_collect_questions = await get_user_collect_questions(db)
|
|
|
|
|
# 返回查询结果
|
|
|
|
|
return {"success": True, "data": user_collect_questions}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 启动 FastAPI
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True, workers=4)
|
|
|
|
|