Skip to content

< CurriculumRetrieval · 31 of 46 ·75 · Level 02, Search

Rewrite the Query Before You Search

medium · implement · Embeddings & Retrieval

A customer asks whether the Aero Pro headphones come in white, hears no, and types and the other model? Searched as written, those four words find nothing useful. Rewrite the latest message into a query that stands on its own.

Implement standalone_query(llm, history, message): history is the conversation so far, a list of {"role", "content"} dicts; message is the latest message. Return a search query as a string.

  1. No history: return the message unchanged, with no model call.
  2. Otherwise, in one model call, rewrite the message so that someone who has not seen the conversation would understand it, pronouns and phrases like the other one replaced by the names they refer to. Give the model PRODUCTS (provided, the product line in one sentence) or it will invent a name for the other model.
  3. The result is a query, not an answer: one line, at most 25 words. Strip any preamble or quotation marks the model adds.
  4. If the customer has changed the subject, the query is about the new subject only.

The catch: a model told to "use the conversation" will attach the old product to a question about returns. Show it an example of a subject change next to an example of a follow-up; that is the reliable way to get both right.

search_all(queries) (provided) is a real retriever over the catalogue. The tests run your queries through it for seven conversations: the right entry must come first for at least six, and more often than with the raw messages.