DEV Community

Orbit Websites
Orbit Websites

Posted on

Orchestrating Complex RAG Migrations with Gemini CLI: A Step-by-Step Guide

Orchestrating Complex RAG Migrations with Gemini CLI: A Step-by-Step Guide

If you're working with Retrieval-Augmented Generation (RAG) systems and need to migrate or refactor them—especially across different vector databases, embedding models, or document pipelines—managing the complexity can quickly become overwhelming.

Enter Gemini CLI, Google’s powerful command-line tool that simplifies building, testing, and migrating AI-powered applications, including RAG systems. In this beginner-friendly, code-heavy guide, you’ll learn how to use Gemini CLI to orchestrate a full RAG migration—from an old Pinecone-backed system to a new one using Chroma and Google’s latest text embeddings.

By the end, you’ll have a working migrated RAG pipeline and understand how to scale this process.


✅ Prerequisites

Before we begin, ensure you have:

  • Python 3.9+
  • Gemini API key (free tier available)
  • pip install google-generativeai chromadb langchain sentence-transformers
  • Node.js & npm (for Gemini CLI)
  • npm install -g @google/generative-cli

Set your Gemini API key:

export GEMINI_API_KEY="your-api-key-here"
Enter fullscreen mode Exit fullscreen mode

🧩 Step 1: Understand Your Current RAG System

Let’s assume your legacy RAG uses:

  • Pinecone for vector storage
  • OpenAI embeddings (costly, vendor-locked)
  • Custom document loader

We want to migrate to:

  • Chroma DB (lightweight, open-source)
  • Google’s text-embedding-004 via Gemini
  • LangChain for orchestration

🛠️ Step 2: Initialize Gemini CLI Project

Use Gemini CLI to scaffold your migration project:

gemini init rag-migration --template rag
cd rag-migration
Enter fullscreen mode Exit fullscreen mode

This creates:

rag-migration/
├── config.yaml
├── data/
├── scripts/
│   └── migrate.py
├── app.py
Enter fullscreen mode Exit fullscreen mode

Edit config.yaml to define your migration targets:

migration:
  source:
    vectorstore: pinecone
    embeddings: openai/text-embedding-ada-002
  target:
    vectorstore: chroma
    embeddings: google/text-embedding-004
    model: gemini-pro
  data_path: ./data/migration_source.json
Enter fullscreen mode Exit fullscreen mode

📥 Step 3: Export Data from Pinecone

First, export your existing vectors and metadata. Install Pinecone:

pip install pinecone-client
Enter fullscreen mode Exit fullscreen mode

Create scripts/export_pinecone.py:

import pinecone
import json

# Initialize Pinecone
pinecone.init(api_key="your-pinecone-key", environment="us-west1-gcp")

index = pinecone.Index("legacy-rag-index")
result = index.query(vector=[0] * 1536, top_k=1000, include_values=True, include_metadata=True)

# Save to JSON
with open("data/migration_source.json", "w") as f:
    json.dump(result["matches"], f, indent=2)

print("✅ Exported 1000 records from Pinecone")
Enter fullscreen mode Exit fullscreen mode

Run it:

python scripts/export_pinecone.py
Enter fullscreen mode Exit fullscreen mode

🔁 Step 4: Migrate Embeddings Using Gemini

Now, re-embed your text using Google’s text-embedding-004 via Gemini.

Install Google’s SDK:

pip install google-generativeai
Enter fullscreen mode Exit fullscreen mode

Create scripts/convert_embeddings.py:

import google.generativeai as genai
import json
from tqdm import tqdm

genai.configure(api_key="your-gemini-api-key")

# Load exported data
with open("data/migration_source.json", "r") as f:
    old_data = json.load(f)

# Configure embedding model
model = "models/text-embedding-004"
migrated_data = []

for item in tqdm(old_data, desc="Re-embedding with Gemini"):
    text = item["metadata"]["text"]
    response = genai.embed_content(
        model=model,
        content=text,
        task_type="retrieval_document"
    )
    embedding = response["embedding"]

    migrated_data.append({
        "id": item["id"],
        "embedding": embedding,
        "metadata": item["metadata"],
        "document": text
    })

# Save new format
with open("data/migrated_chroma.json", "w") as f:
    json.dump(migrated_data, f, indent=2)

print("✅ Re-embedded using Gemini")
Enter fullscreen mode Exit fullscreen mode

💡 Gemini’s text-embedding-004 supports up to 2048 tokens and is optimized for retrieval tasks.


🗃️ Step 5: Load into Chroma DB

Now, ingest the re-embedded data into Chroma.

Create scripts/load_chroma.py:

import chromadb
import json

# Load migrated data
with open("data/migrated_chroma.json", "r") as f:
    data = json.load(f)

# Initialize Chroma
client = chromadb.PersistentClient(path="./chroma_db")
collection = client.create_collection("migrated_rag")

# Add to Chroma
for item in data:
    collection.add(
        ids=item["id"],
        embeddings=item["embedding"],
        documents=item["document"],
        metadatas=item["metadata"]
    )

print("✅ Loaded data into Chroma DB at ./chroma_db")
Enter fullscreen mode Exit fullscreen mode

Run:


bash
python scripts

---

☕ **Factual**
Enter fullscreen mode Exit fullscreen mode

Top comments (0)