Class: RubyLLM::ActiveRecord::Batch

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/ruby_llm/active_record/batch.rb

Overview

RubyLLM's private persistence for provider-side batches.

Constant Summary collapse

STATUSES =
%w[pending succeeded failed cancelled].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fetch(id, provider: nil, context: nil) ⇒ Object



34
35
36
# File 'lib/ruby_llm/active_record/batch.rb', line 34

def fetch(id, provider: nil, context: nil)
  find_record(id, provider:)&.to_llm(context:, store: self)
end

.persist(batch, chats) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_llm/active_record/batch.rb', line 15

def persist(batch, chats)
  records = Array(chats)
  return unless table_exists?
  return unless persistable_chats?(records)

  create!(
    provider_batch_id: batch.id,
    provider: batch.provider,
    status: batch.status,
    raw_status: batch.raw_status,
    completed: batch.complete?,
    request_counts: batch.request_counts,
    reported_cost: batch.reported_cost&.to_h,
    batch_protocol: batch.batch_protocol,
    chat_type: records.first.class.polymorphic_name,
    chat_ids: records.map(&:id)
  )
end

.sync(batch) ⇒ Object



38
39
40
# File 'lib/ruby_llm/active_record/batch.rb', line 38

def sync(batch)
  find_record(batch.id, provider: batch.provider)&.sync_from(batch)
end

Instance Method Details

#chatsObject



61
62
63
64
65
66
# File 'lib/ruby_llm/active_record/batch.rb', line 61

def chats
  klass = chat_type.constantize
  by_id = klass.where(klass.primary_key => Array(chat_ids))
               .index_by { |chat| chat.public_send(klass.primary_key) }
  Array(chat_ids).map { |id| by_id[id] }
end

#sync_from(batch) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_llm/active_record/batch.rb', line 84

def sync_from(batch)
  attributes = {
    status: batch.status,
    raw_status: batch.raw_status,
    completed: batch.complete?,
    request_counts: batch.request_counts,
    batch_protocol: batch.batch_protocol
  }
  attributes[:reported_cost] = batch.reported_cost.to_h if batch.reported_cost
  update!(attributes)
end

#to_llm(context: nil, store: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby_llm/active_record/batch.rb', line 68

def to_llm(context: nil, store: nil)
  config = context&.config || RubyLLM.config
  provider_instance = RubyLLM::Provider.resolve!(provider).new(config)
  RubyLLM::Batch.new(
    provider: provider_instance,
    chats: chats.map { |chat| chat&.to_llm },
    id: provider_batch_id,
    raw_status: raw_status,
    completed: completed,
    request_counts: request_counts,
    reported_cost: reported_cost && RubyLLM::Cost.from_h(reported_cost),
    batch_protocol: batch_protocol,
    store: store
  )
end