From 92252bc81488f20707c694f950d9442e2d21d587 Mon Sep 17 00:00:00 2001 From: Halil Cosdu Date: Tue, 23 Apr 2024 18:19:18 +0300 Subject: [PATCH] Provider validation added. --- src/FineTunerServiceProvider.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/FineTunerServiceProvider.php b/src/FineTunerServiceProvider.php index 1fc9f5c..bc562f3 100644 --- a/src/FineTunerServiceProvider.php +++ b/src/FineTunerServiceProvider.php @@ -3,6 +3,7 @@ namespace HalilCosdu\FineTuner; use HalilCosdu\FineTuner\Commands\FineTunerCommand; +use InvalidArgumentException; use OpenAI; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -25,11 +26,21 @@ public function configurePackage(Package $package): void public function packageRegistered(): void { $this->app->singleton(FineTuner::class, function ($app) { + $apiKey = config('finetuner.api_key'); + $organization = config('finetuner.organization'); + $timeout = config('finetuner.request_timeout', 30); + + if (! is_string($apiKey) || ($organization !== null && ! is_string($organization))) { + throw new InvalidArgumentException( + 'The OpenAI API Key is missing. Please publish the [finetuner.php] configuration file and set the [api_key].' + ); + } + return new FineTuner( OpenAI::factory() - ->withApiKey(config('finetuner.api_key')) - ->withOrganization(config('finetuner.organization')) - ->withHttpClient(new \GuzzleHttp\Client(['timeout' => config('finetuner.request_timeout', 600)])) + ->withApiKey($apiKey) + ->withOrganization($organization) + ->withHttpClient(new \GuzzleHttp\Client(['timeout' => $timeout])) ->make() ); });