# Multi-query rewriting: ask the same question several ways

Users rarely use the documents' words. Rewriting the question several ways and merging the results covers more of the ways an answer can be phrased.

<div data-sim="ragx" data-v="multiquery"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

Step 1 shows the rewritten queries, step 2 the top four each one retrieves, step 3 the merged list with how many rewrites found each chunk. All RAG simulations share one knowledge base of 18 chunks from 11 documents of a fictional outdoor-gear shop, and three questions: one phrased differently from the documents, one that needs two documents, and one about the whole corpus.

## Things to try

- For the torn-tent question, see which rewrite finds the transit-damage rule.
- Count how many chunks were found by more than one rewrite.
- Compare recall with naive RAG.

## How it works in real systems

LangChain's MultiQueryRetriever and LlamaIndex query transformations implement this. It costs one extra LLM call and several searches per question, which is often worth it for user-facing search.

## Code lab

The same idea in real code, step by step. Copy each block into a notebook or a file and run it in order.

<div class="cl-start"></div>

### Step 1 · Index the knowledge base

Install the libraries and index a small knowledge base with a sentence-embedding model. Normalised embeddings make the dot product equal to cosine similarity.

```bash
pip install sentence-transformers rank-bm25 numpy openai
```

```python
import numpy as np
from sentence_transformers import SentenceTransformer
chunks = [
    "Most items can be returned within 30 days of delivery for a full refund.",
    "Damaged or defective items qualify for a full refund or a free replacement; send a photo of the damage to support first.",
    "Refunds are issued within five business days after the returned item reaches our warehouse.",
    "Parcels damaged in transit must be reported to the carrier within 48 hours of delivery.",
    "The Ridge 2 tent weighs 1.9 kg and uses Aurora ripstop fabric for its fly sheet.",
    "Aurora ripstop fabric is woven by Lumen Textiles at its mill in Porto, Portugal.",
    "Customer complaints in Q3 were mostly about late deliveries during the September peak.",
]
emb = SentenceTransformer("all-MiniLM-L6-v2")                 # 384 dimensions, small and fast
E = emb.encode(chunks, normalize_embeddings=True)
def dense_search(q, k=3):
    s = E @ emb.encode([q], normalize_embeddings=True)[0]
    top = np.argsort(-s)[:k]
    return [(int(i), float(s[i])) for i in top]
```

### Step 2 · Let the model write the rewrites

A small helper to call a chat model (any OpenAI-compatible endpoint works, including a local server):

```python
from openai import OpenAI
client = OpenAI()                                              # reads OPENAI_API_KEY
def ask(prompt, model="gpt-4o-mini"):
    r = client.chat.completions.create(model=model, messages=[{"role": "user", "content": prompt}], temperature=0)
    return r.choices[0].message.content
```

```python
def rewrites(q, n=3):
    out = ask(f"Write {n} different search queries for this question, using words a policy document would use. One per line, no numbering.\n\n{q}")
    return [l.strip() for l in out.splitlines() if l.strip()][:n]
qs = rewrites("Can I get my money back if my tent arrived torn?"); print(qs)
```

### Step 3 · Search with each, fuse the results

```python
def rrf(lists, k=60):
    score = {}
    for l in lists:
        for r, i in enumerate(l): score[i] = score.get(i, 0) + 1 / (k + r + 1)
    return sorted(score, key=score.get, reverse=True)
q = "Can I get my money back if my tent arrived torn?"
lists = [[i for i, _ in dense_search(x, 4)] for x in [q] + rewrites(q)]
print(rrf(lists)[:4])
```

LangChain packages the same idea as `MultiQueryRetriever.from_llm(retriever=..., llm=...)`.

<div class="cl-end"></div>

## More simulations

[← Rerank](/ai-simulations/rag-rerank) · [All AI Simulations](/ai-simulations) · [HyDE →](/ai-simulations/rag-hyde)
