From 569b6a5d611f9017cf1734f5a52b2d26f9ce0089 Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Mon, 7 Jul 2025 10:53:00 +0800 Subject: [PATCH] 'commit' --- dsRagAnything/T3_Start.py | 43 ++++++++++--------- .../Chinese/kv_store_llm_response_cache.json | 10 +++++ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/dsRagAnything/T3_Start.py b/dsRagAnything/T3_Start.py index 517f1cb9..b5fac659 100644 --- a/dsRagAnything/T3_Start.py +++ b/dsRagAnything/T3_Start.py @@ -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)) diff --git a/dsRagAnything/Topic/Chinese/kv_store_llm_response_cache.json b/dsRagAnything/Topic/Chinese/kv_store_llm_response_cache.json index dde67cb6..f3d0d201 100644 --- a/dsRagAnything/Topic/Chinese/kv_store_llm_response_cache.json +++ b/dsRagAnything/Topic/Chinese/kv_store_llm_response_cache.json @@ -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 不要输出参考资料!" } } } \ No newline at end of file