liveBook · Manning

LlamaIndex

As explained in Generative AI in Action

LlamaIndex is a data framework that connects large language models with private data sources through ingestion, indexing, retrieval, and query interfaces.

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

How Generative AI in Action covers it

How deep it goes
Shows implementation setup for PDF-based RAG but does not develop LlamaIndex internals.
Examples
Uses a Python PDF RAG listing with PDFReader, SimpleDirectoryReader, OpenAIEmbedding, and a local storage path.
What it assumes
Assumes familiarity with Python and LLM applications; introduces the setup through concrete imports and configuration.
What you can do after
Apply it via LlamaIndex to begin configuring a PDF-backed RAG index with OpenAI embeddings.
How it connects
Relates LlamaIndex to RAG, embeddings, vector indexes, Semantic Kernel, and LangChain.

Also explained in

Compare how 4 books explain LlamaIndex →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.