C.W.K.
Stream
Lesson 05 of 05 · published

Query-Side Tricks: HyDE and Multi-Query

~20 min · hybrid, queries, advanced

Level 0Scout
0 XP0/41 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The query is half the system

Most teams optimize the document side (better embeddings, better chunking) and forget the query side. The query is also a string the embedding model encodes — and short, vague queries embed badly. Two cheap fixes change retrieval quality more than people expect.

Multi-query expansion

Ask an LLM to rewrite the user's query in 3–5 different phrasings. Embed each. Search each. Fuse the rankings with RRF. Cost: one extra LLM call. Win: significantly higher recall on vague or short queries.

HyDE (Hypothetical Document Embeddings)

Instead of embedding the query, ask an LLM to write a hypothetical answer to the query, then embed that answer and search with it. Why it works: answers and documents share lexical and structural patterns; questions do not. Especially strong when your queries are short and your documents are long.

Code

Multi-query expansion·python
def expand_query(query: str, n: int = 4) -> list[str]:
    prompt = (
        f'Rewrite this query in {n} different ways that might appear in a knowledge base. '
        f'Output one rewrite per line, no numbering.\n\nQuery: {query}'
    )
    response = llm.complete(prompt)
    return [line.strip() for line in response.splitlines() if line.strip()][:n]

rewrites = expand_query('how do I cancel my plan')
rankings = [vector_search(r, k=20) for r in [query] + rewrites]
final = reciprocal_rank_fusion([[r['id'] for r in ranking] for ranking in rankings])
HyDE·python
def hyde(query: str) -> str:
    prompt = (
        f'Write a 3-sentence factual answer to this question, even if you have to invent details. '
        f'Style it like a knowledge-base article.\n\nQuestion: {query}'
    )
    return llm.complete(prompt)

hypothetical = hyde('how do I cancel my plan')
results = vector_search(hypothetical, k=10)   # search with the hypothetical answer, not the question

External links

Exercise

Take 10 short, vague queries from your real users. For each, generate 3 multi-query rewrites and one HyDE answer. Compare top-3 results for: original, multi-query+RRF, HyDE. Note which trick wins for which query type.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.