AI Hints
Home›LangChain Tutorial›Examples, Chains, Parsers
LangChain · Lesson 16 of 120

Few-Shot Prompts

Teaching by example.

⏱ 2 min read▶ Video at 16:14🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Sometimes it's easier to show the model what you want than to describe it. A few examples of input and output, called shots, can teach a format or style very quickly.

Few-Shot Prompts
From the video: Few-Shot Prompts
  • Show, don't describe
  • A few examples
  • Called shots

Examples as messages

We want each book turned into a short tag line, with an emoji at the start. Instead of explaining, we show two examples, as human and AI message pairs, before the real question. Run it. The model copies the pattern from the examples, emoji and all, for a book it hasn't seen.

few_shot.pypython
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
prompt = ChatPromptTemplate.from_messages([
    ("system", "Turn each book into a short tagline."),
    ("human", "Moby Dick"), ("ai", "🐋 One man, one whale, one obsession."),
    ("human", "Dracula"), ("ai", "🧛 Never invite him in."),
    ("human", "{book}"),
])
print(model.invoke(prompt.invoke({"book": "Frankenstein"})).text)
Output
⚡ Some things were never meant to live.

Tips for examples

A few tips. Two to five examples are usually enough. Make them varied, so the model learns the pattern, not one specific answer. Keep them short, because every example costs tokens on every call. And make sure they're all correct, because the model will copy their mistakes too.

keyvalue
2–5 examplesusually enough
Variedlearn the pattern
Shortthey cost tokens
Correctmistakes are copied

Summary

Few shot prompts teach by example, using sample human and AI message pairs. Next, connecting a prompt and a model into a chain.

Key takeaways
  • Show examples
  • Human/AI pairs
  • Short, varied, correct
Next up: Chains with the Pipe — prompt then model