Skip to content

Commit fbaf16a

Browse files
authored
Merge pull request #454 from simonx1/add-batches-endpoint-support
Add new /batches endpoint for batch of requests run asynchronously
2 parents 494a39b + 6e96a7a commit fbaf16a

17 files changed

+1236
-0
lines changed

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Stream text with GPT-4, transcribe and translate audio with Whisper, or create i
3535
- [Functions](#functions)
3636
- [Edits](#edits)
3737
- [Embeddings](#embeddings)
38+
- [Batches](#batches)
3839
- [Files](#files)
3940
- [Finetunes](#finetunes)
4041
- [Assistants](#assistants)
@@ -486,6 +487,94 @@ puts response.dig("data", 0, "embedding")
486487
# => Vector representation of your embedding
487488
```
488489

490+
### Batches
491+
492+
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.
493+
494+
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:
495+
496+
```json
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+
}
509+
```
510+
511+
Once you have uploaded the JSONL file, you can create a new batch by providing the file ID, endpoint, and completion window:
512+
513+
```ruby
514+
response = client.batches.create(
515+
parameters: {
516+
input_file_id: "file-abc123",
517+
endpoint: "/v1/chat/completions",
518+
completion_window: "24h"
519+
}
520+
)
521+
batch_id = response["id"]
522+
```
523+
524+
You can retrieve information about a specific batch using its ID:
525+
526+
```ruby
527+
batch = client.batches.retrieve(id: batch_id)
528+
```
529+
530+
To cancel a batch that is in progress:
531+
532+
```ruby
533+
client.batches.cancel(id: batch_id)
534+
```
535+
536+
You can also list all the batches:
537+
538+
```ruby
539+
client.batches.list
540+
```
541+
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:
553+
554+
```json
555+
{
556+
"id": "response-1",
557+
"custom_id": "request-1",
558+
"response": {
559+
"id": "chatcmpl-abc123",
560+
"object": "chat.completion",
561+
"created": 1677858242,
562+
"model": "gpt-3.5-turbo-0301",
563+
"choices": [
564+
{
565+
"index": 0,
566+
"message": {
567+
"role": "assistant",
568+
"content": "2+2 equals 4."
569+
}
570+
}
571+
]
572+
}
573+
}
574+
```
575+
576+
If a request fails with a non-HTTP error, the error object will contain more information about the cause of the failure.
577+
489578
### Files
490579

491580
Put your data in a `.jsonl` file like this:

lib/openai.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require_relative "openai/run_steps"
1515
require_relative "openai/audio"
1616
require_relative "openai/version"
17+
require_relative "openai/batches"
1718

1819
module OpenAI
1920
class Error < StandardError; end

lib/openai/batches.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module OpenAI
2+
class Batches
3+
def initialize(client:)
4+
@client = client.beta(assistants: "v1")
5+
end
6+
7+
def list
8+
@client.get(path: "/batches")
9+
end
10+
11+
def retrieve(id:)
12+
@client.get(path: "/batches/#{id}")
13+
end
14+
15+
def create(parameters: {})
16+
@client.json_post(path: "/batches", parameters: parameters)
17+
end
18+
19+
def cancel(id:)
20+
@client.post(path: "/batches/#{id}/cancel")
21+
end
22+
end
23+
end

lib/openai/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def run_steps
7878
@run_steps ||= OpenAI::RunSteps.new(client: self)
7979
end
8080

81+
def batches
82+
@batches ||= OpenAI::Batches.new(client: self)
83+
end
84+
8185
def moderations(parameters: {})
8286
json_post(path: "/moderations", parameters: parameters)
8387
end

spec/fixtures/cassettes/batches_cancel.yml

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/batches_cancel_input.yml

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)