0
0
LangChainframework~20 mins

Few-shot prompt templates in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Few-shot Prompt Master
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 few-shot prompt template code?
Consider this LangChain few-shot prompt template code snippet. What will be the final prompt text after rendering?
LangChain
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

examples = [
    {"input": "Hello", "output": "Hi there!"},
    {"input": "Bye", "output": "Goodbye!"}
]

example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template="Input: {input}\nResponse: {output}"
)

few_shot = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="You are a helpful assistant.",
    suffix="Input: {user_input}\nResponse:",
    input_variables=["user_input"]
)

final_prompt = few_shot.format(user_input="Hello")
print(final_prompt)
A
You are a helpful assistant.
Input: Hello
Response: Hi there!
Input: Bye
Response: Goodbye!
Input: Hello
Response:
B
You are a helpful assistant.
Input: Hello
Response: Hi there!
Input: Bye
Response: Goodbye!
Input: Hello
Response: Hi there!
C
You are a helpful assistant.
Input: Hello
Response: Hi there!
Input: Bye
Response: Goodbye!
Response: Hello
Input:
D
You are a helpful assistant.
Input: Hello
Response: Goodbye!
Input: Bye
Response: Hi there!
Input: Hello
Response:
Attempts:
2 left
💡 Hint
Look at how the examples are formatted and how the suffix uses the user input.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a FewShotPromptTemplate with multiple input variables?
You want to create a FewShotPromptTemplate that uses examples with inputs 'question' and 'answer', and a suffix that takes 'new_question'. Which code snippet is correct?
A
FewShotPromptTemplate(
  examples=examples,
  example_prompt=PromptTemplate(input_variables=["question", "answer"], template="Q: {question}\nA: {answer}"),
  prefix="Answer the questions.",
  suffix="Q: {new_question}\nA:",
  input_variables=["answer"]
)
B
FewShotPromptTemplate(
  examples=examples,
  example_prompt=PromptTemplate(input_variables=["question", "answer"], template="Q: {question}\nA: {answer}"),
  prefix="Answer the questions.",
  suffix="Q: {question}\nA:",
  input_variables=["new_question"]
)
C
FewShotPromptTemplate(
  examples=examples,
  example_prompt=PromptTemplate(input_variables=["question", "answer"], template="Q: {question}\nA: {answer}"),
  prefix="Answer the questions.",
  suffix="Q: {new_question}\nA:",
  input_variables=["question"]
)
D
FewShotPromptTemplate(
  examples=examples,
  example_prompt=PromptTemplate(input_variables=["question", "answer"], template="Q: {question}\nA: {answer}"),
  prefix="Answer the questions.",
  suffix="Q: {new_question}\nA:",
  input_variables=["new_question"]
)
Attempts:
2 left
💡 Hint
Check that the suffix uses the same variable name as in input_variables.
🔧 Debug
advanced
2:00remaining
What error does this FewShotPromptTemplate code raise?
This code tries to create a FewShotPromptTemplate but fails. What error will it raise?
LangChain
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

examples = [{"input": "Hi", "output": "Hello!"}]

example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template="Input: {input}\nOutput: {output}"
)

few_shot = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Start conversation.",
    suffix="Input: {user_input}\nOutput:",
    input_variables=["user_input", "extra"]
)

few_shot.format(user_input="Hi")
ANo error, outputs the formatted prompt
BKeyError: 'extra'
CValueError: input_variables contains variables not in suffix or prefix
DTypeError: format() missing 1 required positional argument
Attempts:
2 left
💡 Hint
Check which variables are used in suffix and prefix versus input_variables.
state_output
advanced
2:00remaining
What is the number of example blocks in the rendered prompt?
Given this FewShotPromptTemplate with 3 examples, how many example blocks appear in the final prompt string?
LangChain
examples = [
  {"q": "What is AI?", "a": "Artificial Intelligence."},
  {"q": "What is ML?", "a": "Machine Learning."},
  {"q": "What is NLP?", "a": "Natural Language Processing."}
]

example_prompt = PromptTemplate(
  input_variables=["q", "a"],
  template="Q: {q}\nA: {a}"
)

few_shot = FewShotPromptTemplate(
  examples=examples,
  example_prompt=example_prompt,
  prefix="Answer the following questions.",
  suffix="Q: {new_q}\nA:",
  input_variables=["new_q"]
)

prompt_text = few_shot.format(new_q="What is Python?")
A4
B1
C3
D0
Attempts:
2 left
💡 Hint
Count how many example dictionaries are in the examples list.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of 'example_prompt' in FewShotPromptTemplate?
In LangChain's FewShotPromptTemplate, what is the main purpose of the 'example_prompt' parameter?
AIt defines how each example dictionary is formatted into text before being inserted into the prompt.
BIt specifies the variables required by the suffix template for new inputs.
CIt controls the order in which examples appear in the final prompt.
DIt validates the types of inputs passed to the FewShotPromptTemplate.
Attempts:
2 left
💡 Hint
Think about how examples become text in the prompt.