module RubyLLM::ActiveRecord::BatchMethods
Methods added to a batch model by acts_as_batch. A persisted batch mirrors RubyLLM::Batch: it stores the providerโs batch id and the chats it is processing, so a later process can poll it and the answers land back in those conversations.
Creating the record submits the staged chats to the provider in the same step:
chats = tickets.map do |ticket| Chat.create!(model: "claude-haiku-4-5").ask_later(ticket.body) end batch = Batch.create!(chats: chats) BatchPollJob.perform_later(batch.id)
Public Instance Methods
Source
# File lib/ruby_llm/active_record/batch_methods.rb, line 97 def cancel to_llm.cancel cache_status self end
Cancels the batch at the provider and caches the final status onto the record. Requests already processed still return results. Returns self.
Source
# File lib/ruby_llm/active_record/batch_methods.rb, line 41 def chats by_id = batch_chat_class.constantize.where(id: chat_ids).index_by(&:id) Array(chat_ids).map { |id| by_id[id] } end
Returns the chats this batch is processing, loaded from chat_ids in submission order. Holds nil where a chat has since been deleted, so answers still line up with their chats by index.
Source
# File lib/ruby_llm/active_record/batch_methods.rb, line 33 def chats=(chats) @chats = Array(chats) self.chat_ids = @chats.map(&:id) end
Sets the staged chats this batch will submit and records their ids in chat_ids. Assign at creation time, as in Batch.create!(chats: chats); creating the record sends the chats to the provider.
Source
# File lib/ruby_llm/active_record/batch_methods.rb, line 78 def complete? completed end
Returns whether the batch has finished processing, as of the last refresh. Reads the completed column without contacting the provider.
return unless batch.refresh.complete? batch.messages
Source
# File lib/ruby_llm/active_record/batch_methods.rb, line 90 def messages to_llm.messages end
Returns the answers in submission order, nil where a request failed, each also appended to its chat and persisted. Idempotent: re-running after a retry never appends an answer twice.
batch.messages.each do |message| puts message.content end
Source
# File lib/ruby_llm/active_record/batch_methods.rb, line 65 def refresh to_llm.refresh cache_status self end
Polls the provider and caches the current status onto the record, saving when it changed, so the batches still in flight are Batch.where(completed: false). Returns self.
Batch.where(completed: false).find_each(&:refresh)
Source
# File lib/ruby_llm/active_record/batch_methods.rb, line 49 def to_llm @to_llm ||= RubyLLM::Batch.new( provider: batch_provider, chats: chats.map { |chat| chat&.to_llm }, id: provider_batch_id, status: status, completed: completed ) end
Returns the underlying RubyLLM::Batch for this record, rebuilt from the stored provider, chats, batch id, and cached status. The result is memoized.