Langchain Hub pull ChatPromptTemplate returns False

235 Views Asked by At

I was trying to follow the quickstart tutorial for agents for Langchain: https://js.langchain.com/docs/modules/agents/quick_start

I followed the process but faced unexpected errors. I did not know how to solve it and did not find any existing solution. Please advise.

import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
import { ChatOpenAI } from "@langchain/openai";
import { pull } from "langchain/hub";
import { createOpenAIFunctionsAgent } from "langchain/agents";
import { AgentExecutor } from "langchain/agents";
import {
    ChatPromptTemplate,
    PromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
  } from "@langchain/core/prompts";
  import {
    AIMessage,
    HumanMessage,
    SystemMessage,
  } from "@langchain/core/messages";



const searchTool = new TavilySearchResults();

const toolResult = await searchTool.invoke("what is the weather in SF?");

console.log(toolResult);

const tools = [searchTool];

const llm = new ChatOpenAI({
  modelName: "gpt-3.5-turbo",
  temperature: 0,
});


const prompt = await pull<ChatPromptTemplate>(
  "hwchase17/openai-functions-agent"
);
console.log("Prompt Results")
console.log(prompt)

const agent = await createOpenAIFunctionsAgent({
    llm: llm,
    tools: tools,
    prompt: prompt,
  });

  const agentExecutor = new AgentExecutor({
    agent,
    tools,
  });

  const result1 = await agentExecutor.invoke({
    input: "hello!",
  });
  
  console.log(result1);

Prompt Results
false
file:///Users/bytedance/Desktop/AI/ai-terminal/node_modules/langchain/dist/agents/openai_functions/index.js:218
    if (!prompt.inputVariables.includes("agent_scratchpad")) {
                               ^

TypeError: Cannot read properties of undefined (reading 'includes')
    at createOpenAIFunctionsAgent (file:///Users/bytedance/Desktop/AI/ai-terminal/node_modules/langchain/dist/agents/openai_functions/index.js:218:32)
    at file:///Users/bytedance/Desktop/AI/ai-terminal/test.js:43:21
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v20.10.0

I tried debugging but didn't manage to find anything.

2

There are 2 best solutions below

0
user8675754 On

The issue doesn't come from "langchain/hub" but from TypeScript. The "await" has no effect on the type "ChatPromptTemplate". If you remove it, it works.

const prompt = await pull("rlm/rag-prompt");
0
njoku samson ebere On

Hub Pull throws this error.

Remove

const prompt = await pull<ChatPromptTemplate>(
  "hwchase17/openai-functions-agent"
 );

Define a different prompt with a MessagesPlaceholder of "agent_scratchpad" like so:

 const prompt = ChatPromptTemplate.fromMessages([
  new MessagesPlaceholder("agent_scratchpad"),
  ["user", "{input}"],
  ["user", "Answer the question in detail and state your source."],
 ]);

The text ("Answer the question in detail and state your source.") can be any instructions you want.

This fixed mine.