Few-Shot Templates

Open in ColabOpen in GitHub

Overview

LangChain's few-shot prompting provides a robust framework for guiding language models to generate high-quality outputs by supplying carefully selected examples. This technique minimizes the need for extensive model fine-tuning while ensuring precise, context-aware results across diverse use cases.

  • Few-shot prompt templates : Define the structure and format of prompts by embedding illustrative examples, guiding the model to produce consistent outputs.

  • Example selection strategies : Dynamically select the most relevant examples for a given query, enhancing the model's contextual understanding and response accuracy.

  • Chroma vector store : A powerful utility for storing and retrieving examples based on semantic similarity, enabling scalable and efficient prompt construction.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

FewShotPromptTemplate

Few-shot prompting is a powerful technique that guides language models to produce accurate and contextually relevant outputs by providing a small set of carefully designed examples. LangChain's FewShotPromptTemplate streamlines this process, allowing users to construct flexible and reusable prompts for various tasks like question answering, summarization, and correction.

  1. Designing Few-Shot Prompts

    • Define examples that illustrate the desired structure and style of the output.

    • Ensure examples cover edge cases to improve model understanding and performance.

  2. Dynamic Example Selection

    • Use semantic similarity techniques or vector-based search to select the most relevant examples for a query.

  3. Integrating Few-Shot Prompts

    • Combine prompt templates with language models to create robust chains for generating responses.

FewShotPromptTemplate Example

The FewShotPromptTemplate allows you to provide a language model with a small set of examples that demonstrate the desired structure and format of its output. By leveraging these examples, the model can better understand the context and generate more accurate responses for new queries. This technique is especially useful for tasks like question answering, summarization, or generating structured outputs.

Below, we define a few examples to help the model answer questions more effectively by breaking them down into intermediate steps. We then use the FewShotPromptTemplate to format the prompt dynamically based on the query.


Dynamic Example Selection with Chroma

Sometimes we need to go through multiple steps of thinking to evaluate a single question. Breaking down the question into steps and guiding towards the desired answer can lead to better quality responses.Chroma provides an efficient way to store and retrieve examples based on semantic similarity, enabling dynamic example selection in workflows.

  1. Embedding and Vector Store Initialization

    • Use OpenAIEmbeddings to embed examples.

    • Store the embeddings in a Chroma vector store for efficient retrieval.

  2. Example Storage

    • Examples are stored with both their content and metadata.

    • Metadata can include details such as the question and answer, which are used for further processing after retrieval.

  3. Similarity Search

    • Query the vector store to retrieve the most similar examples based on the input.

    • Enables context-aware dynamic prompt creation.

Import the necessary packages and create a vector store.

Create example question-answer processes needed to derive the answer.

Create a vector store and define the DynamicFewShotLearning template.

Let's run it to verify if it produces answers through our desired process.

FewShotChatMessagePromptTemplate

Creating prompts for each situation is a complex and tedious task. The FewShotChatMessagePromptTemplate leverages a few-shot learning approach to dynamically generate chat-based prompts by combining relevant examples with a structured format. This method is especially effective for tasks like summarization, meeting minute creation, or document processing.

Resolving Similarity Search Issues in Example Selector

When calculating similarity, both instruction and input are used. However, searching based only on instruction does not yield accurate similarity results.

To resolve this, a custom class for similarity calculation is defined.

Below is an example of incorrectly retrieved results.

Last updated