-
Couldn't load subscription status.
- Fork 6
Troubleshooting
Nagendra Dhanakeerthi edited this page Oct 30, 2024
·
1 revision
ChatGPT::AuthenticationError: Invalid AuthenticationSolutions:
- Verify API key is correct:
# Check if API key is being loaded
puts ENV['OPENAI_API_KEY'].nil? ? "API key not found" : "API key length: #{ENV['OPENAI_API_KEY'].length}"- Ensure proper configuration:
ChatGPT.configure do |config|
config.api_key = ENV['OPENAI_API_KEY']
end- Verify API key directly in client:
client = ChatGPT::Client.new(your_api_key)ChatGPT::RateLimitError: Rate limit exceededSolutions:
- Implement exponential backoff:
def with_retry(max_attempts: 3)
attempt = 0
begin
attempt += 1
yield
rescue ChatGPT::RateLimitError => e
if attempt < max_attempts
sleep(2 ** attempt)
retry
else
raise
end
end
end- Use in your code:
with_retry do
client.chat(messages)
endChatGPT::TimeoutError: Request timed outSolutions:
- Increase timeout in configuration:
ChatGPT.configure do |config|
config.request_timeout = 60 # Increase to 60 seconds
end- Handle timeouts gracefully:
begin
response = client.chat(messages)
rescue ChatGPT::TimeoutError => e
# Log the error
Rails.logger.error "Request timed out: #{e.message}"
# Retry with increased timeout
client.request_timeout = 60
retry
endOpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0Solutions:
- Update SSL certificates:
# Update system SSL certificates
ENV['SSL_CERT_FILE'] = '/path/to/cacert.pem'- Force SSL verification:
require 'openssl'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE # Use with cautionJSON::ParserError: unexpected tokenSolutions:
- Check response content:
begin
response = client.chat(messages)
rescue => e
puts "Raw response: #{e.response.body}" if e.respond_to?(:response)
raise
endChatGPT.configure do |config|
config.debug = true # Enable debug logging
enddef debug_request(messages, params = {})
puts "Messages: #{messages.inspect}"
puts "Params: #{params.inspect}"
response = client.chat(messages, params)
puts "Response: #{response.inspect}"
rescue => e
puts "Error: #{e.class} - #{e.message}"
raise
enddef verify_configuration
puts "API Key present: #{!ChatGPT.configuration.api_key.nil?}"
puts "Timeout: #{ChatGPT.configuration.request_timeout}"
puts "Max retries: #{ChatGPT.configuration.max_retries}"
end| Error Code | Description | Solution |
|---|---|---|
| 401 | Invalid Authentication | Check API key |
| 429 | Rate Limit Exceeded | Implement retries |
| 500 | Server Error | Retry with backoff |
| 503 | Service Unavailable | Retry later |
def log_error(error)
error_data = {
class: error.class.name,
message: error.message,
backtrace: error.backtrace&.first(5)
}
Rails.logger.error("ChatGPT Error: #{error_data.to_json}")
enddef health_check
begin
response = client.completions("test", max_tokens: 1)
{ status: "ok", message: "API responding" }
rescue => e
{ status: "error", message: e.message }
end
enddef validate_environment
missing_vars = []
required_vars = ['OPENAI_API_KEY']
required_vars.each do |var|
missing_vars << var if ENV[var].nil?
end
raise "Missing environment variables: #{missing_vars.join(', ')}" if missing_vars.any?
endIf you're still experiencing issues:
- Check the GitHub Issues
- Create a new issue with:
- Ruby version
- Gem version
- Error message
- Minimal reproduction code
- Join the discussion in existing issues