Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: <Please write a comprehensive title after the 'DOC: ' prefix>LongthBasedExemplarSelector did not meet expectations #7433

Open
2 tasks done
jinxudong996 opened this issue Dec 26, 2024 · 1 comment
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

Comments

@jinxudong996
Copy link

Checklist

  • I added a very descriptive title to this issue.
  • I included a link to the documentation page I am referring to (if applicable).

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);

const response = await model.invoke([
  formattedPrompt
]);
console.log(response.content);

} 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

Copy link

dosubot bot commented Dec 26, 2024

Hey @jinxudong996! How can I assist you today? I'm here to help you with any bugs, questions, or contributions you have.

The LengthBasedExampleSelector is designed to select examples based on the length of the input. If the examples are not being printed as expected, it might be due to how the input length is being calculated or how the examples are being selected based on the maxLength parameter.

Here are a few things to check and consider:

  1. Input Length Calculation: Ensure that the input length is being calculated correctly. The getTextLength function is used to determine the length of the input and examples. If you haven't specified a custom getTextLength function, it defaults to counting words and lines. You can log the input length to verify it.

  2. Example Selection: The selectExamples method selects examples until the total length of the selected examples reaches the maxLength. If the input length is too large, there might not be enough remaining length to select any examples. You can log the remaining length and the length of each example to debug this.

  3. Constructor Parameters: The constructor for LengthBasedExampleSelector takes an array of examples and an options object. The examples array is correct, and the options object should include examplePrompt and maxLength. Your usage seems correct, but ensure that the maxLength is appropriate for your input and examples.

  4. Debugging: Add logging to the selectExamples method to see the input length, remaining length, and whether each example is being added. This can help identify why examples are not being selected.

Here's a snippet to help with debugging:

async selectExamples(inputVariables: Example): Promise<Example[]> {
  const inputs = Object.values(inputVariables).join(" ");
  const inputLength = this.getTextLength(inputs);
  console.log(`Input Length: ${inputLength}`);
  
  let remainingLength = this.maxLength - inputLength;
  console.log(`Initial Remaining Length: ${remainingLength}`);
  
  let i = 0;
  const examples: Example[] = [];
  
  while (remainingLength > 0 && i < this.examples.length) {
    const exampleLength = this.exampleTextLengths[i];
    console.log(`Example ${i} Length: ${exampleLength}`);
    
    const newLength = remainingLength - exampleLength;
    if (newLength < 0) {
      console.log(`Example ${i} cannot be added, not enough remaining length.`);
      break;
    } else {
      examples.push(this.examples[i]);
      remainingLength = newLength;
      console.log(`Example ${i} added. New Remaining Length: ${remainingLength}`);
    }
    i += 1;
  }

  return examples;
}

This will help you understand how the examples are being selected and why they might not be appearing as expected [1][2].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@dosubot dosubot bot added 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 labels Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

1 participant