The first time you learn retrieval-augmented generation, you do not have to start by building a chat window. Give a program a question, and ask it first to return three passages that may be useful, and to print the document path and the distance beside each one. If the most similar passage is paired with the path of a different document, a later answer can still read smoothly while the reader cannot find what it rests on. A small example revised in lesson 15 of Microsoft’s Generative AI for Beginners is a good place to learn that. [1][3]

Pick one short route out of the course

The public course by the Microsoft Cloud Advocates contains twenty-one lessons in this version. The course separates Learn, which explains concepts, from Build, which is built around code. The home page says that some Python or TypeScript is helpful, and that each lesson can be entered on its own. On a first look at the code, read the environment setup in lesson 0, then learn from lesson 8 how to retrieve with embeddings, and finally open the knowledge-base and nearest-neighbor example in lesson 15. Someone who has already done vector retrieval can start at lesson 15. The teaching text and the code can be read in the public repository. Running an example that involves a model provider needs an account, a key, and a usage allowance configured for the service you choose. [2][3]

Lesson 15 first splits a long text into passages and keeps the original source of each passage, then encodes the passages as vectors. A user’s question can also be turned into a vector, nearby passages can be found in the index, and suitable original text is then brought into the generation request. Two actions can be practiced apart: finding relevant evidence among the candidates, and making the answer actually use that evidence. Show on the screen what was retrieved first. If a later answer goes wrong, there is then a chance to judge whether retrieval brought the wrong material, or whether the generation step left the material. [3]

Read the distance and the row number together

In commit e80335b, the nearest-neighbor example walks the first three positions of the distance array and the index array together. The index goes back to the original table for the passage and the path. The distance takes the value returned at the same rank. First place is not necessarily the first row of the original table, and sorting does not rewrite the original table by itself. If the rank is used as the row number of the original table, the output still has three lines, and each line looks like a complete record, but the pairing of the original text and the distance is already wrong. [1][3]

Three demonstration distances are paired, in retrieval order, with document paths. First place is not the first row of the original table

Figure: distances and original-text paths in a nearest-neighbor result.

You can do a small exercise without a model. Suppose the original table is, in order, a note on project setup, a note on the reset link, and a note on the login email. The row numbers returned by retrieval are one, two, and zero, and the demonstration distances are 0.12, 0.33, and 0.85. Counting rows from zero, the first line should show the path of the reset link and 0.12. After running this pairing exercise with the three pairs, change the order of the original table once, update the returned row numbers at the same time, and check that the evidence displayed still matches the distances. The figure is the result of this demonstration. The distances only explain the pairing. They are not the probability that an answer is correct. [1][3]

Keep an output you can check before you attach generation

Return to the course’s full flow, and keep the question, the retrieved passages, and the generated result separate. Choose a question that has a definite answer in one document. Look first at whether that sentence is among the first three passages, then check whether the answer can point back to the correct file. Change to a question the knowledge base does not answer, and watch the nearest neighbor still return some passages. That helps show why “the nearest one was found” is not the same as “the material answered this question.” Lesson 15 also discusses reranking and evaluating results. Those can be read after this comparison. [3]

The end of the chapter offers two practice routes. One is to add a front end of your own choice to the application. The other is to rebuild the application with a framework such as LangChain or Semantic Kernel. Pick one, and keep the passages, paths, and distances that could already be seen. After the shell or the framework changes, the same question can still be compared. The sign that it has been learned is being able to walk backward from one sentence in the answer to the line of evidence the course example used. [2][3]

References

  1. Microsoft. Fix RAG README neighbor results example

  2. Microsoft. Generative AI for Beginners

  3. Microsoft. Retrieval Augmented Generation (RAG) and Vector Databases