|
| 1 | +# Authoring Tools |
| 2 | + |
| 3 | +You can author your own tools for your 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 project. This file is itself a GPTScript that defines the tool's name, description, and what it should do. |
| 4 | + |
| 5 | +Here's an example of the `tool.gpt` file for the `image-generation` tool: |
| 6 | + |
| 7 | +```yaml |
| 8 | +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. |
| 9 | +args: prompt: (required) The text prompt based on which the GPT model will generate a response |
| 10 | +args: model: (optional) The model to use for image generation. Defaults to "dall-e-3". |
| 11 | +args: size: (optional) The size of the image to generate, format WxH (e.g. 1024x1024). Defaults to 1024x1024. |
| 12 | +args: quality: (optional) The quality of the generated image. Allowed values are "standard" or "hd". Default is "standard". |
| 13 | +args: number: (optional) The number of images to generate. Defaults to 1. |
| 14 | + |
| 15 | +#!/usr/bin/env python3 ./cli.py --prompt="${prompt}" --model="${model}" --size="${size}" --quality="${quality}" --number="${number}" |
| 16 | +``` |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +:::tip |
| 21 | +Every arg becomes an environment variable when the tool is invoked. |
| 22 | +::: |
| 23 | + |
| 24 | +## Binary Tools |
| 25 | +GPTScript can call binaries and scripts located on your system or externally. This is done by defining a `tool.gpt` file in the same directory that the binary or script is located. In that `tool.gpt` file, you call it directly using the `#!` directive. |
| 26 | + |
| 27 | +For example: |
| 28 | + |
| 29 | +```yaml |
| 30 | +description: This is a tool that calls a binary. |
| 31 | +args: arg1: The first argument. |
| 32 | + |
| 33 | +#!/usr/bin/env ./path/to/binary --arg1="${arg1}" |
| 34 | +``` |
| 35 | + |
| 36 | +## Shell |
| 37 | +You can call shell scripts directly using the `#!` directive. For example: |
| 38 | + |
| 39 | +```yaml |
| 40 | +description: This is a tool that calls a shell script. |
| 41 | +args: arg1: The first argument. |
| 42 | + |
| 43 | +#!/usr/bin/env sh |
| 44 | +echo "Hello, world!" |
| 45 | +./path/to/script.sh --arg1="${arg1}" |
| 46 | +``` |
| 47 | + |
| 48 | +## Python |
| 49 | +You can call Python scripts directly, for example: |
| 50 | + |
| 51 | +```yaml |
| 52 | +description: This is a tool that calls a Python script. |
| 53 | +args: arg1: The first argument. |
| 54 | + |
| 55 | +#!/usr/bin/env python3 ./path/to/script.py --arg1="${arg1}" |
| 56 | +``` |
| 57 | + |
| 58 | +If you need to bootstrap a Python environment, you can use a virtual environment. For example: |
| 59 | + |
| 60 | +```yaml |
| 61 | +description: This is a tool that calls a Python script in a virtual environment. |
| 62 | +args: arg1: The first argument. |
| 63 | + |
| 64 | +#!/usr/bin/env sh |
| 65 | +python3 -m venv .venv |
| 66 | +pip3 install -r requirements.txt |
| 67 | +python3 ./path/to/script.py --arg1="${arg1}" |
| 68 | +``` |
| 69 | + |
| 70 | +## Node |
| 71 | + |
| 72 | +You can call Node.js scripts directly, for example: |
| 73 | + |
| 74 | +```yaml |
| 75 | +description: This is a tool that calls a Node.js script. |
| 76 | +args: arg1: The first argument. |
| 77 | + |
| 78 | +#!/usr/bin/env node ./path/to/script.js --arg1="${arg1}" |
| 79 | +``` |
| 80 | + |
| 81 | +If you need to bootstrap a Node.js environment, you can use call npm in the `tool.gpt` file. For example: |
| 82 | + |
| 83 | +```yaml |
| 84 | +description: This is a tool that calls a Node.js script with a package manager. |
| 85 | +args: arg1: The first argument. |
| 86 | + |
| 87 | +#!/usr/bin/env sh |
| 88 | +npm install |
| 89 | +node ./path/to/script.js --arg1="${arg1}" |
| 90 | +``` |
| 91 | + |
| 92 | +## Golang |
| 93 | + |
| 94 | +You can call Golang binaries directly, for example: |
| 95 | + |
| 96 | +```yaml |
| 97 | +description: This is a tool that calls a Golang binary. |
| 98 | +args: arg1: The first argument. |
| 99 | + |
| 100 | +#!/usr/bin/env ./path/to/binary --arg1="${arg1}" |
| 101 | +``` |
| 102 | + |
| 103 | +If you need to bootstrap a Golang binary, you can the go CLI to build it before running it. For example: |
| 104 | + |
| 105 | +```yaml |
| 106 | +description: This is a tool that calls a Golang binary. |
| 107 | +args: arg1: The first argument. |
| 108 | + |
| 109 | +#!/usr/bin/env sh |
| 110 | +go build -o ./path/to/binary |
| 111 | +./path/to/binary --arg1="${arg1}" |
| 112 | +``` |
0 commit comments