|
|
|
@ -38,29 +38,8 @@ logger.addHandler(console_handler)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
# 初始化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
|
|
|
|
|
|
|
|
|
|
# 清理资源
|
|
|
|
|
await app.state.rag.finalize_storages()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def print_stream(stream):
|
|
|
|
@ -90,6 +69,25 @@ async def rag(request: fastapi.Request):
|
|
|
|
|
|
|
|
|
|
async def generate_response_stream(query: str):
|
|
|
|
|
try:
|
|
|
|
|
# 初始化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,
|
|
|
|
|
)
|
|
|
|
|
# 直接使用app.state中已初始化的rag实例
|
|
|
|
|
resp = await app.state.rag.aquery(
|
|
|
|
|
query=query,
|
|
|
|
@ -105,6 +103,9 @@ async def rag(request: fastapi.Request):
|
|
|
|
|
except Exception as e:
|
|
|
|
|
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
|
|
|
|
logger.error(f"处理查询时出错: {query}. 错误: {str(e)}")
|
|
|
|
|
finally:
|
|
|
|
|
# 清理资源
|
|
|
|
|
await app.state.rag.lightrag.finalize_storages()
|
|
|
|
|
|
|
|
|
|
return EventSourceResponse(generate_response_stream(query=query))
|
|
|
|
|
|
|
|
|
|