Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Create a Few-Shot Prompt Template with Langchain
📖 Scenario: You are building a chatbot that answers questions by learning from a few example conversations. You want to create a prompt template that includes these examples to guide the AI's responses.
🎯 Goal: Build a few-shot prompt template using Langchain that includes example questions and answers, and a placeholder for the user's question.
📋 What You'll Learn
Create a list of example conversations as dictionaries with 'question' and 'answer' keys
Define a prefix string that introduces the examples
Use Langchain's FewShotPromptTemplate to combine the prefix, examples, and a suffix with a placeholder
Set up the final prompt template that can be used to generate prompts with new questions
💡 Why This Matters
🌍 Real World
Few-shot prompt templates help AI models understand how to answer questions by showing a few examples first, similar to teaching by example in real life.
💼 Career
Knowing how to build few-shot prompts is useful for AI developers and prompt engineers who create conversational agents and smart assistants.
Progress0 / 4 steps
1
Set up example conversations data
Create a list called examples with two dictionaries. The first dictionary should have 'question': 'What is the capital of France?' and 'answer': 'Paris'. The second dictionary should have 'question': 'Who wrote Hamlet?' and 'answer': 'William Shakespeare'.
LangChain
Hint
Use a list with two dictionaries exactly as described.
2
Define the prefix string for the prompt
Create a string variable called prefix with the exact value: 'Answer the following questions based on the examples:'.
LangChain
Hint
Assign the exact string to the variable prefix.
3
Create the FewShotPromptTemplate with Langchain
Import FewShotPromptTemplate and PromptTemplate from langchain.prompts. Then create a PromptTemplate called example_prompt with input variables question and answer, and template string Q: {question}\nA: {answer}. Next, create a FewShotPromptTemplate called few_shot_prompt using examples, example_prompt, prefix, and a suffix string Q: {input}\nA: with input variable input.
LangChain
Hint
Use the exact class names and variable names as specified.
4
Use the few-shot prompt template with a new question
Create a variable called new_question with the value 'What is the boiling point of water?'. Then create a variable called final_prompt by calling few_shot_prompt.format(input=new_question).
LangChain
Hint
Use the exact variable names and call the format method with the correct argument.
Practice
(1/5)
1. What is the main purpose of a few-shot prompt template in Langchain?
easy
A. To provide example prompts and responses to guide AI behavior
B. To store large datasets for training AI models
C. To execute code on the AI server
D. To create user interfaces for AI applications
Solution
Step 1: Understand few-shot prompt templates
Few-shot prompt templates include example prompts and responses to teach AI how to answer.
Step 2: Identify the main purpose
The main goal is to guide AI behavior by showing examples, not to store data or create interfaces.
Final Answer:
To provide example prompts and responses to guide AI behavior -> Option A
Quick Check:
Few-shot prompt templates guide AI with examples = A [OK]
Hint: Remember: few-shot means showing examples to teach AI [OK]
Common Mistakes:
Confusing prompt templates with data storage
Thinking they run code instead of guiding AI
Assuming they build UI components
2. Which of the following is the correct way to create a few-shot prompt template in Langchain?
easy
A. FewShotPromptTemplate(data=examples, prompt=example_prompt, suffix=prefix_text)
B. FewShotPromptTemplate(samples=examples, prompt_template=example_prompt, header=prefix_text)
C. FewShotPromptTemplate(examples=examples, example_prompt=example_prompt, prefix=prefix_text)
D. FewShotPromptTemplate(inputs=examples, prompt=example_prompt, footer=prefix_text)
Solution
Step 1: Recall Langchain few-shot prompt syntax
The correct constructor uses parameters: examples, example_prompt, and prefix.
Step 2: Match parameters to options
Only FewShotPromptTemplate(examples=examples, example_prompt=example_prompt, prefix=prefix_text) uses the exact parameter names required by Langchain's FewShotPromptTemplate.
Final Answer:
FewShotPromptTemplate(examples=examples, example_prompt=example_prompt, prefix=prefix_text) -> Option C
Quick Check:
Correct parameter names = B [OK]
Hint: Check parameter names exactly as in Langchain docs [OK]
Common Mistakes:
Using wrong parameter names like data or samples
Mixing prefix with suffix or footer
Confusing example_prompt with prompt_template
3. Given this code snippet, what will be the output of print(prompt_template.format(input="Translate to French: Hello") )?
A. Translate English to French:
Input: Translate to French: Hello
Output:
B. Translate English to French:
Input: Hello
Output: Bonjour
Translate to French: Hello
C. Input: Hello
Output: Bonjour
Translate English to French: Hello
D. Translate English to French:
Input: Hello
Output: Bonjour
Input: Translate to French: Hello
Output:
Solution
Step 1: Understand few-shot prompt formatting
The prompt includes the prefix, then example prompts formatted with example data, then the new input prompt.
Step 2: Apply formatting to given input
The prefix is "Translate English to French:", then example "Input: Hello\nOutput: Bonjour", then the new input "Input: Translate to French: Hello\nOutput:" (empty output to be filled by AI).
Final Answer:
Translate English to French:
Input: Hello
Output: Bonjour
Input: Translate to French: Hello
Output: -> Option D
Quick Check:
Prefix + example + new input prompt = C [OK]
Hint: Few-shot templates show prefix, examples, then new input [OK]
Common Mistakes:
Ignoring prefix text in output
Not formatting new input as 'Input: ... Output:'
Confusing example data with new input
4. What is the error in this code snippet that tries to create a few-shot prompt template?
FewShotPromptTemplate requires input_variables parameter to know which inputs to expect.
Step 2: Identify missing parameter
The code misses input_variables in FewShotPromptTemplate, causing an error when calling format.
Final Answer:
Missing input_variables parameter in FewShotPromptTemplate constructor -> Option B
Quick Check:
input_variables missing = D [OK]
Hint: Always include input_variables when creating prompt templates [OK]
Common Mistakes:
Omitting input_variables in FewShotPromptTemplate
Thinking prefix must be a function
Assuming examples can be empty
5. You want to create a few-shot prompt template that filters out examples with empty outputs before formatting. Which approach correctly applies this filtering in Langchain?
hard
A. Filter the examples list before passing it to FewShotPromptTemplate constructor
B. Use a custom example_prompt that skips empty outputs during formatting
C. Set prefix to None and rely on Langchain to ignore empty outputs
D. Pass all examples and filter outputs after calling prompt_template.format()
Solution
Step 1: Understand filtering in few-shot templates
FewShotPromptTemplate uses the examples list as-is; filtering must happen before passing examples.
Step 2: Evaluate options for filtering
Only filtering the examples list before creating the template ensures empty outputs are excluded properly.
Final Answer:
Filter the examples list before passing it to FewShotPromptTemplate constructor -> Option A
Quick Check:
Pre-filter examples before constructor = A [OK]
Hint: Filter examples before creating the prompt template [OK]