Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Building a Simple Prompt Chain with Langchain
📖 Scenario: You are creating a small program that uses Langchain to combine two prompts. The first prompt asks for a user's favorite color. The second prompt uses that answer to create a sentence describing the color.
🎯 Goal: Build a Langchain prompt chain that first asks for a favorite color, then uses that answer to generate a descriptive sentence.
📋 What You'll Learn
Create a Langchain prompt template for asking the favorite color
Create a second prompt template that uses the color answer
Chain the two prompts so the output of the first is input to the second
Run the chain and get the final output
💡 Why This Matters
🌍 Real World
Prompt chaining is useful when you want to break down a complex question into smaller steps and combine their answers smoothly.
💼 Career
Understanding prompt composition and chaining is important for building conversational AI, chatbots, and automated assistants that handle multi-step interactions.
Progress0 / 4 steps
1
Create the first prompt template
Create a Langchain prompt template called color_prompt with the template string: 'What is your favorite color?'
LangChain
Hint
Use PromptTemplate from langchain.prompts and assign it to color_prompt.
2
Create the second prompt template using the color
Create a Langchain prompt template called description_prompt with the template string: 'The color you chose is {color}.' and specify input_variables=['color']
LangChain
Hint
Remember to include input_variables=['color'] so the prompt knows to expect a variable named color.
3
Chain the two prompts using SimpleSequentialChain
Import SimpleSequentialChain from langchain.chains and create a chain called color_chain that runs color_prompt then description_prompt in sequence
LangChain
Hint
Use SimpleSequentialChain and pass a list of the two prompt templates in order.
4
Run the chain and get the final output
Call color_chain.run() and assign the result to a variable called final_output
LangChain
Hint
Use the run() method on color_chain and save the result in final_output.
Practice
(1/5)
1. What is the main purpose of prompt composition in Langchain?
easy
A. To run multiple AI models simultaneously
B. To break a big task into smaller, manageable prompts
C. To store data in a database
D. To create user interfaces for AI
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 B
Quick Check:
Prompt composition = breaking big tasks [OK]
Hint: Think of splitting a big job into small steps [OK]
Common Mistakes:
Confusing prompt composition with running multiple models
Thinking it stores data instead of organizing prompts
Assuming it builds user interfaces
2. Which of the following is the correct way to chain two prompts in Langchain?
easy
A. chain = Chain([prompt1, prompt2])
B. chain = Chain(prompt1, prompt2)
C. chain = Chain.compose(prompt1, prompt2)
D. chain = Chain().add(prompt1).add(prompt2)
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 D
Quick Check:
Use .add() to chain prompts [OK]
Hint: Look for method chaining with .add() calls [OK]
Common Mistakes:
Passing prompts as a list directly to Chain
Using Chain(prompt1, prompt2) without .add()
Assuming a compose method exists
3. Given the code below, what will be the output of 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"})
medium
A. "Hello, Alice! How can I help you today?"
B. "Hello, {name}! How can I help you today?"
C. "Hello, Alice!"
D. Error: Missing input for prompt2
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 A
Quick Check:
Chained prompts combine outputs [OK]
Hint: Chained prompts concatenate outputs with variables replaced [OK]
Common Mistakes:
Thinking prompt2 needs input variables
Expecting placeholders to remain unreplaced
Assuming only first prompt output is returned
4. What is the error in the following code snippet?
prompt1 = Prompt(template="What is your name?")
chain = Chain()
chain.add(prompt1)
chain.run()
medium
A. Missing input arguments for run()
B. Chain object cannot be empty
C. Prompt template syntax is incorrect
D. add() method does not exist on Chain
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 A
Quick Check:
run() needs inputs if prompts have variables [OK]
Hint: Check if run() has required inputs for prompts [OK]
Common Mistakes:
Assuming run() works without inputs always
Thinking add() method is missing
Believing prompt syntax is wrong without variables
5. You want to create a chain where the output of the first prompt is used as input for the second prompt. Which approach correctly achieves this in Langchain?
hard
A. Create two independent chains and merge their results after running
B. Run prompt1 and prompt2 separately and concatenate their outputs manually
C. Use a chain that passes output variables from prompt1 to prompt2 as input
D. Use prompt2 with fixed text ignoring prompt1 output
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 C
Quick Check:
Chaining passes outputs as inputs between prompts [OK]
Hint: Chain outputs flow as inputs to next prompt [OK]