|
|
|
@ -38,8 +38,25 @@ logger.addHandler(console_handler)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
# 初始化RAG
|
|
|
|
|
app.state.rag = await initialize_rag(working_dir="./Topic/Math")
|
|
|
|
|
# 初始化RAG组件
|
|
|
|
|
llm_model_func = create_llm_model_func()
|
|
|
|
|
vision_model_func = create_vision_model_func(llm_model_func)
|
|
|
|
|
embedding_func = create_embedding_func()
|
|
|
|
|
|
|
|
|
|
lightrag_instance = LightRAG(
|
|
|
|
|
working_dir=WORKING_PATH,
|
|
|
|
|
llm_model_func=llm_model_func,
|
|
|
|
|
embedding_func=embedding_func
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await lightrag_instance.initialize_storages()
|
|
|
|
|
await initialize_pipeline_status()
|
|
|
|
|
|
|
|
|
|
# 创建RAG实例并保存到app.state
|
|
|
|
|
app.state.rag = RAGAnything(
|
|
|
|
|
lightrag=lightrag_instance,
|
|
|
|
|
vision_model_func=vision_model_func,
|
|
|
|
|
)
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
# 清理资源
|
|
|
|
@ -58,7 +75,7 @@ app = FastAPI(lifespan=lifespan)
|
|
|
|
|
app.mount("/static", StaticFiles(directory="Static"), name="static")
|
|
|
|
|
|
|
|
|
|
# 数学索引文件目录
|
|
|
|
|
#WORKING_PATH = "./Topic/Math"
|
|
|
|
|
# WORKING_PATH = "./Topic/Math"
|
|
|
|
|
|
|
|
|
|
# 苏轼索引文件目录
|
|
|
|
|
WORKING_PATH = "./Topic/Chinese"
|
|
|
|
@ -68,35 +85,18 @@ WORKING_PATH = "./Topic/Chinese"
|
|
|
|
|
async def rag(request: fastapi.Request):
|
|
|
|
|
data = await request.json()
|
|
|
|
|
query = data.get("query")
|
|
|
|
|
# 关闭参考资料
|
|
|
|
|
query = query + "\n 不要输出参考资料!"
|
|
|
|
|
|
|
|
|
|
async def generate_response_stream(query: str):
|
|
|
|
|
try:
|
|
|
|
|
llm_model_func = create_llm_model_func()
|
|
|
|
|
vision_model_func = create_vision_model_func(llm_model_func)
|
|
|
|
|
embedding_func = create_embedding_func()
|
|
|
|
|
|
|
|
|
|
lightrag_instance = LightRAG(
|
|
|
|
|
working_dir=WORKING_PATH,
|
|
|
|
|
llm_model_func=llm_model_func,
|
|
|
|
|
embedding_func=embedding_func
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await lightrag_instance.initialize_storages()
|
|
|
|
|
await initialize_pipeline_status()
|
|
|
|
|
|
|
|
|
|
rag = RAGAnything(
|
|
|
|
|
lightrag=lightrag_instance,
|
|
|
|
|
vision_model_func=vision_model_func,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 使用stream=True参数确保流式输出
|
|
|
|
|
resp = await rag.aquery(
|
|
|
|
|
# 直接使用app.state中已初始化的rag实例
|
|
|
|
|
resp = await app.state.rag.aquery(
|
|
|
|
|
query=query,
|
|
|
|
|
mode="hybrid", # 直接传入mode参数
|
|
|
|
|
stream=True # 直接传入stream参数
|
|
|
|
|
mode="hybrid",
|
|
|
|
|
stream=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 直接处理流式响应
|
|
|
|
|
async for chunk in resp:
|
|
|
|
|
if not chunk:
|
|
|
|
|
continue
|
|
|
|
|