|
| 1 | +# Overview |
| 2 | + |
| 3 | +In GPTScript, tools are used to extend the capabilities of a script. The idea behind them is that AI performs better when it has very specific instructions for a given task. Tools are a way to break-up the problem into smaller and more focused pieces where each tool is responsible for a specific task. A typical flow like this is to have a main script that imports a set of tools it can use to accomplish its goal. |
| 4 | + |
| 5 | +## Using Tools |
| 6 | + |
| 7 | +GPTScripts can utilize tools in one of three ways: |
| 8 | +1. Built-in system tools |
| 9 | +2. In-script tools |
| 10 | +3. External tools |
| 11 | + |
| 12 | +### System Tools |
| 13 | +All GPTScripts have access to system tools, like `sys.read` and `sys.write`, that can be used without any additional configuration. |
| 14 | + |
| 15 | +```yaml |
| 16 | +tools: sys.read, sys.write |
| 17 | + |
| 18 | +Read all of the files in my current directory, do no recurse over any subdirectories, and give me a description of this directory's contents.s |
| 19 | +``` |
| 20 | + |
| 21 | +System tools are a set of core tools that come packaged with GPTScript by default. |
| 22 | + |
| 23 | +### In-Script Tools |
| 24 | +Things get more interesting when you start to use custom tools. |
| 25 | + |
| 26 | +The most basic example of this is an in-script tool that is defined in the same file as the main script. This is useful for breaking up a large script into smaller, more manageable pieces. |
| 27 | + |
| 28 | +```yaml |
| 29 | +tools: random-number |
| 30 | + |
| 31 | +Select a number at random and, based on the results, write a poem about it. |
| 32 | + |
| 33 | +--- |
| 34 | +name: random-number |
| 35 | +description: Generate a random number between 1 and 100. |
| 36 | + |
| 37 | +Select a number at random between 1 and 100 and return only the number. |
| 38 | +``` |
| 39 | + |
| 40 | +### External Tools |
| 41 | + |
| 42 | +This is where the real power of GPTScript starts to become evident. For this example lets use the external [image-generation](https://github.com/gptscript-ai/image-generation) and [search](https://github.com/gptscript-ai/search) tools to generate an image and then search the web for similar images. |
| 43 | + |
| 44 | +```yaml |
| 45 | +tools: ./image-generation, ./search, sys.read |
| 46 | + |
| 47 | +Generate an image of a city skyline at night and write the resulting image to a file called city_skyline.png. |
| 48 | + |
| 49 | +Take this image and search the web for similar images. Write links for the top 5 results to a file called similar_images.txt. |
| 50 | +``` |
| 51 | + |
| 52 | +External tools are tools that are defined by a `tool.gpt` file in their root directory. They can be imported into a GPTScript by specifying the path to the tool's root directory. |
| 53 | + |
| 54 | +## Authoring External Tools |
| 55 | +You can author your own tools for your own use or to share with others. The process for authoring a tool is as simple as creating a `tool.gpt` file in the root directory of your tool This file is itself a GPTScript that defines the tool's name, description, and what it should do. |
| 56 | + |
| 57 | +Here's an example of the `tool.gpt` file for the `image-generation` tool: |
| 58 | + |
| 59 | +```yaml |
| 60 | +description: I am a tool that can generate images based on arguments that are sent to me. I return a list of URLs to the generated images. |
| 61 | +args: prompt: (required) The text prompt based on which the GPT model will generate a response |
| 62 | +args: model: (optional) The model to use for image generation. Defaults to "dall-e-3". |
| 63 | +args: size: (optional) The size of the image to generate, format WxH (e.g. 1024x1024). Defaults to 1024x1024. |
| 64 | +args: quality: (optional) The quality of the generated image. Allowed values are "standard" or "hd". Default is "standard". |
| 65 | +args: number: (optional) The number of images to generate. Defaults to 1. |
| 66 | + |
| 67 | +#!/usr/bin/env python3 ./cli.py --prompt="${prompt}" --model="${model}" --size="${size}" --quality="${quality}" --number="${number}" |
| 68 | +``` |
| 69 | + |
| 70 | +At the bottom you'll notice a shebang line that specifies the command to run when the tool is invoked. This is the exact command that will be executed when the tool is used in a GPTScript. Doing this with tools allows for a high degree of reliability that the tool would not otherwise have. |
0 commit comments