基于GraphRAG+Ollama验证知识图谱和检索增强融合

之前介绍了知识图谱与检索增强的融合探索GraphRAG。

https://blog.csdn.net/liliang199/article/details/151189579

这里尝试在CPU环境,基于GraphRAG+Ollama,验证GraphRAG构建知识图谱和检索增强查询过程。

1 环境安装

1.1 GraphRAG安装

在本地cpu环境,基于linux conda安装python,pip安装graphrag,过程如下。

conda create -n graphrag python=3.10

conda activate graphrag

pip install graphrag==0.5.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

安装graphrag 0.5.0(之后版本可能和ollama有兼容问题)

1.2 Ollama LLM安装

假设ollama已安装,具体安装过程参考

https://blog.csdn.net/liliang199/article/details/149267372

这里ollama下载llm模型mistral和embedding模型nomic-embed-text

ollama pull nomic-embed-text

ollama pull mistral

默认ollama模型上下文长度为2048,不能有效支持GraphRAG,需要对上下文长度进行修改。

导出现有llm模型配置,配置文件为Modelfile

ollama show --modelfile mistral:latest > Modelfile 

修改Modelfile,在PARAMETER区域添加如下配置,支持10k上下文,可依据具体情况设定。

PARAMETER num_ctx 10000

修改后示例如下

基于修改后的Modelfile,创建新的ollama模型,指令如下。

ollama create mistral:10k -f Modelfile

查看新创建模型

ollama list

2 GraphRAG图谱构建验证

2.1 测试数据准备

首先创建工作目录

mkdir ragtest/input -p

输入为如下小1000行的文本,获取命令如下。

https://www.gutenberg.org/cache/epub/7785/pg7785.txt

wget https://www.gutenberg.org/cache/epub/7785/pg7785.txt -O ragtest/input/Transformers_intro.txt

此时,./ragtest包含测试数据,初始化指令如下。

graphrag init --root ./ragtest

生成参数配置文件ragtest/settings.yaml

2.2 环境变量设置

设置GRAPHRAG_API_KEY和GRAPHRAG_CLAIM_EXTRACTION_ENABLED

export GRAPHRAG_API_KEY=ollama
export GRAPHRAG_CLAIM_EXTRACTION_ENABLED=True

设置参数GRAPHRAG_CLAIM_EXTRACTION_ENABLED=True,否则无法生成协变量,Local Search出错。

2.3 模型参数配置

模型参数配置文件ragtest/settings.yaml

修改llm model为mistral:10k,embedding model为nomic-embed-text

调用本地ollama llm服务,所以设置api_base: http://localhost:11434/v1

本地cpu部署,计算很慢,所以设置一个很长的request_timeout: 18000

没有GPU,过大concurrent_requests没效果,反而导致超时,设置concurrent_requests: 1

### This config file contains required core defaults that must be set, along with a handful of common optional settings.
### For a full list of available settings, see https://microsoft.github.io/graphrag/config/yaml/

### LLM settings ###
## There are a number of settings to tune the threading and token limits for LLM calls - check the docs.

encoding_model: cl100k_base # this needs to be matched to your model!

llm:
  api_key: ${GRAPHRAG_API_KEY} # set this in the generated .env file
  type: openai_chat # or azure_openai_chat
  model: mistral:10k

  api_base: http://localhost:11434/v1

  request_timeout: 18000

  concurrent_requests: 1
  model_supports_json: true # recommended if this is available for your model.
  # audience: "https://cognitiveservices.azure.com/.default"
  # api_base: https://<instance>.openai.azure.com
  # api_version: 2024-02-15-preview
  # organization: <organization_id>
  # deployment_name: <azure_model_deployment_name>

parallelization:
  stagger: 0.3
  # num_threads: 50

async_mode: threaded # or asyncio

embeddings:
  async_mode: threaded # or asyncio
  vector_store:
    type: lancedb
    db_uri: 'output/lancedb'
    container_name: default
    overwrite: true
  llm:
    api_key: ${GRAPHRAG_API_KEY}
    type: openai_embedding # or azure_openai_embedding
    model: nomic-embed-text
    api_base: http://localhost:11434/v1

    request_timeout: 18000

    concurrent_requests: 1
    # api_base: https://<instance>.openai.azure.com
    # api_version: 2024-02-15-preview
    # audience: "https://cognitiveservices.azure.com/.default"
    # organization: <organization_id>
    # deployment_name: <azure_model_deployment_name>

### Input settings ###

input:
  type: file # or blob
  file_type: text # or csv
  base_dir: "input"
  file_encoding: utf-8
  file_pattern: ".*\\.txt$"

chunks:
  size: 1200
  overlap: 100
  group_by_columns: [id]

### Storage settings ###
## If blob storage is specified in the following four sections,
## connection_string and container_name must be provided

cache:
  type: file # or blob
  base_dir: "cache"

reporting:
  type: file # or console, blob
  base_dir: "logs"

storage:
  type: file # or blob
  base_dir: "output"

## only turn this on if running `graphrag index` with custom settings
## we normally use `graphrag update` with the defaults
update_index_storage:
  # type: file # or blob
  # base_dir: "update_output"

### Workflow settings ###

skip_workflows: []

entity_extraction:
  prompt: "prompts/entity_extraction.txt"
  entity_types: [organization,person,geo,event]
  max_gleanings: 1

summarize_descriptions:
  prompt: "prompts/summarize_descriptions.txt"
  max_length: 500

claim_extraction:
  enabled: false
  prompt: "prompts/claim_extraction.txt"
  description: "Any claims or facts that could be relevant to information discovery."
  max_gleanings: 1

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

umap:
  enabled: false # if true, will generate UMAP embeddings for nodes

snapshots:
  graphml: false
  raw_entities: false
  top_level_nodes: false
  embeddings: false
  transient: false

### Query settings ###
## The prompt locations are required here, but each search method has a number of optional knobs that can be tuned.
## See the config docs: https://microsoft.github.io/graphrag/config/yaml/#query

local_search:
  prompt: "prompts/local_search_system_prompt.txt"

global_search:
  map_prompt: "prompts/global_search_map_system_prompt.txt"
  reduce_prompt: "prompts/global_search_reduce_system_prompt.txt"
  knowledge_prompt: "prompts/global_search_knowledge_system_prompt.txt"

drift_search:
  prompt: "prompts/drift_search_system_prompt.txt"

2.4 数据索引构建

然后就是构建索引,这里需要设置--reporter "rich",不设置会报错。

nohup graphrag index --root ./ragtest --reporter "rich" > run.log &

本地CPU运行ollama其实不太有效,耗时太长,导致各种奇怪超时和报错,所以最好有GPU。

另外,虽然可以调用外部LLM服务,但GraphRAG索引会消耗大量tokens,这需要不差钱。

附录

问题1: Invalid value for '--reporter'

Invalid value for '--reporter' (env var: 'None'): <ReporterType.RICH: 'rich'> is not one of 'rich', 'print', 'none'.                           │

补全输出参数"none"/"print"/"rich",比如 --reporter "rich"

reference

---

GraphRAG

https://github.com/msolhab/graphrag

Project Gutenberg

https://www.gutenberg.org/

Global Search Notebook

https://microsoft.github.io/graphrag/examples_notebooks/global_search/

GraphRAG-知识图谱与检索增强的融合探索

https://blog.csdn.net/liliang199/article/details/151189579

GraphTest - 直接使用阿里API,总体费用相对可控。

https://github.com/NanGePlus/GraphragTest

GraphRAG(最新版)+Ollama本地部署,以及中英文示例

https://juejin.cn/post/7439046849883226146

傻瓜操作:GraphRAG、Ollama 本地部署及踩坑记录

https://blog.csdn.net/weixin_42107217/article/details/141649920

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/web/96148.shtml
繁体地址,请注明出处:http://hk.pswp.cn/web/96148.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

36页可编辑PPT | 某制造集团灯塔工厂解决方案

制造业企业订单种类多&#xff0c;传统产线换型慢&#xff0c;库存高&#xff0c;财务压力大。工人年龄大&#xff0c;招工难&#xff0c;工资涨&#xff0c;效率低。海外对手用低价和柔性产线抢单&#xff0c;国内同行用数字化缩短交期。企业想扩产&#xff0c;又怕投资重、回…

