Tool for simple single-argument inputs, and StructuredTool for complex multi-argument configurations validated with Pydantic.
Objectives
- Define custom functions for greeting users, reversing strings, and concatenating values.
- Formulate Pydantic schemas using
BaseModelandFieldto describe and validate tool inputs. - Instantiate
ToolandStructuredToolclasses and bind them to a tool-calling agent.
Implementation Plan
Goal
Construct a set of custom utility tools using constructors, compile them within an OpenAI tools agent, and verify executions on text inputs.Sample Input
Sample Output
Plan
- Define functions:
greet_user,reverse_string, andconcatenate_strings. - Define a Pydantic class
ConcatenateStringsArgscontaining validation schemas for parametersaandb. - Wrap greeting and reversing functions inside the
Tool(...)class constructor. - Wrap string concatenation inside
StructuredTool.from_function(...)mapping the arguments schema. - Load the prompt
hwchase17/openai-tools-agentand initialize the agent executor withcreate_tool_calling_agent. - Invoke the agent with query strings.
Step-by-Step Implementation
Step 1: Define Functions and Pydantic Schema
We write our Python operations and define the Pydantic validation schema for multiple arguments.Step 2: Instantiate Constructors
We wrap the functions intoTool and StructuredTool objects.
Step 3: Create and Invoke Tool-Calling Agent
We pull the specialized tools-agent prompt, initialize the model, build the agent executor, and run tests.Complete Combined Code
Below is the complete, consolidated Python script uniting all of the steps above:Practice & Exercises
To practice setting up tools using standard constructors, open the interactive notebook:Practice & Exercises
Practice initializing Tool and StructuredTool constructor wrappers.💻 VS Code | 🚀 Colab | 📥 Download