Skip to content

GraphRAG 入门:凡人修仙传DEMO

官方文档: https://microsoft.github.io/graphrag/get_started/

创建虚拟环境,python版本<=3.13

bash
#创建
$ conda create -n graph_rag python=3.12
#激活
$ conda activate graph_rag

安装GraphRag

bash
$ pip install graphrag -i https://pypi.tuna.tsinghua.edu.cn/simple

我们需要设置一个数据项目和一些初始配置。首先让我们准备一个示例数据集

bash
$ mkdir -p ./fanren/input

1.初始化GraphRAG

要初始化您的工作区,请首先运行 graphrag init 命令。由于我们已经在上一步中配置了一个名为 ./fanren 的目录,因此请运行以下命令

bash
graphrag init --root ./fanren

这将在 ./fanren 目录中创建两个文件:.envsettings.yaml

  • .env 包含运行 GraphRAG 管道所需的环境变量。如果您检查该文件,您会看到定义了一个环境变量 GRAPHRAG_API_KEY=<API_KEY>。将 <API_KEY> 替换为您自己的 OpenAI 或 Azure API 密钥。
  • settings.yaml 包含管道的设置。您可以修改此文件以更改管道的设置。

初始化失败,需要删除.env和settings.yaml文件

2.准备数据,放到 ./input 目录下

  • 准备一个包含多篇文档的文本数据集
  • 格式可以是 .txt / .csv / .json

3.处理数据,构建索引

bash
$ graphrag index --root ./fanren

Starting pipeline with workflows: load_input_documents, create_base_text_units, create_final_documents, extract_graph, finalize_graph, extract_covariates, create_communities, create_final_text_units, create_community_reports, generate_text_embeddings Starting workflow: load_input_documents No .*.txt$ matches found in storage Pipeline error: Error reading documents, please see logs. Pipeline complete

支持.txt文件,其他文件不支持

4.运行查询

bash
$ graphrag query \
  --root ./fanren \
  --method global \
  "韩立的名字是谁取的?"
  • method: global、local
  • local 答案生成(代价高):针对具体问题,GraphRAG通过结合,这些答案来源于GraphRAG中的特定社区;
  • global 答案生成 (代价非常高):对于需要涵盖整个数据集的全局性问题,GraphRAG

遇到的问题

Model 'openai/qwen3-14b' does not support response schemas.

使用openai的模型,推荐gpt-4.1

Size of FixedSizeList is not the same.

input list: fixed_size_list<item: float>[1024] output list: fixed_size_list<item: float>[3072].

使用 3072维的embedding模型,推荐text-embedding-3-large

最近更新