mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-02-16 08:24:45 +00:00
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
|
||||
|
||||
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
|
||||
|
13
spec/models/llm_model_spec.rb
Normal file
13
spec/models/llm_model_spec.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user