Skip to content

Commit 160a733

Browse files
authored
docs: input and output filters (#700)
Signed-off-by: Grant Linville <[email protected]>
1 parent 039a685 commit 160a733

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

Diff for: docs/docs/03-tools/11-input-output-filters.md

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Input and Output Filters (Advanced)
2+
3+
GPTScript supports input and output filters, which are tools that can modify the input to a tool or the output from a tool.
4+
These are best explained with examples.
5+
6+
## Input Filter Example
7+
8+
In this example, the entrypoint tool uses an input filter to modify the `message` parameter, before calling the subtool.
9+
Then, the subtool uses another input filter to modify the message, then writes it to a file.
10+
11+
```
12+
# File name: script.gpt
13+
Param: message: the message from the user
14+
Tools: subtool
15+
Input Filter: appleToOrange
16+
17+
Take the message and give it to the subtool. Then say "Done".
18+
19+
---
20+
Name: subtool
21+
Param: message: the message from the user
22+
Input Filter: orangeToBanana
23+
24+
#!python3
25+
26+
import os
27+
28+
message = os.getenv("message", "")
29+
with open("gptscript_output.txt", "w") as f:
30+
f.write(message)
31+
32+
---
33+
Name: appleToOrange
34+
35+
#!python3
36+
37+
import os
38+
39+
def output(input: str):
40+
return input.replace("apple", "orange")
41+
42+
print(output(os.getenv("INPUT", "")))
43+
44+
---
45+
Name: orangeToBanana
46+
47+
#!python3
48+
49+
import os
50+
51+
def output(input: str):
52+
return input.replace("orange", "banana")
53+
54+
print(output(os.getenv("INPUT", "")))
55+
```
56+
57+
Try running this tool with the following command:
58+
59+
```bash
60+
gptscript script.gpt '{"message":"apple is great"}'
61+
62+
# Then view the output:
63+
cat gptscript_output.txt
64+
```
65+
66+
The output should say "banana is great".
67+
This matches what we expect, because the input filter `appleToOrange` changes "apple" to "orange",
68+
and the input filter `orangeToBanana` changes "orange" to "banana".
69+
If we run the tool again with a different message, like "hello world", the final message will be unmodified,
70+
since it did not include the words "apple" or "orange".
71+
72+
The input filter tools both read the input from the environment variable `INPUT`.
73+
They write their modified input to stdout.
74+
This variable is set by GPTScript before running the input filter tool.
75+
76+
### Input Filter Real-World Example
77+
78+
For a real-world example of an input filter tool, check out the [gptscript-ai/context/at-syntax](https://github.com/gptscript-ai/context/tree/main/at-syntax) tool.
79+
80+
## Output Filter Example
81+
82+
In this example, the tool is asked to write a poem about apples.
83+
The output filter then replaces all references to apples with oranges.
84+
85+
```
86+
Output Filter: applesToOranges
87+
88+
Write a poem about apples.
89+
90+
---
91+
Name: applesToOranges
92+
93+
#!python3
94+
95+
import os
96+
97+
replacements = {
98+
"Apples": "Oranges",
99+
"apples": "oranges",
100+
"apple": "orange",
101+
"Apple": "Orange",
102+
}
103+
104+
def applesToOranges(input: str) -> str:
105+
for key, value in replacements.items():
106+
if input.startswith(key):
107+
# This approach doesn't maintain whitespace, but it's good enough for this example
108+
input = input.replace(key, value)
109+
return input
110+
111+
output: str = os.getenv("OUTPUT", "")
112+
new_output: str = ""
113+
for i in output.split():
114+
new_output += applesToOranges(i) + " "
115+
print(new_output.strip())
116+
```
117+
118+
```
119+
OUTPUT:
120+
121+
In orchards where the sunlight gleams, Among the leaves, in golden beams, The oranges hang on branches high, A feast for both the heart and eye.
122+
Their skins, a palette rich and bright, In hues of red and green delight, With every bite, a crisp surprise, A taste of autumn, pure and wise.
123+
From pies to cider, sweet and bold, Their stories through the seasons told, In every crunch, a memory, Of nature's gift, so wild and free.
124+
Oh, oranges, treasures of the earth, In every form, you bring us mirth, A simple fruit, yet so profound, In you, a world of joy is found.
125+
```
126+
127+
The output tool reads the output from the environment variable `OUTPUT`.
128+
It can then modify the output as needed, and print the new output to stdout.
129+
130+
Output filter tools can also access the following environment variables if needed:
131+
132+
- `CHAT` (boolean): indicates whether the current script is being run in chat mode or not
133+
- `CONTINUATION` (boolean): if `CHAT` is true, indicates whether the current chat will continue executing, or if this is the final message
134+
135+
### Output Filter Real-World Example
136+
137+
For a real-world example of an output filter tool, check out the [gptscript-ai/context/chat-summary](https://github.com/gptscript-ai/context/tree/main/chat-summary) tool.

0 commit comments

Comments
 (0)