-
Notifications
You must be signed in to change notification settings - Fork 8
/
datasets.go
150 lines (127 loc) · 3.73 KB
/
datasets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package gptscript
import (
"context"
"encoding/json"
"fmt"
)
type DatasetElementMeta struct {
Name string `json:"name"`
Description string `json:"description"`
}
type DatasetElement struct {
DatasetElementMeta `json:",inline"`
Contents string `json:"contents"`
BinaryContents []byte `json:"binaryContents"`
}
type DatasetMeta struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
type datasetRequest struct {
Input string `json:"input"`
DatasetTool string `json:"datasetTool"`
Env []string `json:"env"`
}
type addDatasetElementsArgs struct {
DatasetID string `json:"datasetID"`
Name string `json:"name"`
Description string `json:"description"`
Elements []DatasetElement `json:"elements"`
}
type listDatasetElementArgs struct {
DatasetID string `json:"datasetID"`
}
type getDatasetElementArgs struct {
DatasetID string `json:"datasetID"`
Element string `json:"name"`
}
func (g *GPTScript) ListDatasets(ctx context.Context) ([]DatasetMeta, error) {
out, err := g.runBasicCommand(ctx, "datasets", datasetRequest{
Input: "{}",
DatasetTool: g.globalOpts.DatasetTool,
Env: g.globalOpts.Env,
})
if err != nil {
return nil, err
}
var datasets []DatasetMeta
if err = json.Unmarshal([]byte(out), &datasets); err != nil {
return nil, err
}
return datasets, nil
}
type DatasetOptions struct {
Name, Description string
}
func (g *GPTScript) CreateDatasetWithElements(ctx context.Context, elements []DatasetElement, options ...DatasetOptions) (string, error) {
return g.AddDatasetElements(ctx, "", elements, options...)
}
func (g *GPTScript) AddDatasetElements(ctx context.Context, datasetID string, elements []DatasetElement, options ...DatasetOptions) (string, error) {
args := addDatasetElementsArgs{
DatasetID: datasetID,
Elements: elements,
}
for _, opt := range options {
if opt.Name != "" {
args.Name = opt.Name
}
if opt.Description != "" {
args.Description = opt.Description
}
}
argsJSON, err := json.Marshal(args)
if err != nil {
return "", fmt.Errorf("failed to marshal element args: %w", err)
}
return g.runBasicCommand(ctx, "datasets/add-elements", datasetRequest{
Input: string(argsJSON),
DatasetTool: g.globalOpts.DatasetTool,
Env: g.globalOpts.Env,
})
}
func (g *GPTScript) ListDatasetElements(ctx context.Context, datasetID string) ([]DatasetElementMeta, error) {
args := listDatasetElementArgs{
DatasetID: datasetID,
}
argsJSON, err := json.Marshal(args)
if err != nil {
return nil, fmt.Errorf("failed to marshal element args: %w", err)
}
out, err := g.runBasicCommand(ctx, "datasets/list-elements", datasetRequest{
Input: string(argsJSON),
DatasetTool: g.globalOpts.DatasetTool,
Env: g.globalOpts.Env,
})
if err != nil {
return nil, err
}
var elements []DatasetElementMeta
if err = json.Unmarshal([]byte(out), &elements); err != nil {
return nil, err
}
return elements, nil
}
func (g *GPTScript) GetDatasetElement(ctx context.Context, datasetID, elementName string) (DatasetElement, error) {
args := getDatasetElementArgs{
DatasetID: datasetID,
Element: elementName,
}
argsJSON, err := json.Marshal(args)
if err != nil {
return DatasetElement{}, fmt.Errorf("failed to marshal element args: %w", err)
}
out, err := g.runBasicCommand(ctx, "datasets/get-element", datasetRequest{
Input: string(argsJSON),
DatasetTool: g.globalOpts.DatasetTool,
Env: g.globalOpts.Env,
})
if err != nil {
return DatasetElement{}, err
}
var element DatasetElement
if err = json.Unmarshal([]byte(out), &element); err != nil {
return DatasetElement{}, err
}
return element, nil
}