Module: RubyLLM::Protocols::OpenAI::Batches

Includes:
Batch::Helpers
Included in:
ChatCompletions::Batches, ChatCompletions::EmbeddingBatches, Responses::Batches
Defined in:
lib/ruby_llm/protocols/openai/batches.rb

Overview

The OpenAI file-backed Batch API: upload JSONL, create /batches, then read output/error files back in custom_id order.

Instance Method Summary collapse

Instance Method Details

#batch_results(id) ⇒ Object

Results are JSONL and may arrive out of order. Each line carries the custom_id we assigned from the submission index.



39
40
41
42
43
44
45
46
47
# File 'lib/ruby_llm/protocols/openai/batches.rb', line 39

def batch_results(id)
  batch = @connection.get(batch_url(id)).body
  %w[output_file_id error_file_id].flat_map do |key|
    file_id = batch[key]
    next [] unless file_id && !file_id.to_s.empty?

    download_batch_file(file_id).to_s.each_line.filter_map { |line| parse_batch_result(JSON.parse(line)) }
  end
end

#cancel_batch(id) ⇒ Object



33
34
35
# File 'lib/ruby_llm/protocols/openai/batches.rb', line 33

def cancel_batch(id)
  parse_batch_response @connection.post("#{batch_url(id)}/cancel", {}).body
end

#create_batch(requests) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_llm/protocols/openai/batches.rb', line 16

def create_batch(requests)
  single_batch_model!(requests, @provider.slug)
  validate_batch_requests!(requests)

  response = @connection.post(batch_create_url, {
                                input_file_id: upload_batch_file(requests),
                                endpoint: batch_endpoint,
                                completion_window: '24h'
                              }, idempotent: false)

  parse_batch_response(response.body)
end

#find_batch(id) ⇒ Object



29
30
31
# File 'lib/ruby_llm/protocols/openai/batches.rb', line 29

def find_batch(id)
  parse_batch_response @connection.get(batch_url(id)).body
end