Redis 非缓存核心场景及实例说明

Redis 非缓存核心场景及实例说明 一、分布式锁 分布式锁用于解决分布式系统中多节点竞争同一资源的问题&#xff0c;确保操作原子性。Redis 实现分布式锁的核心思路是利用键的唯一性和原子命令&#xff0c;通常基于 Redisson 框架简化实现&#xff08;底层依赖 Redis 命令&…

【技术教程】如何将ONLYOFFICE文档集成到使用Spring Boot框架编写的Java Web应用程序中

在现代协作办公环境中&#xff0c;将功能强大的文档编辑器无缝集成到自有业务系统中&#xff0c;已成为提升工作效率和用户体验的关键需求。ONLYOFFICE 文档服务器提供了一套成熟的在线文档编辑解决方案&#xff0c;而 Java Spring Boot 则是构建高效、模块化 Web 应用的热门框…

openharmony之AV_CodeC音视频编解码模块详解(二)

1. 音频解码器函数调用流程 1.1 音频解码器架构概览 decoder:解码器 encoder:编码器 前面文章介绍了关于openHarmony的AV_CodeC模块,这篇文章将详细讲解编解码时函数的调用流程 音频解码器采用插件化架构,核心实现位于: services/engine/codec/audio/decoder/audio_ffmpeg…

PDF24 Creator:免费的多功能PDF工具

在处理PDF文件时&#xff0c;一个功能强大且免费的PDF工具是许多用户的首选。PDF24 Creator作为一款免费的PDF工具&#xff0c;提供了丰富的功能&#xff0c;帮助用户创建、编辑和转换PDF文件&#xff0c;满足从初学者到专业用户的各种需求。它不仅支持PDF与Word、Excel等15种以…

VBA 中使用 ADODB 操作 SQLite 插入中文乱码问题

问题 使用 VBA 的 ADODB 对象的 command 对象、parameter 对象&#xff0c;插入的中文数据为乱码 驱动下载、安装、引用 驱动网址(下载路径) 使用的 ODBC 驱动&#xff08;需要梯子才能下载&#xff0c;感谢大佬开源&#xff09; http://www.ch-werner.de/sqliteodbc/ 版本…

执行select * from a where rownum<1;,数据库子进程崩溃,业务中断。

文章目录环境症状触发条件解决方案环境 系统平台&#xff1a;Linux x86-64 Red Hat Enterprise Linux 7 版本&#xff1a;4.5.2 症状 执行select * from a where rownum<1;&#xff0c;数据库子进程崩溃&#xff0c;业务中断。 触发条件 select 和 where条件带有rownum…

python库 Py2app 的详细使用(将 Python 脚本变为 MacOS 独立软件包)

更多内容请见: python3案例和总结-专栏介绍和目录 文章目录 一、Py2app 概述 1.1 Py2app 介绍 1.2 安装 1.3 替代工具推荐 二、基础使用 2.1 最简单的 setup.py 文件 2.2 完整示例 2.3 配置选项详解 2.4 完整项目案例 2.5 打包为单文件应用(可选) 三、高级配置 3.1 处理特定…

NTP配置为客户端广播监听模式

前言 项目需求&#xff1a; 使能ntp为客户端模式&#xff0c;能监服务端广播模式发出的ntp报文&#xff0c;计算出服务端的时间与客户端的时间偏差并上报。 开发状况&#xff1a; 交叉编译ntp源码&#xff0c;将修改后的ntpd进程部署到设备上作为客户端完成项目需求 如何操作&a…

Claude-Flow 使用指南

Claude-Flow 不仅仅是一个工具&#xff0c;更是一个强大的AI驱动开发编排平台。本问初步带您深入了解 Claude-Flow v2.0.0 Alpha 的强大功能&#xff0c;助您在AI开发领域如虎添翼。1. 简介&#xff1a;什么是 Claude-Flow&#xff1f; Claude-Flow v2 Alpha 是一个企业级的AI编…

系统梳理 Test-Time Compute 的主要实现路径

编者按&#xff1a; AI 真的在“思考”吗&#xff1f;当模型面对数学推理、代码生成或复杂决策时&#xff0c;它是如何一步步推演出答案的&#xff1f;如果你曾困惑于大模型在关键任务中表现不稳定、缺乏可解释性&#xff0c;甚至生成结果难以验证&#xff0c;那么你并不孤单。…

vue 经常写的echarts图表模块结构抽取

vue 经常写的echarts图表模块结构抽取将项目中经常写的结构抽取一下, 方便以后用 表头包含标题和右侧操作部分下面为图表 <div class"chartBox"><div class"chartheadbox"><div class"chartheadleft">这是图表标题</div>…

主流的开源协议(MIT,Apache,GPL v2/v3)

文章目录1. MIT 协议 (MIT License)2. Apache 2.0 协议 (Apache License 2.0)3. GPL v2 协议 (GNU General Public License v2)“开源协议选择指南”的流程图 flowchart TDA[开始选择开源协议] --> B{是否要求修改后必须开源?<br>(是否具有 传染性?)};B -- 是&…

CameraService笔记

cameraservicecamera 结构图1. 启动CameraServer1.1 注册media.camera服务1.2 构造CameraService1.3 CameraService::onFirstRef1.4 CameraService::enumerateProviders&#xff1a;前置准备知识1.4 CameraService::enumerateProviders&#xff1a;Provider和Device初始化1.4.1…

MacOS 15.6 编译SDL3 Android平台多架构so库

成功编译输出: 编译: Android平台多架构编译脚本: sdl3_android_build.sh #!/bin/bash# 设置变量 macos 其他系统需要更改路径 SDL_SOURCE_DIR=$(pwd)/SDL BUILD_DIR=${SDL_SOURCE_DIR}/../sdl3_build_android NDK_PATH=$HOME/Library/Android/Sdk/Ndk/25.2.9519653 CMAKE…

Real-IAD D³: A Real-World 2D/Pseudo-3D/3D Dataset for Industrial Anomaly

Real-IAD D: A Real-World 2D/Pseudo-3D/3D Dataset for Industrial Anomaly Detection Paper Github 摘要 随着工业异常检测&#xff08;Industrial Anomaly Detection, IAD&#xff09;复杂程度的不断提升&#xff0c;多模态检测方法已成为机器视觉领域的研究焦点。然而&a…

IT需求提示未读信息查询:深度技术解析与性能优化指南【类似:钉钉已读 功能】

IT需求提示未读信息查询&#xff1a;深度技术解析与性能优化指南【类似&#xff1a;钉钉已读 功能】 DROP TABLE IF EXISTS rs_kpi_it_need_tip; CREATE TABLE IF NOT EXISTS rs_kpi_it_need_tip (id bigint NOT NULL AUTO_INCREMENT COMMENT 主键ID&#xff…

Django中的软删除

软删除&#xff08;Soft Delete&#xff09;是一种数据删除策略&#xff0c;它并不真正从数据库中删除记录&#xff0c;而是通过标记&#xff08;如 is_deleted 字段&#xff09;来表示记录已被删除。 这样做的好处是可以保留数据历史&#xff0c;支持数据恢复和审计。 在 Djan…

JavaEE 进阶第四期:开启前端入门之旅(四)

专栏&#xff1a;JavaEE 进阶跃迁营 个人主页&#xff1a;手握风云 目录 一、常用CSS 1.1. border 1.2. width/height 1.3. padding&#xff1a;内边距 1.4. margin&#xff1a;外边距 二、初始JavaScript 2.1. JavaScript是什么 2.2. 发展历史 2.3. JavaScript 和 HT…

学习日记-SpringMVC-day49-9.4

知识点&#xff1a;1.RequestMapping&#xff08;3&#xff09;知识点核心内容重点RequestMapping注解的parameters属性通过parameters指定请求参数条件&#xff08;如bookID&#xff09;&#xff0c;控制请求匹配规则&#xff08;必须包含/排除特定参数或值&#xff09;参数存…