LightRAG/examples/lightrag_ollama_demo.py

88 lines
2.3 KiB
Python
Raw Normal View History

import asyncio
2025-03-03 18:33:42 +08:00
import nest_asyncio
nest_asyncio.apply()
2024-10-16 15:15:10 +08:00
import os
import inspect
import logging
2024-10-16 15:15:10 +08:00
from lightrag import LightRAG, QueryParam
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
2024-10-16 15:15:10 +08:00
from lightrag.utils import EmbeddingFunc
2025-03-03 18:33:42 +08:00
from lightrag.kg.shared_storage import initialize_pipeline_status
2024-10-16 15:15:10 +08:00
WORKING_DIR = "./dickens"
2024-10-28 17:05:38 +02:00
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
2024-10-16 15:15:10 +08:00
if not os.path.exists(WORKING_DIR):
os.mkdir(WORKING_DIR)
2025-03-03 18:33:42 +08:00
async def initialize_rag():
rag = LightRAG(
working_dir=WORKING_DIR,
llm_model_func=ollama_model_complete,
llm_model_name="gemma2:2b",
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"
),
),
2025-03-03 18:33:42 +08:00
)
await rag.initialize_storages()
await initialize_pipeline_status()
return rag
2024-10-16 15:15:10 +08:00
2025-03-03 18:33:42 +08:00
async def print_stream(stream):
async for chunk in stream:
print(chunk, end="", flush=True)
2024-10-16 15:15:10 +08:00
2025-03-03 18:33:42 +08:00
def main():
# Initialize RAG instance
rag = asyncio.run(initialize_rag())
2024-10-16 15:15:10 +08:00
2025-03-03 18:33:42 +08:00
# Insert example text
with open("./book.txt", "r", encoding="utf-8") as f:
rag.insert(f.read())
2024-10-16 15:15:10 +08:00
2025-03-03 18:33:42 +08:00
# Test different query modes
print("\nNaive Search:")
print(
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
)
2024-10-16 15:15:10 +08:00
2025-03-03 18:33:42 +08:00
print("\nLocal Search:")
print(
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
)
2025-03-03 18:33:42 +08:00
print("\nGlobal Search:")
print(
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
)
2025-03-03 18:33:42 +08:00
print("\nHybrid Search:")
print(
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
)
2025-03-03 18:33:42 +08:00
# stream response
resp = rag.query(
"What are the top themes in this story?",
param=QueryParam(mode="hybrid", stream=True),
)
2025-03-03 18:33:42 +08:00
if inspect.isasyncgen(resp):
asyncio.run(print_stream(resp))
else:
print(resp)
2025-03-03 18:33:42 +08:00
if __name__ == "__main__":
main()