-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample
executable file
·85 lines (69 loc) · 3.14 KB
/
example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env php
<?php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Process;
require_once __DIR__.'/vendor/autoload.php';
$app = (new SingleCommandApplication('LLM Chain Example Runner'))
->setDescription('Runs all LLM Chain examples in folder examples/')
->setCode(function (InputInterface $input, ConsoleOutput $output) {
$io = new SymfonyStyle($input, $output);
$io->title('LLM Chain Examples');
$examples = (new Finder())
->in(__DIR__.'/examples')
->name('*.php')
->sortByName()
->files();
/** @var array{example: SplFileInfo, process: Process} $exampleRuns */
$exampleRuns = [];
foreach ($examples as $example) {
$exampleRuns[] = [
'example' => $example,
'process' => $process = new Process(['php', $example->getRealPath()]),
];
$process->start();
}
$examplesRunning = fn () => array_reduce($exampleRuns, fn ($running, $example) => $running || $example['process']->isRunning(), false);
$examplesSuccessful = fn () => array_reduce($exampleRuns, fn ($successful, $example) => $successful && $example['process']->isSuccessful(), true);
$section = $output->section();
$renderTable = function () use ($exampleRuns, $section) {
$section->clear();
$table = new Table($section);
$table->setHeaders(['Example', 'State', 'Output']);
foreach ($exampleRuns as $run) {
/** @var SplFileInfo $example */
/** @var Process $process */
['example' => $example, 'process' => $process] = $run;
$output = str_replace(PHP_EOL, ' ', $process->getOutput());
$output = strlen($output) <= 100 ? $output : substr($output, 0, 100).'...';
$emptyOutput = 0 === strlen(trim($output));
$state = 'Running';
if ($process->isTerminated()) {
$success = $process->isSuccessful() && !$emptyOutput;
$state = $success ? '<info>Finished</info>'
: (1 === $run['process']->getExitCode() || $emptyOutput ? '<error>Failed</error>' : '<comment>Skipped</comment>');
}
$table->addRow([$example->getFilename(), $state, $output]);
}
$table->render();
};
while ($examplesRunning()) {
$renderTable();
sleep(1);
}
$renderTable();
$io->newLine();
if (!$examplesSuccessful()) {
$io->error('Some examples failed or were skipped!');
return Command::FAILURE;
}
$io->success('All examples ran successfully!');
return Command::SUCCESS;
})
->run();