|
| 1 | +# Authoring Tools |
| 2 | + |
| 3 | +You can author your own tools for your use or to share with others. |
| 4 | +The process for authoring a tool is as simple as creating a `tool.gpt` file in the root directory of your project. |
| 5 | +This file is itself a GPTScript that defines the tool's name, description, and what it should do. |
| 6 | + |
| 7 | +For example, you can create a tool to read the contents of a webpage and return it to the user. The `tool.gpt` file for this tool might look like this: |
| 8 | + |
| 9 | +```yaml |
| 10 | +Description: Read and summarize the content of a webpage. |
| 11 | +Tools: sys.html2text |
| 12 | +Args: url: The URL of the webpage. |
| 13 | + |
| 14 | +Read the the contents of the webpage at ${url} and summarize it. |
| 15 | +``` |
| 16 | + |
| 17 | +You can also utilize traditional code-based tools. For example, here's an example of the `tool.gpt` file for the `image-generation` tool which wraps a python CLI: |
| 18 | + |
| 19 | +```yaml |
| 20 | +Description: Generates images based on the specified parameters and returns a list of URLs to the generated images. |
| 21 | +Args: prompt: (required) The text prompt based on which the GPT model will generate a response |
| 22 | +Args: size: (optional) The size of the image to generate, format WxH (e.g. 1024x1024). Defaults to 1024x1024. |
| 23 | +Args: quality: (optional) The quality of the generated image. Allowed values are "standard" or "hd". Default is "standard". |
| 24 | +Args: number: (optional) The number of images to generate. Defaults to 1. |
| 25 | + |
| 26 | +#!/usr/bin/env python3 ${GPTSCRIPT_TOOL_DIR}/cli.py --prompt="${prompt}" --size="${size}" --quality="${quality}" --number="${number}" |
| 27 | +``` |
| 28 | + |
| 29 | +At the bottom you'll notice a shebang (`#!/usr/bin/env ...`) 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. |
| 30 | +If there is no shebang line, then it will be treated as natural language for the LLM to process. |
| 31 | + |
| 32 | +:::tip |
| 33 | +Every arg becomes an environment variable when the tool is invoked. So instead of accepting args using flags like `--size="${size}", your program can just read the `size` environment variable. |
| 34 | +::: |
| 35 | + |
| 36 | +## Quickstart |
| 37 | + |
| 38 | +This is a guide for writing portable tools for GPTScript. The supported languages currently are Python, NodeJS, and Go. This guide uses Python but you can see documentation for the other language below. |
| 39 | + |
| 40 | +### 1. Write the code |
| 41 | + |
| 42 | +Create a file called `tool.py` with the following contents: |
| 43 | + |
| 44 | +```python |
| 45 | +import os |
| 46 | +import requests |
| 47 | + |
| 48 | +print(requests.get(os.getenv("url")).text) |
| 49 | +``` |
| 50 | + |
| 51 | +Create a file called `requirements.txt` with the following contents: |
| 52 | + |
| 53 | +``` |
| 54 | +requests |
| 55 | +``` |
| 56 | + |
| 57 | +### 2. Create the tool |
| 58 | + |
| 59 | +Create a file called `tool.gpt` with the following contents: |
| 60 | + |
| 61 | +``` |
| 62 | +Description: Returns the contents of a webpage. |
| 63 | +Args: url: The URL of the webpage. |
| 64 | +
|
| 65 | +#!/usr/bin/env python3 ${GPTSCRIPT_TOOL_DIR}/tool.py |
| 66 | +``` |
| 67 | + |
| 68 | +The `GPTSCRIPT_TOOL_DIR` environment variable is automatically populated by GPTScript so that the tool |
| 69 | +will be able to find the `tool.py` file no matter what the user's current working directory is. |
| 70 | + |
| 71 | +If you make the tool available in a public GitHub repo, then you will be able to refer to it by |
| 72 | +the URL, i.e. `github.com/<user>/<repo name>`. GPTScript will automatically set up a Python virtual |
| 73 | +environment, install the required packages, and execute the tool. |
| 74 | + |
| 75 | +### 3. Use the tool |
| 76 | + |
| 77 | +Here is an example of how you can use the tool once it is on GitHub: |
| 78 | + |
| 79 | +``` |
| 80 | +Tools: github.com/<user>/<repo name> |
| 81 | +
|
| 82 | +Get the contents of https://github.com |
| 83 | +``` |
| 84 | + |
| 85 | +## Sharing Tools |
| 86 | + |
| 87 | +GPTScript is designed to easily export and import tools. Doing this is currently based entirely around the use of GitHub repositories. You can export a tool by creating a GitHub repository and ensureing you have the `tool.gpt` file in the root of the repository. You can then import the tool into a GPTScript by specifying the URL of the repository in the `tools` section of the script. For example, we can leverage the `image-generation` tool by adding the following line to a GPTScript: |
| 88 | + |
| 89 | +```yaml |
| 90 | +tools: github.com/gptscript-ai/image-generation |
| 91 | + |
| 92 | +Generate an image of a city skyline at night. |
| 93 | +``` |
| 94 | + |
| 95 | +### Supported Languages |
| 96 | + |
| 97 | +GPTScript can execute any binary that you ask it to. However, it can also manage the installation of a language runtime and dependencies for you. Currently this is only supported for a few languages. Here are the supported languages and examples of tools written in those languages: |
| 98 | + |
| 99 | +| Language | Example | |
| 100 | +|----------|---------| |
| 101 | +| `Python` | [Image Generation](https://github.com/gptscript-ai/image-generation) - Generate images based on a prompt | |
| 102 | +| `Node.js` | [Vision](https://github.com/gptscript-ai/vision) - Analyze and interpret images | |
| 103 | +| `Golang` | [Search](https://github.com/gptscript-ai/search) - Use various providers to search the internet | |
| 104 | + |
| 105 | + |
| 106 | +## Automatic Documentation |
| 107 | + |
| 108 | +Each GPTScript tool is self-documented using the `tool.gpt` file. You can automatically generate documentation for your tools by visiting `tools.gptscript.ai/<github repo url>`. This documentation site allows others to easily search and explore the tools that have been created. |
| 109 | + |
| 110 | +You can add more information about how to use your tool by adding an `examples` directory to your repository and adding a collection of `.gpt` files that demonstrate how to use your tool. These examples will be automatically included in the documentation. |
| 111 | + |
| 112 | +For more information and to explore existing tools, visit [tools.gptscript.ai](https://tools.gptscript.ai). |
0 commit comments