Skip to main content

Medium retrieval · search

Find the right page

The retrieval half of RAG, built from nothing but word overlap.

Retrieval is the step where an assistant looks up pages before it answers. The simplest version scores each document by how many of the question's words it contains.

Write rankDocuments(query, docs): query is a string and docs is an array of strings. Split everything into lowercase words on spaces (ignore empty pieces). A document's score is the number of distinct query words that appear in it. Return all the document indexes, highest score first. On a tie, the lower index comes first.

Call show(scores) with each document's score, in the original order, and the stage draws them as bars.

Example: rankDocuments("red apple", ["green pear", "red apple pie", "apple"]) returns [1, 2, 0].

Tests you can see

  • rankDocuments("red apple", ["green pear","red apple pie","apple"])[1,2,0]
  • rankDocuments("how do I get a refund", ["Shipping takes five days","To get a refund email us","I like refunds"])[1,2,0]

More tests are hidden. The server runs them all before a time counts.

The idea behind this, in a two-minute live page

Call show() while your code works and the run plays back here.

javascriptrankDocuments()

Tab indents · Cmd/Ctrl+Enter runs