AskReference
ProcessIntermediate

In the RAG example from the chapter, how is the Euclidean distance between the user's question and each chunk embedding used to select the top chunks for answer generation?

The question is embedded into the same vector space as the chunks. The Euclidean distance from that question embedding to each chunk embedding is calculated, and the chunks are sorted by increasing distance. The closest chunks, or top k, are selected and inserted into the prompt as context so the LLM can generate the answer.

In the RAG example, the user's question is converted into a vector embedding just like the chunks were during ingestion. Then, the Euclidean distance between the question point and each chunk point in the vector space is computed. These distances are sorted from smallest to largest, which ranks the chunks by semantic closeness to the question. The top k chunks with the smallest distances are chosen because they are considered the most likely to contain the relevant information. For the example question "Who is a nasty cat?", the calculated distances place Fox (0.22), Kit (0.50), and Brian (0.50) as the three closest chunks, so those are selected to go into the prompt. The LLM then uses that retrieved context to answer: "Fox is the nasty cat."

Key points

  • The question is embedded into the same 2D vector space as the stored chunk embeddings.
  • Euclidean distances between the question embedding and all chunk embeddings are calculated.
  • Chunks are sorted by increasing distance, meaning closer chunks are more semantically similar.
  • Only the top k closest chunks are selected as context for the LLM prompt.
  • In the pet example, the top three chunks for "Who is a nasty cat?" were Fox, Kit, and Brian.
Source:AI Agents and Applications· Peeking inside the black box· p. 89–99

Related questions

Cover of AI Agents and Applications

AI Agents and Applications

Andrea De Mauro

MEAP Edition Version 7 · Manning Publications

View this ebook