跳到主要内容
本指南介绍了图 RAG。有关所有支持功能和配置的详细文档,请参阅图 RAG 项目页面

概览

langchain-graph-retriever 包中的 GraphRetriever 提供了一个 LangChain 检索器,它将基于向量的非结构化相似性搜索与元数据属性的结构化遍历相结合。这使得可以在现有向量存储上进行基于图的检索。

集成详情

检索器来源PyPI 包最新项目页面
GraphRetrievergithub.com/datastax/graph-raglangchain-graph-retrieverPyPI - Version图 RAG

优势

设置

安装

此检索器位于 langchain-graph-retriever 包中。
pip install -qU langchain-graph-retriever

实例化

以下示例将展示如何在关于动物的一些示例文档上执行图遍历。

先决条件

填充向量存储

本节展示了如何用示例数据填充各种向量存储。 有关如何选择以下向量存储之一,或如何为您的向量存储添加支持,请查阅关于适配器和受支持的存储的文档。
  • AstraDB
  • Apache Cassandra
  • OpenSearch
  • Chroma
  • 内存中
安装带有 astra 扩展的 langchain-graph-retriever
pip install "langchain-graph-retriever[astra]"
然后创建向量存储并加载测试文档
from langchain_astradb import AstraDBVectorStore

vector_store = AstraDBVectorStore.from_documents(
    documents=animals,
    embedding=embeddings,
    collection_name="animals",
    api_endpoint=ASTRA_DB_API_ENDPOINT,
    token=ASTRA_DB_APPLICATION_TOKEN,
)
有关 ASTRA_DB_API_ENDPOINTASTRA_DB_APPLICATION_TOKEN 凭据,请查阅 AstraDB 向量存储指南:::note 为了更快的初始测试,请考虑使用内存中向量存储。:::

图遍历

此图检索器从最符合查询的单个动物开始,然后遍历到共享相同 habitat 和/或 origin 的其他动物。
from graph_retriever.strategies import Eager
from langchain_graph_retriever import GraphRetriever

traversal_retriever = GraphRetriever(
    store = vector_store,
    edges = [("habitat", "habitat"), ("origin", "origin")],
    strategy = Eager(k=5, start_k=1, max_depth=2),
)
上述代码创建了一个图遍历检索器,它从最近的动物(start_k=1)开始,检索 5 个文档(k=5),并将搜索限制在距离第一只动物最多 2 步的文档(max_depth=2)。 edges 定义了如何使用元数据值进行遍历。在这种情况下,每只动物都与其他具有相同 habitat 和/或 origin 的动物相连。
results = traversal_retriever.invoke("what animals could be found near a capybara?")

for doc in results:
    print(f"{doc.id}: {doc.page_content}")
capybara: capybaras are the largest rodents in the world and are highly social animals.
heron: herons are wading birds known for their long legs and necks, often seen near water.
crocodile: crocodiles are large reptiles with powerful jaws and a long lifespan, often living over 70 years.
frog: frogs are amphibians known for their jumping ability and croaking sounds.
duck: ducks are waterfowl birds known for their webbed feet and quacking sounds.
图遍历通过利用数据中的结构化关系来提高检索质量。与标准相似性搜索(见下文)不同,它为文档选择提供了清晰、可解释的理由。 在这种情况下,文档 capybaraheronfrogcrocodilenewt 都共享相同的 habitat=wetlands,如其元数据所定义。这应该会增加文档相关性和 LLM 回答的质量。

与标准检索的比较

max_depth=0 时,图遍历检索器表现得像一个标准检索器
standard_retriever = GraphRetriever(
    store = vector_store,
    edges = [("habitat", "habitat"), ("origin", "origin")],
    strategy = Eager(k=5, start_k=5, max_depth=0),
)
这会创建一个检索器,它从最近的 5 只动物(start_k=5)开始,并返回它们而不进行任何遍历(max_depth=0)。在这种情况下,边缘定义被忽略。 这与以下情况基本相同:
standard_retriever = vector_store.as_retriever(search_kwargs={"k":5})
对于这两种情况,调用检索器都会返回
results = standard_retriever.invoke("what animals could be found near a capybara?")

for doc in results:
    print(f"{doc.id}: {doc.page_content}")
capybara: capybaras are the largest rodents in the world and are highly social animals.
iguana: iguanas are large herbivorous lizards often found basking in trees and near water.
guinea pig: guinea pigs are small rodents often kept as pets due to their gentle and social nature.
hippopotamus: hippopotamuses are large semi-aquatic mammals known for their massive size and territorial behavior.
boar: boars are wild relatives of pigs, known for their tough hides and tusks.
这些文档仅基于相似性连接。存储中存在的任何结构化数据都将被忽略。与图检索相比,这会降低文档相关性,因为返回结果对回答查询的帮助程度较低。

用法

按照上述示例,invoke 用于启动对查询的检索。

API 参考

要探索所有可用参数和高级配置,请参阅图 RAG API 参考
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.