Skip to content

Building Custom Tools

Overview

Building your own tools is easy with Griptape!

Tools are nothing more than Python classes that inherit from BaseTool. Each method in the class is decorated with an activity decorator which informs the LLM how and when it should use that Tool Activity.

Random Number Generator Tool

Here is a simple random number generator Tool:

import random

from schema import Literal, Optional, Schema

from griptape.artifacts import TextArtifact
from griptape.tools import BaseTool
from griptape.utils.decorators import activity


class RandomNumberGenerator(BaseTool):
    @activity(
        config={
            "description": "Can be used to generate random numbers",
            "schema": Schema(
                {Optional(Literal("decimals", description="Number of decimals to round the random number to")): int}
            ),
        }
    )
    def generate(self, params: dict) -> TextArtifact:
        return TextArtifact(str(round(random.random(), params["values"].get("decimals"))))

Check out other Griptape Tools to learn more about tool implementation details.

Tool Dependencies

Each Tool can also have its own dependencies. You can specify them in a requirements.txt file in the tool directory and Griptape will install them during Tool execution. To start, create a directory for your Tool inside your project. The directory must have the following structure:

  • tool.py file with a tool Python class.
  • requirements.txt file with tool Python dependencies.

That's it! Import and use your Tool in your project as you would with any other Griptape Tool.