Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-llamaindex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# LlamaIndex in Python: A RAG Guide With Examples

This folder provides the code examples for the Real Python tutorial [LlamaIndex in Python: A RAG Guide With Examples](https://realpython.com/llamaindex-examples/).
55 changes: 55 additions & 0 deletions python-llamaindex/async_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import asyncio
from pathlib import Path

from llama_index.core import (
SimpleDirectoryReader,
StorageContext,
VectorStoreIndex,
load_index_from_storage,
)

# Define the storage directory
BASE_DIR = Path(__file__).resolve().parent
PERSIST_DIR = BASE_DIR / "storage"
DATA_FILE = BASE_DIR / "data" / "pep8.rst"


def get_index(persist_dir=PERSIST_DIR, data_file=DATA_FILE):
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function get_index lacks a docstring. Consider adding documentation that explains its purpose, parameters, and return value. For example:

def get_index(persist_dir=PERSIST_DIR, data_file=DATA_FILE):
    """Load or create a VectorStoreIndex.
    
    Args:
        persist_dir: Directory where the index is persisted. Defaults to PERSIST_DIR.
        data_file: Path to the data file to index. Defaults to DATA_FILE.
    
    Returns:
        VectorStoreIndex: The loaded or newly created index.
    """
Suggested change
def get_index(persist_dir=PERSIST_DIR, data_file=DATA_FILE):
def get_index(persist_dir=PERSIST_DIR, data_file=DATA_FILE):
"""
Load or create a VectorStoreIndex.
Args:
persist_dir (Path): Directory where the index is persisted. Defaults to PERSIST_DIR.
data_file (Path): Path to the data file to index. Defaults to DATA_FILE.
Returns:
VectorStoreIndex: The loaded or newly created index.
"""

Copilot uses AI. Check for mistakes.
if persist_dir.exists():
storage_context = StorageContext.from_defaults(
persist_dir=str(persist_dir),
)
index = load_index_from_storage(storage_context)
print("Index loaded from storage...")
else:
reader = SimpleDirectoryReader(input_files=[str(data_file)])
documents = reader.load_data()
index = VectorStoreIndex.from_documents(documents)
index.storage_context.persist(persist_dir=str(persist_dir))
print("Index created and persisted to storage...")

return index


async def main():
index = get_index()
query_engine = index.as_query_engine()

queries = [
"What is this document about?",
"Summarize the naming conventions in Python?",
]

# Run queries asynchronously
tasks = [query_engine.aquery(query) for query in queries]
responses = await asyncio.gather(*tasks)

# Print responses
for i, (query, response) in enumerate(zip(queries, responses), 1):
print(f"\nQuery {i}: {query}")
print(f"Response: {response}\n")
print("-" * 80)


if __name__ == "__main__":
asyncio.run(main())
Loading