Class: RogIQ::Commands::AI

Inherits:
Base
  • Object
show all
Defined in:
lib/rogiq/commands/ai.rb

Instance Method Summary collapse

Instance Method Details

#costObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rogiq/commands/ai.rb', line 26

def cost
  RogIQ.load_rails!
  start_date = options[:days].to_i.days.ago.to_date
  scope = ::AIUsageDaily.where("usage_date >= ?", start_date)
  if options[:account].present?
    acc = RogIQ::Helpers.(options[:account])
    unless acc
      fmt.error_msg("Account not found")
      exit 1
    end
    scope = scope.where(account_id: acc.id)
  end

  rows = scope.group(:provider, :model).sum(:total_cost).sort_by { |_, v| -v.to_f }.first(50)
  fmt.output(headers: %w[provider model total_cost], rows: rows.map { |(p, m), t| [p, m, t.to_f.round(6)] })
  total = scope.sum(:total_cost)
  fmt.say("Total (period): #{total.to_f.round(6)}") unless options[:quiet]
end

#health_checkObject



54
55
56
57
58
# File 'lib/rogiq/commands/ai.rb', line 54

def health_check
  RogIQ.load_rails!
  ::AiModelHealthCheckJob.perform_later
  fmt.success("AiModelHealthCheckJob enqueued.")
end

#modelsObject



62
63
64
65
66
67
68
# File 'lib/rogiq/commands/ai.rb', line 62

def models
  RogIQ.load_rails!
  rows = ::AIModelConfig.order(:sort_order, :display_name).limit(options[:limit]).map do |m|
    [m.model_key, m.provider, m.display_name, m.enabled, m.available?]
  end
  fmt.output(headers: %w[model_key provider display_name enabled available], rows: rows)
end

#statusObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rogiq/commands/ai.rb', line 7

def status
  RogIQ.load_rails!
  payload = {
    openai_key_present: ENV["OPENAI_API_KEY"].present?,
    anthropic_key_present: ENV["ANTHROPIC_API_KEY"].present?,
    ai_engine_ready: defined?(AIEngine) && AIEngine.ready?,
    available_providers: (defined?(AIEngine) ? AIEngine.available_providers : [])
  }
  if defined?(Rails.configuration.ai_engine)
    cfg = Rails.configuration.ai_engine
    payload[:openai_enabled] = cfg.try(:openai_enabled)
    payload[:anthropic_enabled] = cfg.try(:anthropic_enabled)
  end
  fmt.output(payload)
end

#syncObject



46
47
48
49
50
# File 'lib/rogiq/commands/ai.rb', line 46

def sync
  RogIQ.load_rails!
  ::AiModelSyncJob.perform_later
  fmt.success("AiModelSyncJob enqueued.")
end