【实测闭坑】LazyGraphRAG利用本地ollama提供Embedding model服务和火山引擎的deepseek API构建本地知识库

server/2025/3/18 13:04:43/

LazyGraphRAG

  • 2024年4月,为解决传统RAG在全局性的查询总结任务上表现不佳,微软多部门联合提出Project GraphRAG(大模型驱动的KG);
  • 2024年7月,微软正式开源GraphRAG项目,引起极大关注,至今23.2k star,但落地时却面临巨大成本痛点(具体:LLM用于实体关系抽取+描述,社区总结);
  • 2024年11月,为了上述痛点,微软发布了LazyGraphRAG,将数据索引成本降低1000倍,只有GraphRAG的0.1%(使用 NLP 名词短语提取来识别概念及其共现,再利用图形统计来优化概念图并提取分层社区结构);
  • 2025.3月,微软GraphRAG项目迎来2.0.0版本,正式开源LazyGraphRAG,即NLP graph extraction功能。
  • 微软GraphRAG项目迎来2.1.0版本,添加对 JSON 输入文件的支持。

在这里插入图片描述

LazyGraphRAG代码解读

  • NLP图谱抽取 graphrag/index/workflows/extract_graph_nlp.py
    在这里插入图片描述

  • 建立名词短语图

graphrag/index/operations/build_noun_graph/build_noun_graph.py

  1. 主函数 build_noun_graph在这里插入图片描述

    • 功能:构建名词图
    • 输入:
      • text_unit_df: 包含文本单元的DataFrame
      • text_analyzer: 名词短语提取器
      • normalize_edge_weights: 是否标准化边权重
      • num_threads: 线程数(默认4)
      • cache: 缓存对象(可选)
    • 输出:包含节点和边的两个DataFrame
  2. 节点提取函数 _extract_nodes在这里插入图片描述

    • 功能:从文本单元中提取初始节点
    • 主要步骤:
      • 使用缓存加速名词短语提取
      • 并行处理文本单元
      • 对提取的名词短语进行分组统计
    • 输出:包含标题、频率和文本单元ID的DataFrame
  3. 边提取函数 _extract_edges

    • 功能:从节点中提取边
    • 主要步骤:
      • 将出现在同一文本单元中的节点连接
      • 确保源节点始终小于目标节点
      • 计算边权重
      • 可选:使用PMI(点互信息)标准化边权重
    • 输出:包含源节点、目标节点、权重和文本单元ID的DataFrame
  4. 辅助函数

    • _create_relationships: 创建名词短语之间的关系对
    • _calculate_pmi_edge_weights: 计算PMI边权重

这个模块的核心功能是通过自然语言处理技术从文本中提取名词短语,构建名词图,并计算节点之间的关系强度。它使用了并行处理和缓存机制来提高性能,并提供了边权重标准化的选项。

LazyGraphRAG技术原理

  • 使用 NLP 名词短语提取来提取概念及其共现

  • 使用图形统计来优化概念图并提取分层社区结构

微软LazyGraphRAG:新一代超低成本RAG

https://github.com/microsoft/graphrag/blob/main/CHANGELOG.md

实测LazyGraphRAG

网上很多教程都是很老的,说利用ollama需要改源码,目前的版本是不需要改任何源码的,直接设置好配置文件即可
我跑了一个18万单词的例子 50wTokens没了
LazyGraphRAG测试结果如下

conda create -n graphrag python=3.10
conda activate graphrag
pip install graphrag
graphrag init --root ./ragtest

数据:

curl https://www.gutenberg.org/cache/epub/24022/pg24022.txt -o ./ragtest/input/book.txt

我首先测试了LazyGraphRAG利用本地ollama提供Embedding model服务和火山引擎的deepseek API构建本地知识库
失败了
在这里插入图片描述
错误log:
主要是ds的json遵循能力还是有点弱啊

  File "/home/zli/miniconda3/envs/graphrag/lib/python3.10/json/encoder.py", line 438, in _iterencodeo = _default(o)File "/home/zli/miniconda3/envs/graphrag/lib/python3.10/json/encoder.py", line 179, in defaultraise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ModelMetaclass is not JSON serializable
22:53:43,292 graphrag.callbacks.file_workflow_callbacks INFO Community Report Extraction Error details=None
22:53:43,293 graphrag.index.operations.summarize_communities.strategies WARNING No report found for community: 8.0

主要原因deepseek-V3也不是很友好啊,我没钱prompt 微调啊,晕死
将模型从deepseek切换为豆包后成功!
在这里插入图片描述
在这里插入图片描述

配置如下

vim settings.yaml

models:default_chat_model:type: openai_chat # or azure_openai_chatapi_base: https://ark.cn-beijing.volces.com/api/v3/# api_version: 2024-05-01-previewauth_type: api_key # or azure_managed_identityapi_key: ${GRAPHRAG_API_KEY} # set this in the generated .env file# audience: "https://cognitiveservices.azure.com/.default"# organization: <organization_id>model: #deepseek-v3-241226# deployment_name: <azure_model_deployment_name>encoding_model: cl100k_base # automatically set by tiktoken if left undefinedmodel_supports_json: true # recommended if this is available for your model.concurrent_requests: 25 # max number of simultaneous LLM requests allowedasync_mode: threaded # or asyncioretry_strategy: nativemax_retries: -1                   # set to -1 for dynamic retry logic (most optimal setting based on server response)tokens_per_minute: 0              # set to 0 to disable rate limitingrequests_per_minute: 0            # set to 0 to disable rate limitingdefault_embedding_model:type: openai_embedding # or azure_openai_embeddingapi_base: http://localhost:11434/v1/# api_version: 2024-05-01-preview#auth_type: api_key # or azure_managed_identity#type: openai_chatapi_key: ollama# audience: "https://cognitiveservices.azure.com/.default"# organization: <organization_id>model: bge-m3# deployment_name: <azure_model_deployment_name>encoding_model: cl100k_base # automatically set by tiktoken if left undefinedmodel_supports_json: true # recommended if this is available for your model.concurrent_requests: 25 # max number of simultaneous LLM requests allowedasync_mode: threaded # or asyncioretry_strategy: nativemax_retries: -1                   # set to -1 for dynamic retry logic (most optimal setting based on server response)tokens_per_minute: 0              # set to 0 to disable rate limitingrequests_per_minute: 0            # set to 0 to disable rate limitingvector_store:default_vector_store:type: lancedbdb_uri: output/lancedbcontainer_name: defaultoverwrite: Trueembed_text:model_id: default_embedding_modelvector_store_id: default_vector_store### Input settings ###input:type: file # or blobfile_type: text #[csv, text, json]base_dir: "input"chunks:size: 1200overlap: 100group_by_columns: [id]### Output settings ###
## If blob storage is specified in the following four sections,
## connection_string and container_name must be providedcache:type: file # [file, blob, cosmosdb]base_dir: "cache"reporting:type: file # [file, blob, cosmosdb]base_dir: "logs"output:type: file # [file, blob, cosmosdb]base_dir: "output"### Workflow settings ###
# 这里是设置利用GraphRAG,如果利用LazyGraphRAG,下面的需要注释掉
#extract_graph:
#  model_id: default_chat_model
#  prompt: "prompts/extract_graph.txt"
#  entity_types: [organization,person,geo,event]
#  max_gleanings: 1summarize_descriptions:model_id: default_chat_modelprompt: "prompts/summarize_descriptions.txt"max_length: 500
# 这里是设置利用LazyGraphRAG测
extract_graph_nlp:text_analyzer:extractor_type: regex_english # [regex_english, syntactic_parser, cfg]extract_claims:enabled: falsemodel_id: default_chat_modelprompt: "prompts/extract_claims.txt"description: "Any claims or facts that could be relevant to information discovery."max_gleanings: 1community_reports:model_id: default_chat_modelgraph_prompt: "prompts/community_report_graph.txt"text_prompt: "prompts/community_report_text.txt"max_length: 8000max_input_length: 4000cluster_graph:max_cluster_size: 10embed_graph:enabled: false # if true, will generate node2vec embeddings for nodesumap:enabled: false # if true, will generate UMAP embeddings for nodes (embed_graph must also be enabled)snapshots:graphml: falseembeddings: 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/#querylocal_search:chat_model_id: default_chat_modelembedding_model_id: default_embedding_modelprompt: "prompts/local_search_system_prompt.txt"global_search:chat_model_id: default_chat_modelmap_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:chat_model_id: default_chat_modelembedding_model_id: default_embedding_modelprompt: "prompts/drift_search_system_prompt.txt"reduce_prompt: "prompts/drift_search_reduce_prompt.txt"basic_search:chat_model_id: default_chat_modelembedding_model_id: default_embedding_modelprompt: "prompts/basic_search_system_prompt.txt"

