DOC: <Please write a comprehensive title after the 'DOC: ' prefix>LongthBasedExemplarSelector did not meet expectations #7433
Labels
auto:documentation
Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder
auto:question
A specific question about the codebase, product, project, or how to use a feature
Checklist
Issue with current documentation:
I used the example in the documentation while studying the Length Based Exemplar selector and found that it did not print the examples I entered in the console, such as {input: "happy", output: "sad"}
The Length Based Exemplar selector does not restrict the input of prompts, and I am confused. Please help me. Below is my code
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage } from "@langchain/core/messages";
import { PromptTemplate,FewShotPromptTemplate } from "@langchain/core/prompts";
import {LengthBasedExampleSelector} from "@langchain/core/example_selectors";
import * as dotenv from 'dotenv';
// 加载环境变量
dotenv.config();
const examples = [
{ input: "happy", output: "sad" },
{ input: "tall", output: "short" },
{ input: "energetic", output: "lethargic" },
{ input: "sunny", output: "gloomy" },
{ input: "windy", output: "calm" },
]
const model = new ChatOpenAI({
configuration: {
baseURL: "my url",
apiKey: process.env.OPENAI_API_KEY, // 确保 API 密钥已设置
}
});
const 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: 100,
});
const dynamicPrompt = new FewShotPromptTemplate({
exampleSelector: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" }));
const longString = "big and huge adn massive and large and gigantic and tall and much much much much much much bigger then everyone";
(async () => {
try {
const selectedExamples = await exampleSelector.selectExamples({ adjective: 123 });
console.log("Selected Examples:", selectedExamples);
const formattedPrompt = await dynamicPrompt.format({ adjective: longString });
console.log(123)
console.log(formattedPrompt);
} catch (error) {
console.error("Error invoking the model:", error);
}
})();
The version I am using is like this
"@langchain/core": "^0.3.26",
"@langchain/openai": "^0.3.16",
I also looked at the definition of the LengthBasedEmplarSelectorInput interface
He's like this
export interface LengthBasedExampleSelectorInput {
examplePrompt: PromptTemplate;
maxLength?: number;
getTextLength?: (text: string) => number;
}
The first parameter accepted by the constructor in the given example is an array, which also confuses me
Idea or request for content:
No response
The text was updated successfully, but these errors were encountered: