Skip to content

Commit 6e96a7a

Browse files
committed
Add example to README
1 parent b6464fc commit 6e96a7a

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,24 @@ puts response.dig("data", 0, "embedding")
488488
```
489489

490490
### Batches
491+
491492
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.
492493

493494
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:
494495

495496
```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+
"custom_id": "request-1",
499+
"method": "POST",
500+
"url": "/v1/chat/completions",
501+
"body": {
502+
"model": "gpt-3.5-turbo",
503+
"messages": [
504+
{ "role": "system", "content": "You are a helpful assistant." },
505+
{ "role": "user", "content": "What is 2+2?" }
506+
]
507+
}
508+
}
497509
```
498510

499511
Once you have uploaded the JSONL file, you can create a new batch by providing the file ID, endpoint, and completion window:
@@ -516,18 +528,28 @@ batch = client.batches.retrieve(id: batch_id)
516528
```
517529

518530
To cancel a batch that is in progress:
519-
531+
520532
```ruby
521533
client.batches.cancel(id: batch_id)
522534
```
523535

524536
You can also list all the batches:
525-
537+
526538
```ruby
527539
client.batches.list
528540
```
529541

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:
542+
Once the batch["completed_at"] is present, you can fetch the output or error files:
543+
544+
```ruby
545+
batch = client.batches.retrieve(id: batch_id)
546+
output_file_id = batch["output_file_id"]
547+
output_response = client.files.content(id: output_file_id)
548+
error_file_id = batch["error_file_id"]
549+
error_response = client.files.content(id: error_file_id)
550+
```
551+
552+
These files are in JSONL format, with each line representing the output or error for a single request. The lines can be in any order:
531553

532554
```json
533555
{

0 commit comments

Comments
 (0)