Skip to main content

Chapter 2: LlamaIndex Framework

LlamaIndex Overview​

LlamaIndex (formerly GPT Index) focuses on data indexing and querying:

  • Multiple index types
  • Query engines
  • Response synthesis
  • Evaluation tools

Basic RAG with LlamaIndex​

from llama_index import VectorStoreIndex, SimpleDirectoryReader
from llama_index.llms import OpenAI

# Load documents
documents = SimpleDirectoryReader("./data").load_data()

# Create index
index = VectorStoreIndex.from_documents(documents)

# Query
query_engine = index.as_query_engine()
response = query_engine.query("What is RAG?")
print(response)

Index Types​

from llama_index import (
VectorStoreIndex,
SummaryIndex,
TreeIndex,
KeywordTableIndex
)

# Vector Index - similarity search
vector_index = VectorStoreIndex.from_documents(docs)

# Summary Index - summarization
summary_index = SummaryIndex.from_documents(docs)

# Tree Index - hierarchical
tree_index = TreeIndex.from_documents(docs)

# Keyword Index - keyword extraction
keyword_index = KeywordTableIndex.from_documents(docs)

Query Engines​

# Basic query engine
query_engine = index.as_query_engine(
similarity_top_k=5,
response_mode="compact" # or "tree_summarize", "refine"
)

# Chat engine with memory
chat_engine = index.as_chat_engine(
chat_mode="condense_question",
verbose=True
)

# Use chat engine
response = chat_engine.chat("What is RAG?")
response = chat_engine.chat("Tell me more") # Remembers context

Response Modes​

ModeDescriptionBest For
compactFit into single promptDefault
refineIterative refinementLong contexts
tree_summarizeHierarchical summaryMany docs
simple_summarizeBasic summaryQuick overview

LangChain vs LlamaIndex​

FeatureLangChainLlamaIndex
FocusGeneral chainsData indexing
FlexibilityHigherMedium
Learning CurveSteeperGentler
Best ForComplex pipelinesDocument Q&A

Next Steps​

Continue to Chapter 3: Custom RAG.