一、本地部署Lightrag:

1.通过git clone准备代码:git clone https://github.com/HKUDS/LightRAG.git

2.cd LightRAG

下载:pip install -e .

下载测试数据:curl https://raw.githubusercontent.com/gusye1234/nano-graphrag/main/tests/mock_data.txt > ./book.txt

3.本地部署ollama框架:

在终端(PowerShell):ollama list

需要使用两个模型:LLM模型:qwen

嵌入embedding模型:nomic-embed-text

ollama pull +模型名称→下载至本地

ollama run +模型名称→启动模型

ollama ps 查看模型启动状态

4.修改 lightrag_ollama_deom.py文件,包括文件加载位置、embedding维度、模型等参数。

5.run当前目录(python lightrag_ollama_deom.py)

6.python D:\Zhuo\Linghtrag\LightRAG\examples\graph_visual_with_html.py得到可视化结果

lightrag_ollama_deom.py文件代码:

import asyncio

import os

import inspect

import logging

from lightrag import LightRAG, QueryParam

from lightrag.llm.ollama import ollama_model_complete, ollama_embed

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,

    llm_model_func=ollama_model_complete,

    llm_model_name="qwen2",

    llm_model_max_async=4,

    llm_model_max_token_size=32768,

    llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 32768}},

    embedding_func=EmbeddingFunc(

        embedding_dim=768,

        max_token_size=8192,

        func=lambda texts: ollama_embed(

            texts, embed_model="nomic-embed-text", host="http://localhost:11434"

        ),

    ),

)

with open("./data.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"))

)

# stream response

resp = rag.query(

    "What are the top themes in this story?",

    param=QueryParam(mode="hybrid", stream=True),

)

async def print_stream(stream):

    async for chunk in stream:

        print(chunk, end="", flush=True)

if inspect.isasyncgen(resp):

    asyncio.run(print_stream(resp))

else:

    print(resp)

这段代码使用了 LightRAG 库与本地部署的 Ollama 框架实现了一个基于 RAG(Retrieval-Augmented Generation)的方法,来进行文本查询和相关分析。

1. 导入必要的库

import asyncio

import os

import inspect

import logging

from lightrag import LightRAG, QueryParam

from lightrag.llm.ollama import ollama_model_complete, ollama_embed

from lightrag.utils import EmbeddingFunc

  • asyncio:用于处理异步操作。
  • os:用于文件操作。
  • inspect:用于检查对象的类型(如是否为异步生成器等)。
  • logging:用于日志记录。
  • lightrag 和 lightrag.llm.ollama:这些是 LightRAG 库中的模块,用于实现 RAG 功能和与 Ollama 模型进行交互。
  • lightrag.utils.EmbeddingFunc:用于处理嵌入(embedding)功能。

2. 设置工作目录

WORKING_DIR = "./dickens"

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

if not os.path.exists(WORKING_DIR):

    os.mkdir(WORKING_DIR)

  • WORKING_DIR:设置存储数据和索引的工作目录(在此例中为 ./dickens)。
  • 使用 logging.basicConfig 来配置日志记录格式和日志级别为 INFO。
  • 如果指定的 WORKING_DIR 路径不存在,则使用 os.mkdir() 创建该目录。

3. 初始化 LightRAG 实例

rag = LightRAG(

    working_dir=WORKING_DIR,

    llm_model_func=ollama_model_complete,

    llm_model_name="qwen2",

    llm_model_max_async=4,

    llm_model_max_token_size=32768,

    llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 32768}},

    embedding_func=EmbeddingFunc(

        embedding_dim=768,

        max_token_size=8192,

        func=lambda texts: ollama_embed(

            texts, embed_model="nomic-embed-text", host="http://localhost:11434"

        ),

    ),

)

  • LightRAG:是核心类,用于执行基于 RAG 的检索和生成任务。
  • working_dir:指定存储工作数据的目录。
  • llm_model_func=ollama_model_complete:指定 LLM(大语言模型)的方法,这里使用的是本地的 Ollama 框架。
  • llm_model_name="qwen2":指定使用的模型名称。
  • llm_model_max_async=4:允许的最大并发异步请求数。
  • llm_model_max_token_size=32768:最大令牌大小(模型处理的最大文本长度)。
  • llm_model_kwargs:包含 Ollama 模型的主机地址和一些选项,如上下文的最大长度。
  • embedding_func:定义了如何计算文本的嵌入。通过 EmbeddingFunc 类,文本会被转换为嵌入向量,并传递给 Ollama 的嵌入模型来生成嵌入。

