基于微软GraphRAG,支持使用百度千帆、阿里通义、Ollama、字节豆包等大模型服务
Project description
GraphRAG More
GraphRAG More 基于微软 GraphRAG ,支持使用各种大模型:
- OpenAI接口兼容的模型服务(微软GraphRAG本就支持,可直接使用微软 GraphRAG )
- OpenAI
- Azure OpenAI
- 阿里通义
- 字节豆包
- Ollama
- 其他OpenAI接口兼容的模型服务
- 非OpenAI接口兼容的模型服务(微软GraphRAG不支持)
- 百度千帆(推理服务V2版本接口兼容OpenAI,但目前V2版本接口不支持Embedding)
可以先熟悉一下微软官方的demo教程:👉 微软官方文档
使用步骤如下:
要求 Python 3.10-3.12,建议使用 pyenv 来管理多个python版本
1. 安装 GraphRAG More
pip install graphrag-more
# 如果使用百度千帆,还需要安装qianfan sdk
# pip install qianfan
如需二次开发或者调试的话,也可以直接使用源码的方式,步骤如下:
下载 GraphRAG More 代码库
git clone https://github.com/guoyao/graphrag-more.git安装依赖包 这里使用 poetry 来管理python虚拟环境
# 安装 poetry 参考:https://python-poetry.org/docs/#installation cd graphrag-more poetry install
2. 准备demo数据
# 创建demo目录
mkdir -p ./ragtest/input
# 下载微软官方demo数据
# 微软官方提供的demo数据 https://www.gutenberg.org/cache/epub/24022/pg24022.txt 有点大,会消耗不少token,这里改用精简后的数据
curl https://raw.githubusercontent.com/guoyao/graphrag-more/refs/heads/main/examples/resources/pg24022.txt > ./ragtest/input/book.txt
3. 初始化demo目录
graphrag init --root ./ragtest
如果基于源码方式,请在源码根目录下使用poetry命令运行:
poetry run poe init --root ./ragtest
这将在./ragtest目录中创建两个文件:.env和settings.yaml,.env包含运行GraphRAG所需的环境变量,settings.yaml包含GraphRAG全部设置。
4. 配置
GraphRAG More 1.1.0 版本开始的配置文件与 1.1.0 之前版本的变动较大,升级请注意!!!
.env
在.env文件中配置GRAPHRAG_API_KEY,这是您所使用的大模型服务的API密钥,将其替换为您自己的API密钥。- 阿里通义获取API Key官方文档
- 字节豆包获取API Key 官方文档
- 百度千帆获取API Key官方文档
(注意:百度千帆的
API Key是带有效期的,过期后需要重新获取)
百度千帆还需配置 qianfan sdk 所需的环境变量QIANFAN_ACCESS_KEY、QIANFAN_SECRET_KEY,可以配置在系统环境变量中,也可以配置在.env文件中, 参考官方文档:使用安全认证AK/SK调用流程 - Ollama 默认不需要配置
API Key
settings.yaml
在settings.yaml文件中,根据您所使用的大模型配置model和api_base,GraphRAG More的example_settings文件夹提供了 百度千帆、阿里通义、字节豆包、Ollama 的settings.yaml文件供参考(详细的配置参考微软官方文档:https://microsoft.github.io/graphrag/config/yaml/ ), 根据选用的模型和使用的GraphRAG More版本(不同版本settings.yaml可能不一样),您可以直接将将example_settings文件夹(比如:GraphRAG More1.1.0 版本的 example_settings )对应模型的settings.yaml文件复制到 ragtest 目录,覆盖初始化过程生成的settings.yaml文件。# 百度千帆 cp ./example_settings/qianfan/settings.yaml ./ragtest # or 阿里通义 cp ./example_settings/tongyi/settings.yaml ./ragtest # or 字节豆包 cp ./example_settings/doubao/settings.yaml ./ragtest # or ollama cp ./example_settings/ollama/settings.yaml ./ragtest
example_settings的settings.yaml里面有的设置了默认的model,根据您选用的模型来修改model- 百度千帆默认使用 ernie-speed-pro-128k 和 tao-8k
- 阿里通义默认使用 qwen-plus 和 text-embedding-v2
- 字节豆包需要配置模型ID,即推理接入点ID,不是模型名称
- Ollama默认使用 mistral:latest 和 quentinz/bge-large-zh-v1.5:latest
对于
Ollama,需要在构建前安装Ollama并下载您选用的模型:- 安装
Ollama:https://ollama.com/download ,安装后启动 - 使用
Ollama下载模型
ollama pull mistral:latest # 默认使用的模型,请替换成您选用的模型 ollama pull quentinz/bge-large-zh-v1.5:latest # 默认使用的模型,请替换成您选用的模型
- 安装
5. 构建索引
graphrag index --root ./ragtest
如果基于源码方式,请在源码根目录下使用poetry命令运行:
poetry run poe index --root ./ragtest
构建过程可能会触发 rate limit (限速)导致构建失败,重复执行几次,或者尝试调小 settings.yaml 中 的 requests_per_minute 和 concurrent_requests 配置,然后重试
6. 执行查询
# global query
graphrag query \
--root ./ragtest \
--method global \
--query "What are the top themes in this story?"
# local query
graphrag query \
--root ./ragtest \
--method local \
--query "Who is Scrooge, and what are his main relationships?"
如果基于源码方式,请在源码根目录下使用poetry命令运行:
# global query poetry run poe query \ --root ./ragtest \ --method global \ --query "What are the top themes in this story?" # local query poetry run poe query \ --root ./ragtest \ --method local \ --query "Who is Scrooge, and what are his main relationships?"
查询过程可能会出现json解析报错问题,原因是某些模型没按要求输出json格式,可以重复执行几次,或者修改 settings.yaml 的 llm.model 改用其他模型
除了使用cli命令之外,也可以使用API方式来查询,以便集成到自己的项目中,API使用方式请参考:
examples/api_usage(注意:不同GraphRAG More版本API用法可能不一样,参考所使用版本下的文件)
- 基于已有配置文件查询:search_by_config_file.py
- 基于代码的自定义查询:custom_search.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file graphrag_more-1.2.0.tar.gz.
File metadata
- Download URL: graphrag_more-1.2.0.tar.gz
- Upload date:
- Size: 209.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.7 Darwin/22.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b82c2f9385fd158df679cc03077f7ea4ac2655aa65383001cb20617397bddedb
|
|
| MD5 |
0c486ce7ed592f0736b03fe162bf961e
|
|
| BLAKE2b-256 |
ed219864ce26498e519066d2d6b885f5cd61657cbc91aac30905437025a0f393
|
File details
Details for the file graphrag_more-1.2.0-py3-none-any.whl.
File metadata
- Download URL: graphrag_more-1.2.0-py3-none-any.whl
- Upload date:
- Size: 353.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.7 Darwin/22.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b201fe3e9ddb2f9a1206f8d21902bfcab69a88fb47e4e798bb0ac0093c19788e
|
|
| MD5 |
f3a6099a250c1dbda368594f1f1c62da
|
|
| BLAKE2b-256 |
dbd5e3d785623d3c60604c12ab63bd579d89102046fdd47d32f7514fd2acbfb5
|