# RAG Mini Search (/docs/experiments/rag-mini-search)



A minimal **retrieval-augmented generation** demo: embed a small corpus of experiment descriptions into **Vectorize**, retrieve top matches for a question, and generate a grounded answer with **Workers AI** plus cited source titles.

## Features [#features]

* **POST /seed** - Embed and upsert demo experiment docs into Vectorize
* **POST /ask** - Question → retrieve → generate grounded answer
* **Cited sources** - Response includes matched experiment titles and scores
* **Demo corpus** - 10 experiment descriptions from this repo

## API Reference [#api-reference]

### POST /seed [#post-seed]

Load the demo document corpus into Vectorize. Run once after deploy or locally before asking questions.

#### Example Request [#example-request]

```bash
curl -X POST "https://your-worker.workers.dev/seed"
```

#### Success Response [#success-response]

```json
{
  "seeded": 10,
  "documents": ["Is It Down", "Link Shortener", "Cloud AI Proxy"]
}
```

#### Error Codes [#error-codes]

* `502` - Seeding failed (`SEED_ERROR`)

### POST /ask [#post-ask]

Ask a question against the indexed corpus.

<TypeTable
  type="{
  question: {
    description: &#x22;Natural language question (max 500 characters).&#x22;,
    type: &#x22;string&#x22;,
    required: true,
  },
}"
/>

#### Example Request [#example-request-1]

```bash
curl -X POST "https://your-worker.workers.dev/ask" \
  -H "Content-Type: application/json" \
  -d '{"question":"Which experiment uses Durable Objects?"}'
```

#### Success Response [#success-response-1]

```json
{
  "question": "Which experiment uses Durable Objects?",
  "answer": "The Durable Counter experiment uses Durable Objects for a globally consistent counter (Durable Counter).",
  "sources": [{ "id": "durable-counter", "title": "Durable Counter", "score": 0.89 }]
}
```

#### Error Codes [#error-codes-1]

* `400` - Invalid JSON (`INVALID_BODY`) or question (`INVALID_QUESTION`)
* `404` - No indexed documents; run `POST /seed` first (`NOT_INDEXED`)
* `502` - RAG pipeline error (`RAG_ERROR`)

## Use Cases [#use-cases]

* Learn Vectorize + Workers AI RAG patterns end-to-end
* Prototype documentation or support bots over a small corpus
* Compare embedding retrieval quality before scaling to larger indexes

## Limitations [#limitations]

* Demo corpus is fixed (10 documents); not auto-synced with repo changes
* Must call `POST /seed` before `POST /ask`
* Requires Vectorize index `rag-mini-search` and Workers AI

## Deployment [#deployment]

<Steps>
  <Step>
    ### Click the deploy button [#click-the-deploy-button]

    [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/shrinathsnayak/cloudflare-experiments/tree/main/apps/experiments/rag-mini-search)
  </Step>

  <Step>
    ### Create Vectorize index [#create-vectorize-index]

    Create index `rag-mini-search` (or update `wrangler.json`), then seed:

    ```bash
    curl -X POST "https://your-worker.workers.dev/seed"
    ```
  </Step>
</Steps>

## Local Development [#local-development]

```bash
cd apps/experiments/rag-mini-search
npm install
npm run dev
npm run seed
```

## Configuration [#configuration]

| Binding     | Purpose                                                                                |
| ----------- | -------------------------------------------------------------------------------------- |
| `AI`        | Embeddings (`@cf/baai/bge-base-en-v1.5`) + LLM (`@cf/meta/llama-3.1-8b-instruct-fast`) |
| `VECTORIZE` | Vector index `rag-mini-search`                                                         |

## Cloudflare Features Used [#cloudflare-features-used]

* **[Workers](https://developers.cloudflare.com/workers/)** - Edge compute runtime
* **[Workers AI](https://developers.cloudflare.com/workers-ai/)** - Embeddings and text generation
* **[Vectorize](https://developers.cloudflare.com/vectorize/)** - Vector search at the edge
