We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Spell Correction

Typos can kill search results. "Padington" ranks extremely low in keyword search, even though the user clearly wanted "Paddington."

Let's fix the user's spelling for them with AI before we run the search.

LLMs Are Good at Spelling

"Old-school" spell-checkers are often based on dictionaries and simple if/then rules. They can catch common typos, but they have a big limitation: they lack context awareness. For example, the word "read" is a typo in this sentence:

That's a bright read carpet.

But it's correct in this one:

I love to read books.

Luckily, a decent LLM will catch these kinds of errors.

While "prompt engineering" is a bit of a meme, there really are some useful tips for writing better prompts. Read through Anthropic's prompting guide to understand what kinds of detail can be helpful in a prompt.

Assignment

Add spelling correction to improve queries before running hybrid search.

  1. rrf_parser.add_argument(
        "--enhance",
        type=str,
        choices=["spell"],
        help="Query enhancement method",
    )
    
  2. f"""Fix any spelling errors in the user-provided movie search query below.
    Correct only clear, high-confidence typos. Do not rewrite, add, remove, or reorder words.
    Preserve punctuation and capitalization unless a change is required for a typo fix.
    If there are no spelling errors, or if you're unsure, output the original query unchanged.
    Output only the final query text, nothing else.
    User query: "{query}"
    """
    
  3. f"Enhanced query ({METHOD}): '{QUERY}' -> '{ENHANCED_QUERY}'\n"
    

    Where:

    • METHOD is the enhancement method (e.g. "spell")
    • QUERY is the original query
    • ENHANCED_QUERY is the enhanced query returned by the LLM

Run and submit the CLI tests.