new version ndjson
This commit is contained in:
@@ -4,86 +4,27 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Vector\VectorIndexBuilder;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'mto:agent:vector:ingest',
|
||||
description: 'Builds the FAISS vector index from index.json'
|
||||
)]
|
||||
final class VectorIngestCommand extends Command
|
||||
#[AsCommand(name: 'mto:agent:vector:rebuild')]
|
||||
class VectorIngestCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $vectorDir,
|
||||
private readonly string $projectDir
|
||||
private readonly VectorIndexBuilder $builder
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$vectorDir = rtrim($this->vectorDir, '/');
|
||||
$output->writeln('Rebuilding vector index...');
|
||||
$this->builder->rebuildFromNdjson();
|
||||
$output->writeln('Done.');
|
||||
|
||||
if (!is_dir($vectorDir)) {
|
||||
$output->writeln('<error>Vector directory not found</error>');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$script = $vectorDir . '/vector_ingest.py';
|
||||
|
||||
if (!is_file($script)) {
|
||||
$output->writeln('<error>vector_ingest.py not found</error>');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// Enforce venv usage
|
||||
// -------------------------------------------------
|
||||
$venvPython = $vectorDir . '/.venv/bin/python';
|
||||
|
||||
if (!is_file($venvPython)) {
|
||||
$output->writeln('<error>No Python virtual environment found.</error>');
|
||||
$output->writeln('<comment>Run first:</comment>');
|
||||
$output->writeln('<info> php bin/console mto:agent:vector:install</info>');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$knowledgeDir = rtrim($this->projectDir, '/') . '/var/knowledge';
|
||||
|
||||
if (!is_dir($knowledgeDir)) {
|
||||
$output->writeln('<error>Knowledge directory not found:</error>');
|
||||
$output->writeln($knowledgeDir);
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$output->writeln('<info>Building FAISS vector index…</info>');
|
||||
$output->writeln(sprintf(
|
||||
'<comment>Vector dir:</comment> %s',
|
||||
$vectorDir
|
||||
));
|
||||
$output->writeln(sprintf(
|
||||
'<comment>Knowledge dir:</comment> %s',
|
||||
$knowledgeDir
|
||||
));
|
||||
|
||||
$cmd = sprintf(
|
||||
'%s %s %s %s 2>&1',
|
||||
escapeshellarg($venvPython),
|
||||
escapeshellarg($script),
|
||||
escapeshellarg($vectorDir),
|
||||
escapeshellarg($knowledgeDir)
|
||||
);
|
||||
|
||||
exec($cmd, $out, $exitCode);
|
||||
|
||||
foreach ($out as $line) {
|
||||
$output->writeln($line);
|
||||
}
|
||||
|
||||
return $exitCode === 0
|
||||
? Command::SUCCESS
|
||||
: Command::FAILURE;
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user