Skip to content
Snippets Groups Projects
Verified Commit 8f951602 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

feat: refactor ProcessUserAgentJob and AiProviderService for improved...

feat: refactor ProcessUserAgentJob and AiProviderService for improved readability and error handling

- Updated the `handle` method in `ProcessUserAgentJob` to use braces for better readability.
- Refactored request body construction in `AiProviderService` to improve clarity and maintainability.
- Moved API request headers into the API call method to streamline the code.
- Added a scheduled command to process user agents hourly in the console routes.
parent eb2e1560
No related branches found
No related tags found
1 merge request!59Ajouter service ia & détection bots user agents
Pipeline #1035 passed
......@@ -19,7 +19,8 @@ class ProcessUserAgentJob implements ShouldQueue
public function __construct(private readonly UserAgent $userAgent) {}
public function handle(AiProviderService $aiProviderService): void {
public function handle(AiProviderService $aiProviderService): void
{
try {
$result = $aiProviderService->prompt(
'You are a bot detector designed to output JSON. ',
......@@ -32,8 +33,6 @@ public function handle(AiProviderService $aiProviderService): void {
'user_agent_id' => $this->userAgent->id,
'is_bot' => $isBot,
]);
Log::info("UserAgent processed: {$this->userAgent->user_agent}, is_bot: {$isBot}");
} catch (Exception $e) {
Log::error("Failed to process UserAgent: {$this->userAgent->user_agent}", [
'exception' => $e->getMessage(),
......
......@@ -93,14 +93,7 @@ public function prompt(string $systemRole, string $prompt): array
{
$selectedProvider = config('ai-provider.selected-provider');
$requestBody = [
'headers' => [
'Authorization' => 'Bearer '.config('ai-provider.providers.'.$selectedProvider.'.api-key'),
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
'json' => [
'model' => config('ai-provider.providers.'.$selectedProvider.'.model'),
$requestBody = ['model' => config('ai-provider.providers.'.$selectedProvider.'.model'),
'messages' => [
[
'role' => 'system',
......@@ -129,7 +122,6 @@ public function prompt(string $systemRole, string $prompt): array
'response_format' => [
'type' => 'json_object',
],
],
];
return $this->callApi(config('ai-provider.providers.'.$selectedProvider.'.url'), $requestBody);
......@@ -144,13 +136,19 @@ public function prompt(string $systemRole, string $prompt): array
*/
private function callApi(string $url, array $requestBody): array
{
$selectedProvider = config('ai-provider.selected-provider');
Log::info('Calling AI provider API', [
'url' => $url,
'request_body' => $requestBody,
]);
try {
$response = Http::post($url, $requestBody);
$response = Http::withHeaders([
'Authorization' => 'Bearer '.config('ai-provider.providers.'.$selectedProvider.'.api-key'),
'Content-Type' => 'application/json',
'Accept' => 'application/json',
])->post($url, $requestBody);
} catch (ConnectionException $e) {
Log::error('Failed to call AI provider API', [
'exception' => $e,
......
......@@ -8,6 +8,7 @@
})->purpose('Display an inspiring quote')->hourly();
Schedule::command('save:requests')->everyMinute()->sentryMonitor();
Schedule::command('process:user-agents')->hourly()->sentryMonitor();
Schedule::command('flush:unused-uploaded-pictures')->daily()->sentryMonitor();
Schedule::command('backup:clean --disable-notifications')->daily()->at('01:00');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment