之前有用ollama本地部署过graphrag,这次尝试部署一下香港大学的LightRAG,更轻量更便捷!
一、环境配置
首先需要将LightRAG的项目拉到本地,具体可以参考github的项目,
git clone https://github.com/HKUDS/LightRAG.git
安装LightRAG所需要的依赖:
cd LightRAG
pip install -e .
安装ollama,ollama官网提供的方法速度比较慢,经常容易出现中断的现象。这里推荐修改安装脚本的方法进行安装,首先下载安装脚本并修改权限:
# 下载安装脚本
curl -fsSL https://ollama.com/install.sh -o ollama_install.sh
# 给脚本添加执行权限
chmod +x ollama_install.sh
修改下载源,打开ollama_install.sh,找到以下两个下载地址:
https://ollama.com/download/ollama-linux-${ARCH}${VER_PARAM}
https://ollama.com/download/ollama-linux-amd64-rocm.tgz${VER_PARAM}
分别修改成以下的地址,这里推荐用ctrl+H直接进行替换:
https://github.moeyy.xyz/https://github.com/ollama/ollama/releases/download/v0.3.2/ollama-linux-amd64
https://github.moeyy.xyz/https://github.com/ollama/ollama/releases/download/v0.3.2/ollama-linux-amd64-rocm.tgz
运行安装的脚本:
./ollama_install.sh
二、模型挂起
首先将ollama服务开启
ollama serve
用到的两个模型一个是用来embedding的nomic-embed-text,一个是qwen2,分别用ollama pull到本地:
ollama pull nomic-embed-text
ollama pull qwen2
将模型挂起:
ollama run qwen2
挂起后可以通过一下命令查看是否挂起正常:
ollama ps
三、获取测试文件
按照github上面的官方教程,下载演示文本“查尔斯·狄更斯的圣诞颂歌”:
curl https://raw.githubusercontent.com/gusye1234/nano-graphrag/main/tests/mock_data.txt > ./book.txt
四、测试
首先修改./examples/lightrag_ollama_demo.py
,将llm_model_name修改成qwen2
,其他不变:
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,
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_embedding(
texts, embed_model="nomic-embed-text", 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"))
)
通过终端输入:
python examples/lightrag_ollama_demo.py
输出:
1. **Transformation**: A central theme is transformation, particularly exemplified by Ebenezer Scrooge's dramatic shift from a selfish, miserly businessman to a kinder and more empathetic individual over the course of one Christmas Eve. This theme is evident in his evolving interactions with ghosts from his past, present, and future experiences.
2. **Forgiveness**: Another significant theme concerns forgiveness, most notably illustrated through Scrooge's realization and regret for past wrongdoings, especially towards Tiny Tim. The narrative suggests that redemption and change are possible when one acknowledges their mistakes and seeks to amend them.
3. **Human Connection**: Emphasized by encounters with characters like Fezziwig, Bob Cratchit, Master Peter Cratchit, and the various Christmas people who embody joyous holiday traditions, this theme highlights the importance of human interaction, generosity, and shared experiences during festive times.
4. **The Importance of Giving and Kindness**: This theme is exemplified by Scrooge's initial reluctance to partake in Merry Christmas celebrations and charitable activities towards others. However, his interactions with ghosts prompt him to understand the significance of giving and kindness, leading to a transformative change within himself.
5. **The Ghostly Realm**: The spectral encounters with ghosts like Marley, the Spirit of Christmas Past, Present, and Future explore supernatural elements that guide Scrooge's personal development and societal impact. This theme underscores the power of past experiences, present realities, and future possibilities in shaping one’s behavior and understanding.
6. **The Role of Leadership**: Characters such as Alex and Taylor showcase leadership qualities crucial for guiding teams towards significant discoveries or decisions, like making First Contact with an unknown intelligence or managing a crisis response to extraterrestrial communication.
7. **Conflict and Ideological Clash**: The dynamic between Cruz and Jordan represents ideological conflicts regarding control versus freedom, order versus chaos, highlighting the tension that can exist within human societies when contrasting beliefs are present.
8. **The Legacy of Business Partnerships**: Notable through Marley's ghost, this theme explores the impact of business relationships on personal and professional outcomes after death, and how one’s actions in life affect those they leave behind.
9. **Revelation and Disclosure**: The revelation of information about the animal and Old Joe's optimistic outlook on life are examples of how new knowledge can alter perspectives and deepen character development within the story.
10. **Reflection and Learning**: This theme revolves around Scrooge’s journey of self-reflection, which ultimately leads to significant changes in his attitude and actions. His encounters with various characters allow for growth through empathy and understanding different life experiences.
By weaving these themes together, the narrative not only entertains but also offers profound insights into human nature, societal dynamics, and the transformative power of redemption and connection during festive times.
INFO:httpx:HTTP Request: POST http://localhost:11434/api/embeddings "HTTP/1.1 200 OK"
INFO:lightrag:Global query uses 64 entites, 60 relations, 3 text units
INFO:httpx:HTTP Request: POST http://localhost:11434/api/chat "HTTP/1.1 200 OK"
The primary themes of this text revolve around transformation, redemption, generosity, and compassion, all set against a backdrop of Christmas celebrations.
1. **Transformation**: This theme is embodied by Ebenezer Scrooge's journey from being a miserly businessman to a kindhearted soul after encountering the Ghosts of Christmas Past, Present, and Future. His experiences with these spirits force him to reconsider his past actions and contemplate the impact of kindness on personal fulfillment.
2. **Redemption**: A central theme is redemption through Scrooge's realization that he can change for the better. Through interactions with various characters like Tiny Tim and Fred, Scrooge learns about human suffering and the importance of generosity and charity, leading him to make amends for his past behavior.
3. **Generosity and Kindness**: This theme highlights the societal impact of caring for others during festive times. The Ghosts encourage Scrooge to witness various acts of generosity through their visits, which eventually inspire a change in his heart and actions towards sharing joy with those around him.
4. **Compassion**: Compassion emerges as Scrooge develops empathy for Tiny Tim's health struggles and the hardships faced by others like Bob Cratchit and the miners. This theme emphasizes that compassion is not only an emotional response but also a driving force in personal transformation and societal improvement.
5. **Community and Social Responsibility**: The story underscores the importance of community involvement, especially during Christmas time. Characters like Fred and Scrooge's sister discuss charitable actions for poor people, highlighting the role of community support and the responsibility to help those less fortunate.
6. **Legacy and Inheritance**: This theme is explored through Bob Cratchit's hopes for his son Tiny Tim and the legacy left by Jacob Marley (Scrooge's business partner). It touches on the values that families pass down, such as kindness and the importance of charitable contributions to society.
7. **Nature vs. Urbanization**: The presence of animals in London juxtaposes the urban environment with nature, symbolizing a connection between the natural world and human activities. This theme invites readers to consider how urban lifestyles impact our interactions with wildlife and natural resources.
8. **Spiritual Awakening**: Scrooge's encounters with spiritual entities like ghosts play a crucial role in his awakening from selfishness to compassion. The story suggests that supernatural experiences can lead individuals towards moral growth and transformation.
9. **Timelessness of Christmas Spirit**: Regardless of the economic challenges faced by characters like Bob Cratchit, the spirit of Christmas remains a symbol of joy and generosity that transcends financial constraints. This theme emphasizes the universal nature of festive celebrations as sources of comfort and unity.
10. **Moral Responsibility to the Underprivileged**: The text raises questions about societal responsibility towards the poor through discussions on prisons versus charity workhouses and Scrooge's reflection on the impact of his actions on those less fortunate. It encourages readers to consider ethical conduct and social justice.
These themes collectively create a rich tapestry that explores human relationships, personal growth, and societal responsibilities through the lens of Christmas traditions and supernatural experiences.
INFO:httpx:HTTP Request: POST http://localhost:11434/api/embeddings "HTTP/1.1 200 OK"
INFO:lightrag:Local query uses 60 entites, 55 relations, 3 text units
INFO:httpx:HTTP Request: POST http://localhost:11434/api/embeddings "HTTP/1.1 200 OK"
INFO:lightrag:Global query uses 64 entites, 60 relations, 3 text units
INFO:httpx:HTTP Request: POST http://localhost:11434/api/chat "HTTP/1.1 200 OK"
The top themes of "A Christmas Carol" include:
1. **Redemption** - The most prominent theme revolves around Ebenezer Scrooge's transformation from a selfish, greedy businessman to a kinder and more compassionate individual after experiencing encounters with various spirits during the night before Christmas.
2. **Happiness vs. Misery** - This theme is exemplified through contrasting the miserable life of Tiny Tim with that of Scrooge's regretful past and future projections if he does not change his ways. It also highlights the happiness found in community, generosity, and celebrating together during festive times like Christmas.
3. **Generosity and Kindness** - Throughout the narrative, characters such as Bob Cratchit, Fezziwig, and Mrs. Cratchit are shown to embody generosity and kindness, highlighting their importance in Scrooge's redemption journey.
4. **Christmas Spirit** - The story emphasizes the true meaning of Christmas through sharing, love, joy, and forgiveness. It contrasts Scrooge's initial lack of festive spirit with his eventual embracing of its essence after the encounters with the spirits.
5. **Mortality and Legacy** - Themes around death and legacy are explored as Scrooge reflects on Tiny Tim's condition and his own life choices. The Ghost of Christmas Past, Present, and Yet to Come all highlight this concept, encouraging reflection and change before it's too late.
6. **Influence of Others** - The story shows how actions have consequences and influence others both positively and negatively. Scrooge realizes the impact he has had on people like Bob Cratchit and Tiny Tim and learns about empathy through his interactions with them.
7. **Forgiveness and Redemption** - Scrooge's transformation involves forgiveness for himself and potentially for others, especially those who have wronged him in the past. His journey towards redemption is a central part of this theme.
8. **Conflict between Materialism and Human Connection** - The story contrasts Scrooge's materialistic mindset with the warmth, love, and connection that come from human interaction during festive times like Christmas.
9. **The Importance of Giving** - Characters such as the gentleman who advocates for helping poor people during Christmas and the ghost who sprinkles incense show the importance of generosity and charity towards others.
10. **Reflection on Life Choices** - The spirits guide Scrooge through his past, present, and future, encouraging him to reflect on how he has lived his life and consider what path he should take going forward.
These themes collectively intertwine throughout "A Christmas Carol," offering insights into human nature, societal values, and the transformative power of empathy and forgiveness.
至此,通过ollama本地部署LightRAG完成。