-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeinterpreter.go
More file actions
525 lines (476 loc) · 21.5 KB
/
codeinterpreter.go
File metadata and controls
525 lines (476 loc) · 21.5 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package together
import (
"context"
"encoding/json"
"net/http"
"slices"
"github.com/togethercomputer/together-go/internal/apijson"
"github.com/togethercomputer/together-go/internal/requestconfig"
"github.com/togethercomputer/together-go/option"
"github.com/togethercomputer/together-go/packages/param"
"github.com/togethercomputer/together-go/packages/respjson"
"github.com/togethercomputer/together-go/shared/constant"
)
// CodeInterpreterService contains methods and other services that help with
// interacting with the together API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewCodeInterpreterService] method instead.
type CodeInterpreterService struct {
Options []option.RequestOption
Sessions CodeInterpreterSessionService
}
// NewCodeInterpreterService generates a new service that applies the given options
// to each request. These options are applied after the parent client's options (if
// there is one), and before any request-specific options.
func NewCodeInterpreterService(opts ...option.RequestOption) (r CodeInterpreterService) {
r = CodeInterpreterService{}
r.Options = opts
r.Sessions = NewCodeInterpreterSessionService(opts...)
return
}
// Executes the given code snippet and returns the output. Without a session_id, a
// new session will be created to run the code. If you do pass in a valid
// session_id, the code will be run in that session. This is useful for running
// multiple code snippets in the same environment, because dependencies and similar
// things are persisted between calls to the same session.
func (r *CodeInterpreterService) Execute(ctx context.Context, body CodeInterpreterExecuteParams, opts ...option.RequestOption) (res *ExecuteResponseUnion, err error) {
opts = slices.Concat(r.Options, opts)
path := "tci/execute"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// ExecuteResponseUnion contains all possible properties and values from
// [ExecuteResponseSuccessfulExecution], [ExecuteResponseFailedExecution].
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
type ExecuteResponseUnion struct {
// This field is a union of [ExecuteResponseSuccessfulExecutionData], [any]
Data ExecuteResponseUnionData `json:"data"`
// This field is a union of [any], [[]ExecuteResponseFailedExecutionErrorUnion]
Errors ExecuteResponseUnionErrors `json:"errors"`
JSON struct {
Data respjson.Field
Errors respjson.Field
raw string
} `json:"-"`
}
func (u ExecuteResponseUnion) AsSuccessfulExecution() (v ExecuteResponseSuccessfulExecution) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u ExecuteResponseUnion) AsFailedExecution() (v ExecuteResponseFailedExecution) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u ExecuteResponseUnion) RawJSON() string { return u.JSON.raw }
func (r *ExecuteResponseUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ExecuteResponseUnionData is an implicit subunion of [ExecuteResponseUnion].
// ExecuteResponseUnionData provides convenient access to the sub-properties of the
// union.
//
// For type safety it is recommended to directly use a variant of the
// [ExecuteResponseUnion].
//
// If the underlying value is not a json object, one of the following properties
// will be valid: OfExecuteResponseFailedExecutionData]
type ExecuteResponseUnionData struct {
// This field will be present if the value is a [any] instead of an object.
OfExecuteResponseFailedExecutionData any `json:",inline"`
// This field is from variant [ExecuteResponseSuccessfulExecutionData].
Outputs []ExecuteResponseSuccessfulExecutionDataOutputUnion `json:"outputs"`
// This field is from variant [ExecuteResponseSuccessfulExecutionData].
SessionID string `json:"session_id"`
// This field is from variant [ExecuteResponseSuccessfulExecutionData].
Status string `json:"status"`
JSON struct {
OfExecuteResponseFailedExecutionData respjson.Field
Outputs respjson.Field
SessionID respjson.Field
Status respjson.Field
raw string
} `json:"-"`
}
func (r *ExecuteResponseUnionData) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ExecuteResponseUnionErrors is an implicit subunion of [ExecuteResponseUnion].
// ExecuteResponseUnionErrors provides convenient access to the sub-properties of
// the union.
//
// For type safety it is recommended to directly use a variant of the
// [ExecuteResponseUnion].
//
// If the underlying value is not a json object, one of the following properties
// will be valid: OfExecuteResponseSuccessfulExecutionErrors
// OfExecuteResponseFailedExecutionErrors]
type ExecuteResponseUnionErrors struct {
// This field will be present if the value is a [any] instead of an object.
OfExecuteResponseSuccessfulExecutionErrors any `json:",inline"`
// This field will be present if the value is a
// [[]ExecuteResponseFailedExecutionErrorUnion] instead of an object.
OfExecuteResponseFailedExecutionErrors []ExecuteResponseFailedExecutionErrorUnion `json:",inline"`
JSON struct {
OfExecuteResponseSuccessfulExecutionErrors respjson.Field
OfExecuteResponseFailedExecutionErrors respjson.Field
raw string
} `json:"-"`
}
func (r *ExecuteResponseUnionErrors) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ExecuteResponseSuccessfulExecution struct {
Data ExecuteResponseSuccessfulExecutionData `json:"data,required"`
Errors any `json:"errors,required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
Errors respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ExecuteResponseSuccessfulExecution) RawJSON() string { return r.JSON.raw }
func (r *ExecuteResponseSuccessfulExecution) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ExecuteResponseSuccessfulExecutionData struct {
Outputs []ExecuteResponseSuccessfulExecutionDataOutputUnion `json:"outputs,required"`
// Identifier of the current session. Used to make follow-up calls.
SessionID string `json:"session_id,required"`
// Status of the execution. Currently only supports success.
//
// Any of "success".
Status string `json:"status"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Outputs respjson.Field
SessionID respjson.Field
Status respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ExecuteResponseSuccessfulExecutionData) RawJSON() string { return r.JSON.raw }
func (r *ExecuteResponseSuccessfulExecutionData) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ExecuteResponseSuccessfulExecutionDataOutputUnion contains all possible
// properties and values from
// [ExecuteResponseSuccessfulExecutionDataOutputStreamOutput],
// [ExecuteResponseSuccessfulExecutionDataOutputError],
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutput].
//
// Use the [ExecuteResponseSuccessfulExecutionDataOutputUnion.AsAny] method to
// switch on the variant.
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
type ExecuteResponseSuccessfulExecutionDataOutputUnion struct {
// This field is a union of [string], [string],
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData]
Data ExecuteResponseSuccessfulExecutionDataOutputUnionData `json:"data"`
// Any of nil, "error", nil.
Type string `json:"type"`
JSON struct {
Data respjson.Field
Type respjson.Field
raw string
} `json:"-"`
}
func (u ExecuteResponseSuccessfulExecutionDataOutputUnion) AsStreamOutput() (v ExecuteResponseSuccessfulExecutionDataOutputStreamOutput) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u ExecuteResponseSuccessfulExecutionDataOutputUnion) AsError() (v ExecuteResponseSuccessfulExecutionDataOutputError) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u ExecuteResponseSuccessfulExecutionDataOutputUnion) AsDisplayorExecuteOutput() (v ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutput) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u ExecuteResponseSuccessfulExecutionDataOutputUnion) RawJSON() string { return u.JSON.raw }
func (r *ExecuteResponseSuccessfulExecutionDataOutputUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ExecuteResponseSuccessfulExecutionDataOutputUnionData is an implicit subunion of
// [ExecuteResponseSuccessfulExecutionDataOutputUnion].
// ExecuteResponseSuccessfulExecutionDataOutputUnionData provides convenient access
// to the sub-properties of the union.
//
// For type safety it is recommended to directly use a variant of the
// [ExecuteResponseSuccessfulExecutionDataOutputUnion].
//
// If the underlying value is not a json object, one of the following properties
// will be valid: OfString]
type ExecuteResponseSuccessfulExecutionDataOutputUnionData struct {
// This field will be present if the value is a [string] instead of an object.
OfString string `json:",inline"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ApplicationGeoJson map[string]any `json:"application/geo+json"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ApplicationJavascript string `json:"application/javascript"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ApplicationJson map[string]any `json:"application/json"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ApplicationPdf string `json:"application/pdf"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ApplicationVndVegaV5Json map[string]any `json:"application/vnd.vega.v5+json"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ApplicationVndVegaliteV4Json map[string]any `json:"application/vnd.vegalite.v4+json"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ImageGif string `json:"image/gif"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ImageJpeg string `json:"image/jpeg"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ImagePng string `json:"image/png"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
ImageSvgXml string `json:"image/svg+xml"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
TextHTML string `json:"text/html"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
TextLatex string `json:"text/latex"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
TextMarkdown string `json:"text/markdown"`
// This field is from variant
// [ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData].
TextPlain string `json:"text/plain"`
JSON struct {
OfString respjson.Field
ApplicationGeoJson respjson.Field
ApplicationJavascript respjson.Field
ApplicationJson respjson.Field
ApplicationPdf respjson.Field
ApplicationVndVegaV5Json respjson.Field
ApplicationVndVegaliteV4Json respjson.Field
ImageGif respjson.Field
ImageJpeg respjson.Field
ImagePng respjson.Field
ImageSvgXml respjson.Field
TextHTML respjson.Field
TextLatex respjson.Field
TextMarkdown respjson.Field
TextPlain respjson.Field
raw string
} `json:"-"`
}
func (r *ExecuteResponseSuccessfulExecutionDataOutputUnionData) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Outputs that were printed to stdout or stderr
type ExecuteResponseSuccessfulExecutionDataOutputStreamOutput struct {
Data string `json:"data,required"`
// Any of "stdout", "stderr".
Type string `json:"type,required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ExecuteResponseSuccessfulExecutionDataOutputStreamOutput) RawJSON() string { return r.JSON.raw }
func (r *ExecuteResponseSuccessfulExecutionDataOutputStreamOutput) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Errors and exceptions that occurred. If this output type is present, your code
// did not execute successfully.
type ExecuteResponseSuccessfulExecutionDataOutputError struct {
Data string `json:"data,required"`
Type constant.Error `json:"type,required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ExecuteResponseSuccessfulExecutionDataOutputError) RawJSON() string { return r.JSON.raw }
func (r *ExecuteResponseSuccessfulExecutionDataOutputError) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutput struct {
Data ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData `json:"data,required"`
// Any of "display_data", "execute_result".
Type string `json:"type,required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutput) RawJSON() string {
return r.JSON.raw
}
func (r *ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutput) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData struct {
ApplicationGeoJson map[string]any `json:"application/geo+json"`
ApplicationJavascript string `json:"application/javascript"`
ApplicationJson map[string]any `json:"application/json"`
ApplicationPdf string `json:"application/pdf" format:"byte"`
ApplicationVndVegaV5Json map[string]any `json:"application/vnd.vega.v5+json"`
ApplicationVndVegaliteV4Json map[string]any `json:"application/vnd.vegalite.v4+json"`
ImageGif string `json:"image/gif" format:"byte"`
ImageJpeg string `json:"image/jpeg" format:"byte"`
ImagePng string `json:"image/png" format:"byte"`
ImageSvgXml string `json:"image/svg+xml"`
TextHTML string `json:"text/html"`
TextLatex string `json:"text/latex"`
TextMarkdown string `json:"text/markdown"`
TextPlain string `json:"text/plain"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ApplicationGeoJson respjson.Field
ApplicationJavascript respjson.Field
ApplicationJson respjson.Field
ApplicationPdf respjson.Field
ApplicationVndVegaV5Json respjson.Field
ApplicationVndVegaliteV4Json respjson.Field
ImageGif respjson.Field
ImageJpeg respjson.Field
ImagePng respjson.Field
ImageSvgXml respjson.Field
TextHTML respjson.Field
TextLatex respjson.Field
TextMarkdown respjson.Field
TextPlain respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData) RawJSON() string {
return r.JSON.raw
}
func (r *ExecuteResponseSuccessfulExecutionDataOutputDisplayorExecuteOutputData) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ExecuteResponseFailedExecution struct {
Data any `json:"data,required"`
Errors []ExecuteResponseFailedExecutionErrorUnion `json:"errors,required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
Errors respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ExecuteResponseFailedExecution) RawJSON() string { return r.JSON.raw }
func (r *ExecuteResponseFailedExecution) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ExecuteResponseFailedExecutionErrorUnion contains all possible properties and
// values from [string], [map[string]any].
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
//
// If the underlying value is not a json object, one of the following properties
// will be valid: OfString OfExecuteResponseFailedExecutionErrorMapItem]
type ExecuteResponseFailedExecutionErrorUnion struct {
// This field will be present if the value is a [string] instead of an object.
OfString string `json:",inline"`
// This field will be present if the value is a [any] instead of an object.
OfExecuteResponseFailedExecutionErrorMapItem any `json:",inline"`
JSON struct {
OfString respjson.Field
OfExecuteResponseFailedExecutionErrorMapItem respjson.Field
raw string
} `json:"-"`
}
func (u ExecuteResponseFailedExecutionErrorUnion) AsString() (v string) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u ExecuteResponseFailedExecutionErrorUnion) AsAnyMap() (v map[string]any) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u ExecuteResponseFailedExecutionErrorUnion) RawJSON() string { return u.JSON.raw }
func (r *ExecuteResponseFailedExecutionErrorUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CodeInterpreterExecuteParams struct {
// Code snippet to execute.
Code string `json:"code,required"`
// Programming language for the code to execute. Currently only supports Python,
// but more will be added.
//
// Any of "python".
Language CodeInterpreterExecuteParamsLanguage `json:"language,omitzero,required"`
// Identifier of the current session. Used to make follow-up calls. Requests will
// return an error if the session does not belong to the caller or has expired.
SessionID param.Opt[string] `json:"session_id,omitzero"`
// Files to upload to the session. If present, files will be uploaded before
// executing the given code.
Files []CodeInterpreterExecuteParamsFile `json:"files,omitzero"`
paramObj
}
func (r CodeInterpreterExecuteParams) MarshalJSON() (data []byte, err error) {
type shadow CodeInterpreterExecuteParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CodeInterpreterExecuteParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Programming language for the code to execute. Currently only supports Python,
// but more will be added.
type CodeInterpreterExecuteParamsLanguage string
const (
CodeInterpreterExecuteParamsLanguagePython CodeInterpreterExecuteParamsLanguage = "python"
)
// The properties Content, Encoding, Name are required.
type CodeInterpreterExecuteParamsFile struct {
Content string `json:"content,required"`
// Encoding of the file content. Use `string` for text files such as code, and
// `base64` for binary files, such as images.
//
// Any of "string", "base64".
Encoding string `json:"encoding,omitzero,required"`
Name string `json:"name,required"`
paramObj
}
func (r CodeInterpreterExecuteParamsFile) MarshalJSON() (data []byte, err error) {
type shadow CodeInterpreterExecuteParamsFile
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CodeInterpreterExecuteParamsFile) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
func init() {
apijson.RegisterFieldValidator[CodeInterpreterExecuteParamsFile](
"encoding", "string", "base64",
)
}