Initial commit
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Chatbot;
|
||||
use App\Services\DocumentIngestionService;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class IngestDocumentCommand extends Command
|
||||
{
|
||||
protected $signature = 'ingest:document {--url= : URL to scrape and ingest} {--pdf= : Path to PDF file to ingest} {--chatbot= : Slug of the chatbot to associate with}';
|
||||
protected $description = 'Ingests a URL or PDF document into Weaviate vector DB for a specific chatbot';
|
||||
|
||||
public function handle(DocumentIngestionService $ingestionService)
|
||||
{
|
||||
$url = $this->option('url');
|
||||
$pdfPath = $this->option('pdf');
|
||||
$chatbotSlug = $this->option('chatbot');
|
||||
|
||||
if (!$chatbotSlug) {
|
||||
$this->error('Please specify the chatbot using --chatbot=slug option.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$chatbot = Chatbot::where('slug', $chatbotSlug)->first();
|
||||
if (!$chatbot) {
|
||||
$this->error("Chatbot not found for slug: {$chatbotSlug}");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!$url && !$pdfPath) {
|
||||
$this->error('Please provide either a --url or a --pdf option.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$chatbotId = (string) $chatbot->id;
|
||||
|
||||
if ($url) {
|
||||
$this->info("Ingesting URL: {$url} for chatbot '{$chatbot->name}'...");
|
||||
$success = $ingestionService->ingestUrl($url, $chatbotId);
|
||||
if ($success) {
|
||||
$this->info("Successfully ingested URL!");
|
||||
$chatbot->histories()->create([
|
||||
'action' => 'Indexation URL',
|
||||
'target' => $url,
|
||||
'status' => 'success'
|
||||
]);
|
||||
} else {
|
||||
$this->error("Failed to ingest URL.");
|
||||
$chatbot->histories()->create([
|
||||
'action' => 'Indexation URL',
|
||||
'target' => $url,
|
||||
'status' => 'failed'
|
||||
]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if ($pdfPath) {
|
||||
if (!file_exists($pdfPath)) {
|
||||
$this->error("PDF file not found at: {$pdfPath}");
|
||||
return 1;
|
||||
}
|
||||
|
||||
$originalName = basename($pdfPath);
|
||||
$this->info("Ingesting PDF: {$originalName} for chatbot '{$chatbot->name}'...");
|
||||
$success = $ingestionService->ingestPdf($pdfPath, $originalName, $chatbotId);
|
||||
if ($success) {
|
||||
$this->info("Successfully ingested PDF!");
|
||||
$chatbot->histories()->create([
|
||||
'action' => 'Indexation PDF',
|
||||
'target' => $originalName,
|
||||
'status' => 'success'
|
||||
]);
|
||||
} else {
|
||||
$this->error("Failed to ingest PDF.");
|
||||
$chatbot->histories()->create([
|
||||
'action' => 'Indexation PDF',
|
||||
'target' => $originalName,
|
||||
'status' => 'failed'
|
||||
]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user