Few-shot prompting

Developing LLM Applications with LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Limitations of standard prompt templates

 

  • PromptTemplate + ChatPromptTemplate
  • ✅ Handling small numbers of examples
  • ❌ Don't scale for many examples
  • FewShotPromptTemplate

 

examples = [
    {
        "question": "..."
        "answer": "..."
    },
    ...
]
Developing LLM Applications with LangChain

Building an example set

examples = [
    {
        "question": "Does Henry Campbell have any pets?",
        "answer": "Henry Campbell has a dog called Pluto."
    },
    ...
]
# Convert pandas DataFrame to list of dicts
examples = df.to_dict(orient="records")
Developing LLM Applications with LangChain

Formatting the examples

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

example_prompt = PromptTemplate.from_template("Question: {question}\n{answer}")
prompt = example_prompt.invoke({"question": "What is the capital of Italy?"
                                "answer": "Rome"})
print(prompt.text)
Question: What is the capital of Italy?
Rome
Developing LLM Applications with LangChain

FewShotPromptTemplate

 

prompt_template = FewShotPromptTemplate(

examples=examples,
example_prompt=example_prompt,
suffix="Question: {input}",
input_variables=["input"]
)

 

 

  • examples: the list of dicts
  • example_prompt: formatted template
  • suffix: suffix to add to the input
  • input_variables
Developing LLM Applications with LangChain

Invoking the few-shot prompt template

prompt = prompt_template.invoke({"input": "What is the name of Henry Campbell's dog?"})

print(prompt.text)
Question: Does Henry Campbell have any pets?
Henry Campbell has a dog called Pluto.
...

Question: What is the name of Henry Campbell's dog?
Developing LLM Applications with LangChain

Integration with a chain

llm = ChatOpenAI(model="gpt-4o-mini", api_key="...")

llm_chain = prompt_template | llm response = llm_chain.invoke({"input": "What is the name of Henry Campbell's dog?"})
print(response.content)
The name of Henry Campbell's dog is Pluto.
Developing LLM Applications with LangChain

Let's practice!

Developing LLM Applications with LangChain

Preparing Video For Download...