I am trying to build a simple react agent using langchain which uses a tool to get the weather condition of a location
I tried making my custom react prompt template and used 'create_react_agent' and 'AgentExecutor' from langchain
This was my code, how do i fix the infinite loop problem in which the agent even after getting the correct output from the tool is not stopping and keeps logging the error that "Action" is missing after "Thought"
from langchain_ollama import ChatOllama
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain.tools import Tool
from langchain.agents import create_react_agent, AgentExecutor
from langchain_core.prompts import PromptTemplate
llm = ChatOllama(
model="llama3.2:1b",
temperature=0.1,
)
react_template = """
Use the following set of rules and instructions to give the best possible answer for the user's questions, based on the tools provided to you
Tools: {tools}
Follow these steps of reasoning and action strictly to find out the correct answer for the user's question
[Question]: The input question asked by the user
[Thought]: Understanding and thinking about the question asked by the user
[Action]: The appropriate action to be taken based on the question asked by the user, use one of the following tools [{tool_names}]
[Observation]: The result of the action
[Thought]: Is this the answer to the question asked by the user ? If yes then give the final answer, else repeat the process (Thought->Action->Observation)
[Final Answer]: I have the final answer to the user's question
Example 1:
[Question]: What is the weather in france ?
[Thought]: I should first search for the weather in france
[Action]: I should use the get_weather tool to get the weather in france
[Observation]: The weather returned from get_weather was cloudy
[Thought]: This was the answer returned from the get_weather tool
[Final Answer]: cloudy
Critical mistakes to avoid:
1. Do not make up answers. The model should only give the correct answer to the user's question returned by the tools provided to it
2. Do not provide justifications to answer produced
3. Keep the final answers short and to the point
Begin !
[Question]: {question}
{agent_scratchpad}
"""
react_prompt = PromptTemplate(
template=react_template,
input_variables=["question", "tools", "tool_names"],
)
def get_weather_func(location: str) -> str:
location = location.lower().strip()
if location == "france":
return "cloudy"
elif location == "italy":
return "sunny"
else:
return "unknown weather"
weather_tool = Tool(
name= "get_weather",
func=get_weather_func,
description="Useful for getting the weather of a location"
)
tools = [weather_tool]
agent = create_react_agent(
llm=llm,
tools=tools,
prompt=react_prompt
)
agent_exexcutor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
handle_parsing_errors=True
)
if __name__ == "__main__":
response = agent_exexcutor.invoke(
input={
"question": "What is the weather in france ?",
"tools": "\n".join([t.name for t in tools]),
"tool_names": "\n".join([t.name + " : " + t.description for t in tools]),
"agent_scratchpad": "",
}
)
print(response["output"])
These are the errors i am getting on my terminal
[Question]: What is the weather in france ?
The result of the action is cloudy
Invalid Format: Missing 'Action:' after 'Thought:'
[Question]: What is the weather in france ?
The result of the action is cloudy
Invalid Format: Missing 'Action:' after 'Thought:'
The result of the action is cloudy
Invalid Format: Missing 'Action:' after 'Thought:'
Thought: I should first search for the weather in franceInvalid Format: Missing 'Action Input:' after 'Action:'
Thought: I should first search for the weather in franceInvalid Format: Missing 'Action Input:' after 'Action:'
> Finished chain.
> Finished chain.
Agent stopped due to iteration limit or time limit.