0
0
LangChainframework~30 mins

Prompt composition and chaining in LangChain - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use the run() method on color_chain and save the result in final_output.