欢迎来到GraphRAG Local Ollama!这个存储库是对微软的GraphRAG的激动人心的改编,旨在支持使用 Ollama 下载的本地模型。告别昂贵的 OpenAPI 模型,拥抱使用 Ollama 进行高效、具有成本效益的本地推理!
📄 研究论文
有关 GraphRAG 实现的更多详细信息,请参阅GraphRAG 论文。
论文摘要
使用检索增强生成(RAG)从外部知识源中检索相关信息,使大型语言模型(LLMs)能够回答关于私人和/或以前未见过的文档集合的问题。然而,RAG 在针对整个文本语料库的全局问题上失败,比如“数据集中的主题是什么?”,因为这本质上是一个查询聚焦摘要(QFS)任务,而不是一个明确的检索任务。与此同时,先前的 QFS 方法无法扩展到 typica lRAG 系统索引的文本数量。为了结合这些对比方法的优势,我们提出了一种图形 RAG 方法,用于回答关于私人文本语料库的问题,该方法随着用户问题的普遍性和要索引的源文本数量而扩展。我们的方法使用一个LLM在两个阶段构建基于图形的文本索引:首先从源文档中导出实体知识图,然后为所有相关实体组生成社区摘要。给定一个问题,每个社区摘要用于生成部分响应,然后所有部分响应再次总结为最终响应提供给用户。 对于在 100 万令牌范围内的数据集上的一类全局意义问题,我们展示了图形 RAG 相对于天真的 RAG 基线在生成答案的全面性和多样性方面带来了显著改进。
🌟 特点
- 本地模型支持: 利用 Ollama 的本地模型进行LLM和嵌入。
- 具有成本效益:消除对昂贵的 OpenAPI 模型的依赖。
- 简单设置: 简单直接的设置过程。
📦 安装和设置
按照以下步骤设置此存储库,并使用 Ollama 提供的本地模型使用 GraphRag:
-
创建并激活一个新的 conda 环境:(请坚持使用给定的 Python 版本 3.10,以避免错误)
conda create -n graphrag-ollama-local python=3.10 conda activate graphrag-ollama-local
-
安装 Ollama:
- 访问 Ollama 的网站 获取安装说明。
- 或者,运行:
curl -fsSL https://ollama.com/install.sh | sh #ollama for linux pip install ollama
-
下载所需的模型使用 Ollama,我们可以从(mistral,gemma2,qwen2)选择llm和 Ollama 提供的任何嵌入模型:
ollama pull mistral #llm ollama pull nomic-embed-text #embedding
-
克隆存储库:
git clone https://github.com/TheAiSingularity/graphrag-local-ollama.git
-
导航到存储库目录:
cd graphrag-local-ollama/
-
安装 graphrag 包**这是最重要的一步:
pip install -e .
-
创建所需的输入目录:这是实验数据和结果将被存储的地方 - ./ragtest
mkdir -p ./ragtest/input
-
将示例数据文件夹 input/ 复制到 ./ragtest。Input/ 包含运行设置所需的示例数据。您可以在此处以 .txt 格式添加自己的数据。
cp input/* ./ragtest/input
-
初始化./ragtest 文件夹以创建所需的文件:
python -m graphrag.index --init --root ./ragtest
-
移动 settings.yaml 文件,这是配置了 ollama 本地模型的主预定义配置文件:
cp settings.yaml ./ragtest
用户可以通过更改模型进行实验。 llm 模型期望像 llama3、mistral、phi3 等语言模型,嵌入模型部分期望像 mxbai-embed-large、nomic-embed-text 等嵌入模型,这些模型由 Ollama 提供。您可以在这里找到 Ollama 提供的完整模型列表 https://ollama.com/library,可以在本地部署。默认的 API 基本 URL 分别为 http://localhost:11434/v1 用于 LLM 和 http://localhost:11434/api 用于嵌入,因此它们被添加到相应的部分。
encoding_model: cl100k_base
skip_workflows: []
llm:
model: myqwen2
request_timeout: 7200.0
api_base: http://localhost:9997/v1
# 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 generate
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
# target: required # or all
llm:
api_base: http://localhost:11434/v1
model: bge-m3
# 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
chunks:
size: 1200
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$"
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: 1
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: 1
community_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 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
# llm_temperature: 0 # temperature for sampling
# llm_top_p: 1 # top-p sampling
# llm_n: 1 # Number of completions to generate
# max_tokens: 12000
global_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
-
运行索引,生成一个图:
python -m graphrag.index --root ./ragtest
-
运行查询:仅支持全局方法
python -m graphrag.query --root ./ragtest --method global "What is machine learning?"
图表可以保存,进一步可以通过在 settings.yaml 中将 graphml 更改为“true”来用于可视化:
snapshots:
graphml: true
要可视化生成的 graphml 文件,您可以使用:https://gephi.org/users/download/ 或在存储库中提供的脚本 visualize-graphml.py:
将路径传递给 visualize-graphml.py 中的下面一行以指向 .graphml 文件:
graph = nx.read_graphml('output/20240708-161630/artifacts/summarized_graph.graphml')
-
可视化 .graphml:
python visualize-graphml.py