Skip to content

Commit d701206

Browse files
authored
docs: add full 'hello world' Python example (#156)
Signed-off-by: Grant Linville <[email protected]>
1 parent ed1a747 commit d701206

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

docs/docs/100-tools/02-authoring.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
# Authoring Tools
22

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.
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.
46

57
Here's an example of the `tool.gpt` file for the `image-generation` tool:
68

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+
```
10+
description: Generates images based on the specified parameters and returns a list of URLs to the generated images.
911
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".
1112
args: size: (optional) The size of the image to generate, format WxH (e.g. 1024x1024). Defaults to 1024x1024.
1213
args: quality: (optional) The quality of the generated image. Allowed values are "standard" or "hd". Default is "standard".
1314
args: number: (optional) The number of images to generate. Defaults to 1.
1415
15-
#!/usr/bin/env python3 ./cli.py --prompt="${prompt}" --model="${model}" --size="${size}" --quality="${quality}" --number="${number}"
16+
#!/usr/bin/env python3 ${GPTSCRIPT_TOOL_DIR}/cli.py --prompt="${prompt}" --size="${size}" --quality="${quality}" --number="${number}"
1617
```
1718

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+
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.
20+
If there is no shebang line, then it will be treated as natural language for the LLM to process.
1921

2022
:::tip
21-
Every arg becomes an environment variable when the tool is invoked.
23+
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.
2224
:::
2325

2426
## Binary Tools
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Authoring Tools Example Guide
2+
3+
This is a guide for writing portable tools for GPTScript.
4+
The supported languages currently are Python, NodeJS, and Go. This guide uses Python.
5+
6+
## 1. Write the code
7+
8+
Create a file called `tool.py` with the following contents:
9+
10+
```python
11+
import os
12+
import requests
13+
14+
print(requests.get(os.getenv("url")).text)
15+
```
16+
17+
Create a file called `requirements.txt` with the following contents:
18+
19+
```
20+
requests
21+
```
22+
23+
## 2. Create the tool
24+
25+
Create a file called `tool.gpt` with the following contents:
26+
27+
```
28+
description: Returns the contents of a webpage.
29+
args: url: The URL of the webpage.
30+
31+
#!/usr/bin/env python3 ${GPTSCRIPT_TOOL_DIR}/tool.py
32+
```
33+
34+
The `GPTSCRIPT_TOOL_DIR` environment variable is automatically populated by GPTScript so that the tool
35+
will be able to find the `tool.py` file no matter what the user's current working directory is.
36+
37+
If you make the tool available in a public GitHub repo, then you will be able to refer to it by
38+
the URL, i.e. `github.com/<user>/<repo name>`. GPTScript will automatically set up a Python virtual
39+
environment, install the required packages, and execute the tool.
40+
41+
## 3. Use the tool
42+
43+
Here is an example of how you can use the tool once it is on GitHub:
44+
45+
```
46+
tools: github.com/<user>/<repo name>
47+
48+
Get the contents of https://github.com
49+
```

0 commit comments

Comments
 (0)