Overview
What is a Tool in Outspeed?
The Tool class in the Outspeed package is a foundational component designed to facilitate function calling within the Outspeed ecosystem.
It provides a structured way to define and execute functions with specific input and output types, leveraging Pydantic models for validation. This ensures that the data flowing through your tools adheres to the expected formats, enhancing reliability and maintainability.
Key Features of the Tool Class
- Structured Definition: Clearly define the name, description, input parameters, and response types.
- Pydantic Integration: Utilize Pydantic models for robust input validation and response formatting.
- Asynchronous Execution: Implement asynchronous logic within the
run
method for efficient performance. - Lifecycle Management: Manage the entire lifecycle of a function call, from validation to execution and response.
Creating a Tool
Creating a tool involves subclassing the Tool class and implementing the necessary components. Follow these steps to create your own tool:
Step 1: Define Input and Response Models
First, define the Pydantic models that represent the input parameters and the response structure of your tool.
Step 2: Implement the Tool Class
Subclass the Tool class and implement the required attributes and the run
method.
Breakdown of the RAGTool
Class
-
Initialization (
__init__
Method):- Loads documents from the specified directory.
- Parses documents into nodes using
SimpleNodeParser
. - Creates a
VectorStoreIndex
for efficient similarity searches. - Initializes the
query_engine
for conducting neural searches.
-
Execution (
run
Method):- Receives a
Query
object containing the search term. - Logs the search query for debugging purposes.
- Executes the query against the vector index to retrieve relevant information.
- Logs the response and returns it encapsulated within a
RAGResult
object.
- Receives a
Step 3: Integrate the Tool into Your Application
Once your tool is defined, integrate it into your application to handle function calls seamlessly.
Steps for Integration
- Initialize Tools: In the
setup
method, instantiate your tools and integrate them with the language model nodes. - Define Endpoints: Use decorators like
@sp.streaming_endpoint()
to define processing pipelines that utilize your tools. - Manage Resources: Ensure that all tools and services are properly closed in the
teardown
method to free up resources.
By following these guidelines and leveraging the Tool class, you can create sophisticated and reliable function-calling capabilities within your Outspeed applications.
Tool Lifecycle
The Tool class manages the entire lifecycle of a function call. Understanding this lifecycle is crucial for developing effective tools.
- Validation of Input Parameters: The
parameters_type
model validates the incoming input parameters to ensure they meet the expected schema. - Execution of the
run
Method: The validated input is passed to therun
method, where the main logic of the tool is executed asynchronously. - Validation of the Response: The output from the
run
method is validated against theresponse_type
model to ensure consistency and correctness. - Serialization and Response: The validated response is serialized into JSON format for seamless integration with other components.
Was this page helpful?