Skip to content
AI360XpertBeta
Playgrounds

Generative AI

Retrieval-Augmented Generation (RAG)

Ranking, a similarity cutoff and a chunk limit decide which evidence reaches the prompt.

Guided steps 1 / 4

Stage 1 of 4: 1. Embed the query

Query direction [0.728, 0.685].

Query direction 0.728, 0.685.

  • Query
  • Document chunk

Which words move the query point?

Average the recognised word vectors, then normalise the result to get a direction.

Embedding

A vector representation used to compare inputs.

Check this stage

Check your understanding

Trace a query through cosine search, context selection and a scripted answer. Watch which sources reach the prompt as you change the cutoff or chunk limit.

How does retrieval change a prompt?

Retrieval-augmented generation (RAG) supplies external evidence at inference time. A retriever chooses passages for the question, then a generator can use them when answering. The original RAG paper combines learned retrieval and generation. This lab isolates the selection step with five fixed chunks, hand-assigned vectors and a scripted answer. Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks

Follow the France query

  1. Embed. The lab lowercases the query and splits it into runs of letters a–z. It averages the vectors for recognised word occurrences, then scales the average to unit length. In “capital of france”, “of” is ignored. If the average is zero or there are no recognised words, retrieval has no query direction.
  2. Search. Compare the query with each chunk using cosine similarity. The score is the dot product here because both directions have unit length. Sort descending, use corpus order for exact ties, keep scores at or above the cutoff, then take up to the chunk limit.
  3. Build the prompt. Number the retained chunks in rank order and add them before the original question. Switch retrieval off to see the same question with no supplied context.
  4. Read the scripted answer. The rule uses the first retained chunk, shown as source [1], or abstains when no chunk remains.

Find two ways to change the evidence

Raise the cutoff to 1 with the France query. The Paris chunk is close but falls short of a perfect match, so the prompt loses it. Now lower the cutoff to 0 and allow at least two chunks. The unrelated light-speed chunk enters the prompt. Set the chunk limit to 1 and observe that its score still passes, but the chunk is omitted.

Cosine similarity measures alignment in this fixture. A real system needs separate evidence that retrieval found the right passages and that the generator used them correctly. Even when a passage is supplied, its position and surrounding context can affect answers. Lost in the Middle

Stage recap

  1. 1. Embed the query

    Average the recognised word vectors, then normalise the result to get a direction.

  2. 2. Vector search

    Cosine ranks directions. The inclusive cutoff filters those ranks before the chunk limit is applied.

  3. 3. Context injection

    Only the retained chunks are numbered and prepended to the original query.

  4. 4. Generation

    The scripted rule uses the first supplied chunk or abstains when the prompt has no context.

Reference

Cosine Similarity
(A · B) / (||A|| ||B||)
Augmented Prompt
Context + User Query
Retrieval
keep every document where similarity(query, doc) >= threshold
Precision vs. Recall
Raising the cutoff can only remove candidates. Relevance requires separate evidence.
Grounding
The scripted answer cites the first supplied chunk. Language models can still misread or ignore evidence.

Break it on purpose

A cutoff of 1 excludes the Paris chunk for the default query, leaving the scripted answer without a source.