Module: RubyLLM::Protocols::OpenRouter::Batches

Includes:
Batch::Helpers
Defined in:
lib/ruby_llm/protocols/openrouter/batches.rb

Overview

:nodoc: all

Instance Method Summary collapse

Instance Method Details

#batch_results(id) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/ruby_llm/protocols/openrouter/batches.rb', line 35

def batch_results(id)
  data = batch_data(id)
  results = Array(data['results']).map { |row| parse_batch_result(row, data:) }
  unless results.map(&:first).uniq.size == results.size
    raise Error, 'OpenRouter returned duplicate batch request IDs'
  end

  results
end

#cancel_batch(_id) ⇒ Object

Raises:



31
32
33
# File 'lib/ruby_llm/protocols/openrouter/batches.rb', line 31

def cancel_batch(_id)
  raise Error, 'OpenRouter does not expose batch cancellation'
end

#create_batch(requests) ⇒ Object

Raises:

  • (ArgumentError)


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

def create_batch(requests)
  model = single_batch_model!(requests, 'OpenRouter')
  endpoints = requests.map { |request| batch_request_endpoint(request) }.uniq
  raise ArgumentError, 'OpenRouter batches require one API protocol per submission' unless endpoints.one?

  endpoint = endpoints.first
  rows = requests.map { |request| render_batch_request(request, endpoint:) }
  response = @connection.post(@provider.batch_api_base, { endpoint:, model:, requests: rows },
                              idempotent: false)
  parse_batch_response(response.body)
end

#find_batch(id) ⇒ Object



27
28
29
# File 'lib/ruby_llm/protocols/openrouter/batches.rb', line 27

def find_batch(id)
  parse_batch_response(batch_data(id))
end