Module: RubyLLM::Protocols::Gemini::Batches

Includes:
Batch::Helpers, EmbeddingBatches
Defined in:
lib/ruby_llm/protocols/gemini/batches.rb

Overview

Gemini Batch API with inlined generateContent requests.

Instance Method Summary collapse

Instance Method Details

#batch_results(id) ⇒ Object

Inline answers are correlated by the key we sent (the submission index); Gemini also returns them in order, so we fall back to position.



50
51
52
53
54
55
56
# File 'lib/ruby_llm/protocols/gemini/batches.rb', line 50

def batch_results(id)
  body = @connection.get(batch_name(id)).body
  inlined = inline_batch_responses(body)
  return parse_embedding_batch_results(inlined) if embedding_batch_response?(body)

  inlined.each_with_index.map { |response, index| parse_inline_response(response, index) }
end

#cancel_batch(id) ⇒ Object



43
44
45
46
# File 'lib/ruby_llm/protocols/gemini/batches.rb', line 43

def cancel_batch(id)
  @connection.post("#{batch_name(id)}:cancel", {})
  find_batch(id)
end

#create_batch(requests) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_llm/protocols/gemini/batches.rb', line 16

def create_batch(requests)
  model = single_batch_model!(requests, 'gemini')
  action = embedding_batch?(requests) ? 'asyncBatchEmbedContent' : 'batchGenerateContent'
  response = @connection.post("models/#{model}:#{action}", {
                                batch: {
                                  displayName: "ruby_llm_#{SecureRandom.hex(8)}",
                                  inputConfig: {
                                    requests: {
                                      requests: requests.flat_map do |request|
                                        if embedding_batch_payload?(request.fetch(:payload))
                                          embedding_batch_requests(request, model)
                                        else
                                          [gemini_batch_request(request, model)]
                                        end
                                      end
                                    }
                                  }
                                }
                              }, idempotent: false)

  parse_batch_response(response.body)
end

#find_batch(id) ⇒ Object



39
40
41
# File 'lib/ruby_llm/protocols/gemini/batches.rb', line 39

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