@@ -2,10 +2,8 @@ package gptscript
2
2
3
3
import (
4
4
"context"
5
- "encoding/base64"
6
5
"encoding/json"
7
6
"fmt"
8
- "os"
9
7
)
10
8
11
9
type DatasetElementMeta struct {
@@ -15,7 +13,8 @@ type DatasetElementMeta struct {
15
13
16
14
type DatasetElement struct {
17
15
DatasetElementMeta `json:",inline"`
18
- Contents []byte `json:"contents"`
16
+ Contents string `json:"contents"`
17
+ BinaryContents []byte `json:"binaryContents"`
19
18
}
20
19
21
20
type DatasetMeta struct {
@@ -24,34 +23,17 @@ type DatasetMeta struct {
24
23
Description string `json:"description"`
25
24
}
26
25
27
- type Dataset struct {
28
- DatasetMeta `json:",inline"`
29
- BaseDir string `json:"baseDir,omitempty"`
30
- Elements map [string ]DatasetElementMeta `json:"elements"`
31
- }
32
-
33
26
type datasetRequest struct {
34
- Input string `json:"input"`
35
- WorkspaceID string `json:"workspaceID"`
36
- DatasetToolRepo string `json:"datasetToolRepo"`
37
- Env []string `json:"env"`
38
- }
39
-
40
- type createDatasetArgs struct {
41
- Name string `json:"datasetName"`
42
- Description string `json:"datasetDescription"`
43
- }
44
-
45
- type addDatasetElementArgs struct {
46
- DatasetID string `json:"datasetID"`
47
- ElementName string `json:"elementName"`
48
- ElementDescription string `json:"elementDescription"`
49
- ElementContent string `json:"elementContent"`
27
+ Input string `json:"input"`
28
+ DatasetTool string `json:"datasetTool"`
29
+ Env []string `json:"env"`
50
30
}
51
31
52
32
type addDatasetElementsArgs struct {
53
- DatasetID string `json:"datasetID"`
54
- Elements []DatasetElement `json:"elements"`
33
+ DatasetID string `json:"datasetID"`
34
+ Name string `json:"name"`
35
+ Description string `json:"description"`
36
+ Elements []DatasetElement `json:"elements"`
55
37
}
56
38
57
39
type listDatasetElementArgs struct {
@@ -60,19 +42,14 @@ type listDatasetElementArgs struct {
60
42
61
43
type getDatasetElementArgs struct {
62
44
DatasetID string `json:"datasetID"`
63
- Element string `json:"element "`
45
+ Element string `json:"name "`
64
46
}
65
47
66
- func (g * GPTScript ) ListDatasets (ctx context.Context , workspaceID string ) ([]DatasetMeta , error ) {
67
- if workspaceID == "" {
68
- workspaceID = os .Getenv ("GPTSCRIPT_WORKSPACE_ID" )
69
- }
70
-
48
+ func (g * GPTScript ) ListDatasets (ctx context.Context ) ([]DatasetMeta , error ) {
71
49
out , err := g .runBasicCommand (ctx , "datasets" , datasetRequest {
72
- Input : "{}" ,
73
- WorkspaceID : workspaceID ,
74
- DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
75
- Env : g .globalOpts .Env ,
50
+ Input : "{}" ,
51
+ DatasetTool : g .globalOpts .DatasetTool ,
52
+ Env : g .globalOpts .Env ,
76
53
})
77
54
if err != nil {
78
55
return nil , err
@@ -85,98 +62,42 @@ func (g *GPTScript) ListDatasets(ctx context.Context, workspaceID string) ([]Dat
85
62
return datasets , nil
86
63
}
87
64
88
- func (g * GPTScript ) CreateDataset (ctx context.Context , workspaceID , name , description string ) (Dataset , error ) {
89
- if workspaceID == "" {
90
- workspaceID = os .Getenv ("GPTSCRIPT_WORKSPACE_ID" )
91
- }
92
-
93
- args := createDatasetArgs {
94
- Name : name ,
95
- Description : description ,
96
- }
97
- argsJSON , err := json .Marshal (args )
98
- if err != nil {
99
- return Dataset {}, fmt .Errorf ("failed to marshal dataset args: %w" , err )
100
- }
101
-
102
- out , err := g .runBasicCommand (ctx , "datasets/create" , datasetRequest {
103
- Input : string (argsJSON ),
104
- WorkspaceID : workspaceID ,
105
- DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
106
- Env : g .globalOpts .Env ,
107
- })
108
- if err != nil {
109
- return Dataset {}, err
110
- }
111
-
112
- var dataset Dataset
113
- if err = json .Unmarshal ([]byte (out ), & dataset ); err != nil {
114
- return Dataset {}, err
115
- }
116
- return dataset , nil
65
+ type DatasetOptions struct {
66
+ Name , Description string
117
67
}
118
68
119
- func (g * GPTScript ) AddDatasetElement (ctx context.Context , workspaceID , datasetID , elementName , elementDescription string , elementContent []byte ) (DatasetElementMeta , error ) {
120
- if workspaceID == "" {
121
- workspaceID = os .Getenv ("GPTSCRIPT_WORKSPACE_ID" )
122
- }
123
-
124
- args := addDatasetElementArgs {
125
- DatasetID : datasetID ,
126
- ElementName : elementName ,
127
- ElementDescription : elementDescription ,
128
- ElementContent : base64 .StdEncoding .EncodeToString (elementContent ),
129
- }
130
- argsJSON , err := json .Marshal (args )
131
- if err != nil {
132
- return DatasetElementMeta {}, fmt .Errorf ("failed to marshal element args: %w" , err )
133
- }
134
-
135
- out , err := g .runBasicCommand (ctx , "datasets/add-element" , datasetRequest {
136
- Input : string (argsJSON ),
137
- WorkspaceID : workspaceID ,
138
- DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
139
- Env : g .globalOpts .Env ,
140
- })
141
- if err != nil {
142
- return DatasetElementMeta {}, err
143
- }
144
-
145
- var element DatasetElementMeta
146
- if err = json .Unmarshal ([]byte (out ), & element ); err != nil {
147
- return DatasetElementMeta {}, err
148
- }
149
- return element , nil
69
+ func (g * GPTScript ) CreateDatasetWithElements (ctx context.Context , elements []DatasetElement , options ... DatasetOptions ) (string , error ) {
70
+ return g .AddDatasetElements (ctx , "" , elements , options ... )
150
71
}
151
72
152
- func (g * GPTScript ) AddDatasetElements (ctx context.Context , workspaceID , datasetID string , elements []DatasetElement ) error {
153
- if workspaceID == "" {
154
- workspaceID = os .Getenv ("GPTSCRIPT_WORKSPACE_ID" )
155
- }
156
-
73
+ func (g * GPTScript ) AddDatasetElements (ctx context.Context , datasetID string , elements []DatasetElement , options ... DatasetOptions ) (string , error ) {
157
74
args := addDatasetElementsArgs {
158
75
DatasetID : datasetID ,
159
76
Elements : elements ,
160
77
}
78
+
79
+ for _ , opt := range options {
80
+ if opt .Name != "" {
81
+ args .Name = opt .Name
82
+ }
83
+ if opt .Description != "" {
84
+ args .Description = opt .Description
85
+ }
86
+ }
87
+
161
88
argsJSON , err := json .Marshal (args )
162
89
if err != nil {
163
- return fmt .Errorf ("failed to marshal element args: %w" , err )
90
+ return "" , fmt .Errorf ("failed to marshal element args: %w" , err )
164
91
}
165
92
166
- _ , err = g .runBasicCommand (ctx , "datasets/add-elements" , datasetRequest {
167
- Input : string (argsJSON ),
168
- WorkspaceID : workspaceID ,
169
- DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
170
- Env : g .globalOpts .Env ,
93
+ return g .runBasicCommand (ctx , "datasets/add-elements" , datasetRequest {
94
+ Input : string (argsJSON ),
95
+ DatasetTool : g .globalOpts .DatasetTool ,
96
+ Env : g .globalOpts .Env ,
171
97
})
172
- return err
173
98
}
174
99
175
- func (g * GPTScript ) ListDatasetElements (ctx context.Context , workspaceID , datasetID string ) ([]DatasetElementMeta , error ) {
176
- if workspaceID == "" {
177
- workspaceID = os .Getenv ("GPTSCRIPT_WORKSPACE_ID" )
178
- }
179
-
100
+ func (g * GPTScript ) ListDatasetElements (ctx context.Context , datasetID string ) ([]DatasetElementMeta , error ) {
180
101
args := listDatasetElementArgs {
181
102
DatasetID : datasetID ,
182
103
}
@@ -186,10 +107,9 @@ func (g *GPTScript) ListDatasetElements(ctx context.Context, workspaceID, datase
186
107
}
187
108
188
109
out , err := g .runBasicCommand (ctx , "datasets/list-elements" , datasetRequest {
189
- Input : string (argsJSON ),
190
- WorkspaceID : workspaceID ,
191
- DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
192
- Env : g .globalOpts .Env ,
110
+ Input : string (argsJSON ),
111
+ DatasetTool : g .globalOpts .DatasetTool ,
112
+ Env : g .globalOpts .Env ,
193
113
})
194
114
if err != nil {
195
115
return nil , err
@@ -202,11 +122,7 @@ func (g *GPTScript) ListDatasetElements(ctx context.Context, workspaceID, datase
202
122
return elements , nil
203
123
}
204
124
205
- func (g * GPTScript ) GetDatasetElement (ctx context.Context , workspaceID , datasetID , elementName string ) (DatasetElement , error ) {
206
- if workspaceID == "" {
207
- workspaceID = os .Getenv ("GPTSCRIPT_WORKSPACE_ID" )
208
- }
209
-
125
+ func (g * GPTScript ) GetDatasetElement (ctx context.Context , datasetID , elementName string ) (DatasetElement , error ) {
210
126
args := getDatasetElementArgs {
211
127
DatasetID : datasetID ,
212
128
Element : elementName ,
@@ -217,10 +133,9 @@ func (g *GPTScript) GetDatasetElement(ctx context.Context, workspaceID, datasetI
217
133
}
218
134
219
135
out , err := g .runBasicCommand (ctx , "datasets/get-element" , datasetRequest {
220
- Input : string (argsJSON ),
221
- WorkspaceID : workspaceID ,
222
- DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
223
- Env : g .globalOpts .Env ,
136
+ Input : string (argsJSON ),
137
+ DatasetTool : g .globalOpts .DatasetTool ,
138
+ Env : g .globalOpts .Env ,
224
139
})
225
140
if err != nil {
226
141
return DatasetElement {}, err
0 commit comments