Class that selects examples based on semantic similarity. It extends the BaseExampleSelector class.

const exampleSelector = await SemanticSimilarityExampleSelector.fromExamples(
[
{ input: "happy", output: "sad" },
{ input: "tall", output: "short" },
{ input: "energetic", output: "lethargic" },
{ input: "sunny", output: "gloomy" },
{ input: "windy", output: "calm" },
],
new OpenAIEmbeddings(),
HNSWLib,
{ k: 1 },
);
const dynamicPrompt = new FewShotPromptTemplate({
exampleSelector,
examplePrompt: PromptTemplate.fromTemplate(
"Input: {input}\nOutput: {output}",
),
prefix: "Give the antonym of every input",
suffix: "Input: {adjective}\nOutput:",
inputVariables: ["adjective"],
});
console.log(await dynamicPrompt.format({ adjective: "rainy" }));

Type Parameters

Hierarchy (view full)

Constructors

Properties

vectorStoreRetriever: VectorStoreRetrieverInterface<V>
exampleKeys?: string[]
inputKeys?: string[]

Methods

  • Method that selects which examples to use based on semantic similarity. It performs a similarity search in the vectorStore using the input variables and returns the examples with the highest similarity.

    Type Parameters

    • T

    Parameters

    • inputVariables: Record<string, T>

      The input variables used for the similarity search.

    Returns Promise<Example[]>

    Promise that resolves with an array of the selected examples.

  • Static method that creates a new instance of SemanticSimilarityExampleSelector. It takes a list of examples, an instance of Embeddings, a VectorStore class, and an options object as parameters. It converts the examples to strings, creates a VectorStore from the strings and the embeddings, and returns a new SemanticSimilarityExampleSelector with the created VectorStore and the options provided.

    Type Parameters

    Parameters

    • examples: Record<string, string>[]

      The list of examples to be used.

    • embeddings: Embeddings

      The instance of Embeddings to be used.

    • vectorStoreCls: C

      The VectorStore class to be used.

    • options: {
          inputKeys?: string[];
          k?: number;
      } & Parameters<C["fromTexts"]>[3] = {}

      The options object for the SemanticSimilarityExampleSelector.

    Returns Promise<SemanticSimilarityExampleSelector<VectorStoreInterface>>

    Promise that resolves with a new instance of SemanticSimilarityExampleSelector.

""