@@ -2,7 +2,6 @@ package gptscript
2
2
3
3
import (
4
4
"context"
5
- "encoding/base64"
6
5
"encoding/json"
7
6
"fmt"
8
7
"os"
@@ -15,181 +14,92 @@ type DatasetElementMeta struct {
15
14
16
15
type DatasetElement struct {
17
16
DatasetElementMeta `json:",inline"`
18
- Contents []byte `json:"contents"`
19
- }
20
-
21
- type DatasetMeta struct {
22
- ID string `json:"id"`
23
- Name string `json:"name"`
24
- Description string `json:"description"`
17
+ Contents string `json:"contents"`
18
+ BinaryContents []byte `json:"binaryContents"`
25
19
}
26
20
27
21
type Dataset struct {
28
- DatasetMeta `json:",inline "`
29
- BaseDir string `json:"baseDir,omitempty"`
30
- Elements map [string ]DatasetElementMeta `json:"elements"`
22
+ ID string `json:"id "`
23
+ BaseDir string `json:"baseDir,omitempty"`
24
+ Elements map [string ]DatasetElementMeta `json:"elements"`
31
25
}
32
26
33
27
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"`
28
+ Input string `json:"input"`
29
+ DatasetTool string `json:"datasetTool"`
30
+ Env []string `json:"env"`
50
31
}
51
32
52
33
type addDatasetElementsArgs struct {
53
- DatasetID string `json:"datasetID"`
54
- Elements []DatasetElement `json:"elements"`
34
+ WorkspaceID string `json:"workspaceID"`
35
+ DatasetID string `json:"datasetID"`
36
+ Elements []DatasetElement `json:"elements"`
55
37
}
56
38
57
39
type listDatasetElementArgs struct {
58
- DatasetID string `json:"datasetID"`
40
+ WorkspaceID string `json:"workspaceID"`
41
+ DatasetID string `json:"datasetID"`
59
42
}
60
43
61
44
type getDatasetElementArgs struct {
62
- DatasetID string `json:"datasetID"`
63
- Element string `json:"element"`
45
+ WorkspaceID string `json:"workspaceID"`
46
+ DatasetID string `json:"datasetID"`
47
+ Element string `json:"name"`
64
48
}
65
49
66
- func (g * GPTScript ) ListDatasets (ctx context.Context , workspaceID string ) ([]DatasetMeta , error ) {
67
- if workspaceID == "" {
68
- workspaceID = os .Getenv ("GPTSCRIPT_WORKSPACE_ID" )
69
- }
70
-
50
+ func (g * GPTScript ) ListDatasets (ctx context.Context ) ([]string , error ) {
71
51
out , err := g .runBasicCommand (ctx , "datasets" , datasetRequest {
72
- Input : "{}" ,
73
- WorkspaceID : workspaceID ,
74
- DatasetToolRepo : g .globalOpts .DatasetToolRepo ,
75
- Env : g .globalOpts .Env ,
52
+ Input : fmt .Sprintf (`{"workspaceID": %q}` , os .Getenv ("GPTSCRIPT_WORKSPACE_ID" )),
53
+ DatasetTool : g .globalOpts .DatasetTool ,
54
+ Env : g .globalOpts .Env ,
76
55
})
77
56
if err != nil {
78
57
return nil , err
79
58
}
80
59
81
- var datasets []DatasetMeta
60
+ var datasets []string
82
61
if err = json .Unmarshal ([]byte (out ), & datasets ); err != nil {
83
62
return nil , err
84
63
}
85
64
return datasets , nil
86
65
}
87
66
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
117
- }
118
-
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
67
+ func (g * GPTScript ) CreateDatasetWithElements (ctx context.Context , elements []DatasetElement ) (string , error ) {
68
+ return g .AddDatasetElements (ctx , "" , elements )
150
69
}
151
70
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
-
71
+ func (g * GPTScript ) AddDatasetElements (ctx context.Context , datasetID string , elements []DatasetElement ) (string , error ) {
157
72
args := addDatasetElementsArgs {
158
- DatasetID : datasetID ,
159
- Elements : elements ,
73
+ WorkspaceID : os .Getenv ("GPTSCRIPT_WORKSPACE_ID" ),
74
+ DatasetID : datasetID ,
75
+ Elements : elements ,
160
76
}
161
77
argsJSON , err := json .Marshal (args )
162
78
if err != nil {
163
- return fmt .Errorf ("failed to marshal element args: %w" , err )
79
+ return "" , fmt .Errorf ("failed to marshal element args: %w" , err )
164
80
}
165
81
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 ,
82
+ return g .runBasicCommand (ctx , "datasets/add-elements" , datasetRequest {
83
+ Input : string (argsJSON ),
84
+ DatasetTool : g .globalOpts .DatasetTool ,
85
+ Env : g .globalOpts .Env ,
171
86
})
172
- return err
173
87
}
174
88
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
-
89
+ func (g * GPTScript ) ListDatasetElements (ctx context.Context , datasetID string ) ([]DatasetElementMeta , error ) {
180
90
args := listDatasetElementArgs {
181
- DatasetID : datasetID ,
91
+ WorkspaceID : os .Getenv ("GPTSCRIPT_WORKSPACE_ID" ),
92
+ DatasetID : datasetID ,
182
93
}
183
94
argsJSON , err := json .Marshal (args )
184
95
if err != nil {
185
96
return nil , fmt .Errorf ("failed to marshal element args: %w" , err )
186
97
}
187
98
188
99
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 ,
100
+ Input : string (argsJSON ),
101
+ DatasetTool : g .globalOpts .DatasetTool ,
102
+ Env : g .globalOpts .Env ,
193
103
})
194
104
if err != nil {
195
105
return nil , err
@@ -202,25 +112,21 @@ func (g *GPTScript) ListDatasetElements(ctx context.Context, workspaceID, datase
202
112
return elements , nil
203
113
}
204
114
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
-
115
+ func (g * GPTScript ) GetDatasetElement (ctx context.Context , datasetID , elementName string ) (DatasetElement , error ) {
210
116
args := getDatasetElementArgs {
211
- DatasetID : datasetID ,
212
- Element : elementName ,
117
+ WorkspaceID : os .Getenv ("GPTSCRIPT_WORKSPACE_ID" ),
118
+ DatasetID : datasetID ,
119
+ Element : elementName ,
213
120
}
214
121
argsJSON , err := json .Marshal (args )
215
122
if err != nil {
216
123
return DatasetElement {}, fmt .Errorf ("failed to marshal element args: %w" , err )
217
124
}
218
125
219
126
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 ,
127
+ Input : string (argsJSON ),
128
+ DatasetTool : g .globalOpts .DatasetTool ,
129
+ Env : g .globalOpts .Env ,
224
130
})
225
131
if err != nil {
226
132
return DatasetElement {}, err
0 commit comments