Building an LCEL retrieval chain

Retrieval Augmented Generation (RAG) with LangChain

Meri Nova

Machine Learning Engineer

Preparing data for retrieval

Document chunks are stored.

Retrieval Augmented Generation (RAG) with LangChain

Introduction to LCEL for RAG

A user input.

Retrieval Augmented Generation (RAG) with LangChain

Introduction to LCEL for RAG

A user input entering the chain via RunnablePassthrough and being assigned to "question".

Retrieval Augmented Generation (RAG) with LangChain

Introduction to LCEL for RAG

The input being used to query the retriever and being assigned to "context".

Retrieval Augmented Generation (RAG) with LangChain

Introduction to LCEL for RAG

The "context" and "question" being integrated into the prompt template.

Retrieval Augmented Generation (RAG) with LangChain

Introduction to LCEL for RAG

The prompt being integrated into the LLM, or model, to generate an output.

Retrieval Augmented Generation (RAG) with LangChain

Introduction to LCEL for RAG

The model output being passed to a parser.

Retrieval Augmented Generation (RAG) with LangChain

Instantiating a retriever

vector_store = Chroma.from_documents(
    documents=chunks, 
    embedding=embedding_model
)


retriever = vector_store.as_retriever( search_type="similarity", search_kwargs={"k": 2} )
Retrieval Augmented Generation (RAG) with LangChain

Creating a prompt template

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template(""" Use the following pieces of context to answer the question at the end. If you don't know the answer, say that you don't know. Context: {context} Question: {question} """)
llm = ChatOpenAI(model="gpt-4o-mini", api_key="...", temperature=0)
Retrieval Augmented Generation (RAG) with LangChain

Building an LCEL retrieval chain

from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
Retrieval Augmented Generation (RAG) with LangChain

Invoking the retrieval chain

result = chain.invoke({"question": "What are the key findings or results presented in the paper?"})
print(result)
- Top Performance: RAG models set new records on open-domain question answering tasks...
- Better Generation: RAG models produce more specific, diverse, and factual language...
- Dynamic Knowledge Use: The non-parametric memory allows RAG models to access and ...
Retrieval Augmented Generation (RAG) with LangChain

Let's practice!

Retrieval Augmented Generation (RAG) with LangChain

Preparing Video For Download...