Get startedGet started for free

Building an LCEL retrieval chain

1. Building an LCEL retrieval chain

Great work so far!

2. Preparing data for retrieval

Now we have our external data stored and ready for retrieval, it's time to create the retrieval chain to integrate it with an LLM!

3. Introduction to LCEL for RAG

LangChain Expression Language, or LCEL, is a declarative syntax for defining chains suited from prototypes to production. It is particularly relevant to RAG systems because it creates modular, reusable pipelines that can combine retrieval and generation components together. The retrieval chain will take a question input,

4. Introduction to LCEL for RAG

insert it into the chain using RunnablePassthrough and assign it to "question". RunnablePassthrough allows inputs to be inserted into chains unchanged.

5. Introduction to LCEL for RAG

We retrieve the relevant documents from the vector store and assign to "context",

6. Introduction to LCEL for RAG

integrate both of these into a prompt template,

7. Introduction to LCEL for RAG

pass the prompt to the model to generate an output,

8. Introduction to LCEL for RAG

and parse the output into our favored format, such as a string. Before building the chain, we need to create three components: a retriever, which is derived from our vector store, a prompt template for combining the user question and retrieved context, and a model to generate the response.

9. Instantiating a retriever

Previously, we created a vector_store from embedding document chunks and using the .from_documents() method. To convert this into a retriever suitable for our chain, we call the .as_retriever() method on vector_store. The arguments dictate what sort of search should be conducted during retrieval, a similarity search in this case, and how many chunks to retrieve when queried, k, which is two.

10. Creating a prompt template

To construct a prompt template for the model, we'll use ChatPromptTemplate. Inside, we instruct the model to use the context provided to answer the question. We use curly brackets to indicate placeholders for the two input variables: context and question. We'll define an OpenAI chat model to generate the responses.

11. Building an LCEL retrieval chain

Now, let's explore how to build a chain using LCEL and RunnablePassthrough. We start by opening parentheses so we can define our chain over multiple lines. Then, create a dictionary that takes the input from RunnablePassthrough, assigns it to "question", and uses it to query and retrieve chunks from the retriever, which are assigned to "context". This might look complex, but RunnablePassthrough is essentially a placeholder in our chain that allows us to pass data through without modifying it. The retrieved "context" and user "question" are then passed into the prompt using the LCEL pipe, and then into the LLM. Finally, a string output parser is used to parse the model output as a string.

12. Invoking the retrieval chain

Given that our vector_store contains chunks from a RAG academic paper, let's invoke the chain to request the paper's findings. We can see that context from the paper was successfully retrieved, and used by the model to respond to the question. At this stage, we might wonder how we can evaluate and improve our model's performance based on its response. Stick around to find out!

13. Let's practice!

And now it's your turn to build your own retrieval chain!