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 ;
1416use Symfony \AI \Platform \Metadata \Metadata ;
1517use Symfony \AI \Platform \Metadata \TokenUsage ;
1618use Symfony \AI \Platform \Result \ResultPromise ;
19+ use Symfony \AI \Store \Exception \ExceptionInterface as StoreException ;
20+ use Symfony \Component \Console \Helper \Table ;
1721use Symfony \Component \Console \Logger \ConsoleLogger ;
1822use Symfony \Component \Console \Output \ConsoleOutput ;
1923use Symfony \Component \Dotenv \Dotenv ;
2630function 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
4751function 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
5968function 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
7691function 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
8499function 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+ });
0 commit comments