forked from php-llm/llm-chain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample
More file actions
executable file
·110 lines (92 loc) · 4.19 KB
/
example
File metadata and controls
executable file
·110 lines (92 loc) · 4.19 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env php
<?php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
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/')
->addArgument('subdirectory', InputArgument::OPTIONAL, 'Subdirectory to run examples from, e.g. "anthropic" or "huggingface".')
->setCode(function (InputInterface $input, ConsoleOutput $output) {
$io = new SymfonyStyle($input, $output);
$io->title('LLM Chain Examples');
$directory = __DIR__.'/examples';
if ($subdirectory = $input->getArgument('subdirectory')) {
$directory .= '/'.$subdirectory;
if (!is_dir($directory)) {
$io->error(sprintf('Subdirectory "%s" does not exist.', $subdirectory));
return Command::FAILURE;
}
}
$examples = (new Finder())
->in($directory)
->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();
}
$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->getRelativePathname(), $state, $output]);
}
$table->render();
};
$examplesRunning = fn () => array_reduce($exampleRuns, fn ($running, $example) => $running || $example['process']->isRunning(), false);
while ($examplesRunning()) {
$renderTable();
sleep(1);
}
$renderTable();
$io->newLine();
$successCount = array_reduce($exampleRuns, function ($count, $example) {
if ($example['process']->isSuccessful() && strlen(trim($example['process']->getOutput())) > 0) {
return $count + 1;
}
return $count;
}, 0);
$totalCount = count($exampleRuns);
if ($successCount < $totalCount) {
$io->warning(sprintf('%d out of %d examples ran successfully.', $successCount, $totalCount));
} else {
$io->success(sprintf('All %d examples ran successfully!', $totalCount));
}
foreach ($exampleRuns as $run) {
if (!$run['process']->isSuccessful()) {
$io->section('Error in ' . $run['example']->getRelativePathname());
$io->text($run['process']->getOutput());
$io->text($run['process']->getErrorOutput());
}
}
return Command::SUCCESS;
})
->run();