Working through this module?
Module 06 · Page 7 of 20 · 8 sections
Chains in LangChain
What chains are, the chain types, the basic prompt-to-output flow and examples of LLMChain, SequentialChain and RetrievalQA.
What are Chains?
- A Chain is a sequence of steps (components) executed in a logical order.
- It simplifies complex workflows by combining multiple components.
- Chains can include LLMs, prompts, tools, retrievers, memory, etc.
- LangChain provides different types of chains for various use cases.
Why Use Chains?
- Helps in building multi-step workflows.
- Easy to combine multiple components.
- Reduces boilerplate code.
- Supports input/output parsing.
- Useful for RAG, agents, document processing, API integrations, etc.
- Provides a standard interface for complex LLM applications.
Types of Chains in LangChain
| Chain Type | Description |
|---|---|
| LLMChain | Basic chain with LLM and prompt. |
| SequentialChain | Run multiple chains in sequence. |
| SimpleSequentialChain | Sequential chain with simple I/O. |
| RouterChain | Route input to different chains. |
| RetrievalQA Chain | Combine retriever with LLM for RAG. |
| ConversationChain | Maintain conversation (with memory). |
| MapReduceChain | Process large documents in parallel. |
| TransformChain | Custom transformation logic. |
Basic Chain Architecture
InputUser query
Prompt TemplateFormat input
LLMOpenAI, Claude, etc.
Output ParserParse response
Final OutputAnswer
LLMChain
python
from langchain_openai import ChatOpenAI from langchain_core.prompts import PromptTemplate from langchain.chains import LLMChain llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7) prompt = PromptTemplate( input_variables=["topic"], template="Explain {topic} in simple words." ) chain = LLMChain(llm=llm, prompt=prompt) response = chain.invoke({"topic": "Microservices"}) print(response["text"])
SequentialChain
python
from langchain.chains import SequentialChain from langchain.chains import LLMChain chain1 = LLMChain(llm=llm, prompt=PromptTemplate( input_variables=["topic"], template="Give a short summary of {topic}." )) chain2 = LLMChain(llm=llm, prompt=PromptTemplate( input_variables=["summary"], template="List key points from this summary: {summary}" )) sequential_chain = SequentialChain( chains=[chain1, chain2], input_variables=["topic"], output_variables=["summary", "points"] ) result = sequential_chain.invoke({"topic": "LangChain"}) print(result)
RetrievalQA Chain (RAG)
python
from langchain_openai import ChatOpenAI from langchain.chains import RetrievalQA from langchain_community.vectorstores import FAISS from langchain_community.embeddings import OpenAIEmbeddings llm = ChatOpenAI(model="gpt-3.5-turbo") embedding = OpenAIEmbeddings() vectorstore = FAISS.load_local("./faiss_index", embedding, allow_dangerous_deserialization=True) retriever = vectorstore.as_retriever() chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever) response = chain.invoke({"query": "What is LangChain?"}) print(response["result"])
Key Takeaways
- Chains help in creating structured and reusable workflows.
- Choose the right chain based on your use case.
- Use the RetrievalQA chain for RAG applications.
- Sequential chains are useful for multi-step tasks.
- We can also create custom chains when needed.
- Chains can be combined with memory, tools and output parsers.