Bird
0
0

Given this code snippet:

medium📝 component behavior Q4 of 15
LangChain - Prompt Templates
Given this code snippet:
from langchain.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("user", "Hello, what is the weather today?")
])
print(prompt.format())

What will be the output?
AAn empty string
BAn error because from_messages does not exist
CA formatted string combining system and user messages
DOnly the user message printed
Step-by-Step Solution
Solution:
  1. Step 1: Understand from_messages method

    This method creates a ChatPromptTemplate from a list of message tuples with roles and content.
  2. Step 2: Analyze prompt.format() behavior

    Calling format() combines the messages into a single formatted string including both system and user messages.
  3. Final Answer:

    A formatted string combining system and user messages -> Option C
  4. Quick Check:

    from_messages + format() = combined messages [OK]
Quick Trick: from_messages builds prompt; format() outputs combined text [OK]
Common Mistakes:
  • Assuming from_messages is invalid
  • Expecting only one message output
  • Thinking format() returns empty string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes