专业的编程技术博客社区

网站首页 > 博客文章 正文

GraphRAG原理及使用教程(graphpl)

baijin 2025-06-23 14:46:10 博客文章 3 ℃ 0 评论

GraphRag原理

通过利用大模型的能力,将普通的文本文档转成结构化的知识图谱。问答生成的时候将结构化的知识图谱和非结构化的文本数据结合,作为问答上下文提供给大模型总结回答。

部署

Python版本要求:Python 3.10-3.12

安装GraphRAG,可能需要外网环境,否则有些依赖下不到。

pip install graphrag

创建rag索引目录

mkdir -p ./ragtest/input

下载demo文本文档

curl https://www.gutenberg.org/cache/epub/24022/pg24022.txt > ./ragtest/input/book.txt

初始化工作空间

python -m graphrag.index --init --root ./ragtest

初始化完成之后,会产生如下目录

构建索引

下面分两种情况。用云上的模型或者本地部署模型

第一种用云上的模型(OpenAI或者Azure OpenAI)

在官方介绍种称只支持这两种模型。

到OpenAI上生成自己的API KEY,配置到.env文件中。

大模型默认用的是收费模型:gpt-4-turbo-preview,需要付费和开通。

这里可以换成免费的3.5模型,比如gpt-3.5-turbo。

运行索引构建流水线

python -m graphrag.index --root ./ragtest

如果大模型用的是gpt-4o或者gpt-4-turbo-preview,应该会正常执行成功(没试过),或者微软的Azure OpenAI。

如果换成免费的gpt3.5,则会报错,因为构建索引token消耗很大,会超限,导致构建失败。

运行之后,会在output目录下产生一个时间戳的文件夹,里面有此次运行的日志文件,如果报错可以在indexing-engine.log文件中查看日志。

第二种本地部署模型

模型部署

大模型:gemma2,文本嵌入模型:
nomic-embed-text-v1.5-GGUF

gemma2用ollama部署运行:

ollama run gemma2

nomic嵌入模型需用LM Studio运行,用ollama跑会报错。

配置文件

env文件

GRAPHRAG_API_KEY=ollama

settings.yaml文件


encoding_model: cl100k_base
skip_workflows: []
llm:
  api_key: ollama
  type: openai_chat # or azure_openai_chat
  model: gemma2:latest
  model_supports_json: true # recommended if this is available for your model.
  # max_tokens: 4000
  # request_timeout: 180.0
  # 改成ollama部署机器的ip和端口
  api_base: http://XXX.XXX.XXX.XXX:8000/v1
  # api_version: 2024-02-15-preview
  # organization: <organization_id>
  # deployment_name: <azure_model_deployment_name>
  # tokens_per_minute: 150_000 # set a leaky bucket throttle
  # requests_per_minute: 10_000 # set a leaky bucket throttle
  # max_retries: 10
  # max_retry_wait: 10.0
  # sleep_on_rate_limit_recommendation: true # whether to sleep when azure suggests wait-times
  # concurrent_requests: 25 # the number of parallel inflight requests that may be made

parallelization:
  stagger: 0.3
  # num_threads: 50 # the number of threads to use for parallel processing

async_mode: threaded # or asyncio

embeddings:
  ## parallelization: override the global parallelization settings for embeddings
  async_mode: threaded # or asyncio
  llm:
    api_key: lm-studio
    type: openai_embedding # or azure_openai_embedding
    model: nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.Q5_K_M.gguf
    # 改成LM Studio部署机器的ip和端口
    api_base: http://localhost:1234/v1
    # api_version: 2024-02-15-preview
    # organization: <organization_id>
    # deployment_name: <azure_model_deployment_name>
    # tokens_per_minute: 150_000 # set a leaky bucket throttle
    # requests_per_minute: 10_000 # set a leaky bucket throttle
    # max_retries: 10
    # max_retry_wait: 10.0
    # sleep_on_rate_limit_recommendation: true # whether to sleep when azure suggests wait-times
    # concurrent_requests: 25 # the number of parallel inflight requests that may be made
    # batch_size: 16 # the number of documents to send in a single request
    # batch_max_tokens: 8191 # the maximum number of tokens to send in a single request
    # target: required # or optional
  


chunks:
  size: 300
  overlap: 100
  group_by_columns: [id] # by default, we don't allow chunks to cross documents
    
input:
  type: file # or blob
  file_type: text # or csv
  base_dir: "input"
  file_encoding: utf-8
  file_pattern: ".*\\.txt#34;

cache:
  type: file # or blob
  base_dir: "cache"
  # connection_string: <azure_blob_storage_connection_string>
  # container_name: <azure_blob_storage_container_name>

storage:
  type: file # or blob
  base_dir: "output/${timestamp}/artifacts"
  # connection_string: <azure_blob_storage_connection_string>
  # container_name: <azure_blob_storage_container_name>

reporting:
  type: file # or console, blob
  base_dir: "output/${timestamp}/reports"
  # connection_string: <azure_blob_storage_connection_string>
  # container_name: <azure_blob_storage_container_name>

entity_extraction:
  ## llm: override the global llm settings for this task
  ## parallelization: override the global parallelization settings for this task
  ## async_mode: override the global async_mode settings for this task
  prompt: "prompts/entity_extraction.txt"
  entity_types: [organization,person,geo,event]
  max_gleanings: 0

summarize_descriptions:
  ## llm: override the global llm settings for this task
  ## parallelization: override the global parallelization settings for this task
  ## async_mode: override the global async_mode settings for this task
  prompt: "prompts/summarize_descriptions.txt"
  max_length: 500

claim_extraction:
  ## llm: override the global llm settings for this task
  ## parallelization: override the global parallelization settings for this task
  ## async_mode: override the global async_mode settings for this task
  # enabled: true
  prompt: "prompts/claim_extraction.txt"
  description: "Any claims or facts that could be relevant to information discovery."
  max_gleanings: 0

community_report:
  ## llm: override the global llm settings for this task
  ## parallelization: override the global parallelization settings for this task
  ## async_mode: override the global async_mode settings for this task
  prompt: "prompts/community_report.txt"
  max_length: 2000
  max_input_length: 8000

cluster_graph:
  max_cluster_size: 10

embed_graph:
  enabled: false # if true, will generate node2vec embeddings for nodes
  # num_walks: 10
  # walk_length: 40
  # window_size: 2
  # iterations: 3
  # random_seed: 597832

umap:
  enabled: false # if true, will generate UMAP embeddings for nodes

snapshots:
  graphml: false
  raw_entities: false
  top_level_nodes: false

local_search:
  # text_unit_prop: 0.5
  # community_prop: 0.1
  # conversation_history_max_turns: 5
  # top_k_mapped_entities: 10
  # top_k_relationships: 10
  # max_tokens: 12000

global_search:
  # max_tokens: 12000
  # data_max_tokens: 12000
  # map_max_tokens: 1000
  # reduce_max_tokens: 2000
  # concurrency: 32

运行索引构建流水线

python -m graphrag.index --root ./ragtest

ollama部署在4090显卡的那台机器,
nomic-embed-text-v1.5-GGUF部署本地电脑4G显存。

索引构建非常慢,以小时计。

问答

 python -m graphrag.query \
--root ./ragtest \
--method global \
"show me some Prompts about Interpretable Soft Prompts."

生成的知识图谱文件(以凡人修仙传为原始文本生成的)

GraphRAG与普通RAG的对比

中文文档问答效果测试

生成的知识图谱导入Neo4j可视化之后的数据

http://localhost:7474/browser/

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表