@@ -2,7 +2,6 @@ package gptscript
22
33import (
44 "context"
5- "encoding/base64"
65 "encoding/json"
76 "fmt"
87 "os"
@@ -15,181 +14,92 @@ type DatasetElementMeta struct {
1514
1615type DatasetElement struct {
1716 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"`
2519}
2620
2721type 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"`
3125}
3226
3327type 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"`
5031}
5132
5233type 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"`
5537}
5638
5739type listDatasetElementArgs struct {
58- DatasetID string `json:"datasetID"`
40+ WorkspaceID string `json:"workspaceID"`
41+ DatasetID string `json:"datasetID"`
5942}
6043
6144type 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"`
6448}
6549
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 ) {
7151 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 ,
7655 })
7756 if err != nil {
7857 return nil , err
7958 }
8059
81- var datasets []DatasetMeta
60+ var datasets []string
8261 if err = json .Unmarshal ([]byte (out ), & datasets ); err != nil {
8362 return nil , err
8463 }
8564 return datasets , nil
8665}
8766
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 )
15069}
15170
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 ) {
15772 args := addDatasetElementsArgs {
158- DatasetID : datasetID ,
159- Elements : elements ,
73+ WorkspaceID : os .Getenv ("GPTSCRIPT_WORKSPACE_ID" ),
74+ DatasetID : datasetID ,
75+ Elements : elements ,
16076 }
16177 argsJSON , err := json .Marshal (args )
16278 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 )
16480 }
16581
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 ,
17186 })
172- return err
17387}
17488
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 ) {
18090 args := listDatasetElementArgs {
181- DatasetID : datasetID ,
91+ WorkspaceID : os .Getenv ("GPTSCRIPT_WORKSPACE_ID" ),
92+ DatasetID : datasetID ,
18293 }
18394 argsJSON , err := json .Marshal (args )
18495 if err != nil {
18596 return nil , fmt .Errorf ("failed to marshal element args: %w" , err )
18697 }
18798
18899 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 ,
193103 })
194104 if err != nil {
195105 return nil , err
@@ -202,25 +112,21 @@ func (g *GPTScript) ListDatasetElements(ctx context.Context, workspaceID, datase
202112 return elements , nil
203113}
204114
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 ) {
210116 args := getDatasetElementArgs {
211- DatasetID : datasetID ,
212- Element : elementName ,
117+ WorkspaceID : os .Getenv ("GPTSCRIPT_WORKSPACE_ID" ),
118+ DatasetID : datasetID ,
119+ Element : elementName ,
213120 }
214121 argsJSON , err := json .Marshal (args )
215122 if err != nil {
216123 return DatasetElement {}, fmt .Errorf ("failed to marshal element args: %w" , err )
217124 }
218125
219126 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 ,
224130 })
225131 if err != nil {
226132 return DatasetElement {}, err
0 commit comments