A specialized example selector that selects examples based on their length, ensuring that the total length of the selected examples does not exceed a specified maximum length.
class LengthBasedExampleSelectorconst exampleSelector = new LengthBasedExampleSelector(
[
{ input: "happy", output: "sad" },
{ input: "tall", output: "short" },
{ input: "energetic", output: "lethargic" },
{ input: "sunny", output: "gloomy" },
{ input: "windy", output: "calm" },
],
{
examplePrompt: new PromptTemplate({
inputVariables: ["input", "output"],
template: "Input: {input}\nOutput: {output}",
}),
maxLength: 25,
},
);
const dynamicPrompt = new FewShotPromptTemplate({
exampleSelector,
examplePrompt: new PromptTemplate({
inputVariables: ["input", "output"],
template: "Input: {input}\nOutput: {output}",
}),
prefix: "Give the antonym of every input",
suffix: "Input: {adjective}\nOutput:",
inputVariables: ["adjective"],
});
console.log(dynamicPrompt.format({ adjective: "big" }));
console.log(
dynamicPrompt.format({
adjective:
"big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else",
}),
);Examples to format into the prompt. Exactly one of this or exampleSelector must be provided.
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.
Adds an example to the example selector.
Calculates the lengths of the examples.
Selects examples from the example selector given the input variables.
Creates a new instance of LengthBasedExampleSelector and adds a list of examples to it.
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"]