Issue Description
I’m having trouble with my LangChain agent setup. My network checking tool runs perfectly when I test it alone, but when the agent tries to use it, I get a validation error saying the tool doesn’t exist.
Error Output:
user@container:/project# python agent_test.py
> Starting AgentExecutor chain...
Assistant: Should I use a tool? Yes.
Action: check_network(server='127.0.0.1', callbacks='Callbacks' = None)
Action Input: 127.0.0.1check_network(server='127.0.0.1', callbacks='Callbacks' = None) is not a valid tool, try one of [check_network].
Thought: Should I use a tool? Yes
Action: check_network(server='127.0.0.1', callbacks='Callbacks' = None)
Action Input: 127.0.0.1check_network(server='127.0.0.1', callbacks='Callbacks' = None) is not a valid tool, try one of [check_network].
My Code:
import requests
from langchain import hub
from langchain.agents import Tool, AgentExecutor, create_react_agent
from langchain_ollama.llms import OllamaLLM
from langchain.memory import ConversationBufferWindowMemory
from pydantic import BaseModel, Field
from langchain.tools import tool
class NetworkCheck(BaseModel):
hostname: str = Field(description="Target hostname")
@tool("check_network", args_schema=NetworkCheck, return_direct=False)
def check_network(hostname: str) -> str:
'''Checks if a network host is reachable. usage: check_network("example.com")'''
import os
status = os.system(f"ping -c 1 {hostname}")
if status == 0:
message = f"{hostname} is reachable!"
else:
message = f"{hostname} is unreachable!"
return {"text": message}
def save_conversation(user_input, bot_response):
memory.save_context({"input": user_input}, {"output": bot_response})
def run_query(user_input):
request_data = {
"input": user_input,
"chat_history": memory.load_memory_variables({}),
}
result = executor.invoke(request_data)
save_conversation(result["input"], result["output"])
return result
available_tools = [
Tool(
name="check_network",
func=check_network,
description="Use this to test network connectivity to a host. Input: hostname",
),
]
template = hub.pull("hwchase17/react-chat")
memory = ConversationBufferWindowMemory(k=10)
model = OllamaLLM(
model="llama2",
keep_alive=-1,
base_url="http://ollama:11434",
)
my_agent = create_react_agent(model, available_tools, template, stop_sequence=True)
executor = AgentExecutor(
agent=my_agent,
tools=available_tools,
verbose=True,
max_iterations=2,
handle_parsing_errors=True
)
run_query("can you check if 127.0.0.1 is available?")
I’ve tried different approaches from the docs and tested several working examples from other projects. The tool works fine independently but the agent can’t seem to find it. What could be causing this tool recognition problem?