Retriever 接口 (Retrievers)¶
检索器负责从 LightRAG 知识图谱中获取相关文档。每个检索器封装一种查询模式的检索策略,通过向量相似度搜索和/或图遍历获取实体、关系和文本块,返回 LangChain Document 对象。
基类¶
lightrag_langchain.retriever.base.LightRAGBaseRetriever ¶
Bases: BaseRetriever
所有 LightRAG 查询模式 Retriever 的抽象基类。
封装共享基础设施(embedding 生成、异步桥接、错误处理),使每个模式特定子类
只需实现 _aget_relevant_documents 及其自身的策略调用和 Document 转换逻辑 (D-06)。
Parameters¶
vector_store:
用于向量相似度搜索的 PGVectorStore 实例 (D-01)。
embedding_config:
用于延迟创建 embedding 模型的 EmbeddingConfig (D-02)。
graph_store:
用于图查询的 PGGraphStore 实例。可选 — naive 和 bypass 模式不需要(默认 None)。
top_k:
覆盖全局 top_k。当为 None 时,Retriever 使用 Settings 级别的默认值 (D-03)。
chunk_top_k:
覆盖全局 chunk_top_k。当为 None 时,Retriever 使用 Settings 级别的默认值 (D-03)。
Example
源代码位于: src/lightrag_langchain/retriever/base.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
vector_store
instance-attribute
¶
用于向量相似度搜索的 PGVectorStore (D-01 构造函数注入)。
embedding_config
instance-attribute
¶
Embedding provider 配置;由延迟 embedding 属性消费 (D-02)。
graph_store
class-attribute
instance-attribute
¶
用于图查询的 PGGraphStore;可选(naive/bypass 不需要)。
top_k
class-attribute
instance-attribute
¶
覆盖全局 top_k;None 表示使用 Settings 默认值 (D-03)。
chunk_top_k
class-attribute
instance-attribute
¶
覆盖 chunk_top_k;None 表示使用 Settings 默认值 (D-03)。
embedding
property
¶
返回 OpenAIEmbeddings 实例,在首次访问时创建。
使用 :func:create_embedding(self.embedding_config),它返回一个
_LazyEmbedding 代理 — 实际的 OpenAIEmbeddings 构造延迟到
首次访问返回对象的属性时。导入时无网络调用 (D-02)。
查询模式 Retriever 子类¶
lightrag_langchain.retriever.retrievers.NaiveRetriever ¶
Bases: LightRAGBaseRetriever
LightRAG naive 查询模式的 LangChain Retriever。
在 chunks_vdb 表上执行纯向量相似度搜索。 不进行图遍历 — 仅返回 chunk Document。
RETR-01 (naive mode)。graph_store 不使用(始终为 None)。
Example
源代码位于: src/lightrag_langchain/retriever/retrievers.py
lightrag_langchain.retriever.retrievers.LocalRetriever ¶
Bases: LightRAGBaseRetriever
LightRAG local 查询模式的 LangChain Retriever。
在 entities_vdb 中搜索 top-K 实体,扩展到 AGE 图中发现边/邻居, 返回 entity + GraphTriple Document。
RETR-01 (local mode)。需要同时 vector_store 和 graph_store。
Example
源代码位于: src/lightrag_langchain/retriever/retrievers.py
lightrag_langchain.retriever.retrievers.GlobalRetriever ¶
Bases: LightRAGBaseRetriever
LightRAG global 查询模式的 LangChain Retriever。
在 relationships_vdb 中搜索 top-K 关系,批量从 AGE 图中检索边数据, 返回 relation + GraphTriple Document。
RETR-01 (global mode)。需要同时 vector_store 和 graph_store。
Example
源代码位于: src/lightrag_langchain/retriever/retrievers.py
lightrag_langchain.retriever.retrievers.HybridRetriever ¶
Bases: LightRAGBaseRetriever
LightRAG hybrid 查询模式的 LangChain Retriever。
并行运行 local 和 global 策略,然后轮询合并 entities 和 relations。 返回 entity + relation + GraphTriple Document。
RETR-01 (hybrid mode)。需要同时 vector_store 和 graph_store。
Example
源代码位于: src/lightrag_langchain/retriever/retrievers.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
lightrag_langchain.retriever.retrievers.MixRetriever ¶
Bases: LightRAGBaseRetriever
LightRAG mix 查询模式的 LangChain Retriever。
并行运行 hybrid 策略和 chunk 向量搜索,通过轮询将实体伪块与文本块合并。 返回 entity + relation + chunk + GraphTriple Document。
RETR-01 (mix mode)。需要同时 vector_store 和 graph_store。
Example
源代码位于: src/lightrag_langchain/retriever/retrievers.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
lightrag_langchain.retriever.retrievers.BypassRetriever ¶
Bases: LightRAGBaseRetriever
LightRAG bypass 查询模式的 LangChain Retriever。
不进行检索 — 返回空的 list[Document]。无 embedding 生成、无策略调用、
无数据库 I/O。
RETR-01 (bypass mode)。vector_store 和 graph_store 不使用。