网站首页 > 博客文章 正文
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/
猜你喜欢
- 2025-06-23 Jmeter——文件介绍和配置修改(jmeter操作文档)
- 2025-06-23 图像分割掩码标注转YOLO多边形标注
- 2025-06-23 Python与其他语言交互方式总结(python可以和其他语言进行通信)
- 2025-06-23 基于JMeter的性能压测平台实现(jmeter性能压测如何调优)
- 2025-06-23 SmartPLS 3常见问题(smartpls安装)
- 2025-06-23 魔兽一秒学会惩戒骑:打地鼠WA(惩戒骑士打怪)
- 2025-06-23 Java高效开发实战:10个让代码质量与性能飙升的黄金法则
- 2025-06-23 如何在 Java 项目中集成 DeepSeek
- 2025-06-23 50个Java编程技巧,免费送给大家(java编程如何入门)
- 2025-06-23 AI自动化知识图谱构建:模式发现全解析
你 发表评论:
欢迎- 06-23MySQL合集-mysql5.7及mysql8的一些特性
- 06-23MySQL CREATE TABLE 简单设计模板交流
- 06-23MYSQL表设计规范(mysql设计表注意事项)
- 06-23MySQL数据库入门(四)数据类型简介
- 06-23数据丢失?别慌!MySQL备份恢复攻略
- 06-23MySQL设计规范(mysql 设计)
- 06-23MySQL数据实时增量同步到Elasticsearch
- 06-23MySQL 避坑指南之隐式数据类型转换
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)