4. 插入文本数据

with open("./data.txt", "r", encoding="utf-8") as f:

    rag.insert(f.read())

  • 读取本地文件 data.txt,并将文件中的文本插入到 LightRAG 实例中。这个文本数据会被用于后续的查询和生成。

5. 执行不同类型的查询

# 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"))

)

这里通过调用 rag.query() 方法对文本进行不同模式的查询:

  • naive:使用简单的搜索方法(可能是基于关键词的匹配)。
  • local:局部搜索,可能基于索引或近邻查找。
  • global:全局搜索,可能基于全局数据或模型的推理。
  • hybrid:混合搜索,结合了以上多种方法。

6. 流式响应(streaming)

resp = rag.query(

    "What are the top themes in this story?",

    param=QueryParam(mode="hybrid", stream=True),

)

  • stream=True:表示查询将返回一个流式响应,即逐步生成结果,而不是一次性返回整个结果。

7. 异步处理流式响应

async def print_stream(stream):

    async for chunk in stream:

        print(chunk, end="", flush=True)

if inspect.isasyncgen(resp):

    asyncio.run(print_stream(resp))

else:

    print(resp)

  • print_stream:定义了一个异步函数,用于逐步打印流式响应的每个数据块。
  • asyncio.run(print_stream(resp)):如果返回的响应是异步生成器(isasyncgen),则运行异步函数来打印流的内容。如果不是异步生成器,则直接打印完整的响应。

总结

这段代码主要演示了如何使用 LightRAG 库结合 Ollama 模型进行基于 RAG 的查询任务。它展示了不同的查询模式(如 naive, local, global, hybrid),并且支持流式响应,这对于处理大型文本数据或长时间运行的查询非常有用。

结果:

These themes not only enrich the narrative but also serve to convey messages about personal growth, societal responsibilities, and the true meaning of festive celebrations.      

INFO:Non-embedding cached missed(mode:hybrid type:query)

INFO:Non-embedding cached missed(mode:hybrid type:keywords)

INFO:Inserting 1 to llm_response_cache

INFO:Query nodes: Plot, Characters, Setting, Conflict, Resolution, top_k: 60, cosine: 0.2

INFO:Query edges: Top themes, Story content, top_k: 60, cosine: 0.2

INFO:Global query uses 70 entites, 60 relations, 3 chunks

INFO:Local query uses 60 entites, 67 relations, 3 chunks

INFO:Inserting 1 to llm_response_cache

The top themes of "A Christmas Carol" can be categorized as follows:

1. **Redemption**: The central theme of redemption is prevalent throughout the narrative, particularly for the protagonist Ebenezer Scrooge. His transformation from a greedy and cold-hearted businessman to a kinder individual who values generosity and kindness showcases the possibility of personal change.

2. **Christmas Spirit**: The festive spirit that Scrooge experiences during Christmas time introduces themes such as love, charity, and joy among the community members he encounters. This theme contrasts with his previous bitter attitude towards holidays, highlighting the power of communal celebration and goodwill.

3. **Moral Growth**: The concept of moral growth is central to the story, not only through Scrooge's transformation but also through other characters like Bob Cratchit, Tiny Tim, Martha Cratchit, and others who embody values such as family, care for the needy, and kindness.

4. **The Importance of Past Memories**: Scrooge revisits his past actions through encounters with spirits that show him memories from different times in his life, which prompts introspection and emotional responses. This theme highlights how personal history can influence one's present behavior and future prospects.

5. **Community and Society**: The relationship between society and individuals like Scrooge is a recurring theme. It explores societal expectations versus personal values, and how community interactions can lead to growth or reinforce selfish tendencies.

6. **Forgiveness and Second Chances**: Characters such as Jacob Marley’s ghost offer forgiveness for past wrongdoings, providing opportunities for second chances. This theme emphasizes the importance of redemption and the potential for change in one's life path.

7. **Generosity and Self-Sacrifice**: Throughout the story, characters like Scrooge and his nephew encourage acts of generosity towards others during Christmas time. The theme underscores the value of sharing resources and kindness with those less fortunate.

8. **The Impact of Ghostly Interventions**: The encounters with spirits (Marley's ghost, ghosts representing past, present, future, and other entities) serve as catalysts for Scrooge’s transformation by confronting him with his actions and their consequences on others.

9. **Life and Death Reflections**: Questions about life, death, and legacy are raised through the narrative, prompting reflections on mortality and the importance of living a meaningful life rather than just accumulating wealth.

10. **Contrasts between Hard Dealing vs. Kindness**: The stark contrast between Scrooge’s business practices (hard dealing) and his newfound generosity highlights themes of empathy versus self-interest.

These themes collectively explore various facets of human experience through the lens of holiday festivities, personal relationships, societal norms, and spiritual experiences.  

INFO:Non-embedding cached hit(mode:hybrid type:query)

The top themes of "A Christmas Carol" can be categorized as follows:

1. **Redemption**: The central theme of redemption is prevalent throughout the narrative, particularly for the protagonist Ebenezer Scrooge. His transformation from a greedy and cold-hearted businessman to a kinder individual who values generosity and kindness showcases the possibility of personal change.

2. **Christmas Spirit**: The festive spirit that Scrooge experiences during Christmas time introduces themes such as love, charity, and joy among the community members he encounters. This theme contrasts with his previous bitter attitude towards holidays, highlighting the power of communal celebration and goodwill.

3. **Moral Growth**: The concept of moral growth is central to the story, not only through Scrooge's transformation but also through other characters like Bob Cratchit, Tiny Tim, Martha Cratchit, and others who embody values such as family, care for the needy, and kindness.

4. **The Importance of Past Memories**: Scrooge revisits his past actions through encounters with spirits that show him memories from different times in his life, which prompts introspection and emotional responses. This theme highlights how personal history can influence one's present behavior and future prospects.

5. **Community and Society**: The relationship between society and individuals like Scrooge is a recurring theme. It explores societal expectations versus personal values, and how community interactions can lead to growth or reinforce selfish tendencies.

6. **Forgiveness and Second Chances**: Characters such as Jacob Marley’s ghost offer forgiveness for past wrongdoings, providing opportunities for second chances. This theme emphasizes the importance of redemption and the potential for change in one's life path.

7. **Generosity and Self-Sacrifice**: Throughout the story, characters like Scrooge and his nephew encourage acts of generosity towards others during Christmas time. The theme underscores the value of sharing resources and kindness with those less fortunate.

8. **The Impact of Ghostly Interventions**: The encounters with spirits (Marley's ghost, ghosts representing past, present, future, and other entities) serve as catalysts for Scrooge’s transformation by confronting him with his actions and their consequences on others.

9. **Life and Death Reflections**: Questions about life, death, and legacy are raised through the narrative, prompting reflections on mortality and the importance of living a meaningful life rather than just accumulating wealth.

10. **Contrasts between Hard Dealing vs. Kindness**: The stark contrast between Scrooge’s business practices (hard dealing) and his newfound generosity highlights themes of empathy versus self-interest.

These themes collectively explore various facets of human experience through the lens of holiday festivities, personal relationships, societal norms, and spiritual experiences.

Local 和 Global 是 LightRAG 中不同的查询策略,Local 注重局部精准性,Global 强调全局全面性,通过这两种不同的方式来优化对查询的响应和结果的准确性。

二、本地部署Lightrag WebUI界面

1.新建vue项目:

vue create vue-lightrag

cd vue-lightrag

2.通过git clone来迁移代码

git clone https://github.com/HKUDS/LightRAG.git

cd LightRAG

3.创建虚拟环境,可以避免不同项目之间的依赖冲突

python -m venv venv

.\venv\Scripts\Activate

4.按照依赖:pip install -e ".[api]"

5.cd D:\Zhuo\lightrag\vue-lightrag\LightRAG\lightrag\api>

设置一下内容:

set LLM_BINDING_HOST=http://localhost:11434

set LLM_MODEL=qwen2

set EMBEDDING_BINDING_HOST=http://localhost:11434

set EMBEDDING_MODEL=nomic-embed-text

set EMBEDDING_DIM=1024

lightrag-server --llm-binding ollama --embedding-binding ollama

6.运行lightrag-server --llm-binding ollama --embedding-binding ollama

可以直接打开:http://localhost:9621/webui/

导入数据测试,book.txt,得到结果:

Logo

尧米是由西云算力与CSDN联合运营的AI算力和模型开源社区品牌,为基于DaModel智算平台的AI应用企业和泛AI开发者提供技术交流与成果转化平台。

更多推荐