LightRAG成功跑通:Ollama+Qwen2.5+bge-large-zh-v1.5

LightRAG跑通

继GraphRAG之后,LightRAG是有一个热门的项目,从10月开源之后,至今已经有7+Kstar了;
同时,Lightrag的更新非常快!值得持续关注在这里插入图片描述
当前的跑通教程基于2024.11.6这个时间节点,可能是第一个实际的图文跑通教程?
官方的ollama demo写的很不错,但是如果不用其默认的LLM和embeding模型,也还是跑不起来的,下面将给出一个适配自己模型的ollama跑通教程,供来者参考。

1.安装环境

conda create -n LightRAG python=3.10
conda activate LightRAG

推荐源码安装lightrag:
源码:https://github.com/HKUDS/LightRAG
cd 源码路径
pip install -e .

同时还需要安装ollama
LLM推荐:qwen2.5
embeding模型推荐:quentinz/bge-large-zh-v1.5
后面源码的模型将按照这两个模型进行配置

2.示例跑通:

(1)准备样例数据

使用官方的英文示例:
https://raw.githubusercontent.com/gusye1234/nano-graphrag/main/tests/mock_data.txt
复制网页的内容,保存为本地的一个book.txt,将其和下面的程序放在一个路径下。

(2)源码修改

examples/ lightrag_ollama_demo.py
将里面ollama的模型修改为你自己的模型即可。

import os
import logging
from lightrag import LightRAG, QueryParam
from lightrag.llm import ollama_model_complete, ollama_embedding
from lightrag.utils import EmbeddingFunc

WORKING_DIR = "./dickens"    # 导出路径

logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)

if not os.path.exists(WORKING_DIR):
    os.mkdir(WORKING_DIR)

rag = LightRAG(
    working_dir=WORKING_DIR,

    chunk_token_size=300,
    chunk_overlap_token_size=0,

    llm_model_func=ollama_model_complete,
    llm_model_name="qwen2.5",
    llm_model_max_async=1,
    llm_model_max_token_size=8192,
    llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 8192}},

    embedding_func=EmbeddingFunc(
        embedding_dim=1024,     # bge-large-zh-v1.5
        max_token_size=512,    # bge-large-zh-v1.5
        func=lambda texts: ollama_embedding(
            texts, embed_model="quentinz/bge-large-zh-v1.5", host="http://localhost:11434"
        ),
    ),
)

with open("./book.txt", "r", encoding="utf-8") as f:
    rag.insert(f.read())

# Perform naive search
print(
    rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
)

# Perform local search
print(
    rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
)

# Perform global search
print(
    rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
)

# Perform hybrid search
print(
    rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
)

lightrag/base.py
修改下面的地方,将默认的3个4000替换为你embedding模型的支持tokens长度,我使用的是bge-large-zh-v1.5,其最大tokens为512

@dataclass
class QueryParam:
    mode: Literal["local", "global", "hybrid", "naive"] = "global"
    only_need_context: bool = False
    response_type: str = "Multiple Paragraphs"
    # Number of top-k items to retrieve; corresponds to entities in "local" mode and relationships in "global" mode.
    top_k: int = 60
    # Number of tokens for the original chunks.
    # max_token_for_text_unit: int = 4000
    # # Number of tokens for the relationship descriptions
    # max_token_for_global_context: int = 4000
    # # Number of tokens for the entity descriptions
    # max_token_for_local_context: int = 4000

    max_token_for_text_unit: int = 512
    # Number of tokens for the relationship descriptions
    max_token_for_global_context: int = 512
    # Number of tokens for the entity descriptions
    max_token_for_local_context: int = 512

修改完成后,运行examples/ lightrag_ollama_demo.py,顺利的话,可以得到如下结果

同时在dickens中会生成很多中间文件。
在这里插入图片描述

注:若某个阶段不成功,则下次运行时,不会完全从头开始,会从失败的阶段继续往后。

若运行不顺利,出现了报错,请参考下面的问题解决。

(3)中文示例跑通

待添加

4.Neo4j可视化图谱

待添加

5.问题解决:

Q1:ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 768 and the array at index 1 has size 1024
执行rag.insert(f.read())时报错。
原因:嵌入模型的维度不对,官方示例中的嵌入维度为768,而我使用的模型quentinz/bge-large-zh-v1.5:latest维度为1024,修改embedding_dim为如下,可解决:
在这里插入图片描述

Q2:WARNING:lightrag:Didn’t extract any entities, maybe your LLM is not working
WARNING:lightrag:No new entities and relationships found
在这里插入图片描述

怀疑是英文promt导致无法处理中文的小王子数据。
因此建议考虑将prompt换成中文的
或者参考官方issue:https://github.com/HKUDS/LightRAG/issues/30

Q3:ollama._types.ResponseError: DevSidecar Error:

目标网站请求错误:【undefined】 127.0.0.1:11434, 代理请求超时

目标地址:http://127.0.0.1:11434/api/chat

分析:ollama 的embedding是可以正常调用的,LLM不行,
原因:被其它代理软件给影响了,具体的,被我电脑上运行的DevSidecar给影响了
解决措施:关闭电脑上的代理软件

在这里插入图片描述

关闭后,可以正常运行,以后运行ollama时要注意这个工具的问题。

Q4:TypeError: ‘NoneType’ object is not subscriptable
这个是在执行rag.query报错
在这里插入图片描述

在这里插入图片描述

参考官方issue:
https://github.com/HKUDS/LightRAG/issues/95
在这里插入图片描述
在这里插入图片描述

看issue建议是增大嵌入模型的输入长度或者是减少chunk_token_size,是因为问题超出了嵌入模型的长度了吗?

解决措施:修改embedding的最大长度,与实际模型相匹配

修改这几个地方,将这几个参数从4000减少为512可以正常跑通。
在这里插入图片描述

下面没有修改,但是逻辑上理解很重要。
在这里插入图片描述

修改3个4000位512(因为我用的bge-large-zh-v1.5其最大tokens为512),参考上面源码修改。
在这里插入图片描述

跑通后感受:

LightRAG号称是微软GraphRAG的替代品,更轻、更快、更强。
实际跑通后,从整个跑通速度来看,确实要快很多,整个项目目前看起来也还容易理解(后面可能会越来越庞大,越来越不好理解),值得持续关注。
LightRAG发展和更新很快,还有很多地方需要完善,当前模型中很多参数都是基于其自带的模型参数进行配置的,若是用到其它模型,则需要修改参数的地方没有明确给出,所以跑起来就会出现这样那样的问题,本质上都是模型参数设置的问题。
预期后面这些配置项都会变成yaml的方式方便修改,而当前需要摸索着进行修改。

评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值