Skip to content

Commit e6b6bf8

Browse files
committed
feat: add Run type for more manageable implementation
This also introduces chat support. Signed-off-by: Donnie Adams <[email protected]>
1 parent 62ca3d9 commit e6b6bf8

13 files changed

+1619
-1071
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ go.work
2424
.idea/
2525
.vscode/
2626

27+
workspace/
2728
bin/
2829
workspace/

README.md

+79-55
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ To use the module, you need to first set the OPENAI_API_KEY environment variable
1616

1717
Additionally, you need the `gptscript` binary. You can install it on your system using the [installation instructions](https://github.com/gptscript-ai/gptscript?tab=readme-ov-file#1-install-the-latest-release). The binary can be on the PATH, or the `GPTSCRIPT_BIN` environment variable can be used to specify its location.
1818

19+
## Client
20+
21+
There are currently a couple "global" options, and the client helps to manage those. A client without any options is
22+
likely what you want. However, here are the current global options:
23+
24+
- `gptscriptURL`: The URL (including `http(s)://) of an "SDK server" to use instead of the fork/exec model.
25+
- `gptscriptBin`: The path to a `gptscript` binary to use instead of the bundled one.
26+
1927
## Options
2028

2129
These are optional options that can be passed to the various `exec` functions.
@@ -26,6 +34,10 @@ None of the options is required, and the defaults will reduce the number of call
2634
- `quiet`: No output logging
2735
- `chdir`: Change current working directory
2836
- `subTool`: Use tool of this name, not the first tool
37+
- `input`: Input arguments for the tool run
38+
- `workspace`: Directory to use for the workspace, if specified it will not be deleted on exit
39+
- `inlcudeEvents`: Whether to include the streaming of events. Default (false). Note that if this is true, you must stream the events. See below for details.
40+
- `chatState`: The chat state to continue, or null to start a new chat and return the state
2941

3042
## Functions
3143

@@ -45,7 +57,9 @@ import (
4557
)
4658

4759
func listTools(ctx context.Context) (string, error) {
48-
return gogptscript.ListTools(ctx)
60+
client := &gogptscript.Client{}
61+
client.Complete()
62+
return client.ListTools(ctx)
4963
}
5064
```
5165

@@ -65,13 +79,15 @@ import (
6579
)
6680

6781
func listModels(ctx context.Context) ([]string, error) {
68-
return gogptscript.ListModels(ctx)
82+
client := &gogptscript.Client{}
83+
client.Complete()
84+
return client.ListModels(ctx)
6985
}
7086
```
7187

72-
### ExecTool
88+
### Parse
7389

74-
Executes a prompt with optional arguments.
90+
Parse file into a Tool data structure
7591

7692
```go
7793
package main
@@ -82,18 +98,17 @@ import (
8298
gogptscript "github.com/gptscript-ai/go-gptscript"
8399
)
84100

85-
func runTool(ctx context.Context) (string, error) {
86-
t := gogptscript.Tool{
87-
Instructions: "who was the president of the united states in 1928?",
88-
}
89-
90-
return gogptscript.ExecTool(ctx, gogptscript.Opts{}, t)
101+
func parse(ctx context.Context, fileName string) ([]gogptscript.Node, error) {
102+
client := &gogptscript.Client{}
103+
client.Complete()
104+
105+
return client.Parse(ctx, fileName)
91106
}
92107
```
93108

94-
### ExecFile
109+
### ParseTool
95110

96-
Executes a GPT script file with optional input and arguments. The script is relative to the callers source directory.
111+
Parse contents that represents a GPTScript file into a data structure.
97112

98113
```go
99114
package main
@@ -104,18 +119,17 @@ import (
104119
gogptscript "github.com/gptscript-ai/go-gptscript"
105120
)
106121

107-
func execFile(ctx context.Context) (string, error) {
108-
opts := gogptscript.Opts{
109-
DisableCache: &[]bool{true}[0],
110-
}
111-
112-
return gogptscript.ExecFile(ctx, "./hello.gpt", "--input World", opts)
122+
func parseTool(ctx context.Context, contents string) ([]gogptscript.Node, error) {
123+
client := &gogptscript.Client{}
124+
client.Complete()
125+
126+
return client.ParseTool(ctx, contents)
113127
}
114128
```
115129

116-
### StreamExecTool
130+
### Fmt
117131

118-
Executes a gptscript with optional input and arguments, and returns the output streams.
132+
Parse convert a tool data structure into a GPTScript file.
119133

120134
```go
121135
package main
@@ -126,22 +140,17 @@ import (
126140
gogptscript "github.com/gptscript-ai/go-gptscript"
127141
)
128142

129-
func streamExecTool(ctx context.Context) error {
130-
t := gogptscript.Tool{
131-
Instructions: "who was the president of the united states in 1928?",
132-
}
133-
134-
stdOut, stdErr, wait := gogptscript.StreamExecTool(ctx, gogptscript.Opts{}, t)
135-
136-
// Read from stdOut and stdErr before call wait()
137-
138-
return wait()
143+
func parse(ctx context.Context, nodes []gogptscript.node) (string, error) {
144+
client := &gogptscript.Client{}
145+
client.Complete()
146+
147+
return client.Fmt(ctx, nodes)
139148
}
140149
```
141150

142-
### StreamExecToolWithEvents
151+
### Evaluate
143152

144-
Executes a gptscript with optional input and arguments, and returns the stdout, stderr, and gptscript events streams.
153+
Executes a tool with optional arguments.
145154

146155
```go
147156
package main
@@ -152,22 +161,26 @@ import (
152161
gogptscript "github.com/gptscript-ai/go-gptscript"
153162
)
154163

155-
func streamExecTool(ctx context.Context) error {
156-
t := gogptscript.Tool{
164+
func runTool(ctx context.Context) (string, error) {
165+
t := gogptscript.ToolDef{
157166
Instructions: "who was the president of the united states in 1928?",
158167
}
159168

160-
stdOut, stdErr, events, wait := gogptscript.StreamExecToolWithEvents(ctx, gogptscript.Opts{}, t)
169+
client := &gogptscript.Client{}
170+
client.Complete()
161171

162-
// Read from stdOut and stdErr before call wait()
172+
run, err := client.Evaluate(ctx, gogptscript.Opts{}, t)
173+
if err != nil {
174+
return "", err
175+
}
163176

164-
return wait()
177+
return run.Text()
165178
}
166179
```
167180

168-
### streamExecFile
181+
### Run
169182

170-
The script is relative to the callers source directory.
183+
Executes a GPT script file with optional input and arguments. The script is relative to the callers source directory.
171184

172185
```go
173186
package main
@@ -178,22 +191,27 @@ import (
178191
gogptscript "github.com/gptscript-ai/go-gptscript"
179192
)
180193

181-
func streamExecTool(ctx context.Context) error {
194+
func runFile(ctx context.Context) (string, error) {
182195
opts := gogptscript.Opts{
183196
DisableCache: &[]bool{true}[0],
197+
Input: "--input hello",
184198
}
185199

186-
stdOut, stdErr, wait := gogptscript.StreamExecFile(ctx, "./hello.gpt", "--input world", opts)
200+
client := &gogptscript.Client{}
201+
client.Complete()
187202

188-
// Read from stdOut and stdErr before call wait()
203+
run, err := client.Run(ctx, "./hello.gpt", opts)
204+
if err != nil {
205+
return "", err
206+
}
189207

190-
return wait()
208+
return run.Text()
191209
}
192210
```
193211

194-
### streamExecFileWithEvents
212+
### Streaming events
195213

196-
The script is relative to the callers source directory.
214+
In order to stream events, you must set `IncludeEvents` option to `true`. You if you don't set this and try to stream events, then it will succeed, but you will not get any events. More importantly, if you set `IncludeEvents` to `true`, you must stream the events for the script to complete.
197215

198216
```go
199217
package main
@@ -206,14 +224,26 @@ import (
206224

207225
func streamExecTool(ctx context.Context) error {
208226
opts := gogptscript.Opts{
209-
DisableCache: &[]bool{true}[0],
227+
DisableCache: &[]bool{true}[0],
228+
IncludeEvents: true,
229+
Input: "--input world",
210230
}
211231

212-
stdOut, stdErr, events, wait := gogptscript.StreamExecFileWithEvents(ctx, "./hello.gpt", "--input world", opts)
232+
client := &gogptscript.Client{}
233+
client.Complete()
213234

214-
// Read from stdOut and stdErr before call wait()
235+
run, err := client.Run(ctx, "./hello.gpt", opts)
236+
if err != nil {
237+
return err
238+
}
215239

216-
return wait()
240+
for event := range run.Events() {
241+
// Process event...
242+
}
243+
244+
// Wait for the output to ensure the script completes successfully.
245+
_, err = run.Text()
246+
return err
217247
}
218248
```
219249

@@ -235,12 +265,6 @@ func streamExecTool(ctx context.Context) error {
235265
| instructions | string | `""` | Instructions on how to use the tool. |
236266
| jsonResponse | boolean | `false` | Whether the tool returns a JSON response instead of plain text. You must include the word 'json' in the body of the prompt |
237267

238-
### FreeForm Parameters
239-
240-
| Argument | Type | Default | Description |
241-
|-----------|--------|---------|---------------------------------------|
242-
| content | string | `""` | This is a multi-line string that contains the entire contents of a valid gptscript file|
243-
244268
## License
245269

246270
Copyright (c) 2024, [Acorn Labs, Inc.](https://www.acorn.io)

0 commit comments

Comments
 (0)