Overview

This chapter is about using an LLM to improve the search query before running it. Instead of sending the user's raw query straight into hybrid search, we send it to an LLM first to fix, rewrite, or expand it. Then the improved query goes through RRF search like normal.

It builds directly on the hybrid search work from the last chapter. Nothing about the search algorithm changes here, just what we feed into it.

Key Concepts

LLMs before search

LLMs can help with search either before or after retrieval. This chapter only deals with the "before" side: cleaning up or improving the query itself. Fixing typos, rewriting vague phrasing, adding related terms. All of that happens before the query ever touches the search index.

OpenRouter

We used OpenRouter to call an LLM, using the regular openai Python SDK pointed at OpenRouter's API. We used the openrouter/free model so we didn't have to pick one specific model. That's really all there is to it.

Spell correction

Basic spell-checkers don't understand context, so they miss things like "read" being wrong in one sentence and correct in another. An LLM can catch that because it understands meaning, not just word lists. The rule here was to keep it narrow: fix obvious typos only, don't reword or reorder anything else.

Query rewriting

This one goes further than spell correction. Instead of just fixing typos, the LLM turns a vague, conversational query into something short and specific. Something like "that bear movie where leo gets attacked" becomes "The Revenant Leonardo DiCaprio bear attack." It relies on the LLM knowing common movies, actors, and genres.

Query expansion

Expansion doesn't rewrite anything. It adds extra related terms onto the original query so search has more to match against. The chapter covered three types:

Expansion doesn't require an LLM

One point worth remembering: if you already have structured data, like a list of genres in your database, you can do expansion without calling an LLM at all. It's still a valid form of RAG. Use whichever approach is simpler for the problem you actually have.

What We Implemented