微软GraphRAG+ollama复现(附问题解决过程)

一、总体概述

GraphRAG

  • 定义:一种基于AI的内容解释和搜索功能。使用大语言模型(LLM)解析数据以创建知识图谱并回答用户关于用户提供的私有数据集的问题。
  • GraphRAG在信息之间建立连接,可以解决基于关键字和向量搜索机制难以解决或无法回答的问题。
  • 预期用途:
  • 支持关键信息发现和分析用例。例如:在多文档、混杂噪声及虚假信息的情况下提取有用见解;用户输入底层数据需要回答抽象和主题问题时。
  • 用户需要有一定的分析能力并且需要批判的看待答案。GraphRAG能准确提取复杂信息的主题,但需要人工分析,以验证和增强。
  • 部署特定领域的文本数据语料库,本身不收集用户数据。

源码地址:https://github.com/microsoft/graphrag

二、执行过程

  • 配置镜像源:
  • 将conda配置更新为官方的Anaconda通道

conda config --remove-key channels

conda config --add channels defaults

conda config --add channels conda-forge

  • 创建环境:

conda create -n graphrag-ollama-local python=3.10

conda activate graphrag-ollama-local

  • 安装Ollama
  • 安装Ollama依赖

安装IPEX-LLM库,这是Ollama运行所需的依赖:

pip install --pre --upgrade ipex-llm[cpp]

  • 使用安装包安装ollama(如果需要切换磁盘则需参考第二个链接)

Ollama-0001-安装 - 知乎 (zhihu.com)

ollama修改模型的下载位置解决C盘空间不足问题_ollama修改模型位置-CSDN博客

  • 使用ollama下载使用的开源模型:mistral+nomic-embed-text

  • 检查下载是否成功

  • 启动服务

出现报错:端口号被占用。Error: listen tcp 127.0.0.1:11434: bind: Only one usage of each socket address-CSDN博客

原因:Windows中默认安装Ollama会开机启动。因此才会在ollama serve时报错。

  • 解决问题:退出Ollama:
    快捷键win+x打开任务管理器:启动应用中禁用掉ollama,并在进程中结束ollama的任务。
  • 再次尝试ollama serve

成功:

  • 在此处,无法输入,类似于卡主不动的样式。
  • 需要打开一个新的cmd窗口,输入conda activate graphrag-ollama-local,进行接下来的操作和测试
  • 测试ollama是否运行成功:

  • 下载源代码

git clone GitHub - TheAiSingularity/graphrag-local-ollama: Local models support for Microsoft's graphrag using ollama (llama3, mistral, gemma2 phi3)- LLM & Embedding extraction

cd graphrag-local-ollama/

  • 安装依赖包(非常重要!!!)

pip install -e .

报错解决方案:

方案一:更换镜像源下载;

方案二:要使用git clone源代码,再次运行即可

  • 创建GraphRAG目录,并且创建input/目录用于存放原始文档:mkdir ragtest\input
  •  将原始文档放入到./ragtest/input目录下(仅支持txt文件,可多个):xcopy input\* ragtest\input\ /s /e

  • 初始化项目:python -m graphrag.index --init --root ./ragtest

若报错,根据报错安装模块

若运行成功,ragtest目录下有output,input,setting.yaml, prompts.env(默认隐藏)五个目录及文件

  • 移动 settings.yaml 文件,这是用 ollama 本地模型配置的主要预定义配置文件:

move settings.yaml .\ragtest\

  • 修改配置文件(完全粘贴附录中的修改结果)
  • 进行索引,构建图:python -m graphrag.index --root ./ragtest

若报错,则首先检查是否完全粘贴。

当前报错情况(未解决)

三、附录

settings.yaml

修改结果如下:

encoding_model: cl100k_base

skip_workflows: []

llm:

  api_key: ${GRAPHRAG_API_KEY}

  type: openai_chat # or azure_openai_chat

  model: mistral

  model_supports_json: false # 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

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: ${GRAPHRAG_API_KEY}

    type: openai_embedding # or azure_openai_embedding

    model: nomic-embed-text

    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 optional

chunks:

  size: 200

  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: yes

  raw_entities: yes

  top_level_nodes: yes

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

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微软开源的GraphRAG是一种基于图神经网络的强化学习框架,它主要用于文本生成任务,特别是长文本序列的生成。GraphRAG将输入的文本作为图结构处理,通过节点表示单词或词组,边则用于表示它们之间的上下文依赖关系。 ### GraphRAG的基本原理 1. **图构建**:首先,将输入文本转换成图形式,每个单词或词组是一个节点,边则代表它们之间的上下文关系或依赖关系。 2. **图嵌入**:利用预训练的语言模型(如BERT、ELMo等)对节点进行编码,得到每个节点的向量表示。这些向量不仅包含词汇信息,还能捕捉到复杂的语义和句法特征。 3. **注意力机制**:引入注意力机制来计算不同节点间的交互,帮助模型更好地理解句子的结构,并在生成过程中给予重要节点更多的权重。 4. **强化学习策略**:使用强化学习算法(例如Q-learning、Policy Gradient等),通过奖励函数指导模型学习如何生成高质量的文本。奖励通常基于生成文本的质量,比如语言流畅度、逻辑连贯性和主题一致性。 5. **动态规划优化**:为了提高效率和减少搜索空间,可以结合动态规划技术,使得模型能够在有限步内做出最优决策,生成最佳文本片段。 ### 实现步骤概览: 1. **数据准备**:收集并预处理所需的数据集,包括文本、标注和相应的奖励机制设计。 2. **模型搭建**:基于PyTorch或其他深度学习库构建GraphRAG框架的基础模型,包括图结构的创建、图嵌入层的设计以及强化学习组件的整合。 3. **训练过程**:使用优化器迭代更新模型参数,同时调整策略网络以最大化累积奖励。 4. **测试与评估**:在验证集上评估模型性能,关注生成文本的质量、长度和相关性。 5. **应用与扩展**:根据实际应用场景需求,调整模型配置或集成额外的功能,如多模态输入、特定领域的知识增强等。 ### 遇到的问题解决思路: - **过拟合**:通过增加正则化项、使用更丰富的数据集、进行数据增强等方式缓解。 - **训练耗时**:优化模型架构、采用并行计算或GPU加速,选择更适合大规模数据的优化算法。 - **生成质量不稳定**:改进奖励函数设计、加强模型解释力、使用更精细的评价指标辅助训练过程。 ### 相关问题: 1. **GraphRAG与其他图神经网络模型的区别是什么?** 2. **如何优化GraphRAG的训练速度而不牺牲生成文本的质量?** 3. **在哪些领域中GraphRAG特别有优势?它的局限性在哪里?**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值