What is a FewShotPromptTemplate in LangChain?

Few-shot learning represents a fascinating leap in the way models handle new information. It’s similar to teaching someone a new game by showing them just a few rounds rather than explaining every rule in detail. In the context of Large Language Models (LLMs), this approach is particularly groundbreaking. Imagine having a conversation with someone who can grasp the essence of a topic just by skimming through a couple of examples. That’s what few-shot learning enables in LLMs.

FewShotPromptTemplate in LangChain implements this concept. By weaving specific examples into the prompts, it acts like a bridge connecting what the user wants to know with what the model already knows. This isn’t just about feeding data, it’s also about setting a scene, providing a backdrop that allows the LLM to apply its vast knowledge more accurately to the task at hand. It’s like giving someone a few key pieces of a puzzle—with those in place, they’re far better equipped to complete the picture.

This method is a game-changer, particularly when dealing with complex queries. It’s not just about the amount of data anymore; it’s about the relevance and context of that data. By incorporating this approach, LLMs can become even more adept at understanding and responding to our queries in a way that feels incredibly intuitive and, well, human.

A puzzle

Imagine a puzzle symbolizing a tricky problem we want an LLM to crack. It’s a unique puzzle, with pieces that need context to fit together just right. Think of the LLM as a puzzle-savvy buddy who’s new to this particular challenge. Few-shot learning is like handing our buddy a few crucial puzzle pieces beforehand.

These pieces aren’t chosen at random. They’re carefully selected to contextualize the overall picture, each illustrating how to handle similar pieces. For example, if our puzzle revolves around pinpointing historical events with scant details, our chosen pieces might be a couple of well-detailed historical instances, hinting at what to look for in the rest.

FewShotPromptTemplate in LangChain is similar to the puzzle’s border, providing structure. It places our chosen pieces (the few-shot examples) within a frame, showing our buddy (the LLM) how they relate to the new piece (the user’s query).

When we bring a new puzzle piece (a query) to our buddy, it‘s within this structured setup, complete with reference pieces. Our buddy then uses insights from these references to figure out where the new piece fits in the bigger picture. FewShotPromptTemplate ensures these references are well-placed to be most effective.

Few-shot prompt structure with example
Few-shot prompt structure with example

In practice, this means the LLM can provide more accurate, contextually relevant answers to complex queries by referencing the few-shot examples provided in the prompt. So, FewShotPromptTemplate doesn't just give the LLM random hints; it offers a structured way to apply learned patterns from the examples to new, unseen problems, much like using reference pieces to solve a new section of a puzzle.

Practical example

The following code snippets (code cells from the Jupyter Notebook) demonstrate how to utilize FewShotPromptTemplate in LangChain for few-shot learning with LLMs. Each snippet (cell) plays a specific role in setting up, formatting, and using the template to guide the LLM toward more accurate and contextually relevant responses.

from langchain.prompts.few_shot import FewShotPromptTemplate
from langchain.prompts.prompt import PromptTemplate
Importing necessary classes

We import the FewShotPromptTemplate and PromptTemplate classes from LangChain. FewShotPromptTemplate is used for creating prompts with few-shot examples, and PromptTemplate is used for formatting those examples.

Next, we define a list of few-shot examples:

examples = [
# Example 1
{
"question": "What year did the Apollo 11 moon landing occur?",
"answer": "Are follow-up questions needed here: No.\nSo the final answer is: 1969",
},
# Example 2, 3, 4...
]
Defining few-shot examples

Each example is a dictionary with question and answer keys. These examples provide the model with context and demonstrate how to approach answering similar questions. They include both straightforward answers and those requiring follow-up questions, showcasing different types of reasoning. We can see the complete example in the Jupyter Notebook at the end of this Educative Answer.

Next, we initialize PromptTemplate to format the few-shot examples:

example_prompt = PromptTemplate(
input_variables=["question", "answer"], template="Question: {question}\n{answer}"
)
Creating a prompt formatter

input_variables are the keys from the example dictionaries, and template is the structure for how each example should be formatted as a string. This formatting is critical for presenting the examples to the LLM in a coherent and structured manner.

Next, let’s check if this works as intended or not:

print(example_prompt.format(**examples[0]))
Formatting a single example

We format a single example using example_prompt. This is a test to ensure that the examples are formatted correctly before using them in the few-shot prompt template. We have everything we need to set up our FewShotTemplatePrompt now:

prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
suffix="Question: {input}",
input_variables=["input"],
)
Setting up FewShotPromptTemplate

We initialize FewShotPromptTemplate with the defined examples and formatter. Here, suffix is the part of the prompt where the actual input question (to be answered by the LLM) will be appended after the few-shot examples.

Let’s verify if it works as intended or not:

print(prompt.format(input="Who discovered penicillin?"))
Generating a prompt with few-shot examples

We use the few-shot prompt template to generate a full prompt that includes the few-shot examples followed by a new question ("Who discovered penicillin?"). This demonstrates how the template combines predefined examples with new queries to guide the LLM.

Now, we’re ready to actually put this prompt under the test and pass it to our LLM:

formatted_prompt = prompt.format(input="Who discovered penicillin?")
response = llm.invoke(formatted_prompt)
print(response)
Invoking the LLM

We invoke the LLM with the formatted prompts, including few-shot examples and new questions. formatted_prompt is generated using the FewShotPromptTemplate class, and the llm.invoke() method is called to get the LLM’s response. The response is then printed to observe how the LLM answers the new question, guided by the context provided by the few-shot examples.

Overall, this sequence of steps illustrates how to effectively utilize few-shot learning in LangChain by providing the LLM with relevant examples and a structured approach to answering questions, which enhances the model’s ability to generate contextually appropriate and accurate responses.

Try it yourself

Launch the Jupyter Notebook below to see the LangChain’s FewShotPromptTemplate mechanisms in action and discover how they can transform conversational AI applications:

Please note that the notebook cells have been preconfigured to display the outputs
for your convenience and to facilitate an understanding of the concepts covered. 
However, if you possess the key, you are encouraged to actively engage with the material
by changing the variable values. This hands-on approach will allow you to
experiment with the memory techniques discussed, providing a more immersive learning experience.
Try it yourself
Copyright ©2024 Educative, Inc. All rights reserved