0
0
LangChainframework~5 mins

Few-shot prompt templates in LangChain

Choose your learning style9 modes available
Introduction

Few-shot prompt templates help you teach AI models by showing a few examples before asking a question. This makes the AI understand better what you want.

When you want the AI to follow a pattern shown in examples.
When you want to improve AI answers by giving it context.
When you want to create a prompt that includes example questions and answers.
When you want to reuse a prompt with different inputs but same examples.
When you want to guide AI to perform tasks like translation or classification.
Syntax
LangChain
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

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

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

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Answer the following greetings:",
    suffix="Input: {input}",
    input_variables=["input"]
)

The examples list holds example input-output pairs.

The prefix and suffix add text before and after examples.

Examples
This example shows how to create a few-shot prompt with different greetings.
LangChain
examples = [
    {"input": "Good morning", "output": "Morning!"},
    {"input": "Good night", "output": "Sleep well!"}
]

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Respond to greetings:",
    suffix="Input: {input}",
    input_variables=["input"]
)
This formats the prompt by inserting the input after the examples and prefix.
LangChain
few_shot_prompt.format(input="Hello there")
Sample Program

This program creates a few-shot prompt with two example words and definitions. Then it formats the prompt to ask for the definition of "JavaScript".

LangChain
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

example_prompt = PromptTemplate(
    input_variables=["word", "definition"],
    template="Word: {word}\nDefinition: {definition}"
)

examples = [
    {"word": "Python", "definition": "A programming language."},
    {"word": "Java", "definition": "Another programming language."}
]

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Here are some words and their definitions:",
    suffix="Word: {word}\nDefinition:",
    input_variables=["word"]
)

prompt_text = few_shot_prompt.format(word="JavaScript")
print(prompt_text)
OutputSuccess
Important Notes

Keep examples clear and relevant to help the AI understand the task.

You can change the prefix and suffix to guide the AI's response style.

Use format to insert new inputs into the prompt before sending to the AI.

Summary

Few-shot prompt templates show examples to teach AI how to respond.

They combine example prompts, example data, and extra text around them.

This helps AI give better, more accurate answers.