0
0
LangChainframework~10 mins

Few-shot prompt templates in LangChain - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a few-shot prompt template with examples.

LangChain
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

example_prompt = PromptTemplate(input_variables=["word"], template="Translate '{{word}}' to French: '[1]'")

examples = [{"word": "hello", "translation": "bonjour"}]

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    input_variables=["word"]
)
Drag options to blanks, or click blank then click option'
Atranslation
Bbonjour
Chello
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the input variable 'word' instead of the output variable 'translation' in the template.
Confusing the example keys with input variables.
2fill in blank
medium

Complete the code to set the prefix for the few-shot prompt template.

LangChain
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix=[1],
    input_variables=["word"]
)
Drag options to blanks, or click blank then click option'
ATranslate English to French
BTranslate English to French:
C'Translate English to French:'
D"Translate English to French:"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the prefix string.
Using single quotes inconsistently.
3fill in blank
hard

Fix the error in the code to correctly create a few-shot prompt template with suffix.

LangChain
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Translate English to French:",
    suffix=[1],
    input_variables=["word"]
)
Drag options to blanks, or click blank then click option'
A"Input: {word}\nOutput:"
BInput: {word}\nOutput:
C'Input: {word}\nOutput:'
DInput: {word} Output:
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the suffix string.
Not escaping the newline character properly.
4fill in blank
hard

Fill both blanks to create a few-shot prompt template with example separator and suffix.

LangChain
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    example_separator=[1],
    suffix=[2],
    input_variables=["word"]
)
Drag options to blanks, or click blank then click option'
A"\n---\n"
B"\n"
C"Input: {word}\nOutput:"
D"\n\n"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect separator strings without newlines.
Not including the input variable placeholder in the suffix.
5fill in blank
hard

Fill all three blanks to build a few-shot prompt template with prefix, example separator, and suffix.

LangChain
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix=[1],
    example_separator=[2],
    suffix=[3],
    input_variables=["word"]
)
Drag options to blanks, or click blank then click option'
A"Translate English to French:"
B"\n---\n"
C"Input: {word}\nOutput:"
D"Please translate the following words:"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up prefix and suffix strings.
Not using proper escape sequences in separator and suffix.