【个人经验】GraphRAG+Ollama 本地部署 已跑通!

踩了很多坑,也算是侥幸跑通了。

1. 创建虚拟环境

conda create -n graphrag-ollama-local python=3.10
conda activate graphrag-ollama-local

2. 安装Ollama

pip install ollama

3. 下载Ollama中的开源模型

此处使用 mistral (4.1GB) 和 nomic-embed-text (278MB)为例

ollama pull mistral 
ollama pull nomic-embed-text

这里需要先安装Ollama。本地安装比较简单,直接在官网 https://ollama.com/download 上下载即可。linux安装同理,如果下载过慢可以参考帖子 https://blog.csdn.net/u010197332/article/details/137604798 离线安装。

如有需要可以更改模型下载的默认路径(比如下载到linux远程服务器自己的账户分支下),具体方法见 Ollama模型下载路径替换!靠谱!(Linux版本)

如果显示 Error: could not connect to ollama app, is it running? 报错,需要先启动服务再pull

ollama serve &

检查模型是否下载

ollama list

需保证http://localhost:11434的ollama默认端口,不被别的进程占用!!!否则出现 connection reset by peer 报错

4. 下载源代码,进入代码目录

git clone https://github.com/TheAiSingularity/graphrag-local-ollama.git
cd graphrag-local-ollama/

5. 安装依赖包

pip install -e .

6. 创建数据存储目录

mkdir -p ./ragtest/input

7. 将示例数据放入到./ragtest/input目录下

cp input/* ./ragtest/input

8. 目录初始化

python -m graphrag.index --init --root ./ragtest

9. 将 settings.yaml 移动到 ./ragtest 目录下

mv settings.yaml ./ragtest

10. 构建索引

如果使用的不是示例模型,需要现在 settings.yaml 文件中修改调用模型的名称(与ollama list中显示的名称一致)。特别地,如果需要修改embedding模型,还要在 .../graphrag-local-ollama/graphrag/llm/openai/create_openai_client.py 文件中修改。

python -m graphrag.index --root ./ragtest

这一步很容易出现报错,例如
请添加图片描述
此时可以进入 .../graph-local-ollama/ragtest/output/ 下最新的文件夹,其中的 reports 文件夹中的log即日志中找到具体的报错信息。

对于报错 openal.APIConnectionError:connection error.,借鉴 https://github.com/microsoft/graphrag/issues/476 中的回答可以尝试清除缓存后重新运行。(通常是 .../graphrag-local-ollama/ragtest/cache

对于报错 RuntimeError: Failed to generate valid JSON output ,借鉴 https://github.com/microsoft/graphrag/issues/640 中的回答可以尝试修改双花括号。

我的情况同样是报错 RuntimeError: Failed to generate valid JSON output ,参考 https://github.com/microsoft/graphrag/issues/575 是因为我选择的llm无法生成有效的json格式输出,换个模型就好了。(或尝试将 settings.yaml 中的 model_supports_json 选项设为 false
具体的原因解释参考 https://github.com/microsoft/graphrag/issues/452

I got the same issue and after changing to a stronger LLM, in my case Qwen2-72B-Instruct, the issue was disappeared(unfortunately glm-4 didn’t work, but I guess if executing multi times, it might succeed).
checking the log, we can find out the reason is that LLM returns a markdown format json(starting with ```json) instead of a pure string. I added a log below and here is what a successful input looked like.

22:14:22,462 graphrag.llm.openai.utils INFO ####input: {
   "title": "Scrooge and Marley's Business Community",>
   "summary": "The community is centered around Scrooge, a prominent figure in Charles Dickens' 'A Christmas Carol,' who is characterized by his cold, unsympathetic nature and tight-fisted business practices. Scrooge is associated with the business Scrooge and Marley, where he was a partner with Marley, and is linked to various entities such as beggars, blind men's dogs, and children, all of whom avoid him, indicating his negative reputation. The community also includes Marley's funeral, Christmas Eve, and the city where Scrooge's counting-house is located, all of which play significant roles in the narrative.",
   "rating": 7.0,

可以知道,导致这个问题的原因可能是所用的模型无法生成有效的json格式而是生成了字符串,同样的问题可能导致 11.执行查询 中的报错,故换一个更强大的模型或可以解决这个问题。

更多报错问题可以参考 https://github.com/microsoft/graphrag/issues/657 找到和自己相似的问题及回答。

最终有 请添加图片描述 即为成功。

11. 执行查询

python -m graphrag.query --root ./ragtest --method global <"Your Question?">

出现 SUCCESS: Global Search Response: I am sorry but I am unable to answer this question given the provided data. 的输出。参考https://github.com/microsoft/graphrag/issues/575中的如下回答:

Small update: I found out that my model always returned nosense like this:

python -m graphrag.query --root ./myfolder --method global "What are the main topics"

I found out that my local Ollama instance (0.3.0) seemed to ignore the system prompt and I got it working by manually stitching together the two prompts into one:
File: /graphrag/query/structured_search/global_search/search.py , method: _map_response_single_batch

#search_messages = [
#  {"role": "system", "content": search_prompt},
#  {"role": "user", "content": query},
#]
search_messages = [ {"role": "user", "content": search_prompt + "\n\n### USER QUESTION ### \n\n" + query} ]

以上回答表明,可能的问题是本地 Ollama ( 0.3.0)可能忽略了 system prompt,导致返回的内容没有意义,故可以首先尝试修改源代码,将系统提示和用户提示手动拼接(参考以上内容将原本的代码注释掉并进行替换)。

如果修改后的查询效果仍然不好,或出现 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 的报错,可能是同上“所用的模型无法生成有效的json格式而是生成了字符串”的问题导致查询错误,可以在 .../graphrag-local/ollama/graphrag/query/structed_search/global_search/search.py 中的代码做如下添加

		print(search_response) # 添加代码使回答直接输出,可以查看到查询结果
        parsed_elements = json.loads(search_response)["points"]
        return [
            {
                "answer": element["description"],
                "score": int(element["score"]),
            }
            for element in parsed_elements
        ]

如果此时直接打印出来的回答是可行的,即表示回答本身没问题,而是所用llm的输出格式有问题。

修改后对于查询

python -m graphrag.query --root ./ragtest --method global "What is meaning?"

出现确切回答,例如:
请添加图片描述即为成功!

12. 图谱可视化展示

首选确保 settings.yaml 文件中的 graphml 变量为 yes,即有

snapshots:
  graphml: yes

然后使用项目自带的脚本 /graphrag-local-ollama/visualize-graphml.py 传递 .graphml 文件路径。
这一步需要将脚本中的路径替换成自己的测试结果路径(通常使用最新一次test的output),具体见 .../graph-local-ollama/ragtest/output/ 目录。
示例:

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

(特别注意,除了修改日期目录名称,还要在前面添加ragtest目录路径信息)

运行该可视化脚本

python visualize-graphml.py

成功的话会自动跳转网页展示结果。
(此处我第一次运行显示有安装包不存在,直接 pip install 安装即可解决)
结果示例:
请添加图片描述
到此成功!


如果再有踩坑还会继续更新。

参考链接

GraphRAG+Ollama实现本地部署(最全,非常详细,保姆教程)
【深度学习】本地运行 GraphRAG + Ollama

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值