Skip to content

Latest commit

 

History

History
402 lines (268 loc) · 18 KB

mongodb-setup.md

File metadata and controls

402 lines (268 loc) · 18 KB

MongoDB Setup

MongoDB Elasticsearch Redis Docker Voyage AI sentence-transformers SIE

Essential, minimal steps to run the example sweep commands on Atlas Cloud or Atlas Local (Docker). Official vendor docs are linked; details you can skip are marked optional.

Naming note: MongoDB examples live under configs/mongodb/. example-local.yaml means local embedding models (sentence-transformers, 384-dim) — not local MongoDB. Any MongoDB example works on Atlas Cloud or Atlas Local; only MONGODB_ATLAS_CLOUD_URI / MONGODB_ATLAS_LOCAL_URI (or ./start-services.sh --mongodb-local) selects the database. Postgres/pgvector twins (local Docker or Supabase-hosted) live under configs/supabase/ — same YAML keys, runtime STORAGE_BACKEND=postgres — see Postgres Setup.


Choose your MongoDB backend

Feature Atlas Local (Docker) Atlas Cloud (M0)
$vectorSearch identical syntax identical syntax
$search (BM25) identical syntax identical syntax
Index creation Automatic on boot Manual via Atlas UI (M0/M2/M5)
Storage limit Local disk only 512 MB (M0 free tier)
Atlas Admin API not available optional (dashboard quota bar)
Cluster tier/region not shown in dashboard shown when Admin API configured

Path A — Atlas Cloud (M0) — free cloud cluster, manual indexes on M0.

Path B — Atlas Local (Docker) — no cloud account, indexes created automatically.


Path A — Atlas Cloud (M0)

Atlas stores chunks, embeddings, and experiment results. Free M0 is enough.

1. Create an account

Register at cloud.mongodb.com (email, Google, or GitHub).

→ Create an Atlas Account

2. Deploy a free cluster

Atlas UI → Create → M0 (Free) → pick region → Create.

→ Deploy a Free Tier Cluster

3. Create a database user

Database Access → Add New Database User → password auth → Read and write to any database. Save username and password.

→ Create a Database User

4. Allow network access

Network Access → Add IP Address → Add Current IP Address (or 0.0.0.0/0 for local dev only).

→ Configure IP Access List

5. Set MONGODB_ATLAS_CLOUD_URI or MONGODB_ATLAS_LOCAL_URI

Database → Connect → Drivers → copy SRV string → replace <password> → set database to rag_params_finder:

mongodb+srv://<user>:<password>@<cluster>.mongodb.net/rag_params_finder?retryWrites=true&w=majority

Paste into .env:

MONGODB_ATLAS_CLOUD_URI=mongodb+srv://...

→ Connect to Your Cluster

6. Create search indexes (M0 — required before sweep)

On M0/M2/M5, indexes must be created in the Atlas UI before the sweep reaches the QUERYING phase. Both example configs need vector + text indexes.

6a. Create the chunks collection (if it does not exist):

Atlas UI → Browse Collections → database rag_params_finder → Create Collection → name: chunks.

6b. Create indexes on chunks → Search Indexes → Create Search Index → JSON Editor:

Sweep Vector index name numDimensions
mongodb/example-local.yaml vector_index_384 384
mongodb/example-voyage.yaml vector_index_1024 1024
mongodb/example-sie.yaml vector_index_1024, vector_index_30522 1024, 30522
Both (same cluster) create both 384 and 1024

Vector index JSON (set numDimensions and name as above):

{
  "fields": [
    { "type": "vector", "path": "embedding", "numDimensions": 384, "similarity": "cosine" },
    { "type": "filter", "path": "experiment_id" },
    { "type": "filter", "path": "embedding_model" },
    { "type": "filter", "path": "chunking_method" },
    { "type": "filter", "path": "chunk_size" },
    { "type": "filter", "path": "overlap" }
  ]
}

Text index (required — both sweeps use sparse/hybrid), name: text_search_index:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "text": [{ "type": "string" }],
      "experiment_id": [{ "type": "token" }],
      "embedding_model": [{ "type": "token" }]
    }
  }
}

Wait until each index shows ACTIVE (~1–2 min).

→ How to Index Fields for Vector Search

M10+ paid clusters: skip manual creation — the server creates indexes on startup (check uvicorn logs).

Quota check: M0 allows 3 search indexes cluster-wide. Before your first sweep:

rag-params-finder indexes list    # count vs limit; known vs unknown
rag-params-finder indexes reset   # drop stray indexes + ensure required

The server preflights required indexes on experiment submit — missing indexes or exhausted quota returns HTTP 422 before embedding starts (see Troubleshooting → Search index preflight failed).


Path B — Atlas Local (Docker)

MongoDB Atlas Local

Run the full RAG pipeline — including $vectorSearch and $search (BM25) — on your laptop using the official mongodb/mongodb-atlas-local:8.3.3 Docker image (pinned in docker-compose.yml / CI — not :latest). No Atlas cloud account, no 512 MB storage ceiling, no manual UI index creation.

