|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from llama_index.core import ( |
| 6 | + SimpleDirectoryReader, |
| 7 | + StorageContext, |
| 8 | + VectorStoreIndex, |
| 9 | + load_index_from_storage, |
| 10 | +) |
| 11 | + |
| 12 | +# Disable logging messages |
| 13 | +logging.getLogger("llama_index").setLevel(logging.WARNING) |
| 14 | +logging.getLogger("httpx").setLevel(logging.WARNING) |
| 15 | + |
| 16 | +# Define the storage directory |
| 17 | +PERSIST_DIR = "./storage" |
| 18 | + |
| 19 | + |
| 20 | +def get_index(persist_dir=PERSIST_DIR): |
| 21 | + if Path(persist_dir).exists(): |
| 22 | + storage_context = StorageContext.from_defaults(persist_dir=persist_dir) |
| 23 | + index = load_index_from_storage(storage_context) |
| 24 | + print("Index loaded from storage...") |
| 25 | + else: |
| 26 | + reader = SimpleDirectoryReader(input_files=["./data/pep8.rst"]) |
| 27 | + documents = reader.load_data() |
| 28 | + index = VectorStoreIndex.from_documents(documents) |
| 29 | + index.storage_context.persist(persist_dir=persist_dir) |
| 30 | + print("Index created and persisted to storage...") |
| 31 | + |
| 32 | + return index |
| 33 | + |
| 34 | + |
| 35 | +async def main(): |
| 36 | + index = get_index() |
| 37 | + query_engine = index.as_query_engine() |
| 38 | + |
| 39 | + queries = [ |
| 40 | + "What is this document about?", |
| 41 | + "Summarize the naming conventions in Python?", |
| 42 | + ] |
| 43 | + |
| 44 | + # Run queries asynchronously |
| 45 | + tasks = [query_engine.aquery(query) for query in queries] |
| 46 | + responses = await asyncio.gather(*tasks) |
| 47 | + |
| 48 | + # Print responses |
| 49 | + for i, (query, response) in enumerate(zip(queries, responses), 1): |
| 50 | + print(f"\nQuery {i}: {query}") |
| 51 | + print(f"Response: {response}\n") |
| 52 | + print("-" * 80) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + asyncio.run(main()) |
0 commit comments