A platform for community discussion. Free, open, simple.
# frozen_string_literal: true
module DiscourseAi
module Admin
class AiLlmsController < ::Admin::AdminController
requires_plugin PLUGIN_NAME
def index
llms = LlmModel.all.includes(:llm_quotas, :vision_llm_model).order(:display_name)
render json: {
ai_llms:
ActiveModel::ArraySerializer.new(
llms,
each_serializer: LlmModelSerializer,
root: false,
scope: {
llm_usage: DiscourseAi::Configuration::LlmEnumerator.global_usage,
},
).as_json,
meta: {
provider_params: LlmModel.provider_params,
provider_capabilities: DiscourseAi::Completions::Llm.provider_capabilities,
presets: DiscourseAi::Completions::Llm.presets,
providers: DiscourseAi::Completions::Llm.provider_names,
tokenizers:
DiscourseAi::Completions::Llm.tokenizer_names.map { |tn|
{ id: tn, name: tn.split("::").last }
},
ai_secrets:
ActiveModel::ArraySerializer.new(
AiSecret.all.order(:name),
each_serializer: AiSecretSerializer,
root: false,
).as_json,
},
}
end
def new
end
def edit
llm_model = LlmModel.find(params[:id])
render json: LlmModelSerializer.new(llm_model)
end
def create
llm_model = LlmModel.new(ai_llm_params)
# we could do nested attributes but the mechanics are not ideal leading
# to lots of complex debugging, this is simpler
quota_params.each { |quota| llm_model.llm_quotas.build(quota) } if quota_params
if llm_model.save
log_llm_model_creation(llm_model)
render json: LlmModelSerializer.new(llm_model), status: :created
else
render_json_error llm_model
end
end
def update
llm_model = LlmModel.find(params[:id])
# Capture initial state for logging
initial_attributes = llm_model.attributes.dup
initial_quotas = llm_model.llm_quotas.map(&:attributes)
if params[:ai_llm].key?(:llm_quotas)
ActiveRecord::Base.transaction do
if quota_params
existing_quota_group_ids = llm_model.llm_quotas.pluck(:group_id)
new_quota_group_ids = quota_params.map { |q| q[:group_id] }
llm_model
.llm_quotas
.where(group_id: existing_quota_group_ids - new_quota_group_ids)
.destroy_all
quota_params.each do |quota_param|
quota = llm_model.llm_quotas.find_or_initialize_by(group_id: quota_param[:group_id])
quota.update!(quota_param)
end
else
llm_model.llm_quotas.destroy_all
end
end
end
handle_credit_allocation_update(llm_model)
handle_feature_credit_costs_update(llm_model)
if llm_model.seeded?
# For seeded models, only allow quota/credit updates (already handled above)
# Return success with updated model, don't allow other param changes
log_llm_model_update(llm_model, initial_attributes, initial_quotas)
return render json: LlmModelSerializer.new(llm_model)
end
if llm_model.update(ai_llm_params(updating: llm_model))
log_llm_model_update(llm_model, initial_attributes, initial_quotas)
render json: LlmModelSerializer.new(llm_model)
else
render_json_error llm_model
end
rescue ActiveRecord::RecordInvalid => e
render_json_error e.record
end
def destroy
llm_model = LlmModel.find(params[:id])
if llm_model.seeded?
return render_json_error(I18n.t("discourse_ai.llm.cannot_delete_builtin"), status: 403)
end
in_use_by = DiscourseAi::Configuration::LlmValidator.new.is_using(llm_model)
if !in_use_by.empty?
return(
render_json_error(
I18n.t(
"discourse_ai.llm.delete_failed",
settings: in_use_by.join(", "),
count: in_use_by.length,
),
status: 409,
)
)
end
# Capture model details for logging before destruction
model_details = {
model_id: llm_model.id,
display_name: llm_model.display_name,
name: llm_model.name,
provider: llm_model.provider,
}
if llm_model.destroy
log_llm_model_deletion(model_details)
head :no_content
else
render_json_error llm_model
end
end
def test
RateLimiter.new(current_user, "llm_test_#{current_user.id}", 6, 1.minute).performed!
validator = DiscourseAi::Configuration::LlmValidator.new
# For seeded models, test the existing model directly since provider/url/api_key are hidden
if params.dig(:ai_llm, :id).present?
existing_model = LlmModel.find_by(id: params[:ai_llm][:id])
if existing_model&.seeded?
validator.run_test(existing_model)
return render json: { success: true }
end
end
# We don't care about the display_name attr for testing.
llm_model = LlmModel.new(ai_llm_params.merge(display_name: "LLM test"))
if llm_model.valid?
validator.run_test(llm_model)
render json: { success: true }
else
render json: { success: false, validation_errors: llm_model.errors.full_messages }
end
rescue DiscourseAi::Completions::Endpoints::Base::CompletionFailed => e
render json: { success: false, error: e.message, failed_mode: validator&.last_failed_mode }
end
private
def quota_params
if params[:ai_llm][:llm_quotas].present?
params[:ai_llm][:llm_quotas].map do |quota|
mapped = {}
mapped[:group_id] = quota[:group_id].to_i
%i[max_tokens max_usages].each do |key|
mapped[key] = optional_integer_param(quota[key]) if quota.key?(key)
end
mapped[:max_cost] = quota[:max_cost].presence if quota.key?(:max_cost)
mapped[:duration_seconds] = quota[:duration_seconds].to_i
mapped
end
end
end
def optional_integer_param(value)
value.presence&.to_i
end
def credit_allocation_params
return nil if params[:ai_llm][:llm_credit_allocation].blank?
allocation = params[:ai_llm][:llm_credit_allocation]
{
daily_credits: allocation[:daily_credits].to_i,
soft_limit_percentage: allocation[:soft_limit_percentage].to_i,
}
end
def feature_credit_cost_params
return nil if params[:ai_llm][:llm_feature_credit_costs].blank?
params[:ai_llm][:llm_feature_credit_costs].map do |cost|
{ feature_name: cost[:feature_name], credits_per_token: cost[:credits_per_token].to_f }
end
end
def ai_llm_params(updating: nil)
return {} if params[:ai_llm].blank?
permitted =
params.require(:ai_llm).permit(
:display_name,
:name,
:provider,
:tokenizer,
:max_prompt_tokens,
:max_output_tokens,
:api_key,
:ai_secret_id,
:vision_enabled,
:vision_llm_model_id,
:vision_mode,
:input_cost,
:cached_input_cost,
:cache_write_cost,
:output_cost,
allowed_attachment_types: [],
)
normalize_vision_params!(permitted, updating: updating)
provider = updating ? updating.provider : permitted[:provider]
permit_url = provider != LlmModel::BEDROCK_PROVIDER_NAME
new_url = params.dig(:ai_llm, :url)
permitted[:url] = new_url if permit_url && new_url
extra_field_names = LlmModel.provider_params.dig(provider&.to_sym)
if extra_field_names.present?
received_prov_params =
params.dig(:ai_llm, :provider_params)&.slice(*extra_field_names.keys)
if received_prov_params.present?
received_prov_params.each do |pname, value|
field_def = extra_field_names[pname.to_sym]
is_checkbox =
field_def == :checkbox || (field_def.is_a?(Hash) && field_def[:type] == :checkbox)
if is_checkbox
received_prov_params[pname] = ActiveModel::Type::Boolean.new.cast(value)
end
end
sanitize_dependent_params!(received_prov_params, extra_field_names)
permitted[:provider_params] = received_prov_params.permit(*extra_field_names.keys)
end
end
permitted
end
def normalize_vision_params!(permitted, updating:)
mode_supplied = permitted.key?(:vision_mode)
target_supplied = permitted.key?(:vision_llm_model_id)
legacy_supplied = permitted.key?(:vision_enabled)
mode = permitted.delete(:vision_mode)&.to_s
if mode_supplied
permitted[:requested_vision_mode] = mode || ""
case mode
when "native"
permitted[:vision_enabled] = true
permitted[:vision_llm_model_id] = nil
when "delegated"
permitted[:vision_enabled] = false
permitted[:vision_llm_model_id] = permitted[:vision_llm_model_id].presence
when "disabled"
permitted[:vision_enabled] = false
permitted[:vision_llm_model_id] = nil
end
return
end
if updating
if target_supplied
permitted[:vision_llm_model_id] = permitted[:vision_llm_model_id].presence
permitted[:vision_enabled] = false if permitted[:vision_llm_model_id].present?
elsif legacy_supplied && updating.delegated_vision_configured?
permitted.delete(:vision_enabled)
elsif legacy_supplied
permitted[:vision_enabled] = ActiveModel::Type::Boolean.new.cast(
permitted[:vision_enabled],
)
permitted[:vision_llm_model_id] = nil
end
elsif target_supplied && permitted[:vision_llm_model_id].present? && !legacy_supplied
permitted[:vision_enabled] = false
else
permitted[:vision_enabled] = ActiveModel::Type::Boolean.new.cast(
permitted[:vision_enabled],
) || false
permitted[:vision_llm_model_id] = nil
end
end
def sanitize_dependent_params!(prov_params, field_definitions)
prov_params.each do |pname, _value|
field_def = field_definitions[pname.to_sym]
next unless field_def.is_a?(Hash) && field_def[:depends_on]
deps = Array(field_def[:depends_on])
parent_inactive =
deps.any? do |dep|
parent_val = prov_params[dep.to_s]
parent_val.nil? || parent_val == false || parent_val == "false" ||
parent_val == "default" || parent_val == ""
end
if parent_inactive
default = field_def[:default]
prov_params[pname] = default || nil
end
end
end
def ai_llm_logger_fields
{
display_name: {
},
name: {
},
provider: {
},
tokenizer: {
},
url: {
},
max_prompt_tokens: {
},
max_output_tokens: {
},
vision_enabled: {
},
vision_llm_model_id: {
},
api_key: {
type: :sensitive,
},
input_cost: {
},
output_cost: {
},
# JSON fields should be tracked as simple changes
json_fields: [:provider_params],
}
end
def log_llm_model_creation(llm_model)
logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
entity_details = { model_id: llm_model.id, subject: llm_model.display_name }
# Add quota information as a special case
if llm_model.llm_quotas.any?
entity_details[:quotas] = llm_model
.llm_quotas
.map do |quota|
quota_summary(
quota.group_id,
quota.max_tokens,
quota.max_usages,
quota.max_cost,
quota.duration_seconds,
)
end
.join("; ")
end
logger.log_creation("llm_model", llm_model, ai_llm_logger_fields, entity_details)
end
def log_llm_model_update(llm_model, initial_attributes, initial_quotas)
logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
entity_details = { model_id: llm_model.id, subject: llm_model.display_name }
# Track quota changes separately as they're a special case
current_quotas = llm_model.llm_quotas.reload.map(&:attributes)
if initial_quotas != current_quotas
initial_quota_summary =
initial_quotas
.map do |q|
quota_summary(
q["group_id"],
q["max_tokens"],
q["max_usages"],
q["max_cost"],
q["duration_seconds"],
)
end
.join("; ")
current_quota_summary =
current_quotas
.map do |q|
quota_summary(
q["group_id"],
q["max_tokens"],
q["max_usages"],
q["max_cost"],
q["duration_seconds"],
)
end
.join("; ")
entity_details[:quotas_changed] = true
entity_details[:quotas] = "#{initial_quota_summary} → #{current_quota_summary}"
end
logger.log_update(
"llm_model",
llm_model,
initial_attributes,
ai_llm_logger_fields,
entity_details,
)
end
def quota_summary(group_id, max_tokens, max_usages, max_cost, duration_seconds)
limits = []
limits << "#{max_tokens} tokens" if max_tokens.present?
limits << "#{max_usages} usages" if max_usages.present?
limits << "$#{formatted_decimal(max_cost)} cost" if max_cost.present?
limits << "#{duration_seconds}s" if duration_seconds.present?
"Group #{group_id}: #{limits.join(", ")}"
end
def formatted_decimal(value)
value.is_a?(BigDecimal) ? value.to_s("F") : value
end
def log_llm_model_deletion(model_details)
logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
model_details[:subject] = model_details[:display_name]
logger.log_deletion("llm_model", model_details)
end
def handle_credit_allocation_update(llm_model)
return unless llm_model.seeded? && params[:ai_llm].key?(:llm_credit_allocation)
if credit_allocation_params
allocation = llm_model.llm_credit_allocation || llm_model.build_llm_credit_allocation
allocation.update!(credit_allocation_params)
elsif llm_model.llm_credit_allocation
llm_model.llm_credit_allocation.destroy
end
end
def handle_feature_credit_costs_update(llm_model)
return unless llm_model.seeded? && params[:ai_llm].key?(:llm_feature_credit_costs)
if feature_credit_cost_params
existing_costs = llm_model.llm_feature_credit_costs.index_by(&:feature_name)
new_features = feature_credit_cost_params.map { |c| c[:feature_name] }
llm_model
.llm_feature_credit_costs
.where(feature_name: existing_costs.keys - new_features)
.destroy_all
feature_credit_cost_params.each do |cost_param|
cost =
existing_costs[cost_param[:feature_name]] ||
llm_model.llm_feature_credit_costs.build(feature_name: cost_param[:feature_name])
cost.update!(cost_param)
end
else
llm_model.llm_feature_credit_costs.destroy_all
end
end
end
end
end