LightRAG/examples/lightrag_hf_demo.py

44 lines
1.3 KiB
Python
Raw Normal View History

2024-10-15 19:40:08 +08:00
import os
import sys
from lightrag import LightRAG, QueryParam
from lightrag.llm import hf_model_complete, hf_embedding
2024-10-15 21:11:12 +08:00
from lightrag.utils import EmbeddingFunc
2024-10-15 19:40:08 +08:00
from transformers import AutoModel,AutoTokenizer
WORKING_DIR = "./dickens"
if not os.path.exists(WORKING_DIR):
os.mkdir(WORKING_DIR)
rag = LightRAG(
working_dir=WORKING_DIR,
llm_model_func=hf_model_complete,
llm_model_name='meta-llama/Llama-3.1-8B-Instruct',
2024-10-15 21:11:12 +08:00
embedding_func=EmbeddingFunc(
embedding_dim=384,
max_token_size=5000,
2024-10-15 21:21:57 +08:00
func=lambda texts: hf_embedding(
texts,
tokenizer=AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2"),
embed_model=AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
)
2024-10-15 21:11:12 +08:00
),
2024-10-15 19:40:08 +08:00
)
with open("./book.txt") 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")))