文章目录
- 前言
- 一、GraphRAG本地安装
- 1.创建环境并安装
- 2.准备demo数据
- 3.初始化demo目录
- 二、GraphRAG兼容千帆通义等大模型
- 1.安装 graphrag-more
- 2.准备Demo数据
- 3.初始化demo目录
- 4.移动和修改 settings.yaml 文件
- 三、知识库构建与使用
- 1.知识库构建
- 2.执行查询
前言
GraphRAG是一种基于知识图谱的检索增强生成(RAG)应用,不同于传统基于向量检索的RAG应用,其允许进行更深层、更细致与上下文感知的检索,从而帮助获得更高质量的输出。
GitHub地址:https://github.com/microsoft/graphrag
文档地址:https://microsoft.github.io/graphrag/get_started/
graphrag-more地址:https://github.com/guoyao/graphrag-more
一、GraphRAG本地安装
1.创建环境并安装
#使用conda创建graphrag虚拟环境
conda create -n graphrag python=3.10
#安装graphrag
pip install graphrag
2.准备demo数据
# 创建demo目录
mkdir -p ./ragtest/input
# 下载微软官方demo数据
curl https://www.gutenberg.org/cache/epub/24022/pg24022.txt -o ./ragtest/input/book.txt
3.初始化demo目录
要初始化工作区,请先运行graphrag init命令。由于我们在上一步中已经配置了一个名为./ragtest的目录,请运行以下命令:
graphrag init --root ./ragtest
这将在./ragtest目录中创建两个文件:.env和settings.yaml。
- .env包含运行GraphRAG所需的环境变量。如果您检查文件,您将看到一个定义的环境变量,GRAPHRAG_API_KEY=<API_KEY>。这是OpenAI API或Azure OpenAI端点的API密钥。您可以用您自己的API密钥替换它。如果您正在使用另一种形式的身份验证(即托管身份),请删除此文件。
- settings.yaml包含GraphRAG的设置。您可以修改此文件来更改管道的设置。
二、GraphRAG兼容千帆通义等大模型
国内使用 OpenAI / AzureOpenAI 诸多不便,因此fork了官方代码创建了个新代码库:graphrag-more在其基础上做了小部分修改,支持使用百度千帆、阿里通义、Ollama。
安装步骤如下
1.安装 graphrag-more
如需二次开发或者调试的话,也可以直接使用源码的方式,步骤如下:
下载 graphrag-more 代码库
git clone https://github.com/guoyao/graphrag-more.git
安装依赖包
这里使用 poetry 来管理python虚拟环境
安装 poetry 参考:https://python-poetry.org/docs/#installation
cd graphrag-more
poetry install
conda create -n graphrag python=3.10
pip install graphrag-more
#or 如果使用了国内的镜像源需要改为官方的镜像源下载
pip install -i https://pypi.org/simple graphrag-more
2.准备Demo数据
# 创建demo目录
mkdir -p ./ragtest-more/input# 下载微软官方demo数据
curl https://www.gutenberg.org/cache/epub/24022/pg24022.txt -o ./ragtest-more/input/book.txt
3.初始化demo目录
python -m graphrag.index --init --root ./ragtest-more
4.移动和修改 settings.yaml 文件
根据选用的模型(千帆、通义、Ollama)将 example_settings 文件夹对应模型的 settings.yaml 文件复制到 ragtest 目录,覆盖初始化过程生成的 settings.yaml 文件。
每个settings.yaml里面都设置了默认的 llm 和 embeddings 模型,根据你自己要使用的模型修改 settings.yaml 文件的 model 配置
千帆默认使用 qianfan.ERNIE-3.5-128K 和 qianfan.bge-large-zh ,注意:必须带上 qianfan. 前缀 !!!
通义默认使用 tongyi.qwen-plus 和 tongyi.text-embedding-v2 ,注意:必须带上 tongyi. 前缀 !!!
Ollama默认使用 ollama.mistral:latest 和 ollama.quentinz/bge-large-zh-v1.5:latest ,注意:<=0.3.0版本时,其llm模型不用带前缀,>=0.3.1版本时,其llm模型必须带上 ollama. 前缀,embeddings模型必须带 ollama. 前缀 !!!
以下是Ollama、千帆、通义千问的配置文件内容
ollama
encoding_model: cl100k_base
skip_workflows: []
llm:api_key: ${GRAPHRAG_API_KEY}type: openai_chat # or azure_openai_chatmodel: ollama.mistral:latestmodel_supports_json: true # recommended if this is available for your model.# max_tokens: 4000# request_timeout: 180.0# api_base: http://localhost:11434/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# temperature: 0 # temperature for sampling# top_p: 1 # top-p sampling# n: 1 # Number of completions to generateparallelization:stagger: 0.3# num_threads: 50 # the number of threads to use for parallel processingasync_mode: threaded # or asyncioembeddings:## parallelization: override the global parallelization settings for embeddingsasync_mode: threaded # or asyncio# target: required # or allllm:api_key: ${GRAPHRAG_API_KEY}type: openai_embedding # or azure_openai_embeddingmodel: ollama.quentinz/bge-large-zh-v1.5:latest# api_base: http://localhost:11434/api# 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 optionalchunks:size: 300overlap: 100group_by_columns: [id] # by default, we don't allow chunks to cross documentsinput:type: file # or blobfile_type: text # or csvbase_dir: "input"file_encoding: utf-8file_pattern: ".*\\.txt$"cache:type: file # or blobbase_dir: "cache"# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>storage:type: file # or blobbase_dir: "output" # output/${timestamp}/artifacts# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>reporting:type: file # or console, blobbase_dir: "output" # output/${timestamp}/reports# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>entity_extraction:## strategy: fully override the entity extraction strategy.## type: one of graph_intelligence, graph_intelligence_json and nltk## 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 taskprompt: "prompts/entity_extraction.txt"entity_types: [organization,person,geo,event]max_gleanings: 1summarize_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 taskprompt: "prompts/summarize_descriptions.txt"max_length: 500claim_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: trueprompt: "prompts/claim_extraction.txt"description: "Any claims or facts that could be relevant to information discovery."max_gleanings: 1community_reports:## 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 taskprompt: "prompts/community_report.txt"max_length: 2000max_input_length: 8000cluster_graph:max_cluster_size: 10embed_graph:enabled: false # if true, will generate node2vec embeddings for nodes# num_walks: 10# walk_length: 40# window_size: 2# iterations: 3# random_seed: 597832umap:enabled: false # if true, will generate UMAP embeddings for nodessnapshots:graphml: falseraw_entities: falsetop_level_nodes: falselocal_search:# text_unit_prop: 0.5# community_prop: 0.1# conversation_history_max_turns: 5# top_k_mapped_entities: 10# top_k_relationships: 10# llm_temperature: 0 # temperature for sampling# llm_top_p: 1 # top-p sampling# llm_n: 1 # Number of completions to generate# max_tokens: 12000global_search:# llm_temperature: 0 # temperature for sampling# llm_top_p: 1 # top-p sampling# llm_n: 1 # Number of completions to generate# max_tokens: 12000# data_max_tokens: 12000# map_max_tokens: 1000# reduce_max_tokens: 2000# concurrency: 32
千帆
encoding_model: cl100k_base
skip_workflows: []
llm:api_key: ${GRAPHRAG_API_KEY}type: openai_chat # or azure_openai_chatmodel: qianfan.ERNIE-3.5-128Kmodel_supports_json: false # recommended if this is available for your model, original default is true# max_tokens: 4000# request_timeout: 180.0# api_base: https://<instance>.openai.azure.com# 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: 120 # set a leaky bucket throttle,original default is 10_000# max_retries: 10# max_retry_wait: 10.0# sleep_on_rate_limit_recommendation: true # whether to sleep when azure suggests wait-times# concurrent_requests: 2 # the number of parallel inflight requests that may be made,original default is 25temperature: 1e-10 # temperature for sampling, original default is 0# top_p: 1 # top-p sampling# n: 1 # Number of completions to generateparallelization:stagger: 0.3# num_threads: 50 # the number of threads to use for parallel processingasync_mode: asyncio # or threadedembeddings:## parallelization: override the global parallelization settings for embeddingsasync_mode: asyncio # or threaded# target: required # or allllm:api_key: ${GRAPHRAG_API_KEY}type: openai_embedding # or azure_openai_embeddingmodel: qianfan.bge-large-zh# api_base: https://<instance>.openai.azure.com# 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 optionalchunks:size: 1200overlap: 100group_by_columns: [id] # by default, we don't allow chunks to cross documentsinput:type: file # or blobfile_type: text # or csvbase_dir: "input"file_encoding: utf-8file_pattern: ".*\\.txt$"cache:type: file # or blobbase_dir: "cache"# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>storage:type: file # or blobbase_dir: "output" # output/${timestamp}/artifacts# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>reporting:type: file # or console, blobbase_dir: "output" # output/${timestamp}/reports# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>entity_extraction:## strategy: fully override the entity extraction strategy.## type: one of graph_intelligence, graph_intelligence_json and nltk## 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 taskprompt: "prompts/entity_extraction.txt"entity_types: [organization,person,geo,event]max_gleanings: 1summarize_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 taskprompt: "prompts/summarize_descriptions.txt"max_length: 500claim_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: trueprompt: "prompts/claim_extraction.txt"description: "Any claims or facts that could be relevant to information discovery."max_gleanings: 1community_reports:## 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 taskprompt: "prompts/community_report.txt"max_length: 2000max_input_length: 8000cluster_graph:max_cluster_size: 10embed_graph:enabled: false # if true, will generate node2vec embeddings for nodes# num_walks: 10# walk_length: 40# window_size: 2# iterations: 3# random_seed: 597832umap:enabled: false # if true, will generate UMAP embeddings for nodessnapshots:graphml: falseraw_entities: falsetop_level_nodes: falselocal_search:# text_unit_prop: 0.5# community_prop: 0.1# conversation_history_max_turns: 5# top_k_mapped_entities: 10# top_k_relationships: 10llm_temperature: 1e-10 # temperature for sampling, original default is 0# llm_top_p: 1 # top-p sampling# llm_n: 1 # Number of completions to generate# max_tokens: 5000 # original default is 12000global_search:llm_temperature: 1e-10 # temperature for sampling, original default is 0# llm_top_p: 1 # top-p sampling# llm_n: 1 # Number of completions to generate# max_tokens: 5000 # original default is 12000# data_max_tokens: 12000# map_max_tokens: 1000# reduce_max_tokens: 2000# concurrency: 32
通义千问
encoding_model: cl100k_base
skip_workflows: []
llm:api_key: ${GRAPHRAG_API_KEY}type: openai_chat # or azure_openai_chatmodel: tongyi.qwen-plusmodel_supports_json: false # recommended if this is available for your model, original default is true# max_tokens: 4000# request_timeout: 180.0# api_base: https://<instance>.openai.azure.com# 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# temperature: 0 # temperature for sampling# top_p: 1 # top-p sampling# n: 1 # Number of completions to generateparallelization:stagger: 0.3# num_threads: 50 # the number of threads to use for parallel processingasync_mode: threaded # or asyncioembeddings:## parallelization: override the global parallelization settings for embeddingsasync_mode: threaded # or asyncio# target: required # or allllm:api_key: ${GRAPHRAG_API_KEY}type: openai_embedding # or azure_openai_embeddingmodel: tongyi.text-embedding-v2# api_base: https://<instance>.openai.azure.com# 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 optionalchunks:size: 1200overlap: 100group_by_columns: [id] # by default, we don't allow chunks to cross documentsinput:type: file # or blobfile_type: text # or csvbase_dir: "input"file_encoding: utf-8file_pattern: ".*\\.txt$"cache:type: file # or blobbase_dir: "cache"# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>storage:type: file # or blobbase_dir: "output" # output/${timestamp}/artifacts# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>reporting:type: file # or console, blobbase_dir: "output" # output/${timestamp}/reports# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>entity_extraction:## strategy: fully override the entity extraction strategy.## type: one of graph_intelligence, graph_intelligence_json and nltk## 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 taskprompt: "prompts/entity_extraction.txt"entity_types: [organization,person,geo,event]max_gleanings: 1summarize_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 taskprompt: "prompts/summarize_descriptions.txt"max_length: 500claim_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: trueprompt: "prompts/claim_extraction.txt"description: "Any claims or facts that could be relevant to information discovery."max_gleanings: 1community_reports:## 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 taskprompt: "prompts/community_report.txt"max_length: 2000max_input_length: 8000cluster_graph:max_cluster_size: 10embed_graph:enabled: false # if true, will generate node2vec embeddings for nodes# num_walks: 10# walk_length: 40# window_size: 2# iterations: 3# random_seed: 597832umap:enabled: false # if true, will generate UMAP embeddings for nodessnapshots:graphml: falseraw_entities: falsetop_level_nodes: falselocal_search:# text_unit_prop: 0.5# community_prop: 0.1# conversation_history_max_turns: 5# top_k_mapped_entities: 10# top_k_relationships: 10# llm_temperature: 0 # temperature for sampling# llm_top_p: 1 # top-p sampling# llm_n: 1 # Number of completions to generate# max_tokens: 12000global_search:# llm_temperature: 0 # temperature for sampling# llm_top_p: 1 # top-p sampling# llm_n: 1 # Number of completions to generate# max_tokens: 12000# data_max_tokens: 12000# map_max_tokens: 1000# reduce_max_tokens: 2000# concurrency: 32
三、知识库构建与使用
1.知识库构建
构建过程可能会触发 rate limit (限速)导致构建失败,重复执行几次,或者尝试调小 settings.yaml 中 的 requests_per_minute 和 concurrent_requests 配置,然后重试
#如果使用是graphrag
graphrag index --root ./ragtest
#如果使用的是graphrag-more
python -m graphrag.index --root ./ragtest-more
需要注意的是如果使用graphrag-more需要将秘钥配置环境变量
根据选用的模型,配置对应的环境变量,若使用Ollama需要安装并下载对应模型
1.千帆:需配置环境变量 QIANFAN_AK、QIANFAN_SK(注意是应用的AK/SK,不是安全认证的Access Key/Secret Key),如何获取请参考官方文档
2.通义:需配置环境变量 TONGYI_API_KEY(从0.3.6.1版本开始,也支持使用 DASHSCOPE_API_KEY,同时都配置的情况下 TONGYI_API_KEY 优先级高于 DASHSCOPE_API_KEY),如何获取请参考官方文档
3.Ollama:
安装:https://ollama.com/download ,安装后启动
下载模型
ollama pull mistral:latest
ollama pull quentinz/bge-large-zh-v1.5:latest
配置环境变量
linux: export QIANFAN_AK=“value”
2.执行查询
graphrag
# global query
graphrag query \
--root ./ragtest \
--method global \
--query "What are the top themes in this story?"# local query
graphrag query \
--root ./ragtest \
--method local \
--query "Who is Scrooge and what are his main relationships?"
graphrag-more
# global query
python -m graphrag.query \
--root ./ragtest-more \
--method global \
"What are the top themes in this story?"# local query
python -m graphrag.query \
--root ./ragtest-more \
--method local \
"Who is Scrooge, and what are his main relationships?"
运行结果如下