Skip to content

Commit a8e2951

Browse files
feat(api): Add batches.cancel API
1 parent 1558564 commit a8e2951

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

.stats.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
configured_endpoints: 45
1+
configured_endpoints: 46
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/togetherai%2Ftogetherai-1afddc630f2b0684aad99bda9d83dc91ee6648a2b5cd7eac5d42fdc9ff46bbfc.yml
33
openapi_spec_hash: a4cab3a8559f632b66ea7aabd40cd8aa
4-
config_hash: 5e9563faf41fd9a91bea97783683bdb2
4+
config_hash: eb8d7493024f64839cec7401a9451c78

api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,14 @@ Response Types:
270270
- <a href="https://pkg.go.dev/github.com/togethercomputer/together-go">together</a>.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchNewResponse">BatchNewResponse</a>
271271
- <a href="https://pkg.go.dev/github.com/togethercomputer/together-go">together</a>.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchGetResponse">BatchGetResponse</a>
272272
- <a href="https://pkg.go.dev/github.com/togethercomputer/together-go">together</a>.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchListResponse">BatchListResponse</a>
273+
- <a href="https://pkg.go.dev/github.com/togethercomputer/together-go">together</a>.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchCancelResponse">BatchCancelResponse</a>
273274

274275
Methods:
275276

276277
- <code title="post /batches">client.Batches.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchService.New">New</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, body <a href="https://pkg.go.dev/github.com/togethercomputer/together-go">together</a>.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchNewParams">BatchNewParams</a>) (<a href="https://pkg.go.dev/github.com/togethercomputer/together-go">together</a>.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchNewResponse">BatchNewResponse</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
277278
- <code title="get /batches/{id}">client.Batches.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchService.Get">Get</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, id <a href="https://pkg.go.dev/builtin#string">string</a>) (<a href="https://pkg.go.dev/github.com/togethercomputer/together-go">together</a>.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchGetResponse">BatchGetResponse</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
278279
- <code title="get /batches">client.Batches.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchService.List">List</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>) ([]<a href="https://pkg.go.dev/github.com/togethercomputer/together-go">together</a>.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchListResponse">BatchListResponse</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
280+
- <code title="post /batches/{id}/cancel">client.Batches.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchService.Cancel">Cancel</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, id <a href="https://pkg.go.dev/builtin#string">string</a>) (<a href="https://pkg.go.dev/github.com/togethercomputer/together-go">together</a>.<a href="https://pkg.go.dev/github.com/togethercomputer/together-go#BatchCancelResponse">BatchCancelResponse</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
279281

280282
# Evals
281283

batch.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ func (r *BatchService) List(ctx context.Context, opts ...option.RequestOption) (
6464
return
6565
}
6666

67+
// Cancel a batch job by ID
68+
func (r *BatchService) Cancel(ctx context.Context, id string, opts ...option.RequestOption) (res *BatchCancelResponse, err error) {
69+
opts = slices.Concat(r.Options, opts)
70+
if id == "" {
71+
err = errors.New("missing required id parameter")
72+
return
73+
}
74+
path := fmt.Sprintf("batches/%s/cancel", id)
75+
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
76+
return
77+
}
78+
6779
type BatchNewResponse struct {
6880
Job BatchNewResponseJob `json:"job"`
6981
Warning string `json:"warning"`
@@ -253,6 +265,67 @@ const (
253265
BatchListResponseStatusCancelled BatchListResponseStatus = "CANCELLED"
254266
)
255267

268+
type BatchCancelResponse struct {
269+
ID string `json:"id" format:"uuid"`
270+
CompletedAt time.Time `json:"completed_at" format:"date-time"`
271+
CreatedAt time.Time `json:"created_at" format:"date-time"`
272+
Endpoint string `json:"endpoint"`
273+
Error string `json:"error"`
274+
ErrorFileID string `json:"error_file_id"`
275+
// Size of input file in bytes
276+
FileSizeBytes int64 `json:"file_size_bytes"`
277+
InputFileID string `json:"input_file_id"`
278+
JobDeadline time.Time `json:"job_deadline" format:"date-time"`
279+
// Model used for processing requests
280+
ModelID string `json:"model_id"`
281+
OutputFileID string `json:"output_file_id"`
282+
// Completion progress (0.0 to 100)
283+
Progress float64 `json:"progress"`
284+
// Current status of the batch job
285+
//
286+
// Any of "VALIDATING", "IN_PROGRESS", "COMPLETED", "FAILED", "EXPIRED",
287+
// "CANCELLED".
288+
Status BatchCancelResponseStatus `json:"status"`
289+
UserID string `json:"user_id"`
290+
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
291+
JSON struct {
292+
ID respjson.Field
293+
CompletedAt respjson.Field
294+
CreatedAt respjson.Field
295+
Endpoint respjson.Field
296+
Error respjson.Field
297+
ErrorFileID respjson.Field
298+
FileSizeBytes respjson.Field
299+
InputFileID respjson.Field
300+
JobDeadline respjson.Field
301+
ModelID respjson.Field
302+
OutputFileID respjson.Field
303+
Progress respjson.Field
304+
Status respjson.Field
305+
UserID respjson.Field
306+
ExtraFields map[string]respjson.Field
307+
raw string
308+
} `json:"-"`
309+
}
310+
311+
// Returns the unmodified JSON received from the API
312+
func (r BatchCancelResponse) RawJSON() string { return r.JSON.raw }
313+
func (r *BatchCancelResponse) UnmarshalJSON(data []byte) error {
314+
return apijson.UnmarshalRoot(data, r)
315+
}
316+
317+
// Current status of the batch job
318+
type BatchCancelResponseStatus string
319+
320+
const (
321+
BatchCancelResponseStatusValidating BatchCancelResponseStatus = "VALIDATING"
322+
BatchCancelResponseStatusInProgress BatchCancelResponseStatus = "IN_PROGRESS"
323+
BatchCancelResponseStatusCompleted BatchCancelResponseStatus = "COMPLETED"
324+
BatchCancelResponseStatusFailed BatchCancelResponseStatus = "FAILED"
325+
BatchCancelResponseStatusExpired BatchCancelResponseStatus = "EXPIRED"
326+
BatchCancelResponseStatusCancelled BatchCancelResponseStatus = "CANCELLED"
327+
)
328+
256329
type BatchNewParams struct {
257330
// The endpoint to use for batch processing
258331
Endpoint string `json:"endpoint,required"`

batch_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,25 @@ func TestBatchList(t *testing.T) {
8484
t.Fatalf("err should be nil: %s", err.Error())
8585
}
8686
}
87+
88+
func TestBatchCancel(t *testing.T) {
89+
baseURL := "http://localhost:4010"
90+
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
91+
baseURL = envURL
92+
}
93+
if !testutil.CheckTestServer(t, baseURL) {
94+
return
95+
}
96+
client := together.NewClient(
97+
option.WithBaseURL(baseURL),
98+
option.WithAPIKey("My API Key"),
99+
)
100+
_, err := client.Batches.Cancel(context.TODO(), "batch_job_abc123def456")
101+
if err != nil {
102+
var apierr *together.Error
103+
if errors.As(err, &apierr) {
104+
t.Log(string(apierr.DumpRequest(true)))
105+
}
106+
t.Fatalf("err should be nil: %s", err.Error())
107+
}
108+
}

0 commit comments

Comments
 (0)