 Hi, this is Lance from Langchain. This is the fourth short video in our Rag from Scratch series that's going to be focused on generation. Now in the past few videos we walked through the general flow for kind of basic rag, starting with indexing, followed by retrieval, then generation of an answer based upon the documents that we retrieved that are relevant to our question. This is kind of the very basic flow. Now, an important consideration in generation is really what's happening is we're taking the documents we retrieve and we're stuffing them into the LLM context window. So if we kind of walk back through the process, we take documents, we split them for convenience or embedding. We then embed each split and we store that in a vector store as this kind of easily searchable numerical representation or vector. And we take a question, embed it to produce a similar kind of numerical representation. We can then search, for example, using something like it to produce a similar kind of numerical representation. We can then search, for example, using something like K and N in this kind of high-dimensional space for documents that are similar to our question based on their proximity or location in this space. In this case, you can see 3D as a toy, kind of toy example. Now we've recovered relevant splits to our question. We pack those into the context window, and we produce our answer. Now this introduces the notion of a prompt. So the prompt is kind of a placeholder that has, for example, you know in our case, keys. So those keys can be like context and question. So there basically are like buckets that we're going to take those retrieved documents and slot them in. We're going to take our question and also slot it in. And if you kind of walk through this flow, you can kind of see that we can build like a dictionary from our retrieved documents and from our question. And then we can basically populate our prompt template with the values from the dict and then becomes a prompt value which can be passed to an lm like a chat model resulting in chat messages which we then parse into a string and get our answer so that's like the basic workflow that we're going to see and let's just walk through that in code very quickly to kind of give you like a hands-on intuition so we had our notebook we walked through previously install a few packages i'm setting a few langsmith environment variables we'll see it's it's nice for uh kind of observing and debugging our traces um previously we did this quick start we're going to skip that over um and what i will do is i'm going to build our retriever And what I will do is I'm going to build our retriever. So again, I'm going to take documents and load them. And then I'm going to split them here. We've kind of done this previously, so I'll go through this kind of quickly. And then we're going to embed them and store them in our index. So now we have this retriever object here. Now I'm going to jump down here. Now here's where it's kind of fun. This is the generation bit. And you can see here I'm defining something new. This is a prompt template. And my prompt template is something really simple. It's just going to say answer the following question based on this context. It's going to have this context variable and a question. So now I'm building my prompt. So great. Now I have this prompt. Let's define an LLM. I'll choose 3, 5. Now this introduces the notion of a chain. So in Langchain we have an expression language called LCEL, Langchain Expression Language, which lets you really easily compose things like prompts, LLMs, parsers, retrievers, and other things. But the very simple kind of example here is just let's just take our prompt, which we defined right here, and connect it to an LL our prompt which you defined right here and connect it to an lm which you'd find right here into this chain so there's our chain now all we're doing is we're invoking that chain so every language expression language chain has a few common methods like invoke bat stream in this case we just invoke it with a dict so context So, context in question, that maps to the expected keys here in our template. And so, if we run invoke, what we see is it's just going to execute that chain and we get our answer. Now, if we zoom over to Langsmith, we should see that it's been populated. So, yeah, we see a very simple runnable sequence. Here was our document. And here's our output, and here is our prompt. Answer the following question based on the context. Here's the document we passed in, here is the question, and then we get our answer. So that's pretty nice. Now there's a lot of other options for RAG prompts. I'll pull one in from our prompt hub. This one's like kind of a popular prompt. So just like has a little bit more detail, but you know, it's the main, the main intuition is the same. You're passing in documents, you're asking the alumni to reason about the documents, given a question, produce an answer. And now here I'm going to find a rag chain, which will automatically do the retrieval for us. And all I have to do is specify, here's my retriever, which we defined before. Here's our question, which we invoke with. The question gets passed through to the key question in our dict, and it automatically will trigger the retriever, which will return documents, which get passed into our context. So it's exactly what we did up here, except before we did this manually. And now this is all kind of automated for us. We pass that DIC, which is auto-populated, into our prompt, LM, output parser, now let's invoke it. And that should all just run. And great, we get an answer, and we can look at the trace, and we can see everything that happened. So we can see our retriever was run. These documents were retrieved. They get passed into our LLM, and we get our final answer. So this is kind of the end of our overview, where we talked about, I'll go back to the slides here quickly. We talked about indexing, retrieval, and now generation. In follow-up short videos, we'll kind of dig into some of the more complex or detailed themes that address some limitations that can arise in this very simple pipeline. Thanks.
RAG From Scratch: Part 4 (Generation)
This is the fourth video in our series on RAG. The aim of this series is to build up an understanding of RAG from scratch, starting with the basics of indexing, retrieval, and generation. This video focuses on generation, covering the process of RAG prompt construction and passing the prompt to an LLM for answer generation. 

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

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