Building a RAG Pipeline with Genkit and Firebase

Retrieval-Augmented Generation (RAG) combines the power of large language models (LLMs) with real-time, relevant data retrieval. Instead of relying only on a model’s trained knowledge, RAG dynamically pulls information from a database or vector store to ground its answers in fact. Today, we’ll walk through how to build a simple but powerful RAG pipeline using Genkit and Firebase.
Why RAG?
Large Language Models (LLMs) like GPT-4 can produce fluent text but are limited to their training data. RAG solves this by injecting retrieved knowledge into the generation loop:
- Query Understanding – Parse the user’s question.
- Retrieve Context – Search a vector store or database for relevant data.
- Augment Prompt – Combine the original query with retrieved context.
- Generate Answer – Let the LLM produce an informed response.
What is Genkit?
Genkit is a framework for building LLM-powered applications with batteries included. It provides a clean structure for defining workflows, chaining operations, and integrating retrieval, generation, and evaluation — all with TypeScript.
What You’ll Need
- Node.js (v18+)
- Firebase project (Firestore + Functions)
- Genkit CLI
- A vector database (we’ll use Firestore for retrieval simplicity, or you can plug in Weaviate/Chroma)
- OpenAI API key or any supported model
Step 1: Set Up Your Project
npx create-genkit-app my-rag-app cd my-rag-app npm install
Install Firebase support:
npm install firebase-admin
Step 2: Store Knowledge in Firebase
Create a Firestore collection called documents with fields:
- content: the text content
- embedding: vector (optional, depending on your retriever setup)
You can also use an embedding model to preprocess the content and store vector embeddings for similarity search later.
Step 3: Configure Firebase Admin SDK
In your genkit.config.ts:
import * as admin from 'firebase-admin';
admin.initializeApp();
const db = admin.firestore();
Step 4: Build the RAG Pipeline
import { pipeline } from '@genkit-ai/core';
import { generate } from '@genkit-ai/ai';
import { z } from 'zod';
import * as admin from 'firebase-admin';
const db = admin.firestore();
export const ragPipeline = pipeline({
name: 'ragPipeline',
inputSchema: z.object({ query: z.string() }),
outputSchema: z.string(),
run: async ({ query }) => {
// Step 1: Retrieve documents from Firestore
const snapshot = await db.collection('documents').limit(5).get();
const docs = snapshot.docs.map(doc => doc.data().content);
const context = docs.join('\n---\n');
// Step 2: Generate response with context
const result = await generate({
model: 'openai:gpt-4',
prompt: `You are an expert assistant. Use the following context to answer the question:\n\n${context}\n\nQuestion: ${query}`,
});
return result.text;
},
});
Step 5: Test Locally
Run the local dev server:
genkit dev
Try the pipeline from the playground with:
{ "query": "What is Genkit and how does it work?" }
Optional: Add Embedding + Vector Retrieval
To level up:
Embed all documents using generateEmbedding().
Store vectors in a vector DB like Weaviate or Pinecone.
Use similarity search to find relevant documents at runtime.
Step 6: Deploy to Firebase
Use Firebase Functions to expose your pipeline as an endpoint:
import { onRequest } from "firebase-functions/v2/https";
import { ragPipeline } from "./pipelines/rag";
export const ragEndpoint = onRequest(async (req, res) => {
const { query } = req.body;
const response = await ragPipeline.invoke({ query });
res.send({ response });
});
Then deploy:
firebase deploy --only functions
Conclusion
With Genkit and Firebase, you can ship production-ready RAG pipelines quickly. Whether you’re powering a chatbot, a support assistant, or a search experience, this stack gives you flexibility, scalability, and developer speed.
Ready to experience Sluqe?
Transform the way you capture, search, and recall your ideas. Try Sluqe for free and see how effortless note-taking and knowledge management can be.