main
HuangHai 2 weeks ago
parent a29e2a2dd2
commit 4552607419

@ -1,7 +1,6 @@
import json
import logging
import os
import warnings
from logging.handlers import RotatingFileHandler
import fastapi
@ -42,7 +41,7 @@ async def lifespan(app: FastAPI):
# 初始化RAG
app.state.rag = await initialize_rag(working_dir="./Topic/Math")
yield
# 清理资源
await app.state.rag.finalize_storages()
@ -58,12 +57,18 @@ app = FastAPI(lifespan=lifespan)
# 挂载静态文件目录
app.mount("/static", StaticFiles(directory="Static"), name="static")
# 数学索引文件目录
#WORKING_PATH = "./Topic/Math"
# 苏轼索引文件目录
WORKING_PATH = "./Topic/Chinese"
@app.post("/api/rag")
async def rag(request: fastapi.Request):
data = await request.json()
query = data.get("query")
lightrag_working_dir = "./Topic/Math"
async def generate_response_stream(query: str):
try:
llm_model_func = create_llm_model_func()
@ -71,7 +76,7 @@ async def rag(request: fastapi.Request):
embedding_func = create_embedding_func()
lightrag_instance = LightRAG(
working_dir=lightrag_working_dir,
working_dir=WORKING_PATH,
llm_model_func=llm_model_func,
embedding_func=embedding_func
)
@ -88,9 +93,9 @@ async def rag(request: fastapi.Request):
resp = await rag.aquery(
query=query,
mode="hybrid", # 直接传入mode参数
stream=True # 直接传入stream参数
stream=True # 直接传入stream参数
)
# 直接处理流式响应
async for chunk in resp:
if not chunk:

@ -1,77 +0,0 @@
import json
import logging
import os
import warnings
from logging.handlers import RotatingFileHandler
import fastapi
import uvicorn
from fastapi import FastAPI
from sse_starlette import EventSourceResponse
from starlette.staticfiles import StaticFiles
from Util.RagUtil import initialize_rag
from lightrag import QueryParam
# 初始化日志
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# 配置日志处理器
log_file = os.path.join(os.path.dirname(__file__), 'Logs', 'app.log')
os.makedirs(os.path.dirname(log_file), exist_ok=True)
# 文件处理器
file_handler = RotatingFileHandler(
log_file, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8')
file_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
# 控制台处理器
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(file_handler)
logger.addHandler(console_handler)
async def lifespan(app: FastAPI):
# 初始化RAG
app.state.rag = await initialize_rag(working_dir="./Topic/Chinese")
yield
# 清理资源
await app.state.rag.finalize_storages()
app = FastAPI(lifespan=lifespan)
# 挂载静态文件目录
app.mount("/static", StaticFiles(directory="Static"), name="static")
@app.post("/api/rag")
async def rag(request: fastapi.Request):
data = await request.json()
query = data.get("query")
async def generate_response_stream(query: str):
try:
resp = await request.app.state.rag.aquery(
query=query,
param=QueryParam(mode="hybrid", stream=True))
async for chunk in resp:
if not chunk:
continue
yield f"data: {json.dumps({'reply': chunk})}\n\n"
print(chunk, end='', flush=True)
except Exception as e:
yield f"data: {json.dumps({'error': str(e)})}\n\n"
return EventSourceResponse(generate_response_stream(query=query))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

@ -2183,7 +2183,7 @@
"original_prompt": "小学数学有哪些模型"
},
"6a2a99a3bd4688b079a45fedbd1cd493": {
"return": "{\"high_level_keywords\": [\"\\u5c0f\\u5b66\\u6570\\u5b66\", \"\\u6570\\u5b66\\u6a21\\u578b\", \"\\u57fa\\u7840\\u6559\\u80b2\"], \"low_level_keywords\": [\"\\u52a0\\u51cf\\u6cd5\\u6a21\\u578b\", \"\\u51e0\\u4f55\\u56fe\\u5f62\", \"\\u5206\\u6570\\u6a21\\u578b\", \"\\u5e94\\u7528\\u9898\", \"\\u6570\\u8f74\"]}",
"return": "{\"high_level_keywords\": [\"\\u5c0f\\u5b66\\u6570\\u5b66\", \"\\u6570\\u5b66\\u6559\\u80b2\", \"\\u5b66\\u4e60\\u6a21\\u578b\"], \"low_level_keywords\": [\"\\u52a0\\u51cf\\u6cd5\\u6a21\\u578b\", \"\\u4e58\\u9664\\u6cd5\\u6a21\\u578b\", \"\\u51e0\\u4f55\\u56fe\\u5f62\", \"\\u5206\\u6570\\u6a21\\u578b\", \"\\u5e94\\u7528\\u9898\"]}",
"cache_type": "keywords",
"chunk_id": null,
"embedding": null,
@ -2191,6 +2191,16 @@
"embedding_min": null,
"embedding_max": null,
"original_prompt": "小学数学中有哪些模型?"
},
"9b50a199b83407c62d8532b7b904c17d": {
"return": "{\"high_level_keywords\": [\"\\u82cf\\u8f7c\", \"\\u5bb6\\u4eba\", \"\\u5bb6\\u5ead\\u5173\\u7cfb\"], \"low_level_keywords\": [\"\\u82cf\\u6d35\", \"\\u82cf\\u8f99\", \"\\u738b\\u5f17\", \"\\u738b\\u671d\\u4e91\", \"\\u82cf\\u8fc8\", \"\\u82cf\\u8fc7\"]}",
"cache_type": "keywords",
"chunk_id": null,
"embedding": null,
"embedding_shape": null,
"embedding_min": null,
"embedding_max": null,
"original_prompt": "苏轼的家人都有谁?"
}
}
}
Loading…
Cancel
Save