GraphRAG

安装

# GraphRAG - Microsoft 图检索增强生成
# Python 3.10–3.12
graphrag>=0.3.0

# 可选:本地运行脚本时使用
python-dotenv>=1.0.0

编写好
/usr/local/code/py/graphRagTest1/settings.yaml


# GraphRAG 配置 - 与 graphrag init 生成结构兼容
# 详见 https://microsoft.github.io/graphrag/config/yaml/

default_chat_model: default_completion_model
default_embedding_model: default_embedding_model

# 国产 Qwen/通义(阿里云 DashScope),需配置 DASHSCOPE_API_KEY
completion_models:
  default_completion_model:
    type: litellm
    model_provider: dashscope
    model: qwen-turbo
    auth_method: api_key
    api_key: ${DASHSCOPE_API_KEY}

# LiteLLM 的 embedding 未注册 dashscope,故用 OpenAI 兼容端点 + api_base
# call_args.encoding_format=float:DashScope 只接受 float/base64,不接受 LiteLLM 默认传的 None
embedding_models:
  default_embedding_model:
    type: litellm
    model_provider: openai
    model: text-embedding-v3
    auth_method: api_key
    api_key: ${DASHSCOPE_API_KEY}
    api_base: https://dashscope.aliyuncs.com/compatible-mode/v1
    call_args:
      encoding_format: float

# 文档类型与匹配规则
input:
  type: text
  file_pattern: ".*\\.(txt|csv|json)"

# 存储路径(相对项目根)
input_storage:
  type: file
  base_dir: input

output_storage:
  type: file
  base_dir: output

cache:
  type: json
  storage:
    type: file
    base_dir: .cache

执行生成

graphrag index --root /path/to/another-project
该命令会在目录下寻找 settings.yaml,按settings.yaml执行

GraphRAG embeding模型和生成模型

根据库里的配置和调用关系,可以这样理解这两种模型在 GraphRAG 里的分工:


1. embedding_models(向量/嵌入模型)

作用:把文本变成向量,只做“表示”,不做“生成”。

在 GraphRAG 里主要用在:

阶段/功能 用途
索引:embed_text 给文本块、实体描述、社区内容等打向量,存进向量库,供后续检索
索引:generate_text_embeddings / update_text_embeddings 实际执行“文本 → 向量”的 workflow
查询:local_search 把用户问题编码成向量,用向量相似度找相关实体、关系、文本块
查询:basic_search 把问题编码成向量,做“向量 RAG”:找最相似的 k 个文本块
查询:drift_search 同样用 embedding 做语义检索,再配合 completion 做多轮/综合回答

总结:embedding 模型 = 负责“谁和谁在语义上接近”,用于建索引和做检索,不生成句子。


2. completion_models(补全/生成模型)

作用:做“理解 + 生成”的 LLM 任务(读入一段内容,输出新文本)。

在 GraphRAG 里主要用在:

阶段/功能 用途
索引:extract_graph 从文本块里抽取实体和关系(谁、什么组织、发生了啥、之间什么关系)
索引:summarize_descriptions 对实体描述做摘要,压缩成简短说明
索引:extract_claims 从文本里抽取主张/事实(claim)
索引:community_reports 根据社区子图/文本生成社区报告(这段知识在讲什么)
查询:local / global / drift / basic search 在检索到相关上下文后,用 LLM 生成最终回答(map-reduce、综合多段内容等)
查询:question_gen 生成追问/推荐问题

总结:completion 模型 = 负责“读进去,写出来”:抽实体关系、写摘要、写报告、写最终答案。


3. 一句话对照

  • embedding_models:把文字变成向量,用于索引和相似度检索(不生成自然语言)。
  • completion_models:做抽取、摘要、报告、问答生成等需要“理解 + 生成”的步骤。

索引阶段会同时用到两种:embedding 建向量索引,completion 做实体抽取、摘要、社区报告;查询阶段也是:embedding 做检索,completion 根据检索结果生成回答。

posted @ 2026-03-12 08:13  向着朝阳  阅读(2)  评论(0)    收藏  举报