My LangGraph create_react_prompt does not seem to find the input fields I am passing in my invoke. I am not using an AgentExecutor since I want to be able to decide the course of action after each call. Below is a simplified version of my code where the first call to the agent is not happening.
This is the error I get -
KeyError: "Input to ChatPromptTemplate is missing variables {‘input’, ‘tools’, ‘agent_scratchpad’, ‘tool_names’}. Expected: [‘agent_scratchpad’, ‘input’, ‘tool_names’, ‘tools’] Received: [‘messages’, ‘is_last_step’, ‘remaining_steps’]\nNote: if you intended {input} to be part of the string and not a variable, please escape it with double curly braces like: ‘{{input}}’.\nFor troubleshooting, visit: INVALID_PROMPT_INPUT | 🦜️🔗 LangChain "
Can someone please tell me what do i do here. Thanks.
from dotenv import load_dotenv
load_dotenv()
from langchain_openai import ChatOpenAI
from langchain.prompts import(
ChatPromptTemplate,
SystemMessagePromptTemplate
)
from tools.sql import(run_query_tool, get_element_tool)
from langgraph.prebuilt import create_react_agent
from langchain.agents.format_scratchpad.log_to_messages import format_log_to_messages
execute_tools = [run_query_tool, get_element_tool]
prompt3 = ChatPromptTemplate(
messages=[
SystemMessagePromptTemplate.from_template(“”"
Answer the following questions as best you can. You have access to the following tools:
{tools}
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: {input}
Thought:{agent_scratchpad}
"""),
]
)
chat = ChatOpenAI(
model=‘gpt-4o’,
temperature=0,
top_p=1.0,
verbose=True
)
react_agent = create_react_agent(model=chat, tools=execute_tools, prompt=prompt3)
user_input = “Go to website https://www.snapdeal.com/”
agent_scratchpad = “”
inputs = {
“input”: user_input,
“agent_scratchpad”: agent_scratchpad,
“tool_names”: “, “.join([tool.name for tool in execute_tools]),
“tools”: “\n”.join([f”{tool.name}: {tool.description}” for tool in execute_tools]),
“intermediate_steps”: intermediate_steps
}
response = react_agent.invoke(inputs)
print(f"RESPONSE: {response}")