diff --git a/app/models/llm_model.rb b/app/models/llm_model.rb index d4169011..0f3626c5 100644 --- a/app/models/llm_model.rb +++ b/app/models/llm_model.rb @@ -95,7 +95,16 @@ class LlmModel < ActiveRecord::Base end def seeded? - id < 0 + id.present? && id < 0 + end + + def api_key + if seeded? + env_key = "DISCOURSE_AI_SEEDED_LLM_API_KEY_#{id.abs}" + ENV[env_key] || self[:api_key] + else + self[:api_key] + end end private diff --git a/spec/models/llm_model_spec.rb b/spec/models/llm_model_spec.rb new file mode 100644 index 00000000..d06eb1c5 --- /dev/null +++ b/spec/models/llm_model_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +RSpec.describe LlmModel do + describe "api_key" do + fab!(:llm_model) { Fabricate(:seeded_model) } + + before { ENV["DISCOURSE_AI_SEEDED_LLM_API_KEY_2"] = "blabla" } + + it "should use environment variable over database value if seeded LLM" do + expect(llm_model.api_key).to eq("blabla") + end + end +end