main
HuangHai 2 weeks ago
parent a5c5149bd2
commit 717ceb542c

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

@ -2201,6 +2201,16 @@
"embedding_min": null,
"embedding_max": null,
"original_prompt": "苏轼的家人都有谁?"
},
"585eef85444b748e7c7fce9281f7152b": {
"return": "{\"high_level_keywords\": [\"\\u82cf\\u8f7c\", \"\\u5bb6\\u5ead\\u6210\\u5458\", \"\\u5bb6\\u65cf\\u5173\\u7cfb\"], \"low_level_keywords\": [\"\\u7a0b\\u592b\\u4eba\", \"\\u738b\\u5f17\", \"\\u82cf\\u8f99\", \"\\u82cf\\u8fc8\", \"\\u7709\\u5c71\"]}",
"cache_type": "keywords",
"chunk_id": null,
"embedding": null,
"embedding_shape": null,
"embedding_min": null,
"embedding_max": null,
"original_prompt": "苏轼的家人都有谁?\n 不要输出参考资料!"
}
}
}
Loading…
Cancel
Save