DEV: Prefer ENV key for seeded models (#893)

This PR ensures we prefer getting the API key from environment variables when it is a seeded model.
This commit is contained in:
Keegan George 2024-11-05 23:19:13 +09:00 committed by GitHub
parent 9505a8976c
commit 99282612a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 1 deletions

View File

@ -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

View File

@ -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