I am using Laravel Framework 10.15.0
.
I am trying to load my API-Keys the following way:
$apiKeyOpenAI = env('OPENAI_API_KEY');
$client = OpenAI::client($apiKeyOpenAI);
In my .env
file the api key is clearly defined:
OPENAI_API_KEY=xx-xxxxxxxxxxxxxxxxxxxxxxx
However, when executing my application on the server I get that the $apiKeyOpenAI
is null.
Still my .env
file has the OPENAI_API_KEY in it. I checked this!
I tried to clear my cache php artisan config:clear
, I still get the error:
TypeError
OpenAI::client(): Argument #1 ($apiKey) must be of type string, null given, called in /var/www/demo-website/app/Console/Commands/AdminCommand.php on line 151
at vendor/openai-php/client/src/OpenAI.php:13
9▕ {
10▕ /**
11▕ * Creates a new Open AI Client with the given API token.
12▕ */
➜ 13▕ public static function client(string $apiKey, string $organization = null): Client
14▕ {
15▕ return self::factory()
16▕ ->withApiKey($apiKey)
17▕ ->withOrganization($organization)
1 app/Console/Commands/AdminCommand.php:151
OpenAI::client()
2 app/Console/Commands/AdminCommand.php:39
App\Console\Commands\AdminCommand::generateContentUsingOpenAI()
Any suggesitons what I am doing wrong?
I appreciate your replies!
UPDATE
After deploying to the server I need to run this script so that it seems to work:
Route::get('/clear', function() {
Artisan::call('cache:clear');
Artisan::call('config:clear');
return "Cache, Config is cleared";
})->middleware(['auth', 'admin']);
When deploying this script is also automatically run:
#!/bin/sh
set -e
echo "Deploying application ..."
# Enter maintenance mode
(php artisan down) || true
# Update codebase
git fetch origin deploy
git reset --hard origin/deploy
# Install dependencies based on lock file
composer install --no-interaction --prefer-dist --optimize-autoloader
# Migrate database
php artisan migrate --force
# Note: If you're using queue workers, this is the place to restart them.
# ...
# Clear cache
# php artisan optimize
php artisan config:cache
php artisan route:clear
php artisan route:cache
php artisan view:clear
php artisan view:cache
php artisan auth:clear-resets
php artisan cache:clear
php artisan config:clear
# Generate sitemap
# php artisan sitemap:generate
# Reload PHP to update opcache
echo "" | sudo -S service php8.1-fpm reload
# Exit maintenance mode
php artisan up
echo "Application deployed!"