Skip to main content
This example demonstrates how to build a basic RAG (Retrieval-Augmented Generation) application using LlamaIndex.TS.

Overview

RAG applications enhance LLM responses by retrieving relevant information from your documents before generating answers. This example shows:
  1. Loading documents
  2. Creating a vector index
  3. Querying with a query engine
  4. 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 indexed
  • VectorStoreIndex - Creates a vector store for semantic search
  • fs - Read files from the filesystem
  • createInterface - Interactive CLI input

2. Load Your Document

The Document class wraps your text content with optional metadata and an identifier.

3. Create Vector Index

This automatically:
  • Splits the document into chunks
  • Creates embeddings for each chunk
  • Stores them in a vector store

4. Create Query Engine

The query engine handles:
  • 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

  1. Install dependencies:
  1. Set your API key:
  1. 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