-
Notifications
You must be signed in to change notification settings - Fork 1
/
models_client.go
244 lines (210 loc) · 8.23 KB
/
models_client.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package modzy
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/modzy/sdk-go/model"
)
type ModelsClient interface {
// ListModels lists all models. This supports paging and filtering.
ListModels(ctx context.Context, input *ListModelsInput) (*ListModelsOutput, error)
// GetLatestModels returns all of the recent models for your team
GetLatestModels(ctx context.Context) (*GetLatestModelsOutput, error)
// GetMinimumEngines reads the engine configuration values
GetMinimumEngines(ctx context.Context) (*GetMinimumEnginesOutput, error)
// UpdateModelProcessingEngines updates the engine configuration values
UpdateModelProcessingEngines(ctx context.Context, input *UpdateModelProcessingEnginesInput) (*UpdateModelProcessingEnginesOutput, error)
// GetModelDetails reads the details of a model
GetModelDetails(ctx context.Context, input *GetModelDetailsInput) (*GetModelDetailsOutput, error)
// GetModelDetailsByName reads details of a model that matches the provided name
GetModelDetailsByName(ctx context.Context, input *GetModelDetailsByNameInput) (*GetModelDetailsOutput, error)
// ListModelVersions lists all versions of a model. This supports paging and filtering.
ListModelVersions(ctx context.Context, input *ListModelVersionsInput) (*ListModelVersionsOutput, error)
// GetRelatedModels will return all models that are related to the given model.
GetRelatedModels(ctx context.Context, input *GetRelatedModelsInput) (*GetRelatedModelsOutput, error)
// GetModelVersionDetails reads the details of a specific version of a model.
GetModelVersionDetails(ctx context.Context, input *GetModelVersionDetailsInput) (*GetModelVersionDetailsOutput, error)
// GetModelVersionSampleInput gets a simple input for the provided model version.
GetModelVersionSampleInput(ctx context.Context, input *GetModelVersionSampleInputInput) (*GetModelVersionSampleInputOutput, error)
// GetModelVersionSampleOutput gets a simple output for the provided model version.
GetModelVersionSampleOutput(ctx context.Context, input *GetModelVersionSampleOutputInput) (*GetModelVersionSampleOutputOutput, error)
// GetTags returns all tags
GetTags(ctx context.Context) (*GetTagsOutput, error)
// GetTagModels returns models that match the provided tags
GetTagModels(ctx context.Context, input *GetTagModelsInput) (*GetTagModelsOutput, error)
}
type standardModelsClient struct {
baseClient *standardClient
}
var _ ModelsClient = &standardModelsClient{}
func (c *standardModelsClient) GetModelVersionDetails(ctx context.Context, input *GetModelVersionDetailsInput) (*GetModelVersionDetailsOutput, error) {
var out model.ModelVersionDetails
url := fmt.Sprintf("/api/models/%s/versions/%s", input.ModelID, input.Version)
_, err := c.baseClient.requestor.Get(ctx, url, &out)
if err != nil {
return nil, err
}
return &GetModelVersionDetailsOutput{
Details: out,
}, nil
}
func (c *standardModelsClient) GetLatestModels(ctx context.Context) (*GetLatestModelsOutput, error) {
var out []model.ModelDetails
url := "/api/models/latest"
_, err := c.baseClient.requestor.Get(ctx, url, &out)
if err != nil {
return nil, err
}
return &GetLatestModelsOutput{
Models: out,
}, nil
}
func (c *standardModelsClient) GetMinimumEngines(ctx context.Context) (*GetMinimumEnginesOutput, error) {
var out model.MinimumEngines
url := "/api/models/processing-engines"
_, err := c.baseClient.requestor.Get(ctx, url, &out)
if err != nil {
return nil, err
}
return &GetMinimumEnginesOutput{
Details: out,
}, nil
}
func (c *standardModelsClient) GetModelDetails(ctx context.Context, input *GetModelDetailsInput) (*GetModelDetailsOutput, error) {
var out model.ModelDetails
url := fmt.Sprintf("/api/models/%s", input.ModelID)
_, err := c.baseClient.requestor.Get(ctx, url, &out)
if err != nil {
return nil, err
}
return &GetModelDetailsOutput{
Details: out,
}, nil
}
func (c *standardModelsClient) GetRelatedModels(ctx context.Context, input *GetRelatedModelsInput) (*GetRelatedModelsOutput, error) {
var out []model.RelatedModel
url := fmt.Sprintf("/api/models/%s/related-models", input.ModelID)
_, err := c.baseClient.requestor.Get(ctx, url, &out)
if err != nil {
return nil, err
}
return &GetRelatedModelsOutput{
RelatedModels: out,
}, nil
}
func (c *standardModelsClient) ListModels(ctx context.Context, input *ListModelsInput) (*ListModelsOutput, error) {
input.Paging = input.Paging.withDefaults()
var items []model.ModelVersionSummary
url := "/api/models"
_, links, err := c.baseClient.requestor.List(ctx, url, input.Paging, &items)
if err != nil {
return nil, err
}
// decide if we have a next page (the next link is not always accurate?)
var nextPage *ListModelsInput
if _, hasNextLink := links["next"]; len(items) == input.Paging.PerPage && hasNextLink {
nextPage = &ListModelsInput{
Paging: input.Paging.Next(),
}
}
return &ListModelsOutput{
Models: items,
NextPage: nextPage,
}, nil
}
func (c *standardModelsClient) GetTags(ctx context.Context) (*GetTagsOutput, error) {
var items []model.ModelTag
url := "/api/models/tags"
_, err := c.baseClient.requestor.Get(ctx, url, &items)
if err != nil {
return nil, err
}
return &GetTagsOutput{
Tags: items,
}, nil
}
func (c *standardModelsClient) GetTagModels(ctx context.Context, input *GetTagModelsInput) (*GetTagModelsOutput, error) {
var out GetTagModelsOutput
url := fmt.Sprintf("/api/models/tags/%s", strings.Join(input.TagIDs, ","))
_, err := c.baseClient.requestor.Get(ctx, url, &out)
if err != nil {
return nil, err
}
return &out, nil
}
func (c *standardModelsClient) GetModelDetailsByName(ctx context.Context, input *GetModelDetailsByNameInput) (*GetModelDetailsOutput, error) {
models, err := c.ListModels(ctx, (&ListModelsInput{}).
WithPaging(1, 1).
WithFilterAnd(ListModelsFilterFieldName, input.Name),
)
if err != nil {
return nil, err
}
if len(models.Models) != 1 {
return nil, ErrNotFound
}
return c.GetModelDetails(ctx, &GetModelDetailsInput{ModelID: models.Models[0].ID})
}
func (c *standardModelsClient) ListModelVersions(ctx context.Context, input *ListModelVersionsInput) (*ListModelVersionsOutput, error) {
input.Paging = input.Paging.withDefaults()
var items []model.ModelVersion
url := fmt.Sprintf("/api/models/%s/versions", input.ModelID)
_, links, err := c.baseClient.requestor.List(ctx, url, input.Paging, &items)
if err != nil {
return nil, err
}
// decide if we have a next page (the next link is not always accurate?)
var nextPage *ListModelsInput
if _, hasNextLink := links["next"]; len(items) == input.Paging.PerPage && hasNextLink {
nextPage = &ListModelsInput{
Paging: input.Paging.Next(),
}
}
return &ListModelVersionsOutput{
Versions: items,
NextPage: nextPage,
}, nil
}
func (c *standardModelsClient) UpdateModelProcessingEngines(ctx context.Context, input *UpdateModelProcessingEnginesInput) (*UpdateModelProcessingEnginesOutput, error) {
isAdmin, err := c.baseClient.Accounting().HasEntitlement(ctx, "CAN_PATCH_PROCESSING_MODEL_VERSION")
if err != nil {
return nil, err
}
url := fmt.Sprintf("/api/models/%s/versions/%s", input.ModelID, input.Version)
if isAdmin {
url = url + "/processing"
}
var out model.ModelVersionDetails
_, err = c.baseClient.requestor.Patch(ctx, url, input, &out)
if err != nil {
return nil, err
}
return &UpdateModelProcessingEnginesOutput{
Details: out,
}, nil
}
func (c *standardModelsClient) GetModelVersionSampleInput(ctx context.Context, input *GetModelVersionSampleInputInput) (*GetModelVersionSampleInputOutput, error) {
var out interface{}
url := fmt.Sprintf("/api/models/%s/versions/%s/sample-input", input.ModelID, input.Version)
_, err := c.baseClient.requestor.Get(ctx, url, &out)
if err != nil {
return nil, err
}
jsonB, err := json.Marshal(out)
return &GetModelVersionSampleInputOutput{
Sample: string(jsonB),
}, err
}
func (c *standardModelsClient) GetModelVersionSampleOutput(ctx context.Context, input *GetModelVersionSampleOutputInput) (*GetModelVersionSampleOutputOutput, error) {
var out interface{}
url := fmt.Sprintf("/api/models/%s/versions/%s/sample-output", input.ModelID, input.Version)
_, err := c.baseClient.requestor.Get(ctx, url, &out)
if err != nil {
return nil, err
}
jsonB, err := json.Marshal(out)
return &GetModelVersionSampleOutputOutput{
Sample: string(jsonB),
}, err
}