Introduction
Tools help agents do specific jobs easily. They let agents get information or perform tasks quickly.
Jump into concepts and practice - no test required
Tools help agents do specific jobs easily. They let agents get information or perform tasks quickly.
from langchain.agents import Tool tool = Tool( name="tool_name", func=your_function, description="What this tool does" )
The name is how the agent calls the tool.
The func is the function the tool runs when used.
def add_numbers(text: str) -> str: numbers = list(map(int, text.split())) return str(sum(numbers)) add_tool = Tool( name="Adder", func=add_numbers, description="Adds numbers given in text" )
def get_time(_input: str) -> str: from datetime import datetime return datetime.now().strftime('%H:%M') time_tool = Tool( name="TimeChecker", func=get_time, description="Returns the current time" )
This example creates a tool that greets someone by name. Then it uses the tool to greet "Alice".
from langchain.agents import Tool # Define a simple tool function def greet(name: str) -> str: return f"Hello, {name}!" # Create the tool hello_tool = Tool( name="GreetTool", func=greet, description="Greets a person by name" ) # Use the tool result = hello_tool.func("Alice") print(result)
Tools must have clear descriptions so agents know when to use them.
Functions used in tools should accept a single string argument and return a string.
Test your tool functions separately before adding them to agents.
Tools let agents perform specific tasks easily.
Create tools by defining a function and wrapping it with Tool.
Give tools clear names and descriptions for best results.
def greet(name: str) -> str:
return f"Hello, {name}!"
from langchain.agents import Tool
greet_tool = Tool(name='greet', func=greet, description='Greets a person')
result = greet_tool.func('Alice')
print(result)def add_numbers(a, b):
return a + b
from langchain.agents import Tool
add_tool = Tool(name='add', func=add_numbers, description='Adds two numbers')
result = add_tool.func(5)
print(result)