GraphRAG 本地 Ollama - 知识图谱

GraphRAG 本地 Ollama - 知识图谱

欢迎来到GraphRAG Local Ollama!这个存储库是对微软的的激动人心的改编,旨在支持使用 Ollama 下载的本地模型。告别昂贵的 OpenAPI 模型,拥抱使用 Ollama 进行高效、具有成本效益的本地推理!

📄 研究论文

有关 GraphRAG 实现的更多详细信息,请参阅。

论文摘要

使用检索增强生成(RAG)从外部知识源中检索相关信息,使大型语言模型(LLMs)能够回答关于私人和/或以前未见过的文档集合的问题。然而,RAG 在针对整个文本语料库的全局问题上失败,比如“数据集中的主题是什么?”,因为这本质上是一个查询聚焦摘要(QFS)任务,而不是一个明确的检索任务。与此同时,先前的 QFS 方法无法扩展到 typica lRAG 系统索引的文本数量。为了结合这些对比方法的优势,我们提出了一种图形 RAG 方法,用于回答关于私人文本语料库的问题,该方法随着用户问题的普遍性和要索引的源文本数量而扩展。我们的方法使用一个LLM在两个阶段构建基于图形的文本索引:首先从源文档中导出实体知识图,然后为所有相关实体组生成社区摘要。给定一个问题,每个社区摘要用于生成部分响应,然后所有部分响应再次总结为最终响应提供给用户。 对于在 100 万令牌范围内的数据集上的一类全局意义问题,我们展示了图形 RAG 相对于天真的 RAG 基线在生成答案的全面性和多样性方面带来了显著改进。

🌟 特点

  • 本地模型支持: 利用 Ollama 的本地模型进行LLM和嵌入。
  • 具有成本效益:消除对昂贵的 OpenAPI 模型的依赖。
  • 简单设置: 简单直接的设置过程。

📦 安装和设置

按照以下步骤设置此存储库,并使用 Ollama 提供的本地模型使用 GraphRag:

创建并激活一个新的 conda 环境:(请坚持使用给定的 Python 版本 3.10,以避免错误)  conda create -n graphrag-ollama-local python=3.10 conda activate graphrag-ollama-local

安装 Ollama:

  • 访问  获取安装说明。
  • 或者,运行:  curl -fsSL https://ollama.com/install.sh | sh #ollama for linux pip install ollama

下载所需的模型使用 Ollama,我们可以从(mistral,gemma2,qwen2)选择llm和 Ollama 提供的任何嵌入模型:  ollama pull mistral  #llm ollama pull nomic-embed-text  #embedding

克隆存储库:  git clone https://github.com/TheAiSingularity/graphrag-local-ollama.git

导航到存储库目录:  cd graphrag-local-ollama/

安装 graphrag 包**这是最重要的一步:  pip install -e .

创建所需的输入目录:这是实验数据和结果将被存储的地方 - ./ragtest  mkdir -p ./ragtest/input

