|
| 1 | +import asyncio |
| 2 | +import inspect |
| 3 | +import os |
| 4 | + |
| 5 | +# Uncomment these lines below to filter out somewhat verbose INFO level |
| 6 | +# logging prints (the default loglevel is INFO). |
| 7 | +# This has to go before the lightrag imports to work, |
| 8 | +# which triggers linting errors, so we keep it commented out: |
| 9 | +# import logging |
| 10 | +# logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.WARN) |
| 11 | + |
| 12 | +from lightrag import LightRAG, QueryParam |
| 13 | +from lightrag.llm import ollama_embedding, ollama_model_complete |
| 14 | +from lightrag.utils import EmbeddingFunc |
| 15 | + |
| 16 | +WORKING_DIR = "./dickens_gremlin" |
| 17 | + |
| 18 | +if not os.path.exists(WORKING_DIR): |
| 19 | + os.mkdir(WORKING_DIR) |
| 20 | + |
| 21 | +# Gremlin |
| 22 | +os.environ["GREMLIN_HOST"] = "localhost" |
| 23 | +os.environ["GREMLIN_PORT"] = "8182" |
| 24 | +os.environ["GREMLIN_GRAPH"] = "dickens" |
| 25 | + |
| 26 | +# Creating a non-default source requires manual |
| 27 | +# configuration and a restart on the server: use the dafault "g" |
| 28 | +os.environ["GREMLIN_TRAVERSE_SOURCE"] = "g" |
| 29 | + |
| 30 | +# No authorization by default on docker tinkerpop/gremlin-server |
| 31 | +os.environ["GREMLIN_USER"] = "" |
| 32 | +os.environ["GREMLIN_PASSWORD"] = "" |
| 33 | + |
| 34 | +rag = LightRAG( |
| 35 | + working_dir=WORKING_DIR, |
| 36 | + llm_model_func=ollama_model_complete, |
| 37 | + llm_model_name="llama3.1:8b", |
| 38 | + llm_model_max_async=4, |
| 39 | + llm_model_max_token_size=32768, |
| 40 | + llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 32768}}, |
| 41 | + embedding_func=EmbeddingFunc( |
| 42 | + embedding_dim=768, |
| 43 | + max_token_size=8192, |
| 44 | + func=lambda texts: ollama_embedding( |
| 45 | + texts, embed_model="nomic-embed-text", host="http://localhost:11434" |
| 46 | + ), |
| 47 | + ), |
| 48 | + graph_storage="GremlinStorage", |
| 49 | +) |
| 50 | + |
| 51 | +with open("./book.txt", "r", encoding="utf-8") as f: |
| 52 | + rag.insert(f.read()) |
| 53 | + |
| 54 | +# Perform naive search |
| 55 | +print( |
| 56 | + rag.query("What are the top themes in this story?", param=QueryParam(mode="naive")) |
| 57 | +) |
| 58 | + |
| 59 | +# Perform local search |
| 60 | +print( |
| 61 | + rag.query("What are the top themes in this story?", param=QueryParam(mode="local")) |
| 62 | +) |
| 63 | + |
| 64 | +# Perform global search |
| 65 | +print( |
| 66 | + rag.query("What are the top themes in this story?", param=QueryParam(mode="global")) |
| 67 | +) |
| 68 | + |
| 69 | +# Perform hybrid search |
| 70 | +print( |
| 71 | + rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid")) |
| 72 | +) |
| 73 | + |
| 74 | +# stream response |
| 75 | +resp = rag.query( |
| 76 | + "What are the top themes in this story?", |
| 77 | + param=QueryParam(mode="hybrid", stream=True), |
| 78 | +) |
| 79 | + |
| 80 | + |
| 81 | +async def print_stream(stream): |
| 82 | + async for chunk in stream: |
| 83 | + print(chunk, end="", flush=True) |
| 84 | + |
| 85 | + |
| 86 | +if inspect.isasyncgen(resp): |
| 87 | + asyncio.run(print_stream(resp)) |
| 88 | +else: |
| 89 | + print(resp) |
0 commit comments