
LangChain’s “Chains” are composable sequences of components, allowing you to build sophisticated applications by linking together Language Models (LLMs), prompts, utilities, and other chains. Let’s explore each of the core chain types with more detail and practical use cases.
1. LLMChain: Structuring Language Model Interactions
Detail: The LLMChain
is the most fundamental chain. It’s responsible for taking a prompt template, formatting it with provided inputs, and then passing the formatted prompt to a Language Model. Optionally, it can also manage memory for conversational contexts. The key components are an LLM and a PromptTemplate.
Use Cases:
- Basic Text Generation: Creating stories, poems, articles, or any form of creative content based on a specific prompt.
- Question Answering (Simple): Answering questions directly based on the LLM’s internal knowledge or a provided context within the prompt.
- Summarization (Short Texts): Condensing short pieces of text into a concise summary.
- Translation: Translating text from one language to another.
- Code Generation: Generating code snippets based on natural language descriptions.
- Extracting Information: Pulling specific pieces of information from text based on a defined prompt.
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from langchain.chains import LLMChain
llm = OpenAI(model_name="gpt-3.5-turbo-instruct")
prompt = PromptTemplate.from_template("Explain the concept of {concept} in simple terms.")
llm_chain = LLMChain(llm=llm, prompt=prompt)
explanation = llm_chain.run(concept="quantum entanglement")
print(explanation)
2. SequentialChain: Orchestrating Multi-Step Workflows
Detail: The SequentialChain
allows you to execute a series of chains in a predefined order. The output of one chain can be used as the input to subsequent chains. It provides a structured way to build more complex processes by breaking them down into smaller, manageable steps.
Use Cases:
- Content Creation with Multiple Stages: Generating a story, then suggesting a title, and finally writing a short summary or tagline.
- Document Processing Pipelines: Extracting information from a document, then summarizing it, and finally translating the summary.
- Research and Report Generation: Querying for information, then analyzing it, and finally generating a report.
- Code Generation with Refinement: Generating initial code, then asking the LLM to review and improve it.
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from langchain.chains import SequentialChain, LLMChain
llm = OpenAI(model_name="gpt-3.5-turbo-instruct")
# Chain 1: Generate a product description
prompt_description = PromptTemplate(
input_variables=["product_name"],
template="Write a short and engaging product description for {product_name}."
)
description_chain = LLMChain(llm=llm, prompt=prompt_description, output_key="description")
# Chain 2: Generate a catchy tagline based on the description
prompt_tagline = PromptTemplate(
input_variables=["description"],
template="Suggest a catchy tagline for the following product description: {description}"
)
tagline_chain = LLMChain(llm=llm, prompt=prompt_tagline, output_key="tagline")
overall_chain = SequentialChain(
chains=[description_chain, tagline_chain],
input_variables=["product_name"],
output_variables=["description", "tagline"],
verbose=True
)
result = overall_chain({"product_name": "The Self-Watering Plant Pot"})
print(f"Product Description: {result['description']}")
print(f"Tagline: {result['tagline']}")
LangChain SequentialChain Documentation
3. SimpleSequentialChain: Linear Workflows with Single Input/Output
Detail: A simplified version of SequentialChain
where each chain in the sequence expects a single input and produces a single output. This is useful for straightforward linear pipelines where the output of one step directly becomes the input of the next without complex key management.
Use Cases:
- Basic Text Transformation Pipelines: Generating text, then summarizing it, then translating the summary.
- Simple Question Decomposition: Breaking down a complex question into simpler sub-questions to be answered sequentially.
- Generating and Refining Ideas: Brainstorming an initial idea, then elaborating on it, then refining the elaborated version.
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from langchain.chains import SimpleSequentialChain, LLMChain
llm = OpenAI(model_name="gpt-3.5-turbo-instruct")
# Chain 1: Generate a short piece of news
prompt_news = PromptTemplate.from_template("Write a short news snippet about {event}.")
news_chain = LLMChain(llm=llm, prompt=prompt_news)
# Chain 2: Summarize the news snippet
prompt_summary = PromptTemplate.from_template("Summarize the following news snippet: {text}")
summary_chain = LLMChain(llm=llm, prompt=prompt_summary)
sequential_chain = SimpleSequentialChain(chains=[news_chain, summary_chain], verbose=True)
summary = sequential_chain.run("a new AI model being released in Bentonville, Arkansas")
print(summary)
4. TransformChain: Applying Custom Logic
Detail: The TransformChain
allows you to incorporate custom Python functions into your LangChain workflows. It takes an input dictionary, applies the specified transform_func
, and returns a dictionary of output variables. This is invaluable for tasks like data cleaning, formatting, or any custom processing that needs to happen within a chain.
Use Cases:
- Data Preprocessing: Cleaning text data, extracting specific fields, or reformatting information before passing it to an LLM.
- Output Formatting: Structuring the output of an LLM into a specific format (e.g., converting a list of items into a comma-separated string).
- Information Extraction and Structuring: Using regular expressions or other Python logic to extract and structure information from text.
- API Response Handling: Processing the response from an external API before using it in a subsequent LLM call.
from langchain.chains import TransformChain
def extract_location(inputs: dict) -> dict:
text = inputs["user_query"]
# Simple example: look for keywords like "in" or "near" followed by a capitalized word
import re
match = re.search(r"(in|near) (\w+)", text)
location = match.group(2) if match else "unknown"
return {"location": location}
transform_chain = TransformChain(
input_variables=["user_query"], output_variables=["location"], transform_func=extract_location
)
output = transform_chain.run(user_query="What is the weather like in Bentonville?")
print(output)
output_no_location = transform_chain.run(user_query="Tell me a joke.")
print(output_no_location)
5. RouterChain: Dynamic Chain Selection
Detail: The RouterChain
enables dynamic selection of the next chain to execute based on the input. It consists of a router LLM chain that decides which of several destination chains is most appropriate for the given input. This allows you to create branching logic in your applications, handling different types of queries or tasks with specialized sub-chains.
Use Cases:
- Multi-Functional Chatbots: Routing user queries to different chains based on the intent (e.g., a math question goes to a calculator chain, a general knowledge question goes to a retrieval chain, a creative request goes to a text generation chain).
- Task-Specific Agents: Building agents that can handle various tools or sub-tasks by routing the input to the appropriate tool-using chain.
- Handling Different Data Types: Processing different types of input data with specialized chains.
- Implementing Fallback Mechanisms: Routing to a default chain if no specific chain is deemed suitable for the input.
from langchain.chains.router import MultiPromptChain
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from langchain.chains import LLMChain
llm = OpenAI(model_name="gpt-3.5-turbo-instruct")
# Define prompt templates for different topics
prompt_math = PromptTemplate(
input_variables=["input"],
template="You are a helpful math tutor. Solve: {input}"
)
math_chain = LLMChain(llm=llm, prompt=prompt_math, output_key="output")
prompt_code = PromptTemplate(
input_variables=["input"],
template="You are a helpful programming assistant. Write a short Python function to: {input}"
)
code_chain = LLMChain(llm=llm, prompt=prompt_code, output_key="output")
from langchain.chains.router.llm_router import LLMRouterChain
from langchain_core.output_parsers import PydanticOutputParser
from pydantic import BaseModel
class RouterOutput(BaseModel):
next_inputs: dict
destination: str
router_template = """Given a user query, determine if it's related to math or programming.
Respond with your decision in the following format:
{{'destination': 'MATH' or 'CODE', 'next_inputs': {{'input': user_query}}}}
If it's neither, respond with 'DEFAULT' and the original query.
"""
router_prompt = PromptTemplate.from_template(router_template)
router_chain = LLMRouterChain.from_llm(llm, router_prompt, RouterOutputParser())
default_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("{input}"))
multi_prompt_chain = MultiPromptChain(
router_chain=router_chain,
destination_chains={"MATH": math_chain, "CODE": code_chain},
default_chain=default_chain,
verbose=True
)
math_output = multi_prompt_chain.run("Solve 2 + 2 * 3")
print(f"Math Output: {math_output}")
code_output = multi_prompt_chain.run("Write a function to reverse a string")
print(f"Code Output: {code_output}")
general_output = multi_prompt_chain.run("Tell me a joke")
print(f"General Output: {general_output}")
Other Important Chain Types and Use Cases
- RetrievalQA:
Detail: A chain specifically designed for question answering over a knowledge base. It combines a Retriever (to fetch relevant documents) and an LLM (to generate the answer based on the retrieved documents and the query). This is a core component of Retrieval-Augmented Generation (RAG).
Use Cases: Answering questions based on internal documents, FAQs, wikis, or any external corpus of knowledge.
- ConversationalRetrievalChain:
Detail: Extends
RetrievalQA
to handle conversational context. It incorporates memory to keep track of the conversation history, allowing for follow-up questions and more natural interactions with the knowledge base.Use Cases: Building chatbots that can answer questions based on a knowledge base while maintaining context over multiple turns.
- MapReduceDocumentsChain & StuffDocumentsChain:
Detail: Chains for processing and combining information from multiple documents.
StuffDocumentsChain
simply concatenates the documents and passes them to an LLM.MapReduceDocumentsChain
applies an LLM to each document individually (the “map” step) and then combines the results using another LLM (the “reduce” step), which is useful for handling large numbers of documents.Use Cases: Summarizing large sets of documents, extracting key information from multiple sources, answering questions that require synthesizing information from many documents.
- VectorDBQA:
Detail: A specialized chain for question answering over a vector database. It directly leverages the semantic search capabilities of a vector database to retrieve relevant documents and then uses an LLM to answer the question based on those documents.
Use Cases: Question answering over semantically indexed knowledge bases, where the meaning of the query is more important than exact keyword matches.
- SQLDatabaseChain:
Detail: Enables interaction with SQL databases using natural language. It takes a user’s question, generates the appropriate SQL query, executes it against the database, and then uses an LLM to formulate a natural language response based on the query results.
Use Cases: Querying databases using natural language, generating reports from data, and providing insights based on structured information.
- APIChain:
Detail: Allows you to interact with external APIs using natural language. You provide the API specification, and the chain can generate API calls and process the responses to answer user queries.
Use Cases: Interacting with various online services, retrieving real-time data, performing actions through APIs (e.g., sending emails, scheduling events).
- GraphCypherQAChain & GraphQAChain:
Detail: For querying graph databases (like Neo4j) using natural language.
GraphCypherQAChain
translates the natural language query into Cypher (the query language for Neo4j), executes it, and then uses an LLM to generate a natural language answer.GraphQAChain
directly uses the LLM to answer questions about the graph structure and nodes.Use Cases: Answering questions based on relationships and entities in a knowledge graph.
- And many more specialized chains for tasks like code execution, document translation with specific models, and integrations with various tools and services.
LangChain’s diverse set of chains provides a powerful toolkit for building complex and intelligent applications powered by Language Models. By understanding the capabilities of each chain type, you can effectively combine them to create custom workflows tailored to your specific needs.
Leave a Reply