0
0
LangChainframework~10 mins

Few-shot prompt templates in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Few-shot prompt templates
Start: Define examples
Create prompt template with examples
Add user input
Generate full prompt
Send prompt to model
Receive model output
Return output to user
This flow shows how few-shot prompt templates combine example pairs with user input to create a prompt for the model, then get and return the output.
Execution Sample
LangChain
from langchain.prompts import PromptTemplate, FewShotPromptTemplate

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

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

prompt = FewShotPromptTemplate(
  examples=examples,
  input_variables=["input"],
  example_prompt=example_template,
  suffix="Input: {input}\nOutput: "
)

full_prompt = prompt.format(input="Thanks")
This code creates a few-shot prompt with two examples and formats it with a new input.
Execution Table
StepActionInputPrompt ContentOutput
1Define examples[{"input": "Hello", "output": "Hi!"}, {"input": "Bye", "output": "Goodbye!"}]N/AExamples stored
2Create FewShotPromptTemplateexamples + input_variables + example_promptTemplate ready with placeholdersTemplate object created
3Format prompt with input"Thanks"Includes examples + 'Thanks' inputFull prompt string generated
4Send prompt to modelFull prompt stringPrompt sent to language modelModel processes prompt
5Receive model outputModel responseN/A"You're welcome!" (example output)
6Return outputModel outputN/A"You're welcome!" returned to user
💡 Process ends after model output is returned to the user.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5Final
examplesempty[{"input": "Hello", "output": "Hi!"}, {"input": "Bye", "output": "Goodbye!"}][same][same][same][same]
promptundefinedundefinedFewShotPromptTemplate objectFewShotPromptTemplate objectFewShotPromptTemplate objectFewShotPromptTemplate object
full_promptundefinedundefinedundefinedFull prompt string with examples and inputFull prompt stringFull prompt string
model_outputundefinedundefinedundefinedundefined"You're welcome!""You're welcome!"
Key Moments - 3 Insights
Why do we include examples in the prompt before the user input?
Including examples shows the model how to respond, guiding it to produce similar answers. See execution_table step 3 where examples are combined with input.
What happens if we forget to format the prompt with the user input?
The prompt will only have examples but no new input, so the model won't know what to answer. Refer to execution_table step 3 where formatting adds the input.
Is the model output always the same for the same input?
No, the model can produce different outputs due to randomness or settings. The example output in step 5 is just one possible answer.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does the 'full_prompt' contain?
AOnly the examples without user input
BOnly the new user input without examples
CExamples plus the new user input formatted together
DAn empty string
💡 Hint
Check the 'Prompt Content' column at step 3 in the execution_table.
At which step does the model generate a response?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Look at the 'Output' column in the execution_table for when the model output appears.
If we add more examples, how does the 'full_prompt' change at step 3?
AIt stays the same size
BIt includes more example pairs before the user input
CIt removes the user input
DIt becomes empty
💡 Hint
Refer to how examples are combined with input in the 'Prompt Content' at step 3.
Concept Snapshot
Few-shot prompt templates combine example input-output pairs with new user input.
They create a prompt showing examples first, then the new input.
This guides the model to respond similarly.
Use format() to insert user input into the template.
Send the full prompt to the model to get output.
Useful for teaching the model by example.
Full Transcript
Few-shot prompt templates work by first defining example input-output pairs. Then, these examples are combined with the user's new input to create a full prompt. This prompt is sent to the language model, which generates a response based on the examples and input. The process involves creating the template, formatting it with input, sending it to the model, and returning the output. This method helps the model understand how to respond by showing examples first.