Skip to main content

INVALID_PROMPT_INPUT

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

¥A prompt template received missing or invalid input variables.

一种意想不到的情况是,如果你将 JSON 对象直接添加到提示模板中:

¥One unexpected way this can occur is if you add a JSON object directly into a prompt template:

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 对象将被解释为一个附加变量,而应该在大括号前面加上第二个括号,如下所示:

¥You might think that the above prompt template should require a single input key named question, but the JSON object will be interpreted as an additional variable because the curly braces ({) are not escaped, and should be preceded by a second brace instead, like this:

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

故障排除

¥Troubleshooting

以下内容可能有助于解决此错误:

¥The following may help resolve this error:

  • 仔细检查你的提示模板以确保其正确无误。

    ¥Double-check your prompt template to ensure that it is correct.

    • 如果你使用默认格式,并且在模板的任何地方使用大括号 {,则应进行双重转义,如下所示:{{,如上所示。

      ¥If you are using default formatting and you are using curly braces { anywhere in your template, they should be double escaped like this: {{, as shown above.

  • 如果你使用 MessagesPlaceholder,请确保传入的是消息数组或类消息对象。

    ¥If you are using a MessagesPlaceholder, make sure that you are passing in an array of messages or message-like objects.

    • 如果你使用简写元组来声明提示模板,请确保变量名用大括号 (["placeholder", "{messages}"]) 括起来。

      ¥If you are using shorthand tuples to declare your prompt template, make sure that the variable name is wrapped in curly braces (["placeholder", "{messages}"]).

  • 尝试使用 LangSmith 或日志语句查看提示模板中的输入,以确认它们是否按预期显示。

    ¥Try viewing the inputs into your prompt template using LangSmith or log statements to confirm they appear as expected.

  • 如果你从 LangChain Prompt Hub 中提取提示,请尝试提取并记录它,或者使用示例输入单独运行它,以确认它符合你的预期。

    ¥If you are pulling a prompt from the LangChain Prompt Hub, try pulling and logging it or running it in isolation with a sample input to confirm that it is what you expect.