langchain-ai / langchainjs

šŸ¦œšŸ”— Build context-aware reasoning applications šŸ¦œšŸ”—
https://js.langchain.com/docs/
MIT License
12.33k stars 2.09k forks source link

ChatPromptTemplate from langsmith throwing error when you copy the JavaScript Inline code #4956

Closed KeenanFernandes2000 closed 5 months ago

KeenanFernandes2000 commented 5 months ago

Checked other resources

Example Code

const prompt = ChatPromptTemplate.fromMessages(
    ["system", "You are a helpful assistant"],
    ["human", "{input}"],
    ["placeholder", "{agent_scratchpad}"]
  );

Error Message and Stack Trace (if applicable)

Error occurred: Error: Prompt must have an input variable named "agent_scratchpad". Found [] instead. at createOpenAIFunctionsAgent (file:///C:/Users/Keenan/Desktop/Work/PotentialLive/node_modules/langchain/dist/agents/openai_functions/index.js:219:15) at valid (file:///C:/Users/Keenan/Desktop/Work/PotentialLive/index.js:483:23) at file:///C:/Users/Keenan/Desktop/Work/PotentialLive/app/chat.js:82:37 at Layer.handle [as handle_request] (C:\Users\Keenan\Desktop\Work\PotentialLive\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\Keenan\Desktop\Work\PotentialLive\node_modules\express\lib\router\route.js:144:13) at Route.dispatch (C:\Users\Keenan\Desktop\Work\PotentialLive\node_modules\express\lib\router\route.js:114:3) at Layer.handle [as handle_request] (C:\Users\Keenan\Desktop\Work\PotentialLive\node_modules\express\lib\router\layer.js:95:5) at C:\Users\Keenan\Desktop\Work\PotentialLive\node_modules\express\lib\router\index.js:284:15 at Function.process_params (C:\Users\Keenan\Desktop\Work\PotentialLive\node_modules\express\lib\router\index.js:346:12) at next (C:\Users\Keenan\Desktop\Work\PotentialLive\node_modules\express\lib\router\index.js:280:10)

Description

The chatprompttemplate should work as I have copied the exact flow and code for creating an agent.

System Info

Node.js v18.18.0 platform windows

dosubot[bot] commented 5 months ago

The error you're encountering is due to the system expecting an input variable named agent_scratchpad in your prompt template, but it was not found. The agent_scratchpad variable is crucial within the LangChain JS framework, especially for the ChatAgent class, as it serves as a placeholder for the agent's previous steps or work in a conversation. This inclusion helps provide context or a summary of the agent's previous interactions.

To resolve this issue, you should ensure that agent_scratchpad is included as one of the input variables in your prompt template. Here's how you can adjust your ChatPromptTemplate to include agent_scratchpad:

const template = ChatPromptTemplate.fromMessages([
  ["system", "You are an AI assistant with access to an agent_scratchpad"],
  new MessagesPlaceholder("agent_scratchpad"), // Ensure this placeholder is defined and included
  ["human", "What can you tell me today?"],
]);

// When formatting messages, include the 'agent_scratchpad' variable with its value
const messages = await template.formatMessages({
  agent_scratchpad: [new SystemMessage("Here's what I found for you...")], // Example content for agent_scratchpad
});

In this example, agent_scratchpad is explicitly included as a MessagesPlaceholder in the ChatPromptTemplate. When formatting messages with formatMessages, it's essential to provide a value for agent_scratchpad, ensuring it's in the expected format.

If you're using agent_scratchpad in a different context within LangChain JS or if your implementation differs, the key is to ensure that agent_scratchpad is correctly defined as an input variable and provided with the expected value when formatting messages.

Should you have any further questions or require additional clarification, feel free to ask.

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a šŸ‘ if this is helpful and šŸ‘Ž if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
jacoblee93 commented 5 months ago

Just patched this in @langchain/core@0.1.53! Thank you