Image pin vs existing volumes: Pinning an older major/minor than a volume's featureCompatibilityVersion (e.g. FCV 8.3 with image 8.0.x) makes mongod exit immediately (Wrong mongod version / exit 62) and Compose waits forever on health. Fix: ./start-services.sh mongodb reset then ./start-services.sh --mongodb-local. After changing the compose image tag, recreate containers (docker compose --profile mongodb-local down then start again).

Replica set name: Atlas Local uses the container hostname as the replica set name, and that name has to stay the same for the life of the data volume. The healthcheck waits for a writable primary. Logs that mention NodeNotFound, RSGhost, or a set name that differs from the hostname mean the volume was initialized under another hostname. Set MONGODB_LOCAL_HOSTNAME to that replica set name and recreate the container. Do not reset the volume.

Prerequisites: Docker Desktop running; project dependencies installed (uv venv && source .venv/bin/activate && uv pip install -e ".[dev]").

Verify Docker is ready before continuing. Docker Desktop can appear open in the dock while its daemon is still starting (or has crashed). Run:

docker info 2>&1 | head -3

You should see Server Version: .... If you see Cannot connect to the Docker daemon, quit and relaunch Docker Desktop, wait ~20 s for the menu-bar icon to show "Running", then re-run docker info to confirm before proceeding.

Quick start (full stack)

./start-services.sh --mongodb-local
  • MongoDB Atlas Local: localhost:27017
  • Server: http://localhost:8001
  • Dashboard: http://localhost:5374

The server connects to mongodb-local automatically. All vector and text search indexes are created programmatically on first boot (~30–60 s to reach READY).

Run a sweep from the host

export MONGODB_ATLAS_LOCAL_URI="mongodb://localhost:27017/rag_params_finder?directConnection=true"
rag-params-finder run --config configs/mongodb/example-local.yaml

Open http://localhost:5374 to watch progress.

Operational checks (required)

Use these separate flags as a runbook for local service validation:

  • HEALTH_LIVENESS_LOCAL (start-up level): curl -sS http://127.0.0.1:8001/health
  • READINESS_DATA_PLANE (pre-sweep readiness): curl -sS http://127.0.0.1:8001/experiments

/health can return success while /experiments still fails; run both before judging the local stack operational.

Native dev (MongoDB in Docker, server/frontend on host)

# Terminal 1 — MongoDB only
./start-services.sh mongodb start

# Terminal 2 — server
export MONGODB_ATLAS_LOCAL_URI="mongodb://localhost:27017/rag_params_finder?directConnection=true"
uvicorn server.main:app --reload --port 8001

# Terminal 3 — frontend
cd frontend && npm run dev

Wait for MongoDB to be healthy before starting uvicorn. The Atlas Local container takes 30–60 s on first boot to initialise its replica set and create indexes. Starting uvicorn while it is still starting produces Connection reset by peer warnings in the server log — those are non-fatal, but the cleanest approach is to wait:

until [ "$(docker inspect --format='{{.State.Health.Status}}' \
  rag-params-finder-mongodb-local 2>/dev/null)" = "healthy" ]; do
  echo "waiting for MongoDB…"; sleep 5
done

Then start uvicorn in Terminal 2. The server log will show boot OK — server ready with no connection errors.

Auto-created indexes (local URI)

  • vector_index_1024 — Voyage AI + dense SIE models (1024-dim cosine)
  • vector_index_384 — local sentence-transformers (384-dim cosine)
  • vector_index_30522 — SIE SPLADE-v3 learned sparse (30522-dim)
  • text_search_index — BM25 for sparse and hybrid retrieval

The server detects URI type at startup:

  • *.mongodb.net → cloud mode (M0-aware search index quota)
  • anything else → local mode (all indexes created on boot)

Switching backends

No code changes. Two things change between backends: how you start the stack, and which example config you run. The YAML database_provider must match the server's engine, or submit returns 422 (see Config engine mismatch in Troubleshooting).

Action Command
Full stack — Atlas Local ./start-services.sh --mongodb-local
Full stack — Atlas cloud ./start-services.sh --mongodb-cloud (or bare ./start-services.sh when .env is mongodb)
MongoDB container only ./start-services.sh mongodb start
Stop local MongoDB ./start-services.sh mongodb stop
Wipe local data (volume) ./start-services.sh mongodb reset
Status + connection string ./start-services.sh mongodb status

Deprecated env alias (still works): RAG_LOCAL_ATLAS=1 → --mongodb-local. The old --local / -l flags were removed — use --mongodb-local.

To use Elasticsearch for vectors and keep Mongo as run state, leave STORAGE_BACKEND unset and run ./start-services.sh --elasticsearch-local. See Elasticsearch setup.

To switch back to cloud: restore MONGODB_ATLAS_CLOUD_URI in .env to the mongodb+srv://... string and run ./start-services.sh --mongodb-cloud (or bare start with STORAGE_BACKEND=mongodb).

Reset all local data: docker compose --profile mongodb-local down -v (alias profile: local-atlas).

RECOVERY_INTENT_EXPLICIT requirement: only run ./start-services.sh mongodb reset after a deliberate operator-confirmation step that acknowledges local data will be removed.


Before you run a sweep

Both example configs use dense + sparse + hybrid retrieval — you need vector + text search indexes (auto on Path B; manual on Path A M0).

Local embeddings sweep — example-local.yaml

rag-params-finder run --config configs/mongodb/example-local.yaml
# Step Where
1 MongoDB backend ready Path A steps 1–5 or Path B
2 vector_index_384 + text_search_index on chunks Path A step 6 — skip on Path B
3 Server running uvicorn server.main:app --reload --port 8001 or ./start-services.sh [--mongodb-local]

No Voyage account needed.

Voyage sweep — example-voyage.yaml

rag-params-finder run --config configs/mongodb/example-voyage.yaml

Complete the local embeddings sweep checklist above, then add:

# Step Where
4 Voyage account + API key → VOYAGE_API_KEY Voyage → steps 1–2
5 Payment method + ≥ $5 usage credits (Tier 1) Voyage → step 3
6 VOYAGE_RPM_LIMIT=2000 and VOYAGE_TPM_LIMIT=16000000 in .env Voyage → step 3
7 vector_index_1024 instead of vector_index_384 Path A step 6

You need both vector indexes if you run local-embedding and Voyage sweeps on the same cloud cluster.

SIE sweep — example-sie.yaml

rag-params-finder run --config configs/mongodb/example-sie.yaml

Complete the local embeddings sweep checklist above, then add:

Set SIE_ENABLED=true for either path below — it is the same on/off flag; only SIE_ENDPOINT (and usually SIE_API_KEY on remote) differ.

# Step Where
4 Remote gateway: SIE_ENABLED=true, SIE_ENDPOINT, SIE_API_KEY in .env — no Docker SIE setup → Path A
4′ Or self-hosted Docker: SIE container warm (encode probe HTTP 200) SIE setup → Path B
5 vector_index_1024 + text_search_index on chunks Path A step 6

Dense SIE models (bge-m3, stella-v5) use vector_index_1024. Sparse/hybrid retrievers need text_search_index. The example config uses 2 of 3 M0 search-index slots (splade-v3 deferred — exceeds Atlas 4096-dim limit).

No Voyage API key needed.

Quick API demo (no YAML): POST /api/v1/sweep — see SIE setup §6.


Voyage AI (required for Voyage sweep)

Skip entirely for configs/mongodb/example-local.yaml.

1. Create an account

Sign up at dash.voyageai.com.

→ API Key and Python Client

2. Create an API key

Dashboard → API Keys → Create new secret key → add to .env:

VOYAGE_API_KEY=vo-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

3. Unlock Tier 1 rate limits (required for 40-run Voyage sweep)

Without billing, Voyage caps you at 3 RPM / 10,000 TPM — a full sweep will hit rate limits and fail.

  1. Add payment method: Billing → Payment methods
  2. Add ≥ $5 USD credits: Billing → Add to credit balance
  3. Confirm Tier 1 at Organization → Rate Limits
  4. Set in .env and restart uvicorn (comment out free-tier defaults, uncomment Tier 1 lines — see .env.example):
# Voyage rate limits - Tier 1
VOYAGE_RPM_LIMIT=2000
VOYAGE_TPM_LIMIT=16000000

→ Voyage Rate Limits · Prepaid billing FAQ · Pricing

Optional: monitor usage at dash.voyageai.com/usage.


Run the sweep

cp .env.example .env          # once — then fill MONGODB_ATLAS_CLOUD_URI (or MONGODB_ATLAS_LOCAL_URI) — plus Voyage vars if needed
uvicorn server.main:app --reload --port 8001

# Local embeddings — 120 runs, no API key (needs vector_index_384 + text_search_index on cloud M0)
rag-params-finder run --config configs/mongodb/example-local.yaml

# Voyage — 40 runs, requires Voyage steps above
rag-params-finder run --config configs/mongodb/example-voyage.yaml

Dashboard (optional): cd frontend && npm run dev → http://localhost:5374

Docker stack (optional): ./start-services.sh (cloud) or ./start-services.sh --mongodb-local


If something fails

Environment variables

MONGODB_ATLAS_CLOUD_URI / MONGODB_ATLAS_LOCAL_URI is the Atlas or Atlas Local connection string. ./start-services.sh --mongodb-local injects the container URI into the server; the host CLI still exports MONGODB_ATLAS_LOCAL_URI=mongodb://localhost:27017/rag_params_finder?directConnection=true.

Index lifecycle

Atlas Search indexes (vector_index_384, vector_index_1024, text_search_index) must exist before a sweep. Atlas Local creates them on boot. Cloud M0 needs the JSON definitions in Before you run a sweep. rag-params-finder indexes list shows known versus unknown indexes, and indexes reset rebuilds the chunks indexes.

Sizing

An M0 cluster is 512 MB. A 1024-dimension embedding is about 4 KB per chunk before index overhead, so about 10k chunks fit comfortably. Raise the tier when storage or shared CPU becomes the limit.

Diagnostics cheat sheet

curl -sS http://127.0.0.1:8001/healthz | python3 -m json.tool
rag-params-finder indexes list
./start-services.sh mongodb status