# HyDE: search with a hypothetical answer

A question and its answer look different. HyDE writes a plausible answer first and searches with that, because answers sit closer to answers in embedding space.

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

## What you are looking at

Step 1 shows the draft, step 2 compares each chunk's similarity to the question and to the draft, step 3 keeps the chunks found with the draft. The draft itself never reaches the final prompt. 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

- Compare the two similarity columns: which chunks gain most?
- Note that the draft for the fabric question invents nothing specific, yet still helps.
- Think about the risk: a confident but wrong draft can steer retrieval away.

## How it works in real systems

HyDE (Gao et al., 2022) helps most on short questions and specialised corpora. It is usually combined with hybrid search so exact terms are not lost.

## 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 · Draft a hypothetical answer, search with it

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 hyde_search(q, k=3):
    draft = ask(f"Write a short passage from a shop's policy documents that would answer: {q}")
    v = emb.encode([draft], normalize_embeddings=True)[0]
    s = E @ v
    return draft, [int(i) for i in np.argsort(-s)[:k]]
draft, ids = hyde_search("Can I get my money back if my tent arrived torn?")
print(draft); print(ids)
```

### Step 3 · Compare with the plain question

```python
q = "Can I get my money back if my tent arrived torn?"
print("question:", [i for i, _ in dense_search(q)])
print("HyDE    :", hyde_search(q)[1])
```

Only the retrieved chunks go into the final prompt; the draft is thrown away. Combine HyDE with BM25 on the original question so exact terms are not lost.

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

## More simulations

[← Multi-query](/ai-simulations/rag-multi-query) · [All AI Simulations](/ai-simulations) · [Contextual chunks →](/ai-simulations/rag-contextual)
