-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgptscript.go
494 lines (409 loc) · 12.1 KB
/
gptscript.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package gptscript
import (
"bufio"
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)
var (
serverProcess *exec.Cmd
serverProcessCancel context.CancelFunc
gptscriptCount int
serverURL string
lock sync.Mutex
)
const relativeToBinaryPath = "<me>"
type GPTScript struct {
globalOpts GlobalOptions
}
func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
opt := completeGlobalOptions(opts...)
if opt.Env == nil {
opt.Env = os.Environ()
}
opt.Env = append(opt.Env, opt.toEnv()...)
lock.Lock()
defer lock.Unlock()
gptscriptCount++
startSDK := serverProcess == nil && serverURL == "" && opt.URL == ""
if serverURL == "" {
serverURL = os.Getenv("GPTSCRIPT_URL")
startSDK = startSDK && serverURL == ""
}
if startSDK {
ctx, cancel := context.WithCancel(context.Background())
in, _ := io.Pipe()
serverProcess = exec.CommandContext(ctx, getCommand(), "sys.sdkserver", "--listen-address", "127.0.0.1:0")
serverProcess.Env = opt.Env[:]
serverProcess.Stdin = in
stdErr, err := serverProcess.StderrPipe()
if err != nil {
cancel()
return nil, fmt.Errorf("failed to get stderr pipe: %w", err)
}
serverProcessCancel = func() {
cancel()
_ = in.Close()
_ = serverProcess.Wait()
}
if err = serverProcess.Start(); err != nil {
serverProcessCancel()
return nil, fmt.Errorf("failed to start server: %w", err)
}
serverURL, err = readAddress(stdErr)
if err != nil {
serverProcessCancel()
return nil, fmt.Errorf("failed to read server URL: %w", err)
}
go func() {
for {
// Ensure that stdErr is drained as logs come in
_, _, _ = bufio.NewReader(stdErr).ReadLine()
}
}()
if _, url, found := strings.Cut(serverURL, "addr="); found {
// Ensure backwards compatibility with older versions of the SDK server
serverURL = url
}
serverURL = strings.TrimSpace(serverURL)
}
if opt.URL == "" {
opt.URL = serverURL
}
if !strings.HasPrefix(opt.URL, "http://") && !strings.HasPrefix(opt.URL, "https://") {
opt.URL = "http://" + opt.URL
}
opt.Env = append(opt.Env, "GPTSCRIPT_URL="+opt.URL)
if opt.Token == "" {
opt.Token = os.Getenv("GPTSCRIPT_TOKEN")
}
if opt.Token != "" {
opt.Env = append(opt.Env, "GPTSCRIPT_TOKEN="+opt.Token)
}
return &GPTScript{
globalOpts: opt,
}, nil
}
func readAddress(stdErr io.Reader) (string, error) {
addr, err := bufio.NewReader(stdErr).ReadString('\n')
if err != nil {
return "", fmt.Errorf("failed to read server address: %w", err)
}
if _, url, found := strings.Cut(addr, "addr="); found {
// For backward compatibility: older versions of the SDK server print the address in a slightly different way.
addr = url
}
return addr, nil
}
func (g *GPTScript) URL() string {
return g.globalOpts.URL
}
func (g *GPTScript) Close() {
lock.Lock()
defer lock.Unlock()
gptscriptCount--
if gptscriptCount == 0 && serverProcessCancel != nil {
serverProcessCancel()
_ = serverProcess.Wait()
}
}
func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef) (*Run, error) {
opts.GlobalOptions = completeGlobalOptions(g.globalOpts, opts.GlobalOptions)
return (&Run{
url: opts.URL,
token: opts.Token,
requestPath: "evaluate",
state: Creating,
opts: opts,
tools: tools,
}).NextChat(ctx, opts.Input)
}
func (g *GPTScript) Run(ctx context.Context, toolPath string, opts Options) (*Run, error) {
opts.GlobalOptions = completeGlobalOptions(g.globalOpts, opts.GlobalOptions)
return (&Run{
url: opts.URL,
token: opts.Token,
requestPath: "run",
state: Creating,
opts: opts,
toolPath: toolPath,
}).NextChat(ctx, opts.Input)
}
type ParseOptions struct {
DisableCache bool
}
// Parse will parse the given file into an array of Nodes.
func (g *GPTScript) Parse(ctx context.Context, fileName string, opts ...ParseOptions) ([]Node, error) {
var disableCache bool
for _, opt := range opts {
disableCache = disableCache || opt.DisableCache
}
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"file": fileName, "disableCache": disableCache})
if err != nil {
return nil, err
}
var doc Document
if err = json.Unmarshal([]byte(out), &doc); err != nil {
return nil, err
}
for _, node := range doc.Nodes {
node.TextNode.process()
}
return doc.Nodes, nil
}
// ParseContent will parse the given string into a tool.
func (g *GPTScript) ParseContent(ctx context.Context, toolDef string) ([]Node, error) {
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"content": toolDef})
if err != nil {
return nil, err
}
var doc Document
if err = json.Unmarshal([]byte(out), &doc); err != nil {
return nil, err
}
for _, node := range doc.Nodes {
node.TextNode.process()
}
return doc.Nodes, nil
}
// Fmt will format the given nodes into a string.
func (g *GPTScript) Fmt(ctx context.Context, nodes []Node) (string, error) {
for _, node := range nodes {
node.TextNode.combine()
}
out, err := g.runBasicCommand(ctx, "fmt", Document{Nodes: nodes})
if err != nil {
return "", err
}
return out, nil
}
type LoadOptions struct {
DisableCache bool
SubTool string
}
// LoadFile will load the given file into a Program.
func (g *GPTScript) LoadFile(ctx context.Context, fileName string, opts ...LoadOptions) (*Program, error) {
return g.load(ctx, map[string]any{"file": fileName}, opts...)
}
// LoadContent will load the given content into a Program.
func (g *GPTScript) LoadContent(ctx context.Context, content string, opts ...LoadOptions) (*Program, error) {
return g.load(ctx, map[string]any{"content": content}, opts...)
}
// LoadTools will load the given tools into a Program.
func (g *GPTScript) LoadTools(ctx context.Context, toolDefs []ToolDef, opts ...LoadOptions) (*Program, error) {
return g.load(ctx, map[string]any{"toolDefs": toolDefs}, opts...)
}
func (g *GPTScript) load(ctx context.Context, payload map[string]any, opts ...LoadOptions) (*Program, error) {
for _, opt := range opts {
if opt.DisableCache {
payload["disableCache"] = true
}
if opt.SubTool != "" {
payload["subTool"] = opt.SubTool
}
}
out, err := g.runBasicCommand(ctx, "load", payload)
if err != nil {
return nil, err
}
type loadResponse struct {
Program *Program `json:"program"`
}
prg := new(loadResponse)
if err = json.Unmarshal([]byte(out), prg); err != nil {
return nil, err
}
return prg.Program, nil
}
// Version will return the output of `gptscript --version`
func (g *GPTScript) Version(ctx context.Context) (string, error) {
out, err := g.runBasicCommand(ctx, "version", nil)
if err != nil {
return "", err
}
return out, nil
}
type ListModelsOptions struct {
Providers []string
CredentialOverrides []string
}
type Model struct {
CreatedAt int64 `json:"created"`
ID string `json:"id"`
Object string `json:"object"`
OwnedBy string `json:"owned_by"`
Permission []Permission `json:"permission"`
Root string `json:"root"`
Parent string `json:"parent"`
Metadata map[string]string `json:"metadata"`
}
type Permission struct {
CreatedAt int64 `json:"created"`
ID string `json:"id"`
Object string `json:"object"`
AllowCreateEngine bool `json:"allow_create_engine"`
AllowSampling bool `json:"allow_sampling"`
AllowLogprobs bool `json:"allow_logprobs"`
AllowSearchIndices bool `json:"allow_search_indices"`
AllowView bool `json:"allow_view"`
AllowFineTuning bool `json:"allow_fine_tuning"`
Organization string `json:"organization"`
Group interface{} `json:"group"`
IsBlocking bool `json:"is_blocking"`
}
// ListModels will list all the available models.
func (g *GPTScript) ListModels(ctx context.Context, opts ...ListModelsOptions) ([]Model, error) {
var o ListModelsOptions
for _, opt := range opts {
o.Providers = append(o.Providers, opt.Providers...)
o.CredentialOverrides = append(o.CredentialOverrides, opt.CredentialOverrides...)
}
if g.globalOpts.DefaultModelProvider != "" {
o.Providers = append(o.Providers, g.globalOpts.DefaultModelProvider)
}
out, err := g.runBasicCommand(ctx, "list-models", map[string]any{
"providers": o.Providers,
"env": g.globalOpts.Env,
"credentialOverrides": o.CredentialOverrides,
})
if err != nil {
return nil, err
}
var models []Model
if err = json.Unmarshal([]byte(out), &models); err != nil {
return nil, fmt.Errorf("failed to parse models: %w", err)
}
return models, nil
}
func (g *GPTScript) Confirm(ctx context.Context, resp AuthResponse) error {
_, err := g.runBasicCommand(ctx, "confirm/"+resp.ID, resp)
return err
}
func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) error {
_, err := g.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
return err
}
type ListCredentialsOptions struct {
CredentialContexts []string
AllContexts bool
}
func (g *GPTScript) ListCredentials(ctx context.Context, opts ListCredentialsOptions) ([]Credential, error) {
req := CredentialRequest{}
if opts.AllContexts {
req.AllContexts = true
} else if len(opts.CredentialContexts) > 0 {
req.Context = opts.CredentialContexts
} else {
req.Context = []string{"default"}
}
out, err := g.runBasicCommand(ctx, "credentials", req)
if err != nil {
return nil, err
}
var creds []Credential
if err = json.Unmarshal([]byte(out), &creds); err != nil {
return nil, err
}
return creds, nil
}
func (g *GPTScript) CreateCredential(ctx context.Context, cred Credential) error {
credJSON, err := json.Marshal(cred)
if err != nil {
return fmt.Errorf("failed to marshal credential: %w", err)
}
_, err = g.runBasicCommand(ctx, "credentials/create", CredentialRequest{Content: string(credJSON)})
return err
}
func (g *GPTScript) RevealCredential(ctx context.Context, credCtxs []string, name string) (Credential, error) {
out, err := g.runBasicCommand(ctx, "credentials/reveal", CredentialRequest{
Context: credCtxs,
Name: name,
})
if err != nil {
return Credential{}, err
}
var cred Credential
if err = json.Unmarshal([]byte(out), &cred); err != nil {
return Credential{}, err
}
return cred, nil
}
func (g *GPTScript) DeleteCredential(ctx context.Context, credCtx, name string) error {
_, err := g.runBasicCommand(ctx, "credentials/delete", CredentialRequest{
Context: []string{credCtx}, // Only one context can be specified for delete operations
Name: name,
})
return err
}
func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, error) {
run := &Run{
url: g.globalOpts.URL,
requestPath: requestPath,
state: Creating,
basicCommand: true,
}
if err := run.request(ctx, body); err != nil {
return "", err
}
out, err := run.Text()
if err != nil {
return "", err
}
if run.err != nil {
return run.ErrorOutput(), run.err
}
return out, nil
}
func getCommand() string {
if gptScriptBin := os.Getenv("GPTSCRIPT_BIN"); gptScriptBin != "" {
if len(os.Args) == 0 {
return gptScriptBin
}
return determineProperCommand(filepath.Dir(os.Args[0]), gptScriptBin)
}
return "gptscript"
}
// determineProperCommand is for testing purposes. Users should use getCommand instead.
func determineProperCommand(dir, bin string) string {
if !strings.HasPrefix(bin, relativeToBinaryPath) {
return bin
}
bin = filepath.Join(dir, strings.TrimPrefix(bin, relativeToBinaryPath))
if !filepath.IsAbs(bin) {
bin = "." + string(os.PathSeparator) + bin
}
slog.Debug("Using gptscript binary: " + bin)
return bin
}
func GetEnv(key, def string) string {
v := os.Getenv(key)
if v == "" {
return def
}
if strings.HasPrefix(v, `{"_gz":"`) && strings.HasSuffix(v, `"}`) {
data, err := base64.StdEncoding.DecodeString(v[8 : len(v)-2])
if err != nil {
return v
}
gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
return v
}
strBytes, err := io.ReadAll(gz)
if err != nil {
return v
}
return string(strBytes)
}
return v
}