跳至主要内容

如何根据长度选择示例

先决条件

本指南假设您熟悉以下概念

此示例选择器根据长度选择要使用的示例。当您担心构建一个超过上下文窗口长度的提示时,这很有用。对于较长的输入,它将选择较少的示例来包含,而对于较短的输入,它将选择更多的示例。

import { PromptTemplate, FewShotPromptTemplate } from "@langchain/core/prompts";
import { LengthBasedExampleSelector } from "@langchain/core/example_selectors";

export async function run() {
// 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" }));
/*
Give the antonym of every input

Input: happy
Output: sad

Input: tall
Output: short

Input: energetic
Output: lethargic

Input: sunny
Output: gloomy

Input: windy
Output: calm

Input: big
Output:
*/

// An example with long input, so it selects only one example.
const longString =
"big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else";
console.log(await dynamicPrompt.format({ adjective: longString }));
/*
Give the antonym of every input

Input: happy
Output: sad

Input: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else
Output:
*/
}

API 参考

下一步

您现在已经了解了一些关于使用基于长度的示例选择器的信息。

接下来,查看本指南,了解如何使用 基于相似性的示例选择器


此页面是否有用?


您也可以留下详细的反馈 在 GitHub 上.