Code
from langchain import hub
from langchain.agents import (
AgentExecutor,
create_react_agent,
)
from langchain_ollama import ChatOllama
# Define a very simple tool function that returns the current time
def get_current_time(*args, **kwargs):
"""Returns the current time in H:MM AM/PM format."""
import datetime
return now.strftime("%I:%M %p")
tools = [
Tool(
name="Time",
func=get_current_time,
description="Useful for when you need to know the current time."
),
]
# Pull the prompt template from the hub
prompt = hub.pull("hwchase17/react")
llm = ChatOllama(
model="llama3.2",
temperature=0,
)
agent = create_react_agent(
llm=llm,
tools=tools,
prompt=prompt,
stop_sequence=True,
)
agent_executor = AgentExecutor.from_agent_and_tools(
agent= agent,
tools= tools,
verbose = True,
)
response = agent_executor.invoke({"input": "What time is it?"})
print("response:", response)
Error
> Entering new AgentExecutor chain...
Question: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: Question: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: Question: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: Question: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMI'll follow the format you provided. Here's my attempt:
Question: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMHere's my attempt at following the format:
Question: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AMQuestion: What time is it?
Thought: I need to determine the current time.
Action: Time
Action Input: None09:12 AM
> Finished chain.
response: {'input': 'What time is it?', 'output': 'Agent stopped due to iteration limit or time limit.'}