Skip to content

Commit 1ca6683

Browse files
committed
Improve example output, mostly for errors
1 parent 1999b95 commit 1ca6683

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

examples/bootstrap.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111

1212
use Psr\Log\LoggerAwareInterface;
1313
use Psr\Log\LoggerInterface;
14+
use Symfony\AI\Agent\Exception\ExceptionInterface as AgentException;
15+
use Symfony\AI\Platform\Exception\ExceptionInterface as PlatformException;
1416
use Symfony\AI\Platform\Metadata\Metadata;
1517
use Symfony\AI\Platform\Metadata\TokenUsage;
1618
use Symfony\AI\Platform\Result\ResultPromise;
19+
use Symfony\AI\Store\Exception\ExceptionInterface as StoreException;
20+
use Symfony\Component\Console\Helper\Table;
1721
use Symfony\Component\Console\Logger\ConsoleLogger;
1822
use Symfony\Component\Console\Output\ConsoleOutput;
1923
use Symfony\Component\Dotenv\Dotenv;
@@ -26,7 +30,7 @@
2630
function env(string $var): string
2731
{
2832
if (!isset($_SERVER[$var]) || '' === $_SERVER[$var]) {
29-
printf('Please set the "%s" environment variable to run this example.', $var);
33+
output()->writeln(sprintf('<error>Please set the "%s" environment variable to run this example.</error>', $var));
3034
exit(1);
3135
}
3236

@@ -45,6 +49,11 @@ function http_client(): HttpClientInterface
4549
}
4650

4751
function logger(): LoggerInterface
52+
{
53+
return new ConsoleLogger(output());
54+
}
55+
56+
function output(): ConsoleOutput
4857
{
4958
$verbosity = match ($_SERVER['argv'][1] ?? null) {
5059
'-v', '--verbose' => ConsoleOutput::VERBOSITY_VERBOSE,
@@ -53,7 +62,7 @@ function logger(): LoggerInterface
5362
default => ConsoleOutput::VERBOSITY_NORMAL,
5463
};
5564

56-
return new ConsoleLogger(new ConsoleOutput($verbosity));
65+
return new ConsoleOutput($verbosity);
5766
}
5867

5968
function print_token_usage(Metadata $metadata): void
@@ -62,23 +71,29 @@ function print_token_usage(Metadata $metadata): void
6271

6372
assert($tokenUsage instanceof TokenUsage);
6473

65-
echo 'Prompt tokens: '.$tokenUsage->promptTokens.\PHP_EOL;
66-
echo 'Completion tokens: '.$tokenUsage->completionTokens.\PHP_EOL;
67-
echo 'Thinking tokens: '.$tokenUsage->thinkingTokens.\PHP_EOL;
68-
echo 'Tool tokens: '.$tokenUsage->toolTokens.\PHP_EOL;
69-
echo 'Cached tokens: '.$tokenUsage->cachedTokens.\PHP_EOL;
70-
echo 'Remaining tokens minute: '.$tokenUsage->remainingTokensMinute.\PHP_EOL;
71-
echo 'Remaining tokens month: '.$tokenUsage->remainingTokensMonth.\PHP_EOL;
72-
echo 'Remaining tokens: '.$tokenUsage->remainingTokens.\PHP_EOL;
73-
echo 'Utilized tokens: '.$tokenUsage->totalTokens.\PHP_EOL;
74+
$na = '<comment>n/a</comment>';
75+
$table = new Table(output());
76+
$table->setHeaderTitle('Token Usage');
77+
$table->setRows([
78+
['Prompt tokens', $tokenUsage->promptTokens ?? $na],
79+
['Completion tokens', $tokenUsage->completionTokens ?? $na],
80+
['Thinking tokens', $tokenUsage->thinkingTokens ?? $na],
81+
['Tool tokens', $tokenUsage->toolTokens ?? $na],
82+
['Cached tokens', $tokenUsage->cachedTokens ?? $na],
83+
['Remaining tokens minute', $tokenUsage->remainingTokensMinute ?? $na],
84+
['Remaining tokens month', $tokenUsage->remainingTokensMonth ?? $na],
85+
['Remaining tokens', $tokenUsage->remainingTokens ?? $na],
86+
['Utilized tokens', $tokenUsage->totalTokens ?? $na],
87+
]);
88+
$table->render();
7489
}
7590

7691
function print_vectors(ResultPromise $result): void
7792
{
7893
assert([] !== $result->asVectors());
7994
assert(array_key_exists(0, $result->asVectors()));
8095

81-
echo 'Dimensions: '.$result->asVectors()[0]->getDimensions().\PHP_EOL;
96+
output()->writeln(sprintf('Dimensions: %d', $result->asVectors()[0]->getDimensions()));
8297
}
8398

8499
function perplexity_print_search_results(Metadata $metadata): void
@@ -138,3 +153,17 @@ function print_stream(ResultPromise $result): void
138153
}
139154
echo \PHP_EOL;
140155
}
156+
157+
set_exception_handler(function ($exception) {
158+
if ($exception instanceof AgentException || $exception instanceof PlatformException || $exception instanceof StoreException) {
159+
output()->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
160+
161+
if (output()->isVerbose()) {
162+
output()->writeln($exception->getTraceAsString());
163+
}
164+
165+
exit(1);
166+
}
167+
168+
throw $exception;
169+
});

examples/runner

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ $app = (new SingleCommandApplication('Symfony AI Example Runner'))
120120
if (!$run['process']->isSuccessful()) {
121121
$io->section('Error in ' . $run['example']->getRelativePathname());
122122
$io->text($run['process']->getOutput());
123-
$io->text($run['process']->getErrorOutput());
124123
}
125124
}
126125

0 commit comments

Comments
 (0)