@@ -2,10 +2,8 @@ package gptscript
22
33import (
44 "context"
5- "encoding/base64"
65 "encoding/json"
76 "fmt"
8- "os"
97)
108
119type DatasetElementMeta struct {
@@ -15,7 +13,8 @@ type DatasetElementMeta struct {
1513
1614type DatasetElement struct {
1715 DatasetElementMeta `json:",inline"`
18- Contents []byte `json:"contents"`
16+ Contents string `json:"contents"`
17+ BinaryContents []byte `json:"binaryContents"`
1918}
2019
2120type DatasetMeta struct {
@@ -24,34 +23,17 @@ type DatasetMeta struct {
2423 Description string `json:"description"`
2524}
2625
27- type Dataset struct {
28- DatasetMeta `json:",inline"`
29- BaseDir string `json:"baseDir,omitempty"`
30- Elements map [string ]DatasetElementMeta `json:"elements"`
31- }
32-
3326type 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"`
5030}
5131
5232type 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"`
5537}
5638
5739type listDatasetElementArgs struct {
@@ -60,19 +42,14 @@ type listDatasetElementArgs struct {
6042
6143type getDatasetElementArgs struct {
6244 DatasetID string `json:"datasetID"`
63- Element string `json:"element "`
45+ Element string `json:"name "`
6446}
6547
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 ) {
7149 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 ,
7653 })
7754 if err != nil {
7855 return nil , err
@@ -85,98 +62,42 @@ func (g *GPTScript) ListDatasets(ctx context.Context, workspaceID string) ([]Dat
8562 return datasets , nil
8663}
8764
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
11767}
11868
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 ... )
15071}
15172
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 ) {
15774 args := addDatasetElementsArgs {
15875 DatasetID : datasetID ,
15976 Elements : elements ,
16077 }
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+
16188 argsJSON , err := json .Marshal (args )
16289 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 )
16491 }
16592
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 ,
17197 })
172- return err
17398}
17499
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 ) {
180101 args := listDatasetElementArgs {
181102 DatasetID : datasetID ,
182103 }
@@ -186,10 +107,9 @@ func (g *GPTScript) ListDatasetElements(ctx context.Context, workspaceID, datase
186107 }
187108
188109 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 ,
193113 })
194114 if err != nil {
195115 return nil , err
@@ -202,11 +122,7 @@ func (g *GPTScript) ListDatasetElements(ctx context.Context, workspaceID, datase
202122 return elements , nil
203123}
204124
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 ) {
210126 args := getDatasetElementArgs {
211127 DatasetID : datasetID ,
212128 Element : elementName ,
@@ -217,10 +133,9 @@ func (g *GPTScript) GetDatasetElement(ctx context.Context, workspaceID, datasetI
217133 }
218134
219135 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 ,
224139 })
225140 if err != nil {
226141 return DatasetElement {}, err
0 commit comments