diff --git a/src/AiServiceDrivers/AiServiceManager.php b/src/AiServiceDrivers/AiServiceManager.php index 9f374d9..87216c1 100644 --- a/src/AiServiceDrivers/AiServiceManager.php +++ b/src/AiServiceDrivers/AiServiceManager.php @@ -11,10 +11,10 @@ class AiServiceManager extends Manager { public function createOpenaiDriver(): OpenAiDriver { - $apiKey = config('slower.open_ai.api_key'); + $apiKey = config('slower.open_ai.api_key'); $organization = config('slower.open_ai.organization'); - $timeout = config('slower.open_ai.request_timeout', 30); - if ( !is_string($apiKey) || ($organization !== null && !is_string($organization)) ) { + $timeout = config('slower.open_ai.request_timeout', 30); + if (! is_string($apiKey) || ($organization !== null && ! is_string($organization))) { throw new InvalidArgumentException( 'The OpenAI API Key is missing. Please publish the [slower.php] configuration file and set the [api_key].' ); @@ -23,8 +23,9 @@ public function createOpenaiDriver(): OpenAiDriver ->withApiKey($apiKey) ->withOrganization($organization) ->withHttpHeader('OpenAI-Beta', 'assistants=v2') - ->withHttpClient(new Client([ 'timeout' => $timeout ])) + ->withHttpClient(new Client(['timeout' => $timeout])) ->make(); + return new OpenAiDriver($openAI); } diff --git a/src/AiServiceDrivers/OpenAiDriver.php b/src/AiServiceDrivers/OpenAiDriver.php index a81a97d..a4e3d77 100644 --- a/src/AiServiceDrivers/OpenAiDriver.php +++ b/src/AiServiceDrivers/OpenAiDriver.php @@ -7,9 +7,7 @@ class OpenAiDriver implements AiServiceDriver { - public function __construct(protected Client $client) - { - } + public function __construct(protected Client $client) {} public function analyze(string $userMessage): ?string { @@ -20,6 +18,7 @@ public function analyze(string $userMessage): ?string ['role' => 'user', 'content' => $userMessage], ], ]); + return $result->choices[0]->message->content ?? null; } } diff --git a/src/Services/RecommendationService.php b/src/Services/RecommendationService.php index 737f22c..8012bd9 100644 --- a/src/Services/RecommendationService.php +++ b/src/Services/RecommendationService.php @@ -7,10 +7,7 @@ class RecommendationService { - public function __construct(protected AiServiceDriver $aiService) - { - - } + public function __construct(protected AiServiceDriver $aiService) {} public function getRecommendation($record): ?string { @@ -29,7 +26,7 @@ public function getRecommendation($record): ?string return tap( $this->aiService->analyze($userMessage), - static fn($result) => $record->update([ + static fn ($result) => $record->update([ 'is_analyzed' => true, 'recommendation' => $result, ]) diff --git a/src/SlowerServiceProvider.php b/src/SlowerServiceProvider.php index 12baaae..d2302d5 100644 --- a/src/SlowerServiceProvider.php +++ b/src/SlowerServiceProvider.php @@ -6,13 +6,10 @@ use HalilCosdu\Slower\AiServiceDrivers\Contracts\AiServiceDriver; use HalilCosdu\Slower\Commands\AnalyzeQuery; use HalilCosdu\Slower\Commands\SlowLogCleaner; -use HalilCosdu\Slower\Services\RecommendationService; use Illuminate\Database\Connection; use Illuminate\Database\Events\QueryExecuted; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; -use InvalidArgumentException; -use OpenAI as OpenAIFactory; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; diff --git a/tests/SlowerTest.php b/tests/SlowerTest.php index 28bf4da..aa55d92 100644 --- a/tests/SlowerTest.php +++ b/tests/SlowerTest.php @@ -48,7 +48,6 @@ class TestModel extends Illuminate\Database\Eloquent\Model {} }); }); - describe('ai-service', function () { it('returns an instance of the configured driver', function () { expect(