Overview
RAG applications enhance LLM responses by retrieving relevant information from your documents before generating answers. This example shows:- Loading documents
- Creating a vector index
- Querying with a query engine
- Interactive question-answering
Complete Example
Here’s a complete working RAG application:rag-starter.ts
Step-by-Step Explanation
1. Import Dependencies
Document- Represents a document to be indexedVectorStoreIndex- Creates a vector store for semantic searchfs- Read files from the filesystemcreateInterface- Interactive CLI input
2. Load Your Document
Document class wraps your text content with optional metadata and an identifier.
3. Create Vector Index
- Splits the document into chunks
- Creates embeddings for each chunk
- Stores them in a vector store
4. Create Query Engine
- Embedding your query
- Retrieving relevant chunks
- Generating a response with the LLM
5. Query Your Data
Advanced: With Source Attribution
Get source references with your responses:Custom Settings
Configure LLM and embedding models:Running the Example
- Install dependencies:
- Set your API key:
- Run the example:
Try It Yourself
Modify the example to:- Load your own documents
- Use different LLM providers (Anthropic, Groq, etc.)
- Customize chunking strategies
- Add metadata filtering
- Implement streaming responses
Next Steps
Chat Engine
Build conversational interfaces with chat history
Vector Stores
Use production vector databases like Pinecone, Qdrant, or Weaviate
Document Loading
Load PDFs, web pages, and other document types
Advanced RAG
Implement advanced retrieval patterns
Related Examples
- Chat Engine - Conversational RAG
- Metadata Filtering - Filter by metadata
- Sentence Window - Advanced chunking