1111
1212use Psr \Log \LoggerAwareInterface ;
1313use Psr \Log \LoggerInterface ;
14+ use Symfony \AI \Agent \Exception \ExceptionInterface as AgentException ;
15+ use Symfony \AI \Platform \Exception \ExceptionInterface as PlatformException ;
16+ use Symfony \AI \Platform \Exception \ResultException ;
1417use Symfony \AI \Platform \Metadata \Metadata ;
1518use Symfony \AI \Platform \Metadata \TokenUsage ;
1619use Symfony \AI \Platform \Result \ResultPromise ;
20+ use Symfony \AI \Store \Exception \ExceptionInterface as StoreException ;
21+ use Symfony \Component \Console \Helper \Table ;
1722use Symfony \Component \Console \Logger \ConsoleLogger ;
1823use Symfony \Component \Console \Output \ConsoleOutput ;
1924use Symfony \Component \Dotenv \Dotenv ;
2631function env (string $ var ): string
2732{
2833 if (!isset ($ _SERVER [$ var ]) || '' === $ _SERVER [$ var ]) {
29- printf ( ' Please set the "%s" environment variable to run this example. ' , $ var );
34+ output ()-> writeln ( sprintf ( ' <error> Please set the "%s" environment variable to run this example.</error> ' , $ var) );
3035 exit (1 );
3136 }
3237
@@ -45,6 +50,11 @@ function http_client(): HttpClientInterface
4550}
4651
4752function logger (): LoggerInterface
53+ {
54+ return new ConsoleLogger (output ());
55+ }
56+
57+ function output (): ConsoleOutput
4858{
4959 $ verbosity = match ($ _SERVER ['argv ' ][1 ] ?? null ) {
5060 '-v ' , '--verbose ' => ConsoleOutput::VERBOSITY_VERBOSE ,
@@ -53,7 +63,7 @@ function logger(): LoggerInterface
5363 default => ConsoleOutput::VERBOSITY_NORMAL ,
5464 };
5565
56- return new ConsoleLogger ( new ConsoleOutput ($ verbosity) );
66+ return new ConsoleOutput ($ verbosity );
5767}
5868
5969function print_token_usage (Metadata $ metadata ): void
@@ -62,23 +72,29 @@ function print_token_usage(Metadata $metadata): void
6272
6373 assert ($ tokenUsage instanceof TokenUsage);
6474
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 ;
75+ $ na = '<comment>n/a</comment> ' ;
76+ $ table = new Table (output ());
77+ $ table ->setHeaderTitle ('Token Usage ' );
78+ $ table ->setRows ([
79+ ['Prompt tokens ' , $ tokenUsage ->promptTokens ?? $ na ],
80+ ['Completion tokens ' , $ tokenUsage ->completionTokens ?? $ na ],
81+ ['Thinking tokens ' , $ tokenUsage ->thinkingTokens ?? $ na ],
82+ ['Tool tokens ' , $ tokenUsage ->toolTokens ?? $ na ],
83+ ['Cached tokens ' , $ tokenUsage ->cachedTokens ?? $ na ],
84+ ['Remaining tokens minute ' , $ tokenUsage ->remainingTokensMinute ?? $ na ],
85+ ['Remaining tokens month ' , $ tokenUsage ->remainingTokensMonth ?? $ na ],
86+ ['Remaining tokens ' , $ tokenUsage ->remainingTokens ?? $ na ],
87+ ['Utilized tokens ' , $ tokenUsage ->totalTokens ?? $ na ],
88+ ]);
89+ $ table ->render ();
7490}
7591
7692function print_vectors (ResultPromise $ result ): void
7793{
7894 assert ([] !== $ result ->asVectors ());
7995 assert (array_key_exists (0 , $ result ->asVectors ()));
8096
81- echo 'Dimensions: ' . $ result ->asVectors ()[0 ]->getDimensions ().\ PHP_EOL ;
97+ output ()-> writeln ( sprintf ( 'Dimensions: %d ' , $ result ->asVectors ()[0 ]->getDimensions ())) ;
8298}
8399
84100function perplexity_print_search_results (Metadata $ metadata ): void
@@ -138,3 +154,21 @@ function print_stream(ResultPromise $result): void
138154 }
139155 echo \PHP_EOL ;
140156}
157+
158+ set_exception_handler (function ($ exception ) {
159+ if ($ exception instanceof AgentException || $ exception instanceof PlatformException || $ exception instanceof StoreException) {
160+ output ()->writeln (sprintf ('<error>%s</error> ' , $ exception ->getMessage ()));
161+
162+ if ($ exception instanceof ResultException && output ()->isVerbose ()) {
163+ dump ($ exception ->getDetails ());
164+ }
165+
166+ if (output ()->isVeryVerbose ()) {
167+ output ()->writeln ($ exception ->getTraceAsString ());
168+ }
169+
170+ exit (1 );
171+ }
172+
173+ throw $ exception ;
174+ });
0 commit comments