Class SemanticSimilarityExampleSelector<V>

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

Example

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

  • V extends VectorStore = VectorStore

Hierarchy

Constructors

Properties

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

Methods

  • Method that adds a new example to the vectorStore. The example is converted to a string and added to the vectorStore as a document.

    Parameters

    • example: Example

      The example to be added to the vectorStore.

    Returns Promise<void>

    Promise that resolves when the example has been added to the vectorStore.

  • 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

    • C extends typeof VectorStore

    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.

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

      The options object for the SemanticSimilarityExampleSelector.

    Returns Promise<SemanticSimilarityExampleSelector<VectorStore>>

    Promise that resolves with a new instance of SemanticSimilarityExampleSelector.

Generated using TypeDoc