Skip to content

Commit

Permalink
Allows for shared, default API keys. (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbennink authored Jun 11, 2024
1 parent 07bdc44 commit 5e948f3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ class User < ApplicationRecord
def preferences
attributes["preferences"].with_defaults(dark_mode: "system")
end

def openai_key
self.attributes["openai_key"].presence || (Feature.default_llm_keys? ? Setting.default_openai_key : nil)
end

def anthropic_key
self.attributes["anthropic_key"].presence || (Feature.default_llm_keys? ? Setting.default_anthropic_key : nil)
end
end
2 changes: 2 additions & 0 deletions app/views/settings/people/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<li>Add a "Payment method."</li>
</ol>
<%= user_fields.text_field :openai_key,
value: person.user.attributes["openai_key"],
class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full dark:text-black",
placeholder: "OpenAI Key"
%>
Expand Down Expand Up @@ -173,6 +174,7 @@
</li>
</ol>
<%= user_fields.text_field :anthropic_key,
value: person.user.attributes["anthropic_key"],
class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full dark:text-black",
placeholder: "Anthropic Key"
%>
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/allowed_request_origins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# export ALLOWED_REQUEST_ORIGINS=https://myhost.com,https://myotherhost.com
#
if (allowed_request_origins = ENV['ALLOWED_REQUEST_ORIGINS'].to_s.split(',')).any?
if (allowed_request_origins = ENV["ALLOWED_REQUEST_ORIGINS"].to_s.split(',')).any?
Rails.application.configure do
config.action_cable.allowed_request_origins = allowed_request_origins

Expand Down
4 changes: 4 additions & 0 deletions config/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ shared:
features:
default_to_voice: <%= ENV.fetch("DEFAULT_TO_VOICE_FEATURE", false) %>
voice: <%= ENV.fetch("VOICE_FEATURE", false) %>
default_llm_keys: <%= ENV.fetch("DEFAULT_LLM_KEYS_FEATURE", false) %>
google_tools: <%= ENV.fetch("GOOGLE_TOOLS", false) %>
registration: <%= ENV.fetch("REGISTRATION_FEATURE", true) %>
password_authentication: <%= ENV.fetch("PASSWORD_AUTHENTICATION_FEATURE", true) %>
google_authentication: <%= ENV.fetch("GOOGLE_AUTHENTICATION_FEATURE", false) %>
http_header_authentication: <%= ENV.fetch("HTTP_HEADER_AUTHENTICATION_FEATURE", false) %>
settings:
# Be sure to add these ENV to docker-compose.yml
default_openai_key: <%= ENV["DEFAULT_OPENAI_KEY"] %>
default_anthropic_key: <%= ENV["DEFAULT_ANTHROPIC_KEY"] %>
google_auth_client_id: <%= ENV["GOOGLE_AUTH_CLIENT_ID"] || Rails.application.credentials[:google_auth_client_id] %>
google_auth_client_secret: <%= ENV["GOOGLE_AUTH_CLIENT_SECRET"] || Rails.application.credentials[:google_auth_client_secret] %>
http_header_auth_email: <%= ENV["HTTP_HEADER_AUTH_EMAIL"] || "X-WEBAUTH-EMAIL" %>
Expand Down
11 changes: 10 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ services:
context: .
target: development
environment:
# Be sure to add environment variables to config/options.yml
- DATABASE_URL=postgres://app:secret@postgres/app_development
- DEV_HOST=${DEV_HOST:-localhost} # Set if you want to use a different hostname
- ALLOWED_REQUEST_ORIGINS=${ALLOWED_REQUEST_ORIGINS}
- OVERMIND_COLORS=2,3,5
- ALLOWED_REQUEST_ORIGINS
- DEFAULT_LLM_KEYS_FEATURE
- DEFAULT_OPENAI_KEY
- DEFAULT_ANTHROPIC_KEY
- GOOGLE_AUTH_CLIENT_ID
- GOOGLE_AUTH_CLIENT_SECRET
- HTTP_HEADER_AUTH_EMAIL
- HTTP_HEADER_AUTH_NAME
- HTTP_HEADER_AUTH_UID
ports: ["3000:3000"]
volumes:
- .:/rails
Expand Down
9 changes: 9 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,13 @@ class UserTest < ActiveSupport::TestCase
users(:keith).update!(last_name: nil)
end
end

test "it treats blank String API keys as nil to allow for default keys" do
stub_features(default_llm_keys: false) do
user = users(:keith)
user.update(openai_key: '', anthropic_key: ' ')
assert_nil user.openai_key
assert_nil user.anthropic_key
end
end
end

0 comments on commit 5e948f3

Please sign in to comment.