-
Notifications
You must be signed in to change notification settings - Fork 4
/
example
executable file
·77 lines (65 loc) · 3.35 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
#!/usr/bin/env php
<?php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;
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, OutputInterface $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, section: ConsoleSectionOutput, process: Process, state: 'running'|'finished'} $exampleRuns */
$exampleRuns = [];
foreach ($examples as $example) {
$exampleRuns[] = [
'example' => $example,
'section' => $section = $output->section(),
'process' => $process = new Process(['php', $example->getRealPath()]),
'state' => 'running',
];
$process->start();
$section->writeln(sprintf('Example %s: <comment>Running</comment>', $example->getFilename()));
}
$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);
while ($examplesRunning()) {
sleep(1);
foreach ($exampleRuns as $run) {
if ('running' === $run['state'] && !$run['process']->isRunning()) {
$emptyOutput = 0 === strlen(trim($run['process']->getOutput()));
$success = $run['process']->isSuccessful() && !$emptyOutput;
$result = $success ? '<info>Finished</info>'
: (1 === $run['process']->getExitCode() || $emptyOutput ? '<error>Failed</error>' : '<comment>Skipped</comment>');
$run['section']->overwrite(sprintf('Example %s: %s', $run['example']->getFilename(), $result));
$run['state'] = 'finished';
if ($output->isVerbose()) {
$exampleOutput = $emptyOutput ? 'Output was empty' : $run['process']->getOutput();
$exampleOutput = strlen($exampleOutput) <= 100 ? $exampleOutput : substr($exampleOutput, 0, 100).'...';
$run['section']->writeln(
sprintf('<%s>%s</>', $success ? 'fg=#999999' : 'fg=red', trim($exampleOutput))
);
}
}
}
}
$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();