Article
ragllamaindexpythondata-frameworkllm-applicationquery-engineopenai
Build Your First RAG App with LlamaIndex
LlamaIndex is a data framework for LLM apps. This guide shows how to build a basic Retrieval-Augmented Generation (RAG) pipeline to chat with your own documents in just a few lines of Python.
beginner15 min5 steps
The play
- Install LlamaIndex & Set API KeyInstall the necessary Python packages for LlamaIndex and its OpenAI integration. You will also need to set your OpenAI API key as an environment variable for authentication.
- Prepare and Load DataLlamaIndex needs data to work with. Create a directory named 'data' and place a text file inside. Then, use the `SimpleDirectoryReader` to load all documents from that directory.
- Create a Vector IndexAn index is a data structure that allows for efficient querying. The `VectorStoreIndex` creates vector embeddings of your document text, enabling semantic search. This step converts your documents into a queryable format.
- Create a Query EngineA query engine is the primary interface for asking questions over your data. The `as_query_engine()` method builds a simple RAG pipeline that retrieves relevant context from the index and passes it to an LLM to generate an answer.
- Query Your DataUse the query engine to ask a question. LlamaIndex will find relevant text chunks from your documents, feed them to the LLM as context, and return a synthesized answer based on your data, not the LLM's general knowledge.
Starter code
import os
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# --- 1. Setup & API Key ---
# Ensure your OPENAI_API_KEY is set in your environment variables
if "OPENAI_API_KEY" not in os.environ:
raise EnvironmentError("OPENAI_API_KEY environment variable not set.")
# --- 2. Prepare Data ---
# Create a 'data' directory if it doesn't exist
if not os.path.exists("data"):
os.makedirs("data")
# Create a dummy text file with some content
file_path = "data/my_story.txt"
with open(file_path, "w") as f:
f.write("Paul Graham is a computer scientist and entrepreneur. ")
f.write("In 1995, he and Robert Morris founded Viaweb, the first software as a service company. ")
f.write("Viaweb was acquired by Yahoo in 1998. In 2005, he co-founded the seed capital firm Y Combinator.")
print("Created dummy data file: data/my_story.txt")
# --- 3. Load Data ---
# Load the documents from the 'data' directory
documents = SimpleDirectoryReader("data").load_data()
print(f"Loaded {len(documents)} document(s).")
# --- 4. Index Data ---
# Create a VectorStoreIndex from the documents
# This will create vector embeddings and store them in-memory by default
print("Indexing documents...")
index = VectorStoreIndex.from_documents(documents)
# --- 5. Create Query Engine ---
# The query engine is the main interface for asking questions
query_engine = index.as_query_engine()
print("Query engine created.")
# --- 6. Query Data ---
# Ask a question about the content of the documents
query_text = "What did Paul Graham co-found in 2005?"
print(f"\nQuerying: '{query_text}'")
response = query_engine.query(query_text)
print("\nResponse:")
print(response)
# To run this script:
# 1. pip install llama-index llama-index-llms-openai
# 2. export OPENAI_API_KEY='your-openai-api-key'
# 3. python your_script_name.py