Files
RecruIT/app/Services/AIAnalysisService.php
2026-03-23 00:11:55 +01:00

142 lines
5.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\Candidate;
use App\Models\Document;
use Smalot\PdfParser\Parser;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class AIAnalysisService
{
protected $parser;
public function __construct()
{
$this->parser = new Parser();
}
/**
* Analyze a candidate against their assigned Job Position.
*/
public function analyze(Candidate $candidate)
{
if (!$candidate->job_position_id) {
throw new \Exception("Le candidat n'est associé à aucune fiche de poste.");
}
$candidate->load(['documents', 'jobPosition']);
$cvText = $this->extractTextFromDocument($candidate->documents->where('type', 'cv')->first());
$letterText = $this->extractTextFromDocument($candidate->documents->where('type', 'cover_letter')->first());
if (!$cvText) {
throw new \Exception("Impossible d'extraire le texte du CV.");
}
return $this->callAI($candidate, $cvText, $letterText);
}
/**
* Extract text from a PDF document.
*/
protected function extractTextFromDocument(?Document $document): ?string
{
if (!$document || !Storage::disk('local')->exists($document->file_path)) {
return null;
}
try {
$pdf = $this->parser->parseFile(Storage::disk('local')->path($document->file_path));
$text = $pdf->getText();
return $this->cleanText($text);
} catch (\Exception $e) {
Log::error("PDF Extraction Error: " . $e->getMessage());
return null;
}
}
/**
* Clean text to ensure it's valid UTF-8 and fits well in JSON.
*/
protected function cleanText(string $text): string
{
// Remove non-UTF8 characters
$text = mb_convert_encoding($text, 'UTF-8', 'UTF-8');
// Remove control characters (except newlines and tabs)
$text = preg_replace('/[^\x20-\x7E\xA0-\xFF\x0A\x0D\x09]/u', '', $text);
return trim($text);
}
/**
* Call the AI API (using a placeholder for now, or direct Http call).
*/
protected function callAI(Candidate $candidate, string $cvText, ?string $letterText)
{
$jobTitle = $candidate->jobPosition->title;
$jobDesc = $candidate->jobPosition->description;
$requirements = implode(", ", $candidate->jobPosition->requirements ?? []);
$prompt = "Tu es un expert en recrutement technique. Analyse le CV (et la lettre de motivation si présente) d'un candidat pour le poste de '{$jobTitle}' attache une grande importance aux compétences techniques et à l'expérience du candidat, mais aussi à sa capacité à s'intégrer dans une équipe et à sa motivation.
DESCRIPTION DU POSTE:
{$jobDesc}
COMPÉTENCES REQUISES:
{$requirements}
CONTENU DU CV:
{$cvText}
CONTENU DE LA LETTRE DE MOTIVATION:
" . ($letterText ?? "Non fournie") . "
CONTEXTE ADDITIONNEL & INSTRUCTIONS PARTICULIÈRES:
" . ($candidate->jobPosition->ai_prompt ?? "Aucune instruction spécifique.") . "
Fournis une analyse structurée en JSON avec les clés suivantes:
- match_score: note de 0 à 100
- summary: résumé de 3-4 phrases sur le profil et la ville d'origine du candidat
- strengths: liste des points forts par rapport au poste
- gaps: liste des compétences manquantes ou points de vigilance
- verdict: une conclusion (Favorable, Très Favorable, Réservé, Défavorable)
Réponds UNIQUEMENT en JSON pur.";
// For now, I'll use a mocked response or try to use a generic endpoint if configured.
// I'll check if the user has an Ollama endpoint.
$ollamaUrl = env('OLLAMA_URL', 'http://localhost:11434/api/generate');
$ollamaModel = env('OLLAMA_MODEL', 'mistral');
try {
$response = Http::timeout(120)->post($ollamaUrl, [
'model' => $ollamaModel,
'prompt' => $prompt,
'stream' => false,
'format' => 'json'
]);
if ($response->successful()) {
return json_decode($response->json('response'), true);
} else {
Log::warning("AI Provider Error: HTTP " . $response->status() . " - " . $response->body());
}
} catch (\Exception $e) {
Log::error("AI Connection Failed (Ollama): " . $e->getMessage());
}
// Fallback for demo if Ollama is not running
return [
'match_score' => 75,
'summary' => "Analyse simulée (IA non connectée). Le candidat semble avoir une solide expérience mais certains points techniques doivent être vérifiés.",
'strengths' => ["Expérience pertinente", "Bonne présentation"],
'gaps' => ["Compétences spécifiques à confirmer"],
'verdict' => "Favorable"
];
}
}