1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
export const promptSampleSelectorLength = (async () => {
const { LengthBasedExampleSelector, PromptTemplate, FewShotPromptTemplate } =
await import("npm:langchain/prompts");
// Create a prompt template that will be used to format the examples.
const examplePrompt = new PromptTemplate({
inputVariables: ["input", "output"],
template: "Input: {input}\nOutput: {output}",
});
// Create a LengthBasedExampleSelector that will be used to select the examples.
const exampleSelector = await LengthBasedExampleSelector.fromExamples([
{ input: "happy", output: "sad" },
{ input: "tall", output: "short" },
{ input: "energetic", output: "lethargic" },
{ input: "sunny", output: "gloomy" },
{ input: "windy", output: "calm" },
], {
examplePrompt,
maxLength: 25,
});
// Create a FewShotPromptTemplate that will use the example selector.
const dynamicPrompt = new FewShotPromptTemplate({
// We provide an ExampleSelector instead of examples.
exampleSelector,
examplePrompt,
prefix: "Give the antonym of every input",
suffix: "Input: {adjective}\nOutput:",
inputVariables: ["adjective"],
});
// An example with small input, so it selects all examples.
console.log(await dynamicPrompt.format({ adjective: "big" }));
const longString =
"big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else";
return await dynamicPrompt.format({ adjective: longString });
})();
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v2
October 23, 2023