GRAPHRAG API调用
安装
先决条件
确保已安装 Python 3.8+。
通过 pip 安装
使用 pip 安装 GraphRag-API:
pip install graphrag_api
从源码安装
- 克隆源码库:
git clone https://github.com/nightzjp/graphrag_api
- 进入项目目录并安装依赖:
cd graphrag_api
pip install -r requirements.txt
使用
初始化
- 命令行初始化
python -m graphrag.index --init --root ./rag # graphrag初始化
python index_test.py --init --root rag # graphrag_api初始化
2代码初始化
from graphrag_api.index import GraphRagIndexer
indexer = GraphRagIndexer(root="rag", init=True)
indexer.run()
索引创建
- 命令行初始化(会生成rag目录)
python -m graphrag.index --root rag # graphrag初始化
python index_test.py --root rag # graphrag_api初始化
- 代码初始化
from graphrag_api.index import GraphRagIndexer
indexer = GraphRagIndexer(root="rag")
indexer.run()
- 修改配置文件(自动生成,需要修改相应配置)
.env
文件
GRAPHRAG_API_KEY=<API_KEY>
settings.yaml
文件
encoding_model: cl100k_base
skip_workflows: []
llm:
api_key: ${GRAPHRAG_API_KEY}
type: openai_chat # or azure_openai_chat
model: gpt-4o-mini # mini性价比比较高
model_supports_json: true # recommended if this is available for your model.
embeddings:
## parallelization: override the global parallelization settings for embeddings
async_mode: threaded # or asyncio
llm:
api_key: ${GRAPHRAG_API_KEY}
type: openai_embedding # or azure_openai_embedding
model: text-embedding-3-small
input:
type: file # or blob
file_type: csv # or text 这里以csv为例
base_dir: "input"
file_encoding: utf-8
file_pattern: ".*\\.csv$"
source_column: "question" # csv-key
text_column: "answer" # csv-key
q.csv
文件示例
question,answer
"你是谁","你猜啊"
搜索
- 命令行初始化
python -m graphrag.query \
--root ./ragtest \
--method global(local) \
"What are the top themes in this story?" # graphrag初始化
python search_test.py --root rag --method global(local) "What are the top themes in this story?" # graphrag初始化
2代码初始化
from graphrag_api.search import SearchRunner
search_runner = SearchRunner(root_dir="rag")
search_runner.run_local_search(query="What are the top themes in this story?")
search_runner.run_global_search(query="What are the top themes in this story?")
# 对于输出的结果可能带有一些特殊字符,可以采用以下函数去除特殊字符或自行处理。
search_runner.remove_sources(search_runner.run_local_search(query="What are the top themes in this story?"))
本文来自博客园,作者:一石数字欠我15w!!!,转载请注明原文链接:https://www.cnblogs.com/52-qq/p/18346923
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10亿数据,如何做迁移?
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 易语言 —— 开山篇
· Trae初体验
2021-08-07 django-序列化参数为空报错问题
2019-08-07 get_object_or_404返回404(400)