LearnGenerative AI

Retrieval (RAG)

Let a chatbot look things up before it answers, and see how the right notes are found.

  • Generative AI
  • Language models

Type a question or pick one, then change how many notes are sent. Click a note to read it. The notes describe a made-up library and are in English.

How it works

Look it up, then answer.

A language model only knows what was in its training data, so it cannot answer questions about your own documents, and it may guess with confidence. Retrieval-augmented generation, or RAG, fixes this: before answering, the system searches a collection of notes and puts the best matches into the prompt next to the question.

This search uses TF-IDF: each word gets a weight that is higher when the word is rare across the notes, so “parking” counts for more than “library”. The question and every note become vectors of these weights, and the notes are ranked by cosine similarity. Many real systems rank with embeddings instead, which also match words with similar meanings.

The answer here is quoted directly from the notes that were sent, never invented; a real system hands the notes to a language model to write the answer. Sending more notes gives the model more to work with, but makes the prompt longer and adds noise. If nothing matches, a good system says it does not know.