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

Query Rewriting

Users often describe what they want in messy, informal ways. Let's transform their intent into better search queries. For example, users might type:

  • "that bear movie where leo gets attacked"
  • "movie about bear in london with marmalade"
  • "scary movie with bear from few years ago"

But what search engines need to perform well are specific, concise queries. For example:

  • "The Revenant Leonardo DiCaprio bear attack"
  • "Paddington London marmalade"
  • "bear horror movie 2015-2020"

LLMs have approximate knowledge of many things... so the better models are pretty good at transforming vague user input into more specific, searchable terms.

Assignment

Build a query rewriter that handles vague user input.

  1. f"""Rewrite the user-provided movie search query below to be more specific and searchable.
    
    Consider:
    - Common movie knowledge (famous actors, popular films)
    - Genre conventions (horror = scary, animation = cartoon)
    - Keep the rewritten query concise (under 10 words)
    - It should be a Google-style search query, specific enough to yield relevant results
    - Don't use boolean logic
    
    Examples:
    - "that bear movie where leo gets attacked" -> "The Revenant Leonardo DiCaprio bear attack"
    - "movie about bear in london with marmalade" -> "Paddington London marmalade"
    - "scary movie with bear from few years ago" -> "bear horror movie 2015-2020"
    
    If you cannot improve the query, output the original unchanged.
    Output only the rewritten query text, nothing else.
    
    User query: "{query}"
    """
    
  2. f"Enhanced query ({METHOD}): '{QUERY}' -> '{ENHANCED_QUERY}'\n"
    

Run and submit the CLI tests.