微軟開源GraphRAG的使用教程-使用自定義數據測試GraphRAG

在這里插入圖片描述

微軟在今年4月份的時候提出了GraphRAG的概念,然后在上周開源了GraphRAG,Github鏈接見https://github.com/microsoft/graphrag,截止當前,已有6900+Star。

安裝教程

官方推薦使用Python3.10-3.12版本,我使用Python3.10版本安裝時,在初始化項目過程中會報錯,切換到Python3.11版本后運行正常,推測是Python3.10與微軟的一些最新的SDK不兼容。所以建議使用Python3.11的環境,安裝GraphRAG比較簡單,直接下面一行代碼即可安裝成功。

pip install graphrag

使用教程

在這個教程中,我們使用馬伯庸的《太白金星有點煩》這個短篇小說為例,測試下使用微軟開源的GraphRAG的處理效果。

注意,GraphRAG是使用LLM來提取文本片段中的實體關系,因此耗費Token數較多,如果是個人調研使用,不建議使用GPT4級別的模型(費用太高,不差錢的大佬請忽略此條建議)。綜合成本和效果,我這里使用的是DeepSeek-Chat模型。

初始化項目

我這邊先創建了一個臨時測試目錄myTest,然后按照官方教程,在myTest目錄下創建了input目錄,并把《太白金星有點煩》這本書的txt版本重命名為book.txt后放到input目錄下。然后調用python -m graphrag.index --init 進行初始化工作,生成一些配置文件。

mkdir ./myTest/input
curl https://www.xxx.com/太白金星有點煩.txt > ./myTest/input/book.txt  // 這里是示例代碼,大家在測試時根據實際情況放入自己要測試的txt文本即可。
cd ./myTest
python -m graphrag.index --init

執行完成后,會在當前目錄(即MyTest)目錄下生成幾個新的文件夾:output-后續執行生成的中間結果會保存到這個目錄中;prompts-處理過程中用到的一些Prompt內容;.env-大模型API配置文件,里面默認就一個GRAPHRAG_API_KEY 用于配置大模型的apiKey;settings.yaml-該文件是整體的配置信息,如果我們使用的非OPENAI的官方模型和官方API,我們需要修改此配置文件來讓GraphRAG按照我們指定的配置文件執行。

配置相關文件

先在.env文件中配置大模型API的Key,這個配置是全局生效的。我們在.env文件中配置完成后,不需要在settings.yaml文件中重復配置。settings.yaml中使用的默認模型為gpt-4-turbo-preview ,如果不需要修改模型以及調用的API地址,那現在就已經配置完成了,后續的配置內容可以執行忽略并直接到執行階段。

我這里使用的是agicto 提供的APIkey(主要是新用戶注冊可以免費獲取到10塊錢的調用額度,白嫖還是挺爽的)。我在這里主要就修改了API地址和調用模型的名稱,修改完成后的settings文件完整內容如下:

encoding_model: cl100k_base
skip_workflows: []
llm:api_key: ${GRAPHRAG_API_KEY}type: openai_chat # or azure_openai_chatmodel: deepseek-chatmodel_supports_json: false # recommended if this is available for your model.api_base: https://api.agicto.cn/v1# max_tokens: 4000# request_timeout: 180.0# 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 madeparallelization:stagger: 0.3# num_threads: 50 # the number of threads to use for parallel processingasync_mode: threaded # or asyncioembeddings:## parallelization: override the global parallelization settings for embeddingsasync_mode: threaded # or asynciollm:api_key: ${GRAPHRAG_API_KEY}type: openai_embedding # or azure_openai_embeddingmodel: text-embedding-3-smallapi_base: https://api.agicto.cn/v1# 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# target: required # or optionalchunks:size: 300overlap: 100group_by_columns: [id] # by default, we don't allow chunks to cross documentsinput:type: file # or blobfile_type: text # or csvbase_dir: "input"file_encoding: utf-8file_pattern: ".*\\.txt$"cache:type: file # or blobbase_dir: "cache"# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>storage:type: file # or blobbase_dir: "output/${timestamp}/artifacts"# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>reporting:type: file # or console, blobbase_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 taskprompt: "prompts/entity_extraction.txt"entity_types: [organization,person,geo,event]max_gleanings: 0summarize_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 taskprompt: "prompts/summarize_descriptions.txt"max_length: 500claim_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: trueprompt: "prompts/claim_extraction.txt"description: "Any claims or facts that could be relevant to information discovery."max_gleanings: 0community_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 taskprompt: "prompts/community_report.txt"max_length: 2000max_input_length: 8000cluster_graph:max_cluster_size: 10embed_graph:enabled: false # if true, will generate node2vec embeddings for nodes# num_walks: 10# walk_length: 40# window_size: 2# iterations: 3# random_seed: 597832umap:enabled: false # if true, will generate UMAP embeddings for nodessnapshots:graphml: falseraw_entities: falsetop_level_nodes: falselocal_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: 12000global_search:# max_tokens: 12000# data_max_tokens: 12000# map_max_tokens: 1000# reduce_max_tokens: 2000# concurrency: 32

執行并構建圖索引

此流程是GraphRAG的核心流程,即構建基于圖的知識庫用于后續的問答環節,通過以下代碼即可觸發執行。

python -m graphrag.index

基于微軟在論文中提到的實現思路,執行過程GraphRAG主要實現了如下功能:

  1. Source Documents → Text Chunks:將源文檔分割成文本塊。
  2. Text Chunks → Element Instances:從每個文本塊中提取圖節點和邊的實例。
  3. Element Instances → Element Summaries:為每個圖元素生成摘要。
  4. Element Summaries → Graph Communities:使用社區檢測算法將圖劃分為社區。
  5. Graph Communities → Community Summaries:為每個社區生成摘要。
  6. Community Summaries → Community Answers → Global Answer:使用社區摘要生成局部答案,然后匯總這些局部答案以生成全局答案。

整體執行耗時與具體的文本大小有關。我這個例子整體耗時大概20分鐘,耗費人民幣大約4塊錢。執行過程中的輸出如下:


?? Reading settings from settings.yaml
/home/xinfeng/miniconda3/envs/graphrag-new/lib/python3.11/site-packages/numpy/core/fromnumeric.py:59: FutureWarning: 'DataFrame.swapaxes' is deprecated and will 
be removed in a future version. Please use 'DataFrame.transpose' instead.return bound(*args, **kwds)
?? create_base_text_un

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/80781.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/80781.shtml
英文地址,請注明出處:http://en.pswp.cn/web/80781.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

XXX企業云桌面系統建設技術方案書——基于超融合架構的安全高效云辦公平臺設計與實施

目錄 1. 項目背景與目標1.1 背景分析1.2 建設目標2. 需求分析2.1 功能需求用戶規模與場景終端兼容性2.2 非功能需求3. 系統架構設計3.1 總體架構圖流程圖說明3.2 技術選型對比3.3 網絡設計帶寬規劃公式4. 詳細實施方案4.1 分階段部署計劃4.2 桌面模板配置4.3 測試方案性能測試工…

數據直觀分析與可視化

數據直觀分析與可視化 一、數據的直觀分析核心價值 數據的直觀分析旨在通過視覺化的方式&#xff0c;幫助人們更直觀、更快速地理解數據的特征和模式&#xff0c;從而發現趨勢、異常值、分布情況以及變量之間的關系&#xff0c;為決策提供支持。 數據可視化與信息圖形、信息可…

Neo4j數據庫

Neo4j 是一款專門用來處理復雜關系的數據庫。我們可以簡單地將它理解為一個“用圖結構來管理數據的工具”。與我們常見的&#xff0c;像 Excel 那樣用表格&#xff08;行和列&#xff09;來存儲數據的傳統數據庫不同&#xff0c;Neo4j 采用了一種更接近人類思維對現實世界理解的…

Java異常處理全解析:從基礎到自定義

目錄 &#x1f680;前言&#x1f914;異常的定義與分類&#x1f4af;運行時異常&#x1f4af;編譯時異常&#x1f4af;異常的基本處理 &#x1f31f;異常的作用&#x1f427;自定義異常&#x1f4af;自定義運行時異常&#x1f4af;自定義編譯時異常 ??異常的處理方案&#x1…

Redisson分布式集合原理及應用

Redisson是一個用于Redis的Java客戶端&#xff0c;它簡化了復雜的數據結構和分布式服務的使用。 適用場景對比 數據結構適用場景優點RList消息隊列、任務隊列、歷史記錄分布式共享、阻塞操作、分頁查詢RMap緩存、配置中心、鍵值關聯數據支持鍵值對、分布式事務、TTLRSet去重集…

打破次元壁,VR 氣象站開啟氣象學習新姿勢?

在教育領域&#xff0c;VR 氣象站同樣發揮著巨大的作用&#xff0c;為氣象教學帶來了全新的模式&#xff0c;打破了傳統教學的次元壁&#xff0c;讓學生們以全新的姿勢學習氣象知識。? 在傳統的氣象教學中&#xff0c;學生們主要通過課本、圖片和老師的講解來學習氣象知識。這…

k8s面試題-ingress

場景&#xff1a;我通過deployment更新pod&#xff0c;ingress是怎么把新的請求流量發送到我新的pod的&#xff1f;是怎么監控到我更新的pod的&#xff1f; 在 Kubernetes 中&#xff0c;Ingress 是一種 API 對象&#xff0c;用于管理外部訪問到集群內服務的 HTTP 和 HTTPS 路…

RHCE 練習三:架設一臺 NFS 服務器

一、題目要求 1、開放 /nfs/shared 目錄&#xff0c;供所有用戶查詢資料 2、開放 /nfs/upload 目錄&#xff0c;為 192.168.xxx.0/24 網段主機可以上傳目錄&#xff0c;并將所有用戶及所屬的組映射為 nfs-upload,其 UID 和 GID 均為 210 3.將 /home/tom 目錄僅共享給 192.16…

【動態導通電阻】GaN HEMT動態導通電阻的精確測量

2023 年 7 月,瑞士洛桑聯邦理工學院的 Hongkeng Zhu 和 Elison Matioli 在《IEEE Transactions on Power Electronics》期刊發表了題為《Accurate Measurement of Dynamic ON-Resistance in GaN Transistors at Steady-State》的文章,基于提出的穩態測量方法,研究了氮化鎵(…

AI 制作游戲美術素材流程分享(程序員方向粗糙版)

AI 制作游戲美術素材分享(程序員方向粗糙版) 視頻講解: 抖音:https://www.douyin.com/user/self?from_tab_namemain&modal_id7505691614690561295&showTabpost Bilibili: https://www.bilibili.com/video/BV1ojJGzZEve/ 寫在最前面: 本方法比較粗糙,只對對美術風…

Java求職面試:互聯網大廠技術棧深度解析

文章簡述 在這篇文章中&#xff0c;我們將通過一個模擬的面試場景&#xff0c;帶你深入了解Java求職面試中可能會遇到的技術棧問題。通過這個故事&#xff0c;你可以學習到相關技術點的具體應用場景和面試技巧。 正文 場景&#xff1a;某互聯網大廠的面試現場 面試官&#…

學習日記-day11-5.20

完成目標&#xff1a; comment.java package com.zcr.pojo; import org.hibernate.annotations.GenericGenerator;import javax.persistence.*; //JPA操作表中數據&#xff0c;可以將對應的實體類映射到一張表上Entity(name "t_comment")//表示當前的實體類與哪張表…

機器學習第十九講:交叉驗證 → 用五次模擬考試驗證真實水平

機器學習第十九講&#xff1a;交叉驗證 → 用五次模擬考試驗證真實水平 資料取自《零基礎學機器學習》。 查看總目錄&#xff1a;學習大綱 關于DeepSeek本地部署指南可以看下我之前寫的文章&#xff1a;DeepSeek R1本地與線上滿血版部署&#xff1a;超詳細手把手指南 交叉驗證…

Linux面試題集合(6)

創建多級目錄或者同級目錄 mkdir -p 文件名/文件名/文件名 mkdir -p 文件名 文件名 文件名 Linux創建一個文件 touch 文件名 DOS命令創建文件 echo 內容>文件名&#xff08;創建一個有內容的文件&#xff09; echo >文件名&#xff08;創建一個沒有內容的文件&#xff09…

Vue百日學習計劃Day46-48天詳細計劃-Gemini版

Day 46: <KeepAlive> - 組件緩存與優化 (~3 小時) 本日目標: 理解 <KeepAlive> 的作用&#xff0c;學會如何使用它來緩存組件實例&#xff0c;從而優化應用性能和用戶體驗。所需資源: Vue 3 官方文檔 (<KeepAlive>): https://cn.vuejs.org/guide/built-ins/…

SpringBean模塊(三)具有生命周期管理能力的類(1)AutowireCapableBeanFactory

一、介紹 1、簡介 AutowireCapableBeanFactory 是 Spring 框架中的一個接口&#xff0c;位于 org.springframework.beans.factory 包下&#xff0c;它提供了更底層的 Bean 實例化、依賴注入和生命周期管理能力&#xff0c;即使這些 Bean 沒有通過常規的 Component 或 XML 注冊…

Service Mesh

目錄 一、Service Mesh 的核心特點 二、Service Mesh 的典型架構 1. Sidecar 模式 2. 控制平面與數據平面分離 三、Service Mesh 解決的核心問題 四、典型應用場景 五、主流 Service Mesh 框架對比 六、挑戰與局限性 七、未來趨勢 總結 Istio 一、Istio 核心組件與…

黑馬Java基礎筆記-13常用查找算法

查找算法 基本查找(也叫順序查找&#xff0c;線性查找) 二分查找&#xff08;需要有序數據&#xff09; public static int binarySearch(int[] arr, int number){//1.定義兩個變量記錄要查找的范圍int min 0;int max arr.length - 1;//2.利用循環不斷的去找要查找的數據wh…

Go 語言 vs C+Lua(Skynet)游戲服務器方案對比分析

為啥挑這兩個呢&#xff1f;因為兩種技術分別對應CSP模型和Actor模型&#xff0c;都是經過時間檢驗的成熟且可靠的并發模型&#xff0c;問了很多地方&#xff0c;經過gpt整理得出如下報告。 從開發效率、運行性能、熱更新擴展、云部署與水平擴展能力、多類型游戲支持等五個維度…

LeetCode 925. 長按鍵入 java題解

雙指針。不會寫。 https://leetcode.cn/problems/long-pressed-name/description/ class Solution {public boolean isLongPressedName(String name, String typed) {int len1name.length();int len2typed.length();int i0,j0;while(i<len1&&j<len2){if(name.ch…