|
|
|
@ -1,13 +1,20 @@
|
|
|
|
|
import json
|
|
|
|
|
import subprocess
|
|
|
|
|
import tempfile
|
|
|
|
|
import urllib
|
|
|
|
|
import uuid
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
import fastapi
|
|
|
|
|
import uvicorn
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
|
from lightrag import QueryParam
|
|
|
|
|
from sse_starlette import EventSourceResponse
|
|
|
|
|
from starlette.responses import StreamingResponse
|
|
|
|
|
from starlette.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
|
|
from Util.LightRagUtil import *
|
|
|
|
|
from Util.MySQLUtil import init_mysql_pool
|
|
|
|
|
|
|
|
|
|
# 在程序开始时添加以下配置
|
|
|
|
|
logging.basicConfig(
|
|
|
|
@ -85,5 +92,158 @@ async def rag(request: fastapi.Request):
|
|
|
|
|
return EventSourceResponse(generate_response_stream(query=query))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/save-word")
|
|
|
|
|
async def save_to_word(request: fastapi.Request):
|
|
|
|
|
output_file = None
|
|
|
|
|
try:
|
|
|
|
|
# Parse request data
|
|
|
|
|
try:
|
|
|
|
|
data = await request.json()
|
|
|
|
|
markdown_content = data.get('markdown_content', '')
|
|
|
|
|
if not markdown_content:
|
|
|
|
|
raise ValueError("Empty MarkDown content")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Request parsing failed: {str(e)}")
|
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid request: {str(e)}")
|
|
|
|
|
|
|
|
|
|
# 创建临时Markdown文件
|
|
|
|
|
temp_md = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".md")
|
|
|
|
|
with open(temp_md, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(markdown_content)
|
|
|
|
|
|
|
|
|
|
# 使用pandoc转换
|
|
|
|
|
output_file = os.path.join(tempfile.gettempdir(), "【理想大模型】问答.docx")
|
|
|
|
|
subprocess.run(['pandoc', temp_md, '-o', output_file, '--resource-path=static'], check=True)
|
|
|
|
|
|
|
|
|
|
# 读取生成的Word文件
|
|
|
|
|
with open(output_file, "rb") as f:
|
|
|
|
|
stream = BytesIO(f.read())
|
|
|
|
|
|
|
|
|
|
# 返回响应
|
|
|
|
|
encoded_filename = urllib.parse.quote("【理想大模型】问答.docx")
|
|
|
|
|
return StreamingResponse(
|
|
|
|
|
stream,
|
|
|
|
|
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
|
headers={"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"})
|
|
|
|
|
|
|
|
|
|
except HTTPException:
|
|
|
|
|
raise
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Unexpected error: {str(e)}")
|
|
|
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
|
|
|
|
finally:
|
|
|
|
|
# 清理临时文件
|
|
|
|
|
try:
|
|
|
|
|
if temp_md and os.path.exists(temp_md):
|
|
|
|
|
os.remove(temp_md)
|
|
|
|
|
if output_file and os.path.exists(output_file):
|
|
|
|
|
os.remove(output_file)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning(f"Failed to clean up temp files: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/tree-data")
|
|
|
|
|
async def get_tree_data():
|
|
|
|
|
try:
|
|
|
|
|
mysql_pool = await init_mysql_pool()
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping()
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
await cur.execute("""
|
|
|
|
|
SELECT id,
|
|
|
|
|
title,
|
|
|
|
|
parent_id,
|
|
|
|
|
is_leaf,
|
|
|
|
|
prerequisite,
|
|
|
|
|
related
|
|
|
|
|
FROM knowledge_points
|
|
|
|
|
ORDER BY parent_id, id
|
|
|
|
|
""")
|
|
|
|
|
rows = await cur.fetchall()
|
|
|
|
|
|
|
|
|
|
# 构建节点映射
|
|
|
|
|
nodes = {}
|
|
|
|
|
for row in rows:
|
|
|
|
|
prerequisite_data = json.loads(row[4]) if row[4] else []
|
|
|
|
|
# 转换先修知识格式
|
|
|
|
|
if isinstance(prerequisite_data, list) and len(prerequisite_data) > 0 and isinstance(prerequisite_data[0],
|
|
|
|
|
dict):
|
|
|
|
|
# 已经是新格式
|
|
|
|
|
prerequisites = prerequisite_data
|
|
|
|
|
else:
|
|
|
|
|
# 转换为新格式
|
|
|
|
|
prerequisites = [{"id": str(id), "title": title} for id, title in
|
|
|
|
|
(prerequisite_data or [])] if prerequisite_data else None
|
|
|
|
|
|
|
|
|
|
nodes[row[0]] = {
|
|
|
|
|
"id": row[0],
|
|
|
|
|
"title": row[1],
|
|
|
|
|
"parent_id": row[2] if row[2] is not None else 0,
|
|
|
|
|
"isParent": not row[3],
|
|
|
|
|
"prerequisite": prerequisites if prerequisites and len(prerequisites) > 0 else None,
|
|
|
|
|
"related": json.loads(row[5]) if row[5] and len(json.loads(row[5])) > 0 else None,
|
|
|
|
|
"open": True
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 构建树形结构
|
|
|
|
|
tree_data = []
|
|
|
|
|
for node_id, node in nodes.items():
|
|
|
|
|
parent_id = node["parent_id"]
|
|
|
|
|
if parent_id == 0:
|
|
|
|
|
tree_data.append(node)
|
|
|
|
|
else:
|
|
|
|
|
if parent_id in nodes:
|
|
|
|
|
if "children" not in nodes[parent_id]:
|
|
|
|
|
nodes[parent_id]["children"] = []
|
|
|
|
|
nodes[parent_id]["children"].append(node)
|
|
|
|
|
|
|
|
|
|
return {"code": 0, "data": tree_data}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {"code": 1, "msg": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/update-knowledge")
|
|
|
|
|
async def update_knowledge(request: fastapi.Request):
|
|
|
|
|
try:
|
|
|
|
|
data = await request.json()
|
|
|
|
|
node_id = data.get('node_id')
|
|
|
|
|
knowledge = data.get('knowledge', [])
|
|
|
|
|
update_type = data.get('update_type', 'prerequisite') # 默认为先修知识
|
|
|
|
|
|
|
|
|
|
if not node_id:
|
|
|
|
|
raise ValueError("Missing node_id")
|
|
|
|
|
|
|
|
|
|
mysql_pool = await init_mysql_pool()
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping()
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
if update_type == 'prerequisite':
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"""
|
|
|
|
|
UPDATE knowledge_points
|
|
|
|
|
SET prerequisite = %s
|
|
|
|
|
WHERE id = %s
|
|
|
|
|
""",
|
|
|
|
|
(json.dumps([{"id": p["id"], "title": p["title"]} for p in knowledge], ensure_ascii=False),
|
|
|
|
|
node_id)
|
|
|
|
|
)
|
|
|
|
|
else: # related knowledge
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"""
|
|
|
|
|
UPDATE knowledge_points
|
|
|
|
|
SET related = %s
|
|
|
|
|
WHERE id = %s
|
|
|
|
|
""",
|
|
|
|
|
(json.dumps([{"id": p["id"], "title": p["title"]} for p in knowledge], ensure_ascii=False),
|
|
|
|
|
node_id)
|
|
|
|
|
)
|
|
|
|
|
await conn.commit()
|
|
|
|
|
|
|
|
|
|
return {"code": 0, "msg": "更新成功"}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"更新知识失败: {str(e)}")
|
|
|
|
|
return {"code": 1, "msg": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|