sp.Tool
Overview
The Tool
class in the Outspeed package is designed to facilitate the creation of tools that can be used within Outspeed applications. These tools are typically used to perform specific tasks, such as querying an API or processing data, and are integrated into the application workflow.
Creating a Tool
To create a tool, define a class that inherits from sp.Tool
and implement the required methods. Here’s a minimal example:
A Tool must define name
, description
, parameters_type
, and response_type
attributes, and implement the run
method.
- The
name
attribute is a string that identifies the tool. - The
description
attribute provides a brief explanation of what the tool does. - The
parameters_type
andresponse_type
attributes are Pydantic models that define the input and output data structures, respectively. - The
run
method contains the main logic of the tool and must be implemented by the subclass.
Integrating the Tool into an Application
Once defined, the tool can be integrated into an Outspeed application. Here’s how the SearchTool
is utilized within the VoiceBot:
Tool Lifecycle
The Tool
class manages the tool lifecycle:
- It validates the input parameters using the
parameters_type
model. - It executes the
run
method asynchronously. - It validates the response using the
response_type
model.
Best Practices
- Use Pydantic models for
parameters_type
andresponse_type
to ensure data validation. - Implement error handling within the
run
method to manage exceptions gracefully. - Test your tool thoroughly to ensure it behaves as expected within the application.
Error Handling
The Tool
class includes basic error handling:
- If an exception occurs during the
run
method execution, it should be logged and handled appropriately. - Ensure that the tool’s input and output types match the defined Pydantic models to avoid runtime errors.
On more information on how to use Pydantic models with Open AI tools, see:
- OpenAI Tools documentation.
- OpenAI Pydantic documentation.
Was this page helpful?