Hello! Welcome to your fifth lesson in the "Large Language Models and Generative AI" module.
In our last lesson, we demystified Retrieval-Augmented Generation (RAG). You learned its architecture, the importance of embeddings and vector databases, and even built a basic retrieval mechanism from scratch. While that was a great way to understand the core principles, you probably noticed it required a fair bit of manual coding for chunking, embedding, and searching.
Today, we'll learn how to build these powerful applications more efficiently using LangChain. LangChain is a framework designed to simplify the development of applications powered by large language models. It provides a set of modular building blocks that you can compose to create sophisticated pipelines.
This lesson will equip you to:
- Build simple and sequential Chains to execute predefined workflows.
- Construct a complete RAG pipeline using LangChain's components for document loading, splitting, storing, and retrieval.
- Create simple Agents that can use tools to reason and act dynamically.
By the end of this lesson, you'll be able to move beyond theory and use an industry-standard tool to build practical LLM applications.
1. Getting Started: LangChain Chains
In LangChain, the most fundamental concept is the Chain. A chain is a sequence of calls, which can be to an LLM, a tool, or a data preprocessing step. Think of it as a Directed Acyclic Graph (DAG) of components.
Let's start by installing the necessary libraries and setting up your environment.
pip install langchain langchain-openai python-dotenv
# For RAG and Agents, we'll need a few more
pip install beautifulsoup4 pypdf chromadb faiss-cpu tiktoken
Remember to create a .env file and store your OPENAI_API_KEY in it, just as we've done in previous lessons.
Simple and Sequential Chains
The most basic chain, LLMChain, combines a prompt template and an LLM. More complex chains, like SequentialChain, allow you to pipe the output of one chain into the input of another. This lets you build multi-step logic.
To get a practical feel for this, let's watch a clear, code-along tutorial.
LangChain Crash Course For Beginners | LangChain Tutorial
The 'LangChain Crash Course For Beginners' by codebasics provides an excellent introduction to chains. This segment will walk you through creating your first prompt template and then linking chains together.
Watch from the beginning to 17:19. Pay close attention to: Prompt Templates (07:37 - 10:02): How LangChain uses templates to create dynamic prompts, which is more robust than simple f-strings. LLMChain (10:02 - 12:28): The structure of the simplest chain, combining a prompt and an LLM. SimpleSequentialChain (12:28 - 15:10): How to connect two chains where the output of the first is the single input to the second. SequentialChain (15:10 - 17:19): A more general chain that can handle multiple inputs and outputs, allowing you to access intermediate results.
As you can see, chains provide a clear and structured way to define a sequence of operations. This is the foundation upon which more complex applications are built.
2. Building a RAG Pipeline with LangChain
Remember the RAG architecture from our last lesson? LangChain provides specialized components for every step of that process, dramatically reducing the amount of boilerplate code you need to write.

Let's see how to implement this entire pipeline, from loading a document to generating an answer.
4-Langchain Series-Getting Started With RAG Pipeline Using Langchain Chromadb And FAISS
Krish Naik's video on 'Getting Started With RAG Pipeline' is a fantastic, step-by-step guide to building this with LangChain. It covers loading data from multiple sources, splitting it, and using different vector databases.
Watch from the beginning to 30:10. The video covers the entire RAG pipeline as follows: Introduction to RAG components (00:00 - 01:04): A quick overview of the Load, Transform, Embed, and Query stages. Document Loading (01:04 - 18:45): See how to use LangChain's DocumentLoaders to ingest data from text files, web pages (WebBaseLoader), and PDFs (PyPDFLoader). Text Splitting (18:45 - 20:56): Learn how RecursiveCharacterTextSplitter breaks down large documents into manageable chunks with overlap. Embeddings & Vector Stores (20:56 - 24:24): Watch how to use OpenAIEmbeddings and store the resulting vectors in local vector databases like Chroma and FAISS. Similarity Search (24:24 - 30:10): The final step of the retrieval process, where you query the vector store to find the most relevant document chunks.
The video effectively demonstrates the "retrieval" part of RAG. Now, how do we combine this with the "generation" part? LangChain makes this straightforward with a dedicated create_retrieval_chain.
Here is a consolidated code example showing the full pipeline, inspired by the LangChain documentation.
import os
from dotenv import load_dotenv
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
# Load environment variables
load_dotenv()
# 1. Load Documents
print("Loading documents...")
loader = WebBaseLoader("https://docs.smith.langchain.com/user_guide")
docs = loader.load()
# 2. Split Documents
print("Splitting documents...")
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
# 3. Create Vector Store
print("Creating vector store...")
# Note: OpenAIEmbeddings will use the OPENAI_API_KEY from your .env file
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
# 4. Create Retriever
retriever = vectorstore.as_retriever()
# 5. Create the RAG Chain
print("Creating RAG chain...")
llm = ChatOpenAI(model="gpt-3.5-turbo")
# This prompt template is key. It instructs the LLM on how to use the retrieved context.
system_prompt = (
"You are an assistant for question-answering tasks. "
"Use the following pieces of retrieved context to answer "
"the question. If you don't know the answer, say that you "
"don't know. Use three sentences maximum and keep the "
"answer concise."
"\n\n"
"{context}"
)
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("human", "{input}"),
]
)
# This chain takes a question and the retrieved documents and generates an answer.
question_answer_chain = create_stuff_documents_chain(llm, prompt)
# This is the final chain that orchestrates the whole process.
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
# 6. Ask a question
print("Asking question...")
question = "what is langsmith?"
response = rag_chain.invoke({"input": question})
print("\n--- Response ---")
print(response["answer"])
# The response also includes the source documents used for the answer
# print("\n--- Context ---")
# for doc in response["context"]:
# print(doc.page_content)
# print("-" * 20)
This single script accomplishes everything we discussed: loading, splitting, embedding, storing, retrieving, and generating. It's a powerful demonstration of how LangChain streamlines RAG development.
3. Beyond Predefined Steps: LangChain Agents
Chains are powerful, but they follow a fixed path. What if you need the LLM to make decisions? For instance, to decide whether to search for information or to use a calculator to perform a mathematical operation. This is where Agents come in.
An Agent uses an LLM as a "reasoning engine" to decide which Tools to use and in what sequence to accomplish a goal.
Let's see a simple agent in action.
LangChain Crash Course For Beginners | LangChain Tutorial
Let's return to the 'LangChain Crash Course' video. This final segment introduces the concept of agents and shows how they can use tools like Wikipedia and a calculator to answer questions that go beyond the LLM's built-in knowledge.
Watch from 27:27 to 37:16. Focus on: The concept of a reasoning engine (28:45 - 31:40): Understand that agents leverage the LLM's ability to reason and break down a problem. Tools (31:40 - 33:20): Grasp that tools are simply functions the agent can decide to call (e.g., Wikipedia search, math calculation). The thought-action loop (35:10 - 36:00): Observe how the agent thinks about what to do, takes an action (calls a tool), observes the result, and then thinks again until it has a final answer.
The key idea is that instead of you defining the exact workflow, you provide the agent with a set of capabilities (tools) and a goal (the user's query), and it figures out the steps itself.
You can even combine the concepts of RAG and Agents by creating a RAG Agent. In this setup, the retriever we built earlier becomes one of the tools available to the agent. The agent can then decide whether it needs to search your documents to answer a question or if it can answer from its own knowledge.
Build a RAG agent with LangChain
The official LangChain documentation provides a concise tutorial on building a RAG agent. This is a great way to see how the concepts of retrieval and agency come together.
Read the section titled 'RAG agents'. Notice how the similarity_search function of the vector store is wrapped in a @tool decorator. This turns your entire RAG retriever into a single tool that an agent can then use.
Conclusion
In this lesson, you've taken a major step from understanding AI concepts to applying them with a powerful framework. You now have the foundational skills to build a wide range of LLM-powered applications using LangChain.
Key Takeaways:
- LangChain Chains: You can orchestrate LLM workflows by creating
LLMChainsfor single steps andSequentialChainsto link multiple steps together. - LangChain for RAG: LangChain provides a complete toolkit to build RAG pipelines efficiently, including document loaders, text splitters, vector store integrations, and retrieval chains.
- LangChain Agents: For more dynamic tasks, you can build agents that use an LLM as a reasoning engine to decide which tools (like search, a calculator, or even your RAG retriever) to use to solve a problem.
Preview of the Next Lesson:
The chains and agents we've built today are stateless—they treat every query as a new one. For building chatbots, this is a major limitation. In our next lesson, we will address this by learning how to implement conversation memory in chatbot applications, allowing your creations to remember past interactions and have more natural, context-aware conversations.