Sub topic 9
LlamaIndex: data-centric retrieval
This chapter assumes you can write basic Python, have called an LLM chat API, and know roughly what embeddings and retrieval-augmented generation (RAG) are. Earlier chapters built a RAG pipeline by hand. Here you see what LlamaIndex does with the same job.
The design focus
LlamaIndex centers on your data. Its core idea is that most LLM applications need to load documents, split them, embed them, store the vectors, and fetch relevant pieces at question time. LlamaIndex gives each step a named component and a sensible default.
Compare this with LangChain, which focuses on composing many kinds of components. LlamaIndex goes deeper on ingestion, indexing, and querying.
The pipeline in five lines
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
print(query_engine.query("What does the onboarding guide say about access requests?"))The default settings use OpenAI models, so you need an OPENAI_API_KEY in your environment. You also need a data folder containing files such as PDFs, text, or Markdown.
What each line hides
Each line replaces code you would write by hand in a raw SDK pipeline.
| Line | What it does for you |
|---|---|
| `SimpleDirectoryReader(...).load_data()` | Reads files, detects formats, and returns `Document` objects with metadata |
| `VectorStoreIndex.from_documents(...)` | Splits documents into chunks (called nodes), calls the embedding model, and stores the vectors in memory |
| `index.as_query_engine()` | Builds a retriever and a response synthesizer |
| `query_engine.query(...)` | Embeds the question, finds the top matching chunks, builds a prompt, and calls the chat model |
Changing the defaults
The defaults are a starting point. You can change each stage without leaving the framework. The Settings object sets the models and chunk size for the whole program.
from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex
from llama_index.core.node_parser import SentenceSplitter
from llama_index.llms.openai import OpenAI
Settings.llm = OpenAI(model="gpt-4o-mini")
Settings.node_parser = SentenceSplitter(chunk_size=512, chunk_overlap=50)
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine(similarity_top_k=3)
response = query_engine.query("Who approves access requests?")
print(response)
for source in response.source_nodes:
print(source.score, source.node.metadata.get("file_name"))The source_nodes list is useful for debugging. It shows which chunks were retrieved and how well they scored, so you can tell whether a bad answer came from bad retrieval or from the model.
Costs and common errors
- Hidden calls. One
from_documentscall may send many embedding requests. Large folders can cost money and take time. - In-memory index. The default index disappears when the program ends. Use a persistent vector store, or call
index.storage_context.persist(), to keep it. - Silent OpenAI default. If you use another provider for chat but forget to set the embedding model, LlamaIndex still tries OpenAI and fails without a key.
- Chunk settings matter. Chunks that are too large dilute relevant text. Chunks that are too small lose context.
When it fits
LlamaIndex fits best when your main problem is getting a large, varied set of documents into a searchable form. If you only need one prompt and one reply, or tight control over every request, the raw SDK is simpler.
Exercise
A LlamaIndex query returns a wrong answer. Which attribute of the response do you check first to decide whether retrieval or the model caused the problem, and what do you look for?
Show answer
Check response.source_nodes. Look at the retrieved chunks and their scores. If the correct text is missing from them, retrieval failed, so adjust chunk size, similarity_top_k, or the documents. If the correct text is present but the answer is still wrong, the model or prompt is the cause.