Objectives
- Declare custom tools by adding the
@tooldecorator to Python functions. - Bind Pydantic model schemas to the decorator using the
args_schemaproperty. - Combine decorated functions into a tools array and load them inside a tool-calling agent.
Implementation Plan
Goal
Build and execute a tools agent utilizing decorated tools for greeting, reversing strings, and concatenating values.Sample Input
Sample Output
Plan
- Define a simple function
greet_userdecorated with@tool(). - Define Pydantic argument structures:
ReverseStringArgsandConcatenateStringsArgs. - Decorate
reverse_stringwith@tool(args_schema=ReverseStringArgs). - Decorate
concatenate_stringswith@tool(args_schema=ConcatenateStringsArgs). - Group the decorated functions in a list:
tools = [greet_user, reverse_string, concatenate_strings]. - Initialize the agent executor using
create_tool_calling_agentand test the agent.
Step-by-Step Implementation
Step 1: Define Simple Decorated Tool
We write a simple greeting tool. The docstring"""Greets the user by name.""" acts as the tool description.
Step 2: Define Structured Tools with Schemas
We bind Pydantic argument classes to decorated functions.Step 3: Run Agent
We combine the tools list and execute the agent.Complete Combined Code
Below is the complete, consolidated Python script uniting all of the steps above:Practice & Exercises
To practice setting up tools using decorators, open the interactive notebook:Practice & Exercises
Practice defining custom tools using the @tool decorator.💻 VS Code | 🚀 Colab | 📥 Download