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:
parent
9505a8976c
commit
99282612a9
|
@ -95,7 +95,16 @@ class LlmModel < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def seeded?
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue