The R in RAG

In my previous post we looked at saving to the vector store. In this short post we’ll look at retrieving that information.

The simple search is a good starting point and depends on writing a good prompt, but we can do better.

  1. One problem in searching is that we often retrieve redundant data. For example, the basic search might retrieve ten very similar chunks. Maximal Marginal Relevance first builds a set of results that are relevant but also diverse. It then iterates through the relevant documents looking for ones that are as different as possible. As you might expect, this gives you a diverse set of relevant documents.

2. You will remember that we stored metadata with our documents. This gives us a very powerful tool in searching. We can search for documents based on the metadata, and then within those results, we can search semantically.

For example, we might say, “Give me documents that were created in the past month,” and then from that reduced set we can ask for those that provide information on sales. This is faster and more accurate than other search methods and, of course, can be combined with search methods that reduce redundancy.

3. Let the LLM improve your search. The LLM can rewrite your prompt to be more effective, which will get you a better set of results.

4. Interestingly, the order of returned documents matters. After you get your initial results, reorder them with the most important documents at the very beginning or very end (the middle tends to get lost!) You can then search this subset for your most relevant answers.

Unknown's avatar

About Jesse Liberty

** Note ** Jesse is currently looking for a new position. You can learn more about him at https://jesseliberty.bio Thank you. Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET, is now available wherever you buy your books. Liberty was a Team Lead and Senior Software Engineer for various corporations, a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a 13 year Microsoft MVP.
This entry was posted in AI, Essentials and tagged , . Bookmark the permalink.