Challenge - 5 Problems
ChatPromptTemplate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this ChatPromptTemplate usage?
Given this LangChain ChatPromptTemplate code, what will be the final prompt text after formatting?
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
system_template = SystemMessagePromptTemplate.from_template("You are a helpful assistant.")
human_template = HumanMessagePromptTemplate.from_template("Translate this: {text}")
chat_prompt = ChatPromptTemplate.from_messages([system_template, human_template])
final_prompt = chat_prompt.format_prompt(text="Hello world")
print(final_prompt.to_string())LangChain
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate system_template = SystemMessagePromptTemplate.from_template("You are a helpful assistant.") human_template = HumanMessagePromptTemplate.from_template("Translate this: {text}") chat_prompt = ChatPromptTemplate.from_messages([system_template, human_template]) final_prompt = chat_prompt.format_prompt(text="Hello world") print(final_prompt.to_string())
Attempts:
2 left
💡 Hint
Remember that format_prompt replaces placeholders in all message templates.
✗ Incorrect
The ChatPromptTemplate combines system and human messages. The human message has a placeholder {text} replaced by 'Hello world'. The to_string() method shows role and content lines.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in ChatPromptTemplate usage?
Identify which code snippet will raise a syntax error when creating a ChatPromptTemplate with message templates.
Attempts:
2 left
💡 Hint
Check if all curly braces are properly closed in the template strings.
✗ Incorrect
Option A has an unclosed placeholder {word which causes a syntax error during template parsing.
❓ state_output
advanced2:00remaining
What is the value of 'final_prompt.messages[1].content' after formatting?
Consider this code snippet:
What will be printed?
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
chat_prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("System: {system_msg}"),
HumanMessagePromptTemplate.from_template("User says: {user_msg}")
])
final_prompt = chat_prompt.format_prompt(system_msg="Ready", user_msg="Hello")
result = final_prompt.messages[1].content
print(result)What will be printed?
LangChain
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate chat_prompt = ChatPromptTemplate.from_messages([ SystemMessagePromptTemplate.from_template("System: {system_msg}"), HumanMessagePromptTemplate.from_template("User says: {user_msg}") ]) final_prompt = chat_prompt.format_prompt(system_msg="Ready", user_msg="Hello") result = final_prompt.messages[1].content print(result)
Attempts:
2 left
💡 Hint
final_prompt.messages is a list of message objects in order of templates.
✗ Incorrect
The second message (index 1) is the human message with placeholder replaced by 'Hello'.
🔧 Debug
advanced2:00remaining
Why does this ChatPromptTemplate.format_prompt call raise a KeyError?
Examine this code:
Why does the last line raise a KeyError?
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
chat_prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("System: {system_msg}"),
HumanMessagePromptTemplate.from_template("User says: {user_msg}")
])
final_prompt = chat_prompt.format_prompt(system_msg="Ready")Why does the last line raise a KeyError?
LangChain
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate chat_prompt = ChatPromptTemplate.from_messages([ SystemMessagePromptTemplate.from_template("System: {system_msg}"), HumanMessagePromptTemplate.from_template("User says: {user_msg}") ]) final_prompt = chat_prompt.format_prompt(system_msg="Ready")
Attempts:
2 left
💡 Hint
Check if all placeholders in all message templates have matching arguments.
✗ Incorrect
The human message template has {user_msg} placeholder but format_prompt was called without user_msg argument, causing KeyError.
🧠 Conceptual
expert2:00remaining
Which option best describes the role of ChatPromptTemplate in LangChain?
Select the most accurate description of what ChatPromptTemplate does in LangChain.
Attempts:
2 left
💡 Hint
Think about how ChatPromptTemplate helps prepare prompts for chat models.
✗ Incorrect
ChatPromptTemplate organizes system and human message templates with placeholders, then formats them into a full prompt for chat models.