GraphRAG 本地 Ollama - 知识图谱

欢迎来到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:

  1. 创建并激活一个新的 conda 环境:(请坚持使用给定的 Python 版本 3.10,以避免错误)

    conda create -n graphrag-ollama-local python=3.10
    conda activate graphrag-ollama-local
  2.  安装 Ollama:

    curl -fsSL https://ollama.com/install.sh | sh #ollama for linux
    pip install ollama
  3. 下载所需的模型使用 Ollama,我们可以从(mistral,gemma2,qwen2)选择llm和 Ollama 提供的任何嵌入模型:

    ollama pull mistral  #llm
    ollama pull nomic-embed-text  #embedding
  4.  克隆存储库:

    git clone https://github.com/TheAiSingularity/graphrag-local-ollama.git
  5. 导航到存储库目录:

    cd graphrag-local-ollama/
  6. 安装 graphrag 包**这是最重要的一步:

    pip install -e .
  7. 创建所需的输入目录:这是实验数据和结果将被存储的地方 - ./ragtest

    mkdir -p ./ragtest/input
  8. 将示例数据文件夹 input/ 复制到 ./ragtest。Input/ 包含运行设置所需的示例数据。您可以在此处以 .txt 格式添加自己的数据。

    cp input/* ./ragtest/input
  9. 初始化./ragtest 文件夹以创建所需的文件:

    python -m graphrag.index --init --root ./ragtest
  10. 移动 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
  1. 运行索引,生成一个图:

    python -m graphrag.index --root ./ragtest
  2. 运行查询:仅支持全局方法

    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') 
  1.  可视化 .graphml:

    python visualize-graphml.py

 引用

  • 原始 GraphRAG 存储库由 Microsoft 创建:GraphRAG
  • Ollama: Ollama
### GraphRAGOllama 项目介绍 GraphRAG 是一种用于构建知识图谱的技术框架,能够处理大规模结构化和非结构化的数据集。通过集成自然语言处理技术和机器学习算法,该工具可以实现高效的知识抽取、表示以及推理功能[^1]。 Ollama 则是一个专注于提供大模型即服务(LLMaaS)平台的服务提供商,在此平台上用户可以通过 API 调用来访问预训练好的大型语言模型并应用于各种场景之中[^2]。 两者结合使用时,GraphRAG 可以为用户提供强大的中文知识图谱解决方案;而借助于 Ollama 提供的大规模计算资源支持,则使得整个系统的性能得到了极大提升,并降低了部署成本和技术门槛。 ### 安装与配置指南 为了启动基于 GraphRAG 的应用实例,首先需要准备初始的数据源文件夹 `./ragtest` 并执行如下命令来完成环境搭建: ```bash python -m graphrag.index --init --root ./ragtest ``` 上述指令会读取位于 `ragtest/input/` 目录下的文档作为输入材料,并对其进行索引操作以便后续查询检索之用。 对于想要深入了解底层工作原理或者参与开发贡献的人来说,可以从 GitHub 上获取最新版本的开源代码库来进行研究分析或修改优化。 ### 获取源码方法 如果希望下载 GraphRAG 或者其他相关组件的源代码,建议前往官方仓库页面进行克隆操作。通常情况下,这类项目的托管地址会在其官方网站或是论文中有所提及。例如,针对 Python 包管理器 pip 所安装的部分模块,也可以直接查看本地路径 `/site-packages/graphrag/query/llm/oai/embedding.py` 来了解具体实现细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值