0
0
LangChainframework~30 mins

PromptTemplate basics in LangChain - Mini Project: Build & Apply

Choose your learning style9 modes available
PromptTemplate basics
📖 Scenario: You are building a simple chatbot that uses a prompt template to ask questions. The prompt template will help you create messages with placeholders for user input.
🎯 Goal: Create a PromptTemplate that takes a user's name and question, then formats a message asking the question politely.
📋 What You'll Learn
Create a PromptTemplate with input variables name and question.
Set the template string to: 'Hello {name}, can you please answer: {question}?'
Use the format method to fill in the template with example values.
Print the formatted prompt message.
💡 Why This Matters
🌍 Real World
PromptTemplate helps create dynamic messages for chatbots, AI assistants, or any system that needs to ask questions or generate text with user input.
💼 Career
Understanding PromptTemplate is useful for AI developers, chatbot builders, and anyone working with language models to create flexible and reusable prompts.
Progress0 / 4 steps
1
Import PromptTemplate and create the template
Import PromptTemplate from langchain.prompts and create a variable called template that is a PromptTemplate with input variables name and question, and the template string set to 'Hello {name}, can you please answer: {question}?' .
LangChain
Need a hint?

Use PromptTemplate(input_variables=[...], template=...) to create the template.

2
Create example input values
Create a dictionary called inputs with keys 'name' and 'question'. Set 'name' to 'Alice' and 'question' to 'What is the weather today'.
LangChain
Need a hint?

Use a dictionary with exact keys and values as shown.

3
Format the prompt with inputs
Use the format method of template with the inputs dictionary to create a variable called formatted_prompt.
LangChain
Need a hint?

Use template.format(**inputs) to fill the template.

4
Print the formatted prompt
Write a line to print the variable formatted_prompt.
LangChain
Need a hint?

Use print(formatted_prompt) to show the final message.