main
HuangHai 2 weeks ago
parent 717ceb542c
commit 569b6a5d61

@ -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))

@ -2211,6 +2211,16 @@
"embedding_min": null,
"embedding_max": null,
"original_prompt": "苏轼的家人都有谁?\n 不要输出参考资料!"
},
"5c5ee10a1a6deda17447c905118427e3": {
"return": "{\"high_level_keywords\": [\"\\u82cf\\u8f7c\", \"\\u597d\\u53cb\", \"\\u4eba\\u9645\\u5173\\u7cfb\"], \"low_level_keywords\": [\"\\u9ec4\\u5ead\\u575a\", \"\\u79e6\\u89c2\", \"\\u7c73\\u82be\", \"\\u4f5b\\u5370\", \"\\u6587\\u4eba\\u4ea4\\u5f80\"]}",
"cache_type": "keywords",
"chunk_id": null,
"embedding": null,
"embedding_shape": null,
"embedding_min": null,
"embedding_max": null,
"original_prompt": "苏轼的好朋友都有谁?\n 不要输出参考资料!"
}
}
}
Loading…
Cancel
Save