0
0
LangchainHow-ToBeginner ยท 4 min read

How to Use Few Shot Prompt in LangChain: Simple Guide

In LangChain, use FewShotPromptTemplate to create prompts with example pairs that guide the language model. Provide a list of examples, a example_prompt template, and a prefix and suffix to build the full prompt for better context and accuracy.
๐Ÿ“

Syntax

The FewShotPromptTemplate in LangChain requires these parts:

  • examples: A list of example inputs and outputs to show the model how to respond.
  • example_prompt: A prompt template that formats each example.
  • prefix: Text before the examples to set the task context.
  • suffix: Text after the examples where the new input will be inserted.
  • input_variables: Variables used in the suffix for the new input.

This structure helps the model understand the task by showing it examples before asking it to generate a new answer.

python
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

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

examples = [
    {"input": "Hello", "output": "Hi there!"},
    {"input": "How are you?", "output": "I'm good, thanks!"}
]

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="The following are greetings and responses.",
    suffix="Input: {input}\nOutput:",
    input_variables=["input"]
)
๐Ÿ’ป

Example

This example shows how to create a few shot prompt and generate a response using OpenAI's GPT-4 model with LangChain.

python
from langchain.chat_models import ChatOpenAI
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

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

# Provide example input-output pairs
examples = [
    {"input": "Hello", "output": "Hi there!"},
    {"input": "How are you?", "output": "I'm good, thanks!"}
]

# Create few shot prompt template
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="The following are greetings and responses.",
    suffix="Input: {input}\nOutput:",
    input_variables=["input"]
)

# Prepare the prompt with a new input
new_input = "Good morning"
prompt_text = few_shot_prompt.format(input=new_input)

# Initialize the chat model
chat = ChatOpenAI(model_name="gpt-4", temperature=0)

# Get the response
response = chat.predict(prompt_text)
print(response)
Output
Good morning! How can I help you today?
โš ๏ธ

Common Pitfalls

Common mistakes when using few shot prompts in LangChain include:

  • Not formatting examples correctly with example_prompt, causing confusing prompts.
  • Forgetting to include all input_variables in the suffix template.
  • Using too many or irrelevant examples, which can overwhelm the model or reduce clarity.
  • Not setting a clear prefix to explain the task context.

Always test your prompt output before using it with the model to ensure clarity.

python
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

# Wrong: Missing input_variables in suffix
wrong_few_shot = FewShotPromptTemplate(
    examples=[{"input": "Hi", "output": "Hello!"}],
    example_prompt=PromptTemplate(input_variables=["input", "output"], template="Input: {input}\nOutput: {output}"),
    prefix="Greet the user.",
    suffix="Input:\nOutput:",  # Missing {input} variable
    input_variables=["input"]
)

# Right: Include {input} in suffix
right_few_shot = FewShotPromptTemplate(
    examples=[{"input": "Hi", "output": "Hello!"}],
    example_prompt=PromptTemplate(input_variables=["input", "output"], template="Input: {input}\nOutput: {output}"),
    prefix="Greet the user.",
    suffix="Input: {input}\nOutput:",
    input_variables=["input"]
)
๐Ÿ“Š

Quick Reference

  • examples: List of example dictionaries with input/output pairs.
  • example_prompt: Template to format each example.
  • prefix: Text before examples to explain the task.
  • suffix: Text after examples with placeholder for new input.
  • input_variables: Variables used in suffix for new input.

Use FewShotPromptTemplate.format() to create the full prompt string before sending it to the model.

โœ…

Key Takeaways

Use FewShotPromptTemplate with examples, example_prompt, prefix, and suffix to guide the model.
Always include all input variables in the suffix template for correct prompt formatting.
Keep examples relevant and concise to improve model understanding and output quality.
Test your prompt output before sending it to the language model to avoid errors.
Few shot prompting helps the model learn task patterns from examples, improving accuracy.