@@ -388,6 +388,176 @@ func (g *GPTScript) DeleteCredential(ctx context.Context, credCtx, name string)
388
388
return err
389
389
}
390
390
391
+ // Dataset methods
392
+
393
+ func (g * GPTScript ) ListDatasets (ctx context.Context , workspace string ) ([]DatasetMeta , error ) {
394
+ if workspace == "" {
395
+ workspace = os .Getenv ("GPTSCRIPT_WORKSPACE_DIR" )
396
+ }
397
+
398
+ out , err := g .runBasicCommand (ctx , "datasets" , datasetRequest {
399
+ Input : "{}" ,
400
+ Workspace : workspace ,
401
+ DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
402
+ })
403
+
404
+ if err != nil {
405
+ return nil , err
406
+ }
407
+
408
+ if strings .HasPrefix (out , "ERROR:" ) {
409
+ return nil , fmt .Errorf (out )
410
+ }
411
+
412
+ var datasets []DatasetMeta
413
+ if err = json .Unmarshal ([]byte (out ), & datasets ); err != nil {
414
+ return nil , err
415
+ }
416
+ return datasets , nil
417
+ }
418
+
419
+ func (g * GPTScript ) CreateDataset (ctx context.Context , workspace , name , description string ) (Dataset , error ) {
420
+ if workspace == "" {
421
+ workspace = os .Getenv ("GPTSCRIPT_WORKSPACE_DIR" )
422
+ }
423
+
424
+ args := createDatasetArgs {
425
+ Name : name ,
426
+ Description : description ,
427
+ }
428
+ argsJSON , err := json .Marshal (args )
429
+ if err != nil {
430
+ return Dataset {}, fmt .Errorf ("failed to marshal dataset args: %w" , err )
431
+ }
432
+
433
+ out , err := g .runBasicCommand (ctx , "datasets/create" , datasetRequest {
434
+ Input : string (argsJSON ),
435
+ Workspace : workspace ,
436
+ DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
437
+ })
438
+
439
+ if err != nil {
440
+ return Dataset {}, err
441
+ }
442
+
443
+ if strings .HasPrefix (out , "ERROR:" ) {
444
+ return Dataset {}, fmt .Errorf (out )
445
+ }
446
+
447
+ var dataset Dataset
448
+ if err = json .Unmarshal ([]byte (out ), & dataset ); err != nil {
449
+ return Dataset {}, err
450
+ }
451
+ return dataset , nil
452
+ }
453
+
454
+ func (g * GPTScript ) AddDatasetElement (ctx context.Context , workspace , datasetID , elementName , elementDescription , elementContent string ) (DatasetElementMeta , error ) {
455
+ if workspace == "" {
456
+ workspace = os .Getenv ("GPTSCRIPT_WORKSPACE_DIR" )
457
+ }
458
+
459
+ args := addDatasetElementArgs {
460
+ DatasetID : datasetID ,
461
+ ElementName : elementName ,
462
+ ElementDescription : elementDescription ,
463
+ ElementContent : elementContent ,
464
+ }
465
+ argsJSON , err := json .Marshal (args )
466
+ if err != nil {
467
+ return DatasetElementMeta {}, fmt .Errorf ("failed to marshal element args: %w" , err )
468
+ }
469
+
470
+ out , err := g .runBasicCommand (ctx , "datasets/add-element" , datasetRequest {
471
+ Input : string (argsJSON ),
472
+ Workspace : workspace ,
473
+ DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
474
+ })
475
+
476
+ if err != nil {
477
+ return DatasetElementMeta {}, err
478
+ }
479
+
480
+ if strings .HasPrefix (out , "ERROR:" ) {
481
+ return DatasetElementMeta {}, fmt .Errorf (out )
482
+ }
483
+
484
+ var element DatasetElementMeta
485
+ if err = json .Unmarshal ([]byte (out ), & element ); err != nil {
486
+ return DatasetElementMeta {}, err
487
+ }
488
+ return element , nil
489
+ }
490
+
491
+ func (g * GPTScript ) ListDatasetElements (ctx context.Context , workspace , datasetID string ) ([]DatasetElementMeta , error ) {
492
+ if workspace == "" {
493
+ workspace = os .Getenv ("GPTSCRIPT_WORKSPACE_DIR" )
494
+ }
495
+
496
+ args := listDatasetElementArgs {
497
+ DatasetID : datasetID ,
498
+ }
499
+ argsJSON , err := json .Marshal (args )
500
+ if err != nil {
501
+ return nil , fmt .Errorf ("failed to marshal element args: %w" , err )
502
+ }
503
+
504
+ out , err := g .runBasicCommand (ctx , "datasets/list-elements" , datasetRequest {
505
+ Input : string (argsJSON ),
506
+ Workspace : workspace ,
507
+ DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
508
+ })
509
+
510
+ if err != nil {
511
+ return nil , err
512
+ }
513
+
514
+ if strings .HasPrefix (out , "ERROR:" ) {
515
+ return nil , fmt .Errorf (out )
516
+ }
517
+
518
+ var elements []DatasetElementMeta
519
+ if err = json .Unmarshal ([]byte (out ), & elements ); err != nil {
520
+ return nil , err
521
+ }
522
+ return elements , nil
523
+ }
524
+
525
+ func (g * GPTScript ) GetDatasetElement (ctx context.Context , workspace , datasetID , elementName string ) (DatasetElement , error ) {
526
+ if workspace == "" {
527
+ workspace = os .Getenv ("GPTSCRIPT_WORKSPACE_DIR" )
528
+ }
529
+
530
+ args := getDatasetElementArgs {
531
+ DatasetID : datasetID ,
532
+ Element : elementName ,
533
+ }
534
+ argsJSON , err := json .Marshal (args )
535
+ if err != nil {
536
+ return DatasetElement {}, fmt .Errorf ("failed to marshal element args: %w" , err )
537
+ }
538
+
539
+ out , err := g .runBasicCommand (ctx , "datasets/get-element" , datasetRequest {
540
+ Input : string (argsJSON ),
541
+ Workspace : workspace ,
542
+ DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
543
+ })
544
+
545
+ if err != nil {
546
+ return DatasetElement {}, err
547
+ }
548
+
549
+ if strings .HasPrefix (out , "ERROR:" ) {
550
+ return DatasetElement {}, fmt .Errorf (out )
551
+ }
552
+
553
+ var element DatasetElement
554
+ if err = json .Unmarshal ([]byte (out ), & element ); err != nil {
555
+ return DatasetElement {}, err
556
+ }
557
+
558
+ return element , nil
559
+ }
560
+
391
561
func (g * GPTScript ) runBasicCommand (ctx context.Context , requestPath string , body any ) (string , error ) {
392
562
run := & Run {
393
563
url : g .globalOpts .URL ,
0 commit comments