-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.go
More file actions
178 lines (161 loc) · 6.1 KB
/
batch.go
File metadata and controls
178 lines (161 loc) · 6.1 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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package together
import (
"context"
"errors"
"fmt"
"net/http"
"slices"
"time"
"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"
)
// BatchService 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 [NewBatchService] method instead.
type BatchService struct {
Options []option.RequestOption
}
// NewBatchService 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 NewBatchService(opts ...option.RequestOption) (r BatchService) {
r = BatchService{}
r.Options = opts
return
}
// Create a new batch job with the given input file and endpoint
func (r *BatchService) New(ctx context.Context, body BatchNewParams, opts ...option.RequestOption) (res *BatchNewResponse, err error) {
opts = slices.Concat(r.Options, opts)
path := "batches"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// Get details of a batch job by ID
func (r *BatchService) Get(ctx context.Context, id string, opts ...option.RequestOption) (res *BatchJob, err error) {
opts = slices.Concat(r.Options, opts)
if id == "" {
err = errors.New("missing required id parameter")
return nil, err
}
path := fmt.Sprintf("batches/%s", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// List all batch jobs for the authenticated user
func (r *BatchService) List(ctx context.Context, opts ...option.RequestOption) (res *[]BatchJob, err error) {
opts = slices.Concat(r.Options, opts)
path := "batches"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// Cancel a batch job by ID
func (r *BatchService) Cancel(ctx context.Context, id string, opts ...option.RequestOption) (res *BatchJob, err error) {
opts = slices.Concat(r.Options, opts)
if id == "" {
err = errors.New("missing required id parameter")
return nil, err
}
path := fmt.Sprintf("batches/%s/cancel", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return res, err
}
type BatchJob struct {
ID string `json:"id" format:"uuid"`
CompletedAt time.Time `json:"completed_at" format:"date-time"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
Endpoint string `json:"endpoint"`
Error string `json:"error"`
ErrorFileID string `json:"error_file_id"`
// Size of input file in bytes
FileSizeBytes int64 `json:"file_size_bytes"`
InputFileID string `json:"input_file_id"`
JobDeadline time.Time `json:"job_deadline" format:"date-time"`
// Model used for processing requests
ModelID string `json:"model_id"`
OutputFileID string `json:"output_file_id"`
// Completion progress (0.0 to 100)
Progress float64 `json:"progress"`
// Current status of the batch job
//
// Any of "VALIDATING", "IN_PROGRESS", "COMPLETED", "FAILED", "EXPIRED",
// "CANCELLED".
Status BatchJobStatus `json:"status"`
UserID string `json:"user_id"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
CompletedAt respjson.Field
CreatedAt respjson.Field
Endpoint respjson.Field
Error respjson.Field
ErrorFileID respjson.Field
FileSizeBytes respjson.Field
InputFileID respjson.Field
JobDeadline respjson.Field
ModelID respjson.Field
OutputFileID respjson.Field
Progress respjson.Field
Status respjson.Field
UserID respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r BatchJob) RawJSON() string { return r.JSON.raw }
func (r *BatchJob) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Current status of the batch job
type BatchJobStatus string
const (
BatchJobStatusValidating BatchJobStatus = "VALIDATING"
BatchJobStatusInProgress BatchJobStatus = "IN_PROGRESS"
BatchJobStatusCompleted BatchJobStatus = "COMPLETED"
BatchJobStatusFailed BatchJobStatus = "FAILED"
BatchJobStatusExpired BatchJobStatus = "EXPIRED"
BatchJobStatusCancelled BatchJobStatus = "CANCELLED"
)
type BatchNewResponse struct {
Job BatchJob `json:"job"`
Warning string `json:"warning"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Job respjson.Field
Warning respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r BatchNewResponse) RawJSON() string { return r.JSON.raw }
func (r *BatchNewResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type BatchNewParams struct {
// The endpoint to use for batch processing
Endpoint string `json:"endpoint" api:"required"`
// ID of the uploaded input file containing batch requests
InputFileID string `json:"input_file_id" api:"required"`
// Time window for batch completion (optional)
CompletionWindow param.Opt[string] `json:"completion_window,omitzero"`
// Model to use for processing batch requests
ModelID param.Opt[string] `json:"model_id,omitzero"`
// Priority for batch processing (optional)
Priority param.Opt[int64] `json:"priority,omitzero"`
paramObj
}
func (r BatchNewParams) MarshalJSON() (data []byte, err error) {
type shadow BatchNewParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *BatchNewParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}