 Hi, this is Lance from Langchain. This is the second video of our deep dive on query translation in our RAG from scratch series focused on the method called RAG fusion. So as we kind of showed before, query translation you can think of as the first stage in an advanced RAG pipeline. We're taking an input user question and we're translating in some way in order to improve retrieval. Now, we showed this general mapping of approaches previously. So again, you have kind of like rewriting. So you can take a question and kind of break it down into differently worded or different perspectives of the same question. So that's kind of rewriting. There's sub-questions where you take a question, break it down into smaller problems, solve each one independently. And then there's step-back where you take a question and kind of go more abstract, where you kind of ask a higher-level question as a precondition to answer the user question. So those are the approaches. And we're going to dig into one of the particular approaches for rewriting called rank fusion. Now, this is really similar to what we just saw with multi-query. The difference being we actually apply a kind of a clever ranking step of our retrieved documents, which we call reciprocal rank fusion. That's really the only difference. The input stage of taking a question, breaking it out into a few kind of differently worded questions, retrieval on each one is all the same. And we're going to see that in the code here shortly. So let's just hop over there and then look at this. So again, here's a notebook that we introduced previously. Here's the packages we've installed. We've set a few API keys for a langsmith, which you'll see why it's quite useful. And you can kind of go down here to our ragfusion section. And the first thing you'll note is what our prompt is. So it looks really similar to the prompt we just saw with multi-query. And simply, your helpful assistant that generates multiple search queries based on the user input. And here's the question output for queries. So let's define our prompt. And here is our query generation chain. Again, this looks a lot like we just saw. We take our prompt, plumb that into an LLM, and then basically parse by new lines, and that'll basically split out these questions into a list. That's all that's going to happen here. So that's cool. Now here's where the novelty comes in. Each time we do retrieval from one of those questions, we're going to get back a list of documents from our retriever. And so we do it over that. We generated four questions here based on our prompt. We do those over four questions. Well, like a list of lists, basically. Now reciprocal rank fusion is really well suited for this exact problem. We want to take this list of lists and build a single consolidated list. And really all that's going on is it's looking at the documents in each list and kind of aggregating them into a final output ranking and that's really the intuition around what's happening here so let's go ahead and so let's go ahead and look at that in some detail. So we can see we run retrieval. That's great. Now let's go over to Langsmith and have a look at what's going on here. Now let's go over to Langsmith and have a look at what's going on here. So we can see that here is our prompt, your helpful assistant that generates multiple search queries based on a single input. And here is our search queries. And then here are our four retrievals. So that's really good. So we know that all is working. And then those retrievals simply went into this rank function and are correspondingly ranked to a final list of six unique rank documents. That's really all we did. So let's actually put that all together into a full rag chain that's going to run retrieval, return that final list of rank documents, and pass it to our context, pass through our question, send that to our rag prompt, pass it to an LM, parse it to an output, and let's run all that together and see that working. Cool. So there's our final answer. Now let's have a look in LankSmith. We can see here was our four questions. Here's our retrievals. And then our final rag prompt plumbed through the final list of ranked six questions, which we can see laid out here, and our final answer. So this can be really convenient, particularly if we're operating across like maybe different vector stores, or we want to do like retrieval across a large number of kind of differently worded questions. This reciprocal rank fusion step is really nice. For example, if we wanted to only take the top three documents or something, it can be really nice to build that consolidated ranking across all these independent retrievals and pass that to the final generation. So that's really the intuition about what's happening here. Thanks.
RAG from scratch: Part 6 (Query Translation -- RAG Fusion)
Query rewriting is a popular strategy to improve retrieval. RAG-fusion is an approach that re-writes a question from multiple perspectives, performs retrieval on each re-written question, and performs reciprocal rank fusion on the results from each retrieval, giving a consolidated ranking. 

Slides:
https://docs.google.com/presentation/d/1EwykmdVSQqlh6XpGt8APOMYp4q1CZqqeclAx61pUcjI/edit?usp=sharing

Code:
https://github.com/langchain-ai/rag-from-scratch/blob/main/rag_from_scratch_5_to_9.ipynb

Reference:
https://github.com/Raudaschl/rag-fusion