Class that selects examples based on semantic similarity. It extends the BaseExampleSelector class.
class SemanticSimilarityExampleSelectorconst 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" }));A path to the module that contains the class, eg. ["langchain", "llms"] Usually should be the same as the entrypoint the class is exported from.
Method that adds a new example to the vectorStore. The example is converted to a string and added to the vectorStore as a document.
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.
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.
The name of the serializable. Override to provide an alias or to preserve the serialized module name in minified environments.
Implemented as a static method to support loading logic.
A path to the module that contains the class, eg. ["langchain", "llms"]
Adds an example to the example selector.
Selects examples from the example selector given the input variables.
The name of the serializable. Override to provide an alias or
A path to the module that contains the class, eg. ["langchain", "llms"]