将示例数据文件夹 input/ 复制到 ./ragtest。Input/ 包含运行设置所需的示例数据。您可以在此处以 .txt 格式添加自己的数据。  cp input/* ./ragtest/input

初始化./ragtest 文件夹以创建所需的文件:  python -m graphrag.index --init --root ./ragtest

移动 settings.yaml 文件,这是配置了 ollama 本地模型的主预定义配置文件:  cp settings.yaml ./ragtest

用户可以通过更改模型进行实验。 llm 模型期望像 llama3、mistral、phi3 等语言模型,嵌入模型部分期望像 mxbai-embed-large、nomic-embed-text 等嵌入模型,这些模型由 Ollama 提供。您可以在这里找到 Ollama 提供的完整模型列表 ,可以在本地部署。默认的 API 基本 URL 分别为  用于 LLM 和  用于嵌入,因此它们被添加到相应的部分。


encoding_model: cl100k_base
skip_workflows: []
llm:
  model: myqwen2
  request_timeout: 7200.0
  api_base: http://localhost:9997/v1
  # max_tokens: 4000
  # request_timeout: 180.0
  # 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
  # temperature: 0 # temperature for sampling
  # top_p: 1 # top-p sampling
  # n: 1 # Number of completions to generate

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
  # target: required # or all
  llm:
    api_base: http://localhost:11434/v1
    model: bge-m3
    # 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
    
  


chunks:
  size: 1200
  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: 1

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

community_reports:
  ## 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
  # llm_temperature: 0 # temperature for sampling
  # llm_top_p: 1 # top-p sampling
  # llm_n: 1 # Number of completions to generate
  # max_tokens: 12000

global_search:
  # llm_temperature: 0 # temperature for sampling
  # llm_top_p: 1 # top-p sampling
  # llm_n: 1 # Number of completions to generate
  # max_tokens: 12000
  # data_max_tokens: 12000
  # map_max_tokens: 1000
  # reduce_max_tokens: 2000
  # concurrency: 32

运行索引,生成一个图:  python -m graphrag.index --root ./ragtest

运行查询:仅支持全局方法  python -m graphrag.query --root ./ragtest --method global "What is machine learning?"

图表可以保存,进一步可以通过在 settings.yaml 中将 graphml 更改为“true”来用于可视化:

snapshots:
graphml: true

要可视化生成的 graphml 文件,您可以使用: 或在存储库中提供的脚本 visualize-graphml.py:

将路径传递给 visualize-graphml.py 中的下面一行以指向 .graphml 文件:

graph = nx.read_graphml('output/20240708-161630/artifacts/summarized_graph.graphml') 

可视化 .graphml:  python visualize-graphml.py

引用

  • 原始 GraphRAG 存储库由 Microsoft 创建:
  • Ollama:

Read more

前端防范 XSS(跨站脚本攻击)

目录 一、防范措施 1.layui util  核心转义的特殊字符 示例 2.js-xss.js库 安装 1. Node.js 环境(npm/yarn) 2. 浏览器环境 核心 API 基础使用 1. 基础过滤(默认规则) 2. 自定义过滤规则 (1)允许特定标签 (2)允许特定属性 (3)自定义标签处理 (4)自定义属性处理 (5)转义特定字符 常见场景示例 1. 过滤用户输入的评论内容 2. 允许特定富文本标签(如富文本编辑器内容) 注意事项 更多配置 XSS(跨站脚本攻击)是一种常见的网络攻击手段,它允许攻击者将恶意脚本注入到其他用户的浏览器中。

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

目录 1. 打开浏览器开发者工具 2. 使用 Network 面板 3. 查看具体的API请求 a. Headers b. Payload c. Response d. Preview e. Timing 4. 实际操作步骤 5. 常见问题及解决方法 a. 无法看到API请求 b. 请求失败 c. 跨域问题(CORS) 作为一名后端工程师,理解前端如何调用接口、传递参数以及接收返回值是非常重要的。下面将详细介绍如何通过浏览器开发者工具(F12)查看和分析这些信息,并附带图片案例帮助你更好地理解。 1. 打开浏览器开发者工具 按下 F12 或右键点击页面选择“检查”可以打开浏览器的开发者工具。常用的浏览器如Chrome、Firefox等都内置了开发者工具。下面是我选择我的一篇文章,打开开发者工具进行演示。 2. 使用

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例)

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例) 前端开发中最令人头疼的莫过于那些难以定位的UI问题——元素错位、样式冲突、响应式失效...传统调试方式往往需要反复修改代码、刷新页面、检查元素。现在,通过Cursor编辑器集成的Codex功能,你可以直接用截图交互快速定位和修复这些问题。本文将带你从零开始,掌握这套革命性的调试工作流。 1. 环境准备与基础配置 在开始之前,确保你已经具备以下环境: * Cursor编辑器最新版(v2.5+) * Node.js 18.x及以上版本 * React 18项目(本文以Chakra UI 2.x为例) 首先在Cursor中安装Codex插件: 1. 点击左侧扩展图标 2. 搜索"Codex"并安装 3. 登录你的OpenAI账户(需要ChatGPT Plus订阅) 关键配置项: // 在项目根目录创建.