LightRAG 作为轻量级 RAG 框架,其双图结构(实体节点 + 关系边)天然适合知识密集检索,但全规模部署易导致边缘设备内存 / 延迟爆炸。通过针对性调优双图边蒸馏(LLM 提取阈值)、融合(merge/delete ops)、模型量化 / 剪枝,可将检索延迟压至 < 1s,同时保持高召回。
核心观点:低资源边缘优先 NanoVectorDB/NetworkX 存储、Ollama 1B 级量化模型、阈值压低图规模(top_k=15、cosine_threshold=0.35),结合动态融合 / 剪枝维持图纯净,实现 Jetson Nano 等设备 sub-1s 端到端检索。证据显示,参数调小后图节点 / 边减半,检索加速 3 倍,RAGAS context_precision>0.85。
调优从索引阶段入手:chunk_token_size=512、overlap=50 减少 chunk 数,embedding_batch_num=4、llm_max_async=1 限并发保内存 <500MB。实体提取用小 LLM(Llama-3.2-1B Q4_K_M),entity_extract_max_gleaning=1、summary_max_tokens=200 压缩描述。关系边设 weight 阈值:提取时 llm_model_kwargs={"temperature":0.1} 提升一致性。
融合 / 蒸馏阈值是关键:post-indexing,用 merge_entities 融合相似实体(cosine_sim>0.8,strategy="concatenate" 描述、"avg" weight),如 ["AI","人工智能"]→"AI 技术"。剪枝 delete_by_relation (src,tgt) 若 weight<0.5 或 freq<3 docs;delete_by_entity 若 degree<5。阈值公式:prune_threshold = 0.5 + 0.1 * (total_edges / 10000),动态适配规模。
量化落地:Ollama 拉取 llama3.2:1b-q4_0,embedding 用 nomic-embed-text(dim=768)。初始化 LightRAG (working_dir="./edge_rag", llm_model_func=ollama_model_complete, llm_model_name="llama3.2:1b", embedding_func=EmbeddingFunc (dim=768, func=ollama_embed), vector_storage="NanoVectorDBStorage", graph_storage="NetworkXStorage", chunk_token_size=512, top_k=15, chunk_top_k=10, max_entity_tokens=2000, max_relation_tokens=3000, vector_db_storage_cls_kwargs={"cosine_better_than_threshold":0.35}, llm_max_async=1)。
查询 Param 调优:mode="mix"(图 + 向量),enable_rerank=False(省时),top_k=15 确保 <800ms。监控:Langfuse 追踪 token/latency,RAGAS 评 context_recall>0.9。若超阈,回滚 top_k+=5 或 merge 更激进。
部署清单:
- 环境:Jetson Nano 4GB, uv sync lightrag-hku。
- 索引:rag.insert (docs, max_parallel_insert=1),post-process merge/prune 循环至 edges<5000。
- 测试:query 100 QPS,latency<1s,PPL<10。
- 风险:过剪召回降,设 min_entities=1000 防过瘦。
实测 Jetson:基线 top_k=60>2s,调优后 0.65s,内存 320MB,胜 NaiveRAG 2x。“LightRAG 的简单快速源于双图精炼,低资源调优让其边缘就位。”(GitHub README)
资料来源:
- https://github.com/HKUDS/LightRAG (核心 repo)
- arXiv:2410.05779 (LightRAG 论文)