Skip to main content
Retrieval-Augmented Generation (RAG) extends a model’s knowledge by finding and injecting relevant documents into the prompt at query time. Instead of relying solely on what was in the model’s training data, your application can search a private document corpus—product docs, support tickets, internal wikis—and pass the most relevant excerpts to the model. The typical RAG pipeline has two phases:
  1. Indexing — Chunk documents, embed them into vectors, and store them in a vector database.
  2. Retrieval + Generation — Embed the user’s query, retrieve the most similar documents, and include them in the prompt.
Genkit provides three primitives that map directly onto this pipeline: indexers, embedders, and retrievers.

The three RAG primitives

Embedders

An embedder converts text (or other content) into a numeric vector for similarity search.

Indexers

An indexer takes documents and stores them in a vector database.

Retrievers

A retriever takes a query and returns the most relevant documents.

Indexing documents

Use ai.index() to store documents via any registered indexer:

Retrieving documents

Use ai.retrieve() to fetch relevant documents at query time:

End-to-end RAG flow

Here is a complete example that combines indexing and retrieval into a working Q&A application:
Alternatively, pass retrieved docs directly via the docs option on generate() and let Genkit format them for you:

Plugin-provided vector stores

In production you will use a plugin-provided indexer and retriever rather than hand-rolling one. Genkit plugins are available for the most popular vector stores:

Example: local dev vector store

Example: Firebase Firestore vector store

See Firebase plugin and Vertex AI plugin for full configuration options.

Defining a simple retriever

If you already have data in a database and just need to map query results to Document objects, defineSimpleRetriever is a convenient shorthand:

Next steps

Firebase Plugin

Firestore-backed vector store and Firebase deployment.

Vertex AI Plugin

Vertex AI Vector Search and Gemini embeddings.

Flows

Wrap RAG logic in traced, deployable flows.

Evaluation

Measure RAG pipeline quality with built-in evaluators.