LlamaIndex connects large language models to private data through ingestion, indexing, retrieval, and query interfaces. It is one option for building retrieval-augmented generation (RAG) applications alongside Semantic Kernel and LangChain. section 10.3.3
For a PDF-backed application, use PDFReader and SimpleDirectoryReader to load local files, OpenAIEmbedding to configure embeddings, and VectorStoreIndex to build an index. The illustrated setup reads an API key from an environment variable and specifies a local storage directory. section 10.3.3
Listing 10.5 An example showing RAG with LlamaIndex
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
load_index_from_storage,
Settings
)
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.readers.file import PDFReader
PERSIST_DIR = "./storage/llamaindex"
DOG_BOOKS = "./data/dog_books/"
OPENAI_KEY = os.getenv('OPENAI_API_BOOK_KEY') #1
Settings.embed_model = OpenAIEmbedding(api_key=OPENAI_KEY) #1
def load_or_create_index():
if not os.path.exists(PERSIST_DIR): #2
try:
parser = PDFReader()
file_extractor = {".pdf": parser}
# load only PDFs
required_exts = [".pdf"] #3
documents = SimpleDirectoryReader(DOG_BOOKS,
file_extractor=file_extractor,
required_exts=required_exts).load_data()
index = VectorStoreIndex.from_documents( #4
documents, show_progress=True)
# store the index for later
index.storage_context.persist(persist_dir=PERSIST_DIR) #5
print("Index created and stored in", PERSIST_DIR)
except Exception as e:
print("Error while creating index:", e)
exit()
else:
print("Loading existing index from", PERSIST_DIR)
try:
# load the existing index
storage_context = StorageContext.from_defaults(
persist_dir=PERSIST_DIR)
index = load_index_from_storage(storage_context) #6
except Exception as e:
print("Error while loading index:", e)
exit()
return index
def main():
index = load_or_create_index()
query_engine = index.as_query_engine()
while True:
prompt = input("Ask a question about dogs:")
response = query_engine.query(prompt)
print(response)
if __name__ == "__main__":
main()
A Python RAG example showing the PDF reader, embedding configuration, index imports, and storage path. section 10.3.3



