You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Batches endpoint allows you to create and manage large batches of API requests to run asynchronously. Currently, only the `/v1/chat/completions` endpoint is supported for batches.
492
+
493
+
To use the Batches endpoint, you need to first upload a JSONL file containing the batch requests using the Files endpoint. The file must be uploaded with the purpose set to `batch`. Each line in the JSONL file represents a single request and should have the following format:
494
+
495
+
```json
496
+
{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-3.5-turbo", "messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is 2+2?"}]}}
497
+
```
498
+
499
+
Once you have uploaded the JSONL file, you can create a new batch by providing the file ID, endpoint, and completion window:
500
+
501
+
```ruby
502
+
response = client.batches.create(
503
+
parameters: {
504
+
input_file_id:"file-abc123",
505
+
endpoint:"/v1/chat/completions",
506
+
completion_window:"24h"
507
+
}
508
+
)
509
+
batch_id = response["id"]
510
+
```
511
+
512
+
You can retrieve information about a specific batch using its ID:
513
+
514
+
```ruby
515
+
batch = client.batches.retrieve(id: batch_id)
516
+
```
517
+
518
+
To cancel a batch that is in progress:
519
+
520
+
```ruby
521
+
client.batches.cancel(id: batch_id)
522
+
```
523
+
524
+
You can also list all the batches:
525
+
526
+
```ruby
527
+
client.batches.list
528
+
```
529
+
530
+
The output and error files for a batch can be accessed using the `output_file_id` and `error_file_id` fields in the batch object, respectively. These files are in JSONL format, with each line representing the output or error for a single request. The output object has the following format:
531
+
532
+
```json
533
+
{
534
+
"id": "response-1",
535
+
"custom_id": "request-1",
536
+
"response": {
537
+
"id": "chatcmpl-abc123",
538
+
"object": "chat.completion",
539
+
"created": 1677858242,
540
+
"model": "gpt-3.5-turbo-0301",
541
+
"choices": [
542
+
{
543
+
"index": 0,
544
+
"message": {
545
+
"role": "assistant",
546
+
"content": "2+2 equals 4."
547
+
}
548
+
}
549
+
]
550
+
}
551
+
}
552
+
```
553
+
554
+
If a request fails with a non-HTTP error, the error object will contain more information about the cause of the failure.
0 commit comments