 Hi, this is Lance from Langchain. Over the next few videos, we're going to be talking about query translation. And in this first video, we're going to cover the topic of multi-query. So query translation sits kind of at the first stage of an advanced RAG pipeline. And the goal of query translation is really to take an input user question and to translate it in some way in order to improve retrieval. So the problem statement is pretty intuitive. User queries can be ambiguous. And if the query is poorly written, because we're typically doing some kind of semantic similarity search between the query and our documents, if the query is poorly written or ill-posed, we won't retrieve the proper documents from our index. So there's a few approaches to attack this problem, and you can kind of group them in a few different ways. So here's one way I like to think about it. A few approaches has involved query rewriting. So taking a query and reframing it, like writing it from a different perspective, and that's what we're going to talk about a little bit here in depth using approaches like multi-query or ragfusion, which we'll talk about in the next video. You can also do things like take a question and break it down to make it less abstract, like into sub-questions, and there's a bunch of interesting papers focused on that, like Least to Most from Google. You can also take the opposite approach of take a question and make it more abstract. And there's actually an approach we're gonna talk about later in a future video called step-back prompting that focuses on like kind of a higher level question from the input. So the intuition, though, for this multi-query approach, we're taking a question and we're going to break it down into a few differently worded questions from different perspectives. And the intuition here is simply that it is possible that the way a question is initially worded, once embedded, it is not well aligned or in close proximity in this high dimensional embedding space to a document we want to retrieve that's actually related. kind of rewriting it in a few different ways, you actually increase the likelihood of actually retrieving the document that you really want to. Because of nuances in the way that documents and questions are embedded, this kind of more shotgun approach of taking a question, fanning it out into a few different perspectives, may improve and increase the reliability of retrieval. That's like the intuition, really. And, of course, we can combine this with retrieval so we can take our kind of fanned out questions, do retrieval on each one, and combine them in some way and perform RAG. So that's kind of the overview. And now let's go over to our code. So this is a notebook and we're going to share all this. So this is a notebook and we're going to share all this. We're just installing a few packages. We're setting a Langsmith API keys, which we'll see why that's quite useful here shortly. There's our diagram. Now first I'm going to index this blog post on agents. I'm going to split it. Well I'm going to load it. I'm going to split it and then I'm going to index it in Chroma locally. So this is a vector store. We've done this previously, so now I have my index defined. So here is where I'm defining my prompt for multi-query, which is your assistant. Your task is to basically reframe this question into a few different sub-questions. So there's our prompt right here. We'll pass that to an LLM, parse it into a string, and then split the string by new lines. And so we'll get a list of questions out of this chain. That's really all we're doing here. Now, all we're doing is here's a sample input question. There's our generate queries chain, which we defined. We're going to take that list and then simply apply each question to a retriever. So we'll do retrieval per question and this little function here is just going to take the unique union of documents across all those retrievals. So let's run this and see what happens. So we're going to run this and we're going to get some set of questions or documents back. So let's go to Langsmith now. We can actually see what happened under the hood. So here's the key point. We ran our initial chain to generate a set of reframed questions from our input. And here is that prompt. And here is that set of questions that we generated. Now what happened is for every one of those questions, we did an independent retrieval. That's what we're showing here. So that's kind of the first step, which is great. Now I can go back to the notebook and we can show this working end to end. So now we're going to take that retrieval chain. We'll pass it into context of our final rag prompt. We'll also pass through the question. We'll pass that to our rag prompt here, pass it to an LM, and then parse the output. Now let's kind of see how that works. So again, that's okay. There it is. So let's actually go into Langstaff and see what happened under the hood. So this is our final chain. This is great. We took our input question. We broke it down to these like five rephrase questions. For every one of those, we did a retrieval. That's all great. We then took the unique union of documents, and you can see in our final LLM prompt, answer the following question based on the context. This is the final set of unique documents that we retrieved from all of our sub questions. Here's our initial question. There's our answer. So that kind of shows you I can set this up really easily. I can use LankSmith to kind of investigate what's going on and in particular use LankSmith to investigate those intermediate questions that you generate in that like kind of question generation phase. And in future talks we're going to go through some of these other methods that we kind of introduced at the start of this one. Thank you.
RAG from scratch: Part 5 (Query Translation -- Multi Query)
Query rewriting is a popular strategy to improve retrieval. Multi-query is an approach that re-writes a question from multiple perspectives, performs retrieval on each re-written question, and takes the unique union of all docs. 

Slides:
https://docs.google.com/presentation/d/15pWydIszbQG3Ipur9COfTduutTZm6ULdkkyX-MNry8I/edit?usp=sharing

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