Class: CompletionKit::ProviderCredential

Inherits:
ApplicationRecord show all
Includes:
Turbo::Broadcastable
Defined in:
app/models/completion_kit/provider_credential.rb

Constant Summary collapse

PROVIDERS =
%w[openai anthropic ollama openrouter].freeze
PROVIDER_LABELS =
{
  "openai" => "OpenAI",
  "anthropic" => "Anthropic",
  "ollama" => "Ollama / local endpoint",
  "openrouter" => "OpenRouter"
}.freeze

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



14
15
16
17
18
19
# File 'app/models/completion_kit/provider_credential.rb', line 14

def as_json(options = {})
  {
    id: id, provider: provider, api_endpoint: api_endpoint,
    created_at: created_at, updated_at: updated_at
  }
end

#available_modelsObject



37
38
39
40
41
# File 'app/models/completion_kit/provider_credential.rb', line 37

def available_models
  LlmClient.for_provider(provider, config_hash).available_models
rescue StandardError
  []
end

#broadcast_discovery_completeObject



79
80
81
82
# File 'app/models/completion_kit/provider_credential.rb', line 79

def broadcast_discovery_complete
  broadcast_discovery_progress
  broadcast_model_dropdowns
end

#broadcast_discovery_progressObject



71
72
73
74
75
76
77
# File 'app/models/completion_kit/provider_credential.rb', line 71

def broadcast_discovery_progress
  broadcast_replace_to(
    "completion_kit_provider_#{id}",
    target: "discovery_status_#{id}",
    html: render_partial("completion_kit/provider_credentials/discovery_status", provider_credential: self)
  )
end

#config_hashObject



29
30
31
32
33
34
35
# File 'app/models/completion_kit/provider_credential.rb', line 29

def config_hash
  {
    provider: provider,
    api_key: api_key,
    api_endpoint: api_endpoint
  }.compact
end

#configured?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'app/models/completion_kit/provider_credential.rb', line 43

def configured?
  LlmClient.for_provider(provider, config_hash).configured?
rescue StandardError
  false
end

#display_providerObject



21
22
23
# File 'app/models/completion_kit/provider_credential.rb', line 21

def display_provider
  PROVIDER_LABELS[provider] || provider.titleize
end

#judge_countObject



55
56
57
58
59
# File 'app/models/completion_kit/provider_credential.rb', line 55

def judge_count
  model_ids = Model.where(provider: provider).pluck(:model_id)
  return 0 if model_ids.empty?
  Run.where(judge_model: model_ids).count
end

#last_used_atObject



61
62
63
64
65
66
67
68
69
# File 'app/models/completion_kit/provider_credential.rb', line 61

def last_used_at
  model_ids = Model.where(provider: provider).pluck(:model_id)
  return nil if model_ids.empty?
  prompt_scope = Prompt.where(llm_model: model_ids).select(:id)
  Run.where("prompt_id IN (:prompts) OR judge_model IN (:models)",
            prompts: prompt_scope, models: model_ids)
     .where.not(status: "pending")
     .maximum(:created_at)
end

#prompt_countObject



49
50
51
52
53
# File 'app/models/completion_kit/provider_credential.rb', line 49

def prompt_count
  model_ids = Model.where(provider: provider).pluck(:model_id)
  return 0 if model_ids.empty?
  Prompt.where(llm_model: model_ids, current: true).count
end