|
|
@ -54,10 +54,10 @@ app = FastAPI(lifespan=lifespan)
|
|
|
|
# 挂载静态文件目录
|
|
|
|
# 挂载静态文件目录
|
|
|
|
app.mount("/static", StaticFiles(directory="Static"), name="static")
|
|
|
|
app.mount("/static", StaticFiles(directory="Static"), name="static")
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/rag", response_model=None)
|
|
|
|
@app.post("/api/rag")
|
|
|
|
async def rag(request: fastapi.Request):
|
|
|
|
async def rag(request: fastapi.Request):
|
|
|
|
data = await request.json()
|
|
|
|
data = await request.json()
|
|
|
|
query = data.get('query', '')
|
|
|
|
query = data.get("query")
|
|
|
|
working_dir = "./Test/Math" # 使用与T2_Query.py相同的目录
|
|
|
|
working_dir = "./Test/Math" # 使用与T2_Query.py相同的目录
|
|
|
|
|
|
|
|
|
|
|
|
async def generate_response_stream(query: str):
|
|
|
|
async def generate_response_stream(query: str):
|
|
|
@ -69,17 +69,15 @@ async def rag(request: fastapi.Request):
|
|
|
|
if not chunk:
|
|
|
|
if not chunk:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
# 正确的SSE格式应该是单层data:前缀
|
|
|
|
# 确保SSE格式正确
|
|
|
|
yield f"data: {json.dumps({'reply': chunk})}\n\n"
|
|
|
|
yield f"data: {json.dumps({'reply': chunk})}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
# 打印到控制台用于调试
|
|
|
|
|
|
|
|
print(chunk, end='', flush=True)
|
|
|
|
print(chunk, end='', flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
|
|
|
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
|
|
|
finally:
|
|
|
|
finally:
|
|
|
|
if 'rag' in locals():
|
|
|
|
if 'rag' in locals():
|
|
|
|
await rag.finalize()
|
|
|
|
await rag.finalize_storages()
|
|
|
|
|
|
|
|
|
|
|
|
return EventSourceResponse(generate_response_stream(query=query))
|
|
|
|
return EventSourceResponse(generate_response_stream(query=query))
|
|
|
|
|
|
|
|
|
|
|
|