跳到主要内容

INVALID_PROMPT_INPUT

提示模板 接收到缺失或无效的输入变量。

一种可能发生这种情况的意外方式是,您将 JSON 对象直接添加到提示模板中

import { PromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";

const prompt = PromptTemplate.fromTemplate(`You are a helpful assistant.

Here is an example of how you should respond:

{
"firstName": "John",
"lastName": "Doe",
"age": 21
}

Now, answer the following question:

{question}`);

您可能认为上面的提示模板应该只需要一个名为 question 的输入键,但是 JSON 对象将被解释为一个额外的变量,因为花括号 ({) 没有被转义,应该用第二个花括号代替,像这样

import { PromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";

const prompt = PromptTemplate.fromTemplate(`You are a helpful assistant.

Here is an example of how you should respond:

{{
"firstName": "John",
"lastName": "Doe",
"age": 21
}}

Now, answer the following question:

{question}`);

故障排除

以下方法可能有助于解决此错误

  • 仔细检查您的提示模板以确保它是正确的。
    • 如果您使用的是默认格式,并且在模板中的任何位置使用了花括号 {,则应像这样双重转义它们:{{,如上所示。
  • 如果您正在使用 MessagesPlaceholder,请确保您传入的是消息数组或类似消息的对象。
    • 如果您使用简写元组来声明您的提示模板,请确保变量名包含在花括号中 (["placeholder", "{messages}"])。
  • 尝试使用 LangSmith 或日志语句查看提示模板的输入,以确认它们是否如预期显示。
  • 如果您从 LangChain Prompt Hub 中拉取提示,请尝试拉取并记录日志或使用示例输入隔离运行它,以确认它是否符合您的预期。

此页面是否对您有帮助?


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