http://www.ppmy.cn/server/175963.html

相关文章

【前端面试题】宏任务与微任务的区别

宏任务与微任务的区别 JavaScript采用单线程模型&#xff0c;通过 事件循环&#xff08;Event Loop&#xff09; 机制处理异步操作。 类比于厨师上菜的过程&#xff0c;顾客点的菜可能存在容易处理的 “软菜” 与难处理的 “硬菜” &#xff0c;以及要加米饭酒水这些立马可以上…

Java常用设计模式

设计模式是软件开发中解决常见问题的模板或指南。Java中的23种设计模式通常被分为三大类&#xff1a;创建型模式&#xff08;Creational Patterns&#xff09;、结构型模式&#xff08;Structural Patterns&#xff09;和行为型模式&#xff08;Behavioral Patterns&#xff09…

python脚本实现服务器内存和cpu使用监控,并记录日志,可以设置阈值和采样频率

Python 脚本&#xff0c;实现以下功能&#xff1a; 按日期自动生成日志文件&#xff08;例如 cpu_mem_20231001.csv&#xff09;当 CPU 或内存超过阈值时触发记录独立记录报警事件&#xff08;保存到 alert.log&#xff09;支持自定义阈值和监控间隔 脚本代码 import psutil …

HCIA-PPP

一、基本概念 1、定义&#xff1a;PPP 协议是一种数据链路层协议&#xff0c;在两点之间建立直接通信连接。常用于拨号上网、宽带接入、路由器间通信等。 2、核心功能&#xff1a; ①链路控制&#xff1a;建立、配置和测试数据链路连接。 ②网络层协议支持&#xff1a;支持…

【密码学——基础理论与应用】李子臣编著 第四章 SM4分组密码算法 课后习题

免责声明 这里都是自己搓或者手写的。 里面不少题目感觉有问题或者我的理解有偏颇&#xff0c;请大佬批评指正&#xff01; 不带思考抄作业的请自动退出&#xff0c;我的并非全对&#xff0c;仅仅提供思维&#xff01; SM4的python实现 基于AI生成的SM4加密算法-CSDN博客 题…

【云原生技术】容器技术的发展史

一、Jail 时代 容器不是一个新概念或者新技术&#xff0c;很早就有了&#xff0c;只是近几年遇到了云计算&#xff0c;整个技术 被彻底引爆了。 1.1 1979年 贝尔实验室发明 chroot chroot系统调用是在 1979 年开发第 7 版 Unix 期间引入的。贝尔实验室在 Unix V7 的 开发过程…

【STM32】USART串口协议串口外设-学习笔记

串口协议 通信接口 通信的目的&#xff1a;将一个设备的数据传送到另一个设备&#xff0c;扩展硬件系统。比如STM32芯片内部集成了很多功能模块&#xff0c;像定时器计数、PWM输出、AD采集等等。这些都是芯片内部的电路&#xff0c;这些电路的配置寄存器&#xff0c;数据寄存…

C++ 类和对象----构造函数

一、构造函数 概念&#xff1a; 在平时初始化&#xff0c;都要调用Init公有方法来初始化&#xff0c;调用比较频繁&#xff0c;因此&#xff0c;构造函数就是在对象创建时&#xff0c;就将信息设置进去。 没有构造函数前&#xff1a; #include<iostream> using namesp…