Skip to content

Repository files navigation

Mini RAG

A Retrieval-Augmented Generation (RAG) application built with Next.js, using:

  • Google Cloud Storage (GCS) for file storage
  • Upstash Vector for vector database
  • Google Gemini for embeddings and LLM (free tier)
  • Cohere for reranking (free tier)

Architecture

User Input (Text/PDF) 
    → Upload to GCS 
    → Chunk text 
    → Generate Embeddings (Gemini) 
    → Store in Upstash Vector

User Query 
    → Embed query (Gemini)
    → Retrieve top-k from Upstash 
    → Rerank with Cohere 
    → Generate answer with citations (Gemini)

Setup

1. Install Dependencies

npm install

2. Get API Keys (All Free Tiers)

Google Gemini API Key

  1. Go to Google AI Studio
  2. Create a new API key
  3. Copy the key

Upstash Vector

  1. Go to Upstash Console
  2. Create a new Vector Index
  3. Important: Set dimensions to 768 (Gemini embedding size)
  4. Copy the REST URL and REST Token

Cohere API Key

  1. Go to Cohere Dashboard
  2. Create a free API key
  3. Copy the key

Google Cloud Storage

  1. Go to GCP Console
  2. Create a bucket (or use existing: process-venue-assignment)
  3. For authentication, run in terminal:
    gcloud auth application-default login
    OR download a service account JSON key and set path in env.

3. Create .env.local

# Gemini (Free)
GEMINI_API_KEY=your_gemini_api_key

# Upstash Vector (Free tier)
UPSTASH_VECTOR_REST_URL=https://your-index.upstash.io
UPSTASH_VECTOR_REST_TOKEN=your_token

# Google Cloud Storage
GCS_BUCKET_NAME=process-venue-assignment
# Optional: only if not using gcloud auth
# GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json

# Cohere (Free tier)
COHERE_API_KEY=your_cohere_key

4. Run Development Server

npm run dev

Open http://localhost:3000

API Endpoints

POST /api/ingest

Ingest documents into the knowledge base.

Request (FormData):

  • file: PDF or text file
  • text: Raw text input

Response:

{ "success": true, "chunks": 5 }

POST /api/chat

Query the knowledge base.

Request:

{ "message": "What is..." }

Response:

{
  "answer": "Based on the documents, ... [1]",
  "citations": [
    { "text": "...", "source": "document.pdf" }
  ]
}

Index Configuration

When creating your Upstash Vector index:

  • Dimensions: 768 (required for Gemini text-embedding-004)
  • Metric: cosine (recommended)

Remarks

Limits & Trade-offs

  • Gemini free tier: 60 requests/minute for embeddings
  • Upstash free tier: 10K vectors, 10K queries/day
  • Cohere free tier: 100 requests/minute

What I'd do next

  • Add streaming responses for better UX
  • Implement batch embedding for large documents
  • Add document management (list, delete)
  • Add session history for multi-turn conversations

About

assignment

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages