微软开源GraphRAG+Ollama 本地离线部署笔记

2,843 阅读5分钟

前言

本篇文章主要记录基于微软开源项目GraphRAG实现本地部署的方案,直接使用OpenAI GPT的直接忽略本文章,本地部署效果可能略次于GPT的版本,但优势也很明显,保证数据隐私、省Token等于省钱。

官方帮助文档

microsoft.github.io/graphrag/

前期准备

  • PyCharm : python开发平台(不限)
  • Ollama : ollama.com/
  • LM Studio: lmstudio.ai/
  • Models: llama3 及 nomic-embed-text

操作步骤

  • 新建一个纯Python项目,位置及解释器环境不限

image.png

  • 在新建的项目终端中下载GraphRAG
pip install graphrag
  • (等待下载完成后继续)新建存放源文件的目录及索引
mkdir -p ./ragtest/input
  • 下载示例文件,打开网址后另存到ragtest/input目录 命名为 book.txt
https://www.gutenberg.org/cache/epub/24022/pg24022.txt

image.png

  • 初始化工作空间
python -m graphrag.index --init --root ./ragtest

image.png

  • 修改配置前需要安装LLM模型及文本向量模型

1.在ollama中安装llama3 8b版本

ollama run llama3

2.测试下llama3

image.png

3.在LM studio 中 下载 Embedding 模型nomic-embed-text 并开启服务 模拟OPENAI方式

    注意点:
  
    不要在ollama 中下载nomic-embed-text 直接下载的nomic-embed-text不兼容openai的方式

    LM studio 下载模型时可能超时

    解决方式:使用vs code 打开 C:\Users\Administrator\AppData\Local\LM-Studio 文件 全文替换huggingface.co为hf-mirror.com

4.在LM studio中开启服务 Start Server 端口号默认为 1234

image.png

  • 准备工作完成,开始修改配置文件 settings.yaml,内容如下
encoding_model: cl100k_base
skip_workflows: []
llm:
  api_key: ollama
  type: openai_chat # or azure_openai_chat
  model: llama3
  model_supports_json: true # recommended if this is available for your model.
  max_tokens: 1200
  # 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

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: ollama
    type: openai_embedding # or azure_openai_embedding
    model: nomic-ai/nomic-embed-text-v1.5-GGUF
    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$"

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
    llm部分注意的配置项:
    model: llama3                        说明: 修改为本地的模型名称 llama3
    api_base: http://localhost:11434/v1  说明: 修改为本地的模型地址
    max_tokens: 1200                     说明: 离线版本性能有限
    
    embeddings部分注意的配置项:
    model: nomic-ai/nomic-embed-text-v1.5-GGUF  说明: 修改为本地的nomic-embed-text模型名称
    api_base: http://localhost:1234/v1          说明: 修改为LM studio 启动的地址
   
  • 开始运行管道
python -m graphrag.index --root ./ragtest
常见的错误及解决方式:
    1)文本编码问题 注意book.txt 文件的编码格式与setting.yaml中的编码一致
    2)错误日志的排查方式,查看目录
    -output
      -20240808-174102
         -reports
           -indexing-engine.log 
    3) create_base_entity_graph 经常llm出错导致,先排除llm正常访问,也可在日志中查看是否响应超时或404
    当然 max_tokens 过大也会导致出错。
    
    4)提示verb "text_embed" in create_final_entities: 'NoneType' object is not iterable,说明使用的向量化插件不符合openai方式 所以要使用LM studio 来开启 nomic-embed-text 模型来兼容openai方式
   
  • 等待执行完成,很长时间

image.png

  • 全局查询及本地查询
python -m graphrag.query --root ./ragtest --method global "What are the top themes in this story?"
python -m graphrag.query --root ./ragtest --method local "Who is Scrooge, and what are his main relationships?"

结束语