Files
jobifygarden/app/Console/Commands/GenerateJobSummaryCommand.php
T

89 lines
2.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Job;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
#[Signature('jobs:summary {--batch=50}')]
#[Description('Command description')]
class GenerateJobSummaryCommand extends Command
{
protected $signature = 'jobs:summary {--batch=50}';
protected $description = 'Generate AI summaries for jobs';
public function handle()
{
$batchSize = (int) $this->option('batch');
$log = Log::channel('jobs_summary');
$log->info('Starting summary generation', ['batch_size' => $batchSize]);
$jobs = $this->getJobs($batchSize);
$processed = 0;
$failed = 0;
foreach ($jobs as $job) {
try {
$this->processJob($job);
$processed++;
} catch (\Exception $e) {
$failed++;
$log->error("Failed to generate summary for job #{$job->id}", ['error' => $e->getMessage()]);
}
}
$log->info('Summary generation complete', ['processed' => $processed, 'failed' => $failed]);
}
private function getJobs(int $batchSize)
{
return Job::whereNUll('summary')
->limit($batchSize)
->orderBy('created_at', 'desc')
->get();
}
private function processJob(mixed $job)
{
$summary = $this->generateSummary($job);
if ($summary) {
$summary = str_replace('Summary:', "", $summary);
$job->summary = trim($summary);
$job->save();
}
}
private function generateSummary(Job $job)
{
$apiUrl = 'https://olm.c0rrup7.net/api/generate';
$body = 'This is the content of a job ad, you need to read it and extract a short and RELEVANT summary from it. Respond only with the summary, NO OTHER TEXT BEFORE OR AFTER THE SUMMARY.';
$body .= 'Title: '. htmlentities($job->title);
$body .= 'Description: '. htmlentities($job->description);
$body .= 'RETURN ONLY THE SUMMARY, NO TEXT BEFORE OR AFTER!';
$response = Http::timeout(130)
->withHeaders([
'Accept' => 'application/json',
'CF-Access-Client-Id' => '8c4cc272c7f08f8d7911a28eb5d7db44.access',
'CF-Access-Client-Secret' => 'adeeed2f75236053953cc9dd898c7756bab5e00f7d0901bf7d488b6db8aba336',
])
->post($apiUrl, [
'model' => 'mixtral',
'stream' => false,
'prompt' => $body,
]);
if ($response->successful()) {
$responseArray = $response->json();
return $responseArray['response'];
}
return null;
}
}