0
0
LangChainframework~20 mins

ChatPromptTemplate for conversations in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ChatPromptTemplate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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())
AYou are a helpful assistant.\nTranslate this: Hello world
BSystem: You are a helpful assistant.\nHuman: Translate this: {text}
CSystem: You are a helpful assistant.\nHuman: Translate this: Hello world
DSystem: You are a helpful assistant.\nHuman: Hello world
Attempts:
2 left
💡 Hint
Remember that format_prompt replaces placeholders in all message templates.
📝 Syntax
intermediate
2: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.
AChatPromptTemplate.from_messages([SystemMessagePromptTemplate.from_template("Hello"), HumanMessagePromptTemplate.from_template("Say {word")])
BChatPromptTemplate.from_messages([SystemMessagePromptTemplate.from_template("Hello"), HumanMessagePromptTemplate.from_template("Say {word}")])
CChatPromptTemplate.from_messages([SystemMessagePromptTemplate.from_template("Hello"), HumanMessagePromptTemplate.from_template("Say {word}!")])
DChatPromptTemplate.from_messages([SystemMessagePromptTemplate.from_template("Hello"), HumanMessagePromptTemplate.from_template("Say {word}.")])
Attempts:
2 left
💡 Hint
Check if all curly braces are properly closed in the template strings.
state_output
advanced
2:00remaining
What is the value of 'final_prompt.messages[1].content' after formatting?
Consider this code snippet:

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)
ASystem: Ready
BUser says: Hello
CUser says: {user_msg}
DSystem: {system_msg}
Attempts:
2 left
💡 Hint
final_prompt.messages is a list of message objects in order of templates.
🔧 Debug
advanced
2:00remaining
Why does this ChatPromptTemplate.format_prompt call raise a KeyError?
Examine this code:

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")
ABecause the placeholder {system_msg} is missing in the template
BBecause the HumanMessagePromptTemplate is not properly imported
CBecause format_prompt requires no arguments for this template
DBecause the placeholder {user_msg} is not provided in format_prompt arguments
Attempts:
2 left
💡 Hint
Check if all placeholders in all message templates have matching arguments.
🧠 Conceptual
expert
2:00remaining
Which option best describes the role of ChatPromptTemplate in LangChain?
Select the most accurate description of what ChatPromptTemplate does in LangChain.
AIt combines multiple message templates into a single prompt that can be formatted with variables for chat models.
BIt executes chat models and returns their responses directly without formatting.
CIt stores raw text prompts without any variable placeholders or message roles.
DIt is used to train chat models by providing labeled conversation data.
Attempts:
2 left
💡 Hint
Think about how ChatPromptTemplate helps prepare prompts for chat models.