Skip to content

Commit 7af6deb

Browse files
authored
Update API token validation messages (#52)
This commit: - updates the API token validation from using regex match to making a free API call to OpenAI - updates validation failure messages to be more clear and never assume a key is from prepend.me
1 parent 647519f commit 7af6deb

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

examples/15_proxy.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
puts "\n=== AI::Chat Proxy Examples ==="
1212
puts
1313

14+
puts "0. Validate API key:"
15+
chat = AI::Chat.new(api_key_env_var: "PROXY_API_KEY")
16+
chat.proxy = true
17+
chat.user("What's the capital of Florida?")
18+
chat.generate![:content]
19+
puts " API Key validated: #{chat.instance_variable_get(:@api_key_validated)}"
20+
puts
21+
1422
puts "1. Basic conversation:"
1523
chat = AI::Chat.new(api_key_env_var: "PROXY_API_KEY")
1624
chat.proxy = true

lib/ai/chat.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def initialize(api_key: nil, api_key_env_var: "OPENAI_API_KEY")
3434
@last_response_id = nil
3535
@image_generation = false
3636
@image_folder = "./images"
37+
@api_key_validated = false
3738
end
3839

3940
def self.generate_schema!(description, location: "schema.json", api_key: nil, api_key_env_var: "OPENAI_API_KEY", proxy: false)
@@ -133,7 +134,7 @@ def assistant(message, response: nil, status: nil)
133134
# :reek:NilCheck
134135
# :reek:TooManyStatements
135136
def generate!
136-
validate_api_key
137+
validate_api_key unless @api_key_validated
137138
response = create_response
138139
parse_response(response)
139140

@@ -568,25 +569,17 @@ def warn_if_file_fails_to_save
568569
end
569570

570571
def validate_api_key
571-
openai_api_key_used = @api_key.start_with?("sk-proj")
572-
proxy_api_key_used = !openai_api_key_used
573-
proxy_enabled = proxy
574-
proxy_disabled = !proxy
575-
576-
if openai_api_key_used && proxy_enabled
572+
# Simple API call to validate the token
573+
client.models.list
574+
@api_key_validated = true
575+
rescue OpenAI::Errors::AuthenticationError => e
576+
if proxy
577577
raise WrongAPITokenUsedError, <<~STRING
578-
It looks like you're using an official API key from OpenAI with proxying enabled. When proxying is enabled you must use an OpenAI API key from prepend.me. Please disable proxy or update your API key before generating a response.
578+
It looks like you're using an invalid API key. Proxying is enabled, so you must use an OpenAI API key from prepend.me. Please disable proxy or update your API key before generating a response.
579579
STRING
580-
elsif proxy_api_key_used && proxy_disabled
580+
else
581581
raise WrongAPITokenUsedError, <<~STRING
582-
It looks like you're using an unofficial OpenAI API key from prepend.me. When using an unofficial API key you must enable proxy before generating a response. Proxying is currently disabled, please enable it before generating a response.
583-
584-
Example:
585-
586-
chat = AI::Chat.new
587-
chat.proxy = true
588-
chat.user(...)
589-
chat.generate!
582+
It looks like you're using an invalid API key. Check to make sure your API key is valid before generating a response.
590583
STRING
591584
end
592585
end

0 commit comments

Comments
 (0)