Prompt composition and chaining help you build complex tasks by connecting simple prompts step-by-step. This makes your AI work smarter and more organized.
Prompt composition and chaining in LangChain
from langchain.prompts import PromptTemplate from langchain.chains import LLMChain, SimpleSequentialChain # Define individual prompt templates prompt1 = PromptTemplate(input_variables=["name"], template="Hello {name}, how can I help you today?") prompt2 = PromptTemplate(input_variables=["response"], template="You said: {response}. Can you tell me more?") # Create chains for each prompt chain1 = LLMChain(llm=llm, prompt=prompt1) chain2 = LLMChain(llm=llm, prompt=prompt2) # Chain them together overall_chain = SimpleSequentialChain(chains=[chain1, chain2]) # Run the chain result = overall_chain.run("Alice")
Each prompt template defines how to format the input for the AI.
Chains connect prompts so outputs flow from one to the next automatically.
from langchain.prompts import PromptTemplate from langchain.chains import LLMChain, SimpleSequentialChain # Empty input example prompt1 = PromptTemplate(input_variables=["text"], template="Summarize: {text}") chain1 = LLMChain(llm=llm, prompt=prompt1) # Running with empty string result = chain1.run("")
from langchain.prompts import PromptTemplate from langchain.chains import LLMChain, SimpleSequentialChain # Single prompt chain prompt1 = PromptTemplate(input_variables=["topic"], template="Explain {topic} simply.") chain1 = LLMChain(llm=llm, prompt=prompt1) result = chain1.run("photosynthesis")
from langchain.prompts import PromptTemplate from langchain.chains import LLMChain, SimpleSequentialChain # Chain with two steps prompt1 = PromptTemplate(input_variables=["question"], template="Answer this: {question}") prompt2 = PromptTemplate(input_variables=["answer"], template="Explain why: {answer}") chain1 = LLMChain(llm=llm, prompt=prompt1) chain2 = LLMChain(llm=llm, prompt=prompt2) overall_chain = SimpleSequentialChain(chains=[chain1, chain2]) result = overall_chain.run("What is AI?")
This program first asks the AI to define a topic, then asks why that definition matters. It shows how chaining passes the first answer to the next prompt.
from langchain.llms import OpenAI from langchain.prompts import PromptTemplate from langchain.chains import SimpleSequentialChain # Initialize the language model llm = OpenAI(temperature=0) # Define the first prompt template prompt1 = PromptTemplate( input_variables=["topic"], template="Give a short definition of {topic}." ) # Define the second prompt template prompt2 = PromptTemplate( input_variables=["definition"], template="Explain why this definition is important: {definition}" ) # Create chains for each prompt from langchain.chains import LLMChain chain1 = LLMChain(llm=llm, prompt=prompt1, output_key="definition") chain2 = LLMChain(llm=llm, prompt=prompt2, input_key="definition") # Chain them together overall_chain = SimpleSequentialChain(chains=[chain1, chain2]) # Run the chain with a topic result = overall_chain.run("machine learning") print("Final output:", result)
Prompt chaining helps organize complex tasks into simple steps.
Time complexity depends on the number of chained prompts and AI response time.
Common mistake: forgetting to pass output keys correctly between chains.
Prompt composition breaks big tasks into smaller prompts.
Chaining connects prompts so outputs flow smoothly.
This makes AI workflows easier to build and understand.
Practice
Solution
Step 1: Understand prompt composition
Prompt composition means dividing a large task into smaller prompts to handle each part separately.Step 2: Identify the main purpose
This helps make complex AI tasks easier to manage and understand by working on smaller pieces.Final Answer:
To break a big task into smaller, manageable prompts -> Option BQuick Check:
Prompt composition = breaking big tasks [OK]
- Confusing prompt composition with running multiple models
- Thinking it stores data instead of organizing prompts
- Assuming it builds user interfaces
Solution
Step 1: Recall chaining syntax
In Langchain, chaining prompts is done by creating a Chain object and adding prompts step-by-step.Step 2: Identify correct method
The method .add() is used to add prompts to the chain, so chaining looks like Chain().add(prompt1).add(prompt2).Final Answer:
chain = Chain().add(prompt1).add(prompt2) -> Option DQuick Check:
Use .add() to chain prompts [OK]
- Passing prompts as a list directly to Chain
- Using Chain(prompt1, prompt2) without .add()
- Assuming a compose method exists
final_output?
prompt1 = Prompt(template="Hello, {name}!")
prompt2 = Prompt(template="How can I help you today?")
chain = Chain().add(prompt1).add(prompt2)
final_output = chain.run({"name": "Alice"})Solution
Step 1: Understand prompt templates and chaining
Prompt1 uses the variable {name} which is replaced by "Alice". Prompt2 is a fixed string without variables.Step 2: Analyze chain.run behavior
Running the chain passes the input to prompt1, producing "Hello, Alice!" then continues to prompt2, appending "How can I help you today?".Final Answer:
"Hello, Alice! How can I help you today?" -> Option AQuick Check:
Chained prompts combine outputs [OK]
- Thinking prompt2 needs input variables
- Expecting placeholders to remain unreplaced
- Assuming only first prompt output is returned
prompt1 = Prompt(template="What is your name?") chain = Chain() chain.add(prompt1) chain.run()
Solution
Step 1: Check run() method usage
The run() method requires input arguments matching prompt variables. Here, run() is called without arguments.Step 2: Confirm prompt template variables
Prompt1 has no variables, so no input is needed. But if prompt1 expected variables, missing inputs cause error.Final Answer:
Missing input arguments for run() -> Option AQuick Check:
run() needs inputs if prompts have variables [OK]
- Assuming run() works without inputs always
- Thinking add() method is missing
- Believing prompt syntax is wrong without variables
Solution
Step 1: Understand chaining with variable passing
To pass output from one prompt to another, the chain must connect outputs as inputs for the next prompt.Step 2: Identify correct chaining method
Langchain supports chaining where prompt2 receives variables produced by prompt1 automatically within the chain.Final Answer:
Use a chain that passes output variables from prompt1 to prompt2 as input -> Option CQuick Check:
Chaining passes outputs as inputs between prompts [OK]
- Running prompts separately without chaining
- Merging results manually instead of chaining
- Ignoring output-input flow in chains
Ready to prove it?
Pass the test and this pattern moves unknown
