Creating Custom Tools in PySpur using @tool_function
This guide will walk you through the process of converting an arbitrary Python function into a PySpur tool using the@tool_function
decorator. This allows you to integrate custom logic into your PySpur workflows seamlessly.
Prerequisites
- Ensure you have initialized a PySpur project using the
pyspur init
command.
Step-by-Step Guide
Step 1: Create a Tool File
-
Navigate to the
tools
directory in your PySpur project. This directory is created automatically when you initialize your project. -
Create a new Python file for your tool. For example,
my_custom_tool.py
.
Step 2: Define Your Function
- In your new tool file, define the Python function you want to convert into a tool. This function should contain the logic you wish to execute.
Step 3: Decorate Your Function
-
Import the
tool_function
decorator from the appropriate module. -
Use the
@tool_function
decorator to register your function as a tool. You can specify optional parameters such asname
,description
,category
, andoutput_model
.
@tool_function Parameters
- name (Optional[str]): The name of the tool. Defaults to the function name if not provided.
- display_name (Optional[str]): A human-readable name for the tool, used in the UI. Defaults to a title-cased version of the function name.
- description (Optional[str]): A brief description of what the tool does. Defaults to the function’s docstring if not provided.
- category (Optional[str]): A category for organizing the tool within the UI. Useful for grouping similar tools.
- visual_tag (Optional[Dict[str, str]]): Visual styling options for the tool in the UI.
- has_fixed_output (bool): Indicates whether the tool has a fixed output schema. Defaults to
True
. - output_model (Optional[Type[BaseNodeOutput]]): A custom output model to use instead of generating one automatically@tool_function
Returns
- ToolFunction: The decorated function, which can still be called normally but is now also registered as a PySpur tool.
Step 4: Using the Tool in a SpuNow you should be able to see the
A tool namedbar
will now appear in the tools panel in the app under the Custom
section.
This tool can be used in your Spur workflows just like any other built-in tool.