langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.24k stars 2.07k forks source link

Input Variable Error in ChatPromptTemplate When using ExampleSelector #6147

Open komi786 opened 1 month ago

komi786 commented 1 month ago

Checked other resources

Example Code

Here is the code. I keep encountering the error "Error in prompt 'variable_name'" regardless of the variable name I use; changing it to 'input' or 'query' does not resolve the issue. `

def extract_information(query, model): try: active_model = LLMManager.get_instance(model=model) mapping_fordomain, ,_ = load_mapping(MAPPING_FILE, 'unknown') examples = mapping_for_domain['examples'] print(f"Examples={examples}") embedding = FastEmbedEmbeddings() example_selector = SemanticSimilarityExampleSelector.from_examples( examples, embedding,
FAISS, k=4 ) few_shot_prompt = FewShotChatMessagePromptTemplate(

We provide an ExampleSelector instead of examples.

        example_selector=example_selector,
        example_prompt=ChatPromptTemplate.from_messages(
        [("human", "{input}"), ("ai", "{output}")]
        ),
        input_variables=["input"]
    )
    print(f"few_shot_prompt={few_shot_prompt.format()}")
    base_prompt=f"""Role: You are a helpful assistant  ...............\n
            input: "{input}"\n output: 
            """
    print(f"base_prompt={base_prompt}")
    final_prompt = (
            SystemMessagePromptTemplate.from_template(
                base_prompt
            )
            + few_shot_prompt
            + HumanMessagePromptTemplate.from_template("{input}")
        )
    print(f"final prompt={final_prompt.format()}")
    chain = final_prompt | active_model
    result = chain.invoke({"input": query})
    print(f"result={result}")
except Exception as e:
    print(f"Error in prompt:{e}")
    return None`

Error Message and Stack Trace (if applicable)

The error likely originates from this segment of code: ` few_shot_prompt = FewShotChatMessagePromptTemplate(

We provide an ExampleSelector instead of examples.

        example_selector=example_selector,
        example_prompt=ChatPromptTemplate.from_messages(
        [("human", "{input}"), ("ai", "{output}")]
        ),
        input_variables=["input"]
    ) ` The subsequent print statement is not executed, and the only message returned is "Error in prompt 'input'."

Description

I am using example selector with chatprompt template. I am getting error on input variable name.

System Info

langchain latest package linux python

komi786 commented 1 month ago

I applogize, The error likely originates from this segment of code: final_prompt = ( SystemMessagePromptTemplate.from_template( base_prompt ) ,few_shot_prompt ,HumanMessagePromptTemplate.from_template("{input}") ) print(f"final prompt={final_prompt.format()}") as The subsequent print statement is not executed, and the only message returned is "Error in prompt 'input'."

dosubot[bot] commented 1 month ago

To resolve the "Error in prompt 'input'" when using an example selector with a chat prompt template in LangChain.js, ensure that you provide either examples or exampleSelector, but not both. The error occurs if both or neither are provided.

Here is an example of how to correctly use an exampleSelector with a FewShotPromptTemplate:

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:
   */
}

Ensure that you do not provide both examples and exampleSelector in the FewShotPromptTemplate constructor. Additionally, make sure that the template format and input variables are correctly specified and validated using the checkValidTemplate function from the LangChain core library to avoid any template parsing issues [1][2][3].

To continue talking to Dosu, mention @dosu.