Bird
0
0

Given the following code, what will be the output of result?

medium📝 state output Q4 of 15
LangChain - Prompt Templates
Given the following code, what will be the output of result?
from langchain import PromptTemplate, LLMChain
prompt1 = PromptTemplate(template="Hello {name}")
prompt2 = PromptTemplate(template="How are you, {name}?")
chain = LLMChain(prompt=prompt1)
output1 = chain.run(name="Alice")
chain.prompt = prompt2
result = chain.run(name="Alice")
A"Hello Alice"
B"Hello Alice" then "How are you, Alice?"
CError because prompt cannot be changed after creation
D"How are you, Alice?"
Step-by-Step Solution
Solution:
  1. Step 1: Understand prompt assignment in LLMChain

    The chain is first created with prompt1, then prompt is replaced by prompt2 before second run.
  2. Step 2: Analyze output of second run

    Since prompt2 is active at second run, output will be from prompt2: "How are you, Alice?".
  3. Final Answer:

    "How are you, Alice?" -> Option D
  4. Quick Check:

    Changing prompt before run changes output [OK]
Quick Trick: Last assigned prompt defines output in LLMChain run [OK]
Common Mistakes:
  • Assuming output1 and result combine
  • Believing prompt cannot be changed after creation
  • Expecting output from first prompt on second run

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes