超越向量检索:用 Spring Boot + Neo4j 手搓 GraphRAG 知识图谱记忆

在之前的博客中,我们通过"多路召回 + Rerank"把向量 RAG 的召回精度拉到了极限。但只要你还在企业知识库、客服工单或风控场景里深耕,迟早会遇到一类向量检索从原理上就无法回答的问题:

  • "用户 A 投诉的订单,对应的商家,和用户 B 上周投诉的商家,是不是同一个?"
  • "这个故障涉及的微服务,依赖了哪些数据库?哪些下游系统会受影响?"

这类问题的答案不在任何单个文档片段 里,而在实体与实体之间的关系里。向量检索做的是"找相似的段落",它天然不关心"张三的老板是谁,老板的部门在哪"这种链式结构。

这就是 GraphRAG 要解决的核心问题:把企业知识从"段落集合"升级为"关系网络",让检索沿着图谱的边做多跳推理。

今天,我们用 Spring Boot + Neo4j 从零手搓一个 GraphRAG 记忆系统。

一、纯向量 RAG 的天花板:为什么"找得到"不等于"找得准"

向量 RAG 的检索单元是 Chunk(文本切片) 。无论切片多精细,每一条检索结果都是孤立的。当问题需要跨文档关联时,向量检索要么召回一堆彼此无关的片段,要么因为"问题本身不包含关键词"而完全漏召回。

GraphRAG 的检索单元是 实体(Entity)和关系(Relation) 。离线阶段,LLM 从文档中抽取实体和关系,构建知识图谱;在线阶段,检索从向量找到"锚点实体",再沿着图谱的边遍历 2~3 跳,把结构化的关系上下文一并带回给 LLM。
#mermaid-svg-PhI5fFDH19E5fcf2{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-PhI5fFDH19E5fcf2 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-PhI5fFDH19E5fcf2 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-PhI5fFDH19E5fcf2 .error-icon{fill:#552222;}#mermaid-svg-PhI5fFDH19E5fcf2 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-PhI5fFDH19E5fcf2 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-PhI5fFDH19E5fcf2 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-PhI5fFDH19E5fcf2 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-PhI5fFDH19E5fcf2 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-PhI5fFDH19E5fcf2 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-PhI5fFDH19E5fcf2 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-PhI5fFDH19E5fcf2 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-PhI5fFDH19E5fcf2 .marker.cross{stroke:#333333;}#mermaid-svg-PhI5fFDH19E5fcf2 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-PhI5fFDH19E5fcf2 p{margin:0;}#mermaid-svg-PhI5fFDH19E5fcf2 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-PhI5fFDH19E5fcf2 .cluster-label text{fill:#333;}#mermaid-svg-PhI5fFDH19E5fcf2 .cluster-label span{color:#333;}#mermaid-svg-PhI5fFDH19E5fcf2 .cluster-label span p{background-color:transparent;}#mermaid-svg-PhI5fFDH19E5fcf2 .label text,#mermaid-svg-PhI5fFDH19E5fcf2 span{fill:#333;color:#333;}#mermaid-svg-PhI5fFDH19E5fcf2 .node rect,#mermaid-svg-PhI5fFDH19E5fcf2 .node circle,#mermaid-svg-PhI5fFDH19E5fcf2 .node ellipse,#mermaid-svg-PhI5fFDH19E5fcf2 .node polygon,#mermaid-svg-PhI5fFDH19E5fcf2 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-PhI5fFDH19E5fcf2 .rough-node .label text,#mermaid-svg-PhI5fFDH19E5fcf2 .node .label text,#mermaid-svg-PhI5fFDH19E5fcf2 .image-shape .label,#mermaid-svg-PhI5fFDH19E5fcf2 .icon-shape .label{text-anchor:middle;}#mermaid-svg-PhI5fFDH19E5fcf2 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-PhI5fFDH19E5fcf2 .rough-node .label,#mermaid-svg-PhI5fFDH19E5fcf2 .node .label,#mermaid-svg-PhI5fFDH19E5fcf2 .image-shape .label,#mermaid-svg-PhI5fFDH19E5fcf2 .icon-shape .label{text-align:center;}#mermaid-svg-PhI5fFDH19E5fcf2 .node.clickable{cursor:pointer;}#mermaid-svg-PhI5fFDH19E5fcf2 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-PhI5fFDH19E5fcf2 .arrowheadPath{fill:#333333;}#mermaid-svg-PhI5fFDH19E5fcf2 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-PhI5fFDH19E5fcf2 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-PhI5fFDH19E5fcf2 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-PhI5fFDH19E5fcf2 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-PhI5fFDH19E5fcf2 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-PhI5fFDH19E5fcf2 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-PhI5fFDH19E5fcf2 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-PhI5fFDH19E5fcf2 .cluster text{fill:#333;}#mermaid-svg-PhI5fFDH19E5fcf2 .cluster span{color:#333;}#mermaid-svg-PhI5fFDH19E5fcf2 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-PhI5fFDH19E5fcf2 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-PhI5fFDH19E5fcf2 rect.text{fill:none;stroke-width:0;}#mermaid-svg-PhI5fFDH19E5fcf2 .icon-shape,#mermaid-svg-PhI5fFDH19E5fcf2 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-PhI5fFDH19E5fcf2 .icon-shape p,#mermaid-svg-PhI5fFDH19E5fcf2 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-PhI5fFDH19E5fcf2 .icon-shape rect,#mermaid-svg-PhI5fFDH19E5fcf2 .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-PhI5fFDH19E5fcf2 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-PhI5fFDH19E5fcf2 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-PhI5fFDH19E5fcf2 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} GraphRAG
问题: A的老板的部门在哪?
向量找到锚点实体: A
沿图遍历: A --汇报给--> B

B --所属--> 部门C
返回结构化路径

LLM 可直接基于路径推理
向量 RAG
问题: A的老板的部门在哪?
Embedding
召回 Top-K 文本片段
片段彼此孤立

无法回答链式关系

一句话总结:Vector RAG 解决"有没有",GraphRAG 解决"连不连得上" 。三代 RAG 的演进脉络中,GraphRAG 填补的正是向量检索在多跳推理上的结构性空白。

二、GraphRAG 架构总览:离线建图 + 在线混合检索

一个可落地的 GraphRAG 系统分为两条链路,离线建图的质量决定了在线检索的上限。
#mermaid-svg-RpCdMjLOEEzCCvaM{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-RpCdMjLOEEzCCvaM .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-RpCdMjLOEEzCCvaM .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-RpCdMjLOEEzCCvaM .error-icon{fill:#552222;}#mermaid-svg-RpCdMjLOEEzCCvaM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RpCdMjLOEEzCCvaM .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-RpCdMjLOEEzCCvaM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RpCdMjLOEEzCCvaM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RpCdMjLOEEzCCvaM .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-RpCdMjLOEEzCCvaM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RpCdMjLOEEzCCvaM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RpCdMjLOEEzCCvaM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RpCdMjLOEEzCCvaM .marker.cross{stroke:#333333;}#mermaid-svg-RpCdMjLOEEzCCvaM svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RpCdMjLOEEzCCvaM p{margin:0;}#mermaid-svg-RpCdMjLOEEzCCvaM .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-RpCdMjLOEEzCCvaM .cluster-label text{fill:#333;}#mermaid-svg-RpCdMjLOEEzCCvaM .cluster-label span{color:#333;}#mermaid-svg-RpCdMjLOEEzCCvaM .cluster-label span p{background-color:transparent;}#mermaid-svg-RpCdMjLOEEzCCvaM .label text,#mermaid-svg-RpCdMjLOEEzCCvaM span{fill:#333;color:#333;}#mermaid-svg-RpCdMjLOEEzCCvaM .node rect,#mermaid-svg-RpCdMjLOEEzCCvaM .node circle,#mermaid-svg-RpCdMjLOEEzCCvaM .node ellipse,#mermaid-svg-RpCdMjLOEEzCCvaM .node polygon,#mermaid-svg-RpCdMjLOEEzCCvaM .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RpCdMjLOEEzCCvaM .rough-node .label text,#mermaid-svg-RpCdMjLOEEzCCvaM .node .label text,#mermaid-svg-RpCdMjLOEEzCCvaM .image-shape .label,#mermaid-svg-RpCdMjLOEEzCCvaM .icon-shape .label{text-anchor:middle;}#mermaid-svg-RpCdMjLOEEzCCvaM .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-RpCdMjLOEEzCCvaM .rough-node .label,#mermaid-svg-RpCdMjLOEEzCCvaM .node .label,#mermaid-svg-RpCdMjLOEEzCCvaM .image-shape .label,#mermaid-svg-RpCdMjLOEEzCCvaM .icon-shape .label{text-align:center;}#mermaid-svg-RpCdMjLOEEzCCvaM .node.clickable{cursor:pointer;}#mermaid-svg-RpCdMjLOEEzCCvaM .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-RpCdMjLOEEzCCvaM .arrowheadPath{fill:#333333;}#mermaid-svg-RpCdMjLOEEzCCvaM .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RpCdMjLOEEzCCvaM .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RpCdMjLOEEzCCvaM .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RpCdMjLOEEzCCvaM .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-RpCdMjLOEEzCCvaM .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RpCdMjLOEEzCCvaM .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-RpCdMjLOEEzCCvaM .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RpCdMjLOEEzCCvaM .cluster text{fill:#333;}#mermaid-svg-RpCdMjLOEEzCCvaM .cluster span{color:#333;}#mermaid-svg-RpCdMjLOEEzCCvaM div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RpCdMjLOEEzCCvaM .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-RpCdMjLOEEzCCvaM rect.text{fill:none;stroke-width:0;}#mermaid-svg-RpCdMjLOEEzCCvaM .icon-shape,#mermaid-svg-RpCdMjLOEEzCCvaM .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RpCdMjLOEEzCCvaM .icon-shape p,#mermaid-svg-RpCdMjLOEEzCCvaM .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-RpCdMjLOEEzCCvaM .icon-shape rect,#mermaid-svg-RpCdMjLOEEzCCvaM .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RpCdMjLOEEzCCvaM .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-RpCdMjLOEEzCCvaM .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-RpCdMjLOEEzCCvaM :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 在线混合检索
关系型问题
简单事实型问题
用户提问
查询路由
向量索引找锚点实体
Cypher 多跳遍历

(2~3 hops)
结构化路径上下文
纯向量 Top-K
LLM 基于路径生成答案
离线建图 (ETL)
文档/工单/邮件
文本切片
LLM 实体与关系抽取

(Structured Output)
实体消解

(别名合并/去重)
Neo4j

知识图谱 + 向量索引

关键设计决策:

  1. Neo4j 同时承担向量索引和图存储,避免"向量库 + 图库"双写带来的双倍查询延迟和一致性问题。
  2. 在线检索不是"纯图遍历":先用向量索引找到语义上最相关的"锚点实体",再从锚点出发遍历图。如果跳过向量直接全图遍历,延迟和噪声都不可接受。
  3. 查询路由是生产级 GraphRAG 的标配:不是所有问题都值得走图。简单 FAQ、改写型问题走纯向量;只有涉及实体间关系、多跳链路的问题才路由到图检索。

三、代码实战:Spring AI + Neo4j 手搓 GraphRAG

3.1 依赖与版本说明

Spring AI 没有独立的 GraphRAG 模块 ,需要组合 spring-ai-neo4j-store 和 ChatClient 来实现。以下代码基于 Spring AI 2.0.x + Spring Boot 4.x,Neo4j 5.x/2026.x 均可运行。

xml 复制代码
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-client-chat</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-neo4j-store</artifactId>
</dependency>
<dependency>
    <groupId>org.neo4j.driver</groupId>
    <artifactId>neo4j-java-driver</artifactId>
</dependency>

application.yml:

yaml 复制代码
spring:
  ai:
    openai:
      api-key: ${OPENAI_API_KEY}
    vectorstore:
      neo4j:
        uri: bolt://localhost:7687
        username: neo4j
        password: your-password
        index-name: entity-embeddings
        initialize-schema: true

3.2 离线建图:LLM 实体与关系抽取

GraphRAG 的建图质量取决于抽取 Prompt 的设计。核心原则:给 LLM 一个封闭的关系类型白名单,不要让它自由发明关系名。

java 复制代码
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class GraphExtractionService {

    private final ChatClient chatClient;

    public GraphExtractionService(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    public record Entity(String name, String type) {}
    public record Relation(String from, String to, String type) {}
    public record Extraction(List<Entity> entities, List<Relation> relations) {}

    public Extraction extract(String chunk) {
        var converter = new BeanOutputConverter<>(Extraction.class);

        String prompt = """
                从以下文本中抽取实体和关系。
                实体类型只允许:PERSON, ORGANIZATION, DEPARTMENT, SERVICE, DATABASE
                关系类型只允许:REPORTS_TO, BELONGS_TO, DEPENDS_ON, COMPLAINED_ABOUT
                只抽取文本中明确出现的关系,不要推测。
                
                文本:
                %s
                
                %s
                """.formatted(chunk, converter.getFormat());

        return chatClient.prompt()
                .user(prompt)
                .call()
                .entity(converter);
    }
}

BeanOutputConverter 会强制 LLM 输出结构化 JSON,并自动反序列化为 Extraction 记录。这一步的稳定性直接决定了图谱的信噪比。

3.3 写入 Neo4j:实体节点 + 关系边 + 向量索引

java 复制代码
import org.neo4j.driver.Driver;
import org.neo4j.driver.Session;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.stereotype.Service;

@Service
public class GraphWriteService {

    private final Driver neo4jDriver;
    private final EmbeddingModel embeddingModel;

    public GraphWriteService(Driver neo4jDriver, EmbeddingModel embeddingModel) {
        this.neo4jDriver = neo4jDriver;
        this.embeddingModel = embeddingModel;
    }

    public void write(Extraction extraction) {
        try (Session session = neo4jDriver.session()) {
            // 写入实体节点,并为实体名生成向量
            for (Entity entity : extraction.entities()) {
                float[] embedding = embeddingModel.embed(entity.name());

                session.run("""
                        MERGE (e:Entity {name: $name})
                        SET e.type = $type, e.embedding = $embedding
                        """,
                        Map.of("name", entity.name(),
                               "type", entity.type(),
                               "embedding", embedding)
                );
            }

            // 写入关系
            for (Relation relation : extraction.relations()) {
                session.run("""
                        MATCH (a:Entity {name: $from})
                        MATCH (b:Entity {name: $to})
                        MERGE (a)-[r:%s]->(b)
                        """.formatted(relation.type()), // 注意:关系类型来自白名单,已校验
                        Map.of("from", relation.from(), "to", relation.to())
                );
            }
        }
    }
}

实体向量索引是后续"锚点检索"的基础。在 Neo4j 中创建向量索引:

cypher 复制代码
CREATE VECTOR INDEX entity-embeddings IF NOT EXISTS
FOR (e:Entity) ON (e.embedding)
OPTIONS { indexConfig: {
  `vector.dimensions`: 1536,
  `vector.similarity_function`: 'cosine'
}};

3.4 在线检索:向量锚点 + Cypher 多跳遍历

这是 GraphRAG 的"最后一公里"。先用 Neo4jVectorStore 找到锚点实体,再用 Cypher 从锚点出发遍历 2~3 跳。

java 复制代码
import org.springframework.ai.vectorstore.Neo4jVectorStore;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.stereotype.Service;

@Service
public class GraphRagRetriever {

    private final Neo4jVectorStore vectorStore;
    private final Driver neo4jDriver;

    public GraphRagRetriever(Neo4jVectorStore vectorStore, Driver neo4jDriver) {
        this.vectorStore = vectorStore;
        this.neo4jDriver = neo4jDriver;
    }

    public List<String> retrieve(String query, int hops) {
        // 1. 向量检索找到锚点实体
        var anchorNames = vectorStore
                .similaritySearch(SearchRequest.builder()
                        .query(query).topK(3).build())
                .stream().map(doc -> doc.getMetadata().get("name").toString())
                .toList();

        if (anchorNames.isEmpty()) return List.of();

        // 2. 从锚点出发,做变量长度路径遍历
        String cypher = """
                MATCH (anchor:Entity)
                WHERE anchor.name IN $anchorNames
                MATCH path = (anchor)-[*1..%d]-(neighbor:Entity)
                RETURN DISTINCT
                  reduce(s = '', r IN relationships(path) |
                    s + ' -[' + type(r) + ']-> ' + endNode(r).name
                  ) AS pathText
                LIMIT 20
                """.formatted(hops);

        try (var session = neo4jDriver.session()) {
            return session.run(cypher, Map.of("anchorNames", anchorNames))
                    .list(r -> r.get("pathText").asString());
        }
    }
}

将返回的 pathText 列表拼接到 Prompt 中,LLM 就能看到类似这样的结构化上下文:

text 复制代码
张三 -[REPORTS_TO]-> 李四 -[BELONGS_TO]-> 华东销售部
张三 -[COMPLAINED_ABOUT]-> 商家X -[DEPENDS_ON]-> 订单服务

这种上下文是纯向量 RAG 永远无法产出的。

四、生产环境避坑:GraphRAG 不是"免费的午餐"

如果面试官问"GraphRAG 这么好,为什么不直接全量替换向量 RAG",以下三组数据就是你的答案。

4.1 延迟:Graph 查询比 Vector 慢一个数量级

在生产基准测试中,GraphRAG 的单次查询墙钟时间约为 18 秒,而纯向量检索约为 0.9 秒 ;GraphRAG 的响应时间通常为 20~24 秒,其中 10~15 秒用于首 token 之前的图遍历和 LLM 合成。这意味着纯 Graph 路由在任何 QPS 超过 3 的场景下在经济上都不可行。

4.2 成本:Token 消耗是向量 RAG 的数倍

GraphRAG 每次检索需要将遍历到的子图路径、实体描述等大量结构化上下文塞入 Prompt,单次查询可能消耗数十万 Token 。有基准测试显示 GraphRAG 单次检索消耗约 61 万 Token ,成本开销约为向量 RAG 的 3 倍。

4.3 正确姿势:Shape-Aware 路由,而不是二选一

生产级方案不是"用 GraphRAG 替换 Vector RAG",而是根据查询形态路由:

查询形态 推荐检索 理由
事实型、改写型、FAQ 纯向量 低延迟、低成本,Graph 无增益
实体间关系、多跳链路 GraphRAG 向量无法回答链式问题
全局主题总结 GraphRAG(社区摘要) 需要语料级别的聚合视图

微软 GraphRAG 的公开指导也与此一致:纯 GraphRAG 在生产 QPS 下有结构性成本上限,架构的胜利在于"按形态路由",而不是"二选一" 。实际系统中,可以先用一个小分类器或单次 LLM 调用判断查询形态,再决定走哪条检索链路。

五、面试要点:如何把 GraphRAG 讲出 Infra 深度

5.1 "GraphRAG 和向量 RAG 的本质区别是什么?"

"向量 RAG 的检索单元是孤立文本片段,GraphRAG 的检索单元是实体和关系。前者解决'找得到',后者解决'连得上'。对于需要跨文档做 2~3 跳关系推理的问题,向量检索从原理上就无法回答。"

5.2 "你们线上怎么选型?"

"不做二选一。我们有一个轻量查询路由器:事实型问题走纯向量,延迟控制在 1 秒内;只有实体关系型和全局总结型问题才路由到 GraphRAG。这样既保住了 P99 延迟,又在真正需要图结构的问题上拿到了关系上下文。"

5.3 "GraphRAG 最大的坑是什么?"

"语料工程比检索算法重要得多。 实体抽取的质量、别名消解的策略、关系类型白名单的设计,直接决定了图谱的信噪比。一个噪声图谱上的图遍历,比纯向量检索更危险------它会用看似结构化的方式把 LLM 引向错误结论。"

六、总结

GraphRAG 不是向量 RAG 的替代品,而是在向量检索触达结构性边界时,补上的第二套检索引擎。

用 Spring Boot + Neo4j 实现 GraphRAG 的核心链条只有三步:LLM 结构化抽取建图 → 向量锚点 + Cypher 多跳遍历 → Shape-Aware 路由决定何时走图。

相关推荐
2601_9679801112 分钟前
Python中heapq模块的各种用法实例
数据结构·python·算法·应用实例·heapq模块
RobinDevNotes26 分钟前
24GB Mac 做数字人:声音放本地,画面租云端
人工智能·python·macos·开发工具
吃我一窝jio26 分钟前
Java博客极简写作三支柱
java
知识搬运临时工29 分钟前
双路服务器跑得比单路慢:JVM 没看见内存的距离
java·后端
Gopher_HBo33 分钟前
mprof.go全程跟拍-跟着一个请求走完四种采集
后端
吃我一窝jio1 小时前
Java Bug复盘与性能优化实战
java·开发语言·笔记
tong_li1 小时前
给娃做个 AI 陪写作业助手:LangChain4j 入门(三):配本古诗书(RAG 入门),答得不瞎编
后端
用户48678952533902 小时前
多租户第二集:读隔离做完之后,写路径才是真坑
后端
Flynn Liang2 小时前
Java 序列化也能成为瓶颈:Jackson、Protobuf 与 Redis 序列化的生产实践
java·redis·性能优化·jackson·序列化·protobuf