Skip to content

Commit 2e833a6

Browse files
authored
Merge pull request #718 from realpython/python-llamaindex
Sample code for the article on LlamaIndex
2 parents 0e25d33 + f8ffabb commit 2e833a6

File tree

11 files changed

+1953
-0
lines changed

11 files changed

+1953
-0
lines changed

python-llamaindex/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# LlamaIndex in Python: A RAG Guide With Examples
2+
3+
This folder provides the code examples for the Real Python tutorial [LlamaIndex in Python: A RAG Guide With Examples](https://realpython.com/llamaindex-examples/).

python-llamaindex/async_query.py

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

0 commit comments

Comments
 (0)