11
11
12
12
use Psr \Log \LoggerAwareInterface ;
13
13
use Psr \Log \LoggerInterface ;
14
+ use Symfony \AI \Agent \Exception \ExceptionInterface as AgentException ;
15
+ use Symfony \AI \Platform \Exception \ExceptionInterface as PlatformException ;
14
16
use Symfony \AI \Platform \Metadata \Metadata ;
15
17
use Symfony \AI \Platform \Metadata \TokenUsage ;
16
18
use Symfony \AI \Platform \Result \ResultPromise ;
19
+ use Symfony \AI \Store \Exception \ExceptionInterface as StoreException ;
20
+ use Symfony \Component \Console \Helper \Table ;
17
21
use Symfony \Component \Console \Logger \ConsoleLogger ;
18
22
use Symfony \Component \Console \Output \ConsoleOutput ;
19
23
use Symfony \Component \Dotenv \Dotenv ;
26
30
function env (string $ var ): string
27
31
{
28
32
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) );
30
34
exit (1 );
31
35
}
32
36
@@ -45,6 +49,11 @@ function http_client(): HttpClientInterface
45
49
}
46
50
47
51
function logger (): LoggerInterface
52
+ {
53
+ return new ConsoleLogger (output ());
54
+ }
55
+
56
+ function output (): ConsoleOutput
48
57
{
49
58
$ verbosity = match ($ _SERVER ['argv ' ][1 ] ?? null ) {
50
59
'-v ' , '--verbose ' => ConsoleOutput::VERBOSITY_VERBOSE ,
@@ -53,7 +62,7 @@ function logger(): LoggerInterface
53
62
default => ConsoleOutput::VERBOSITY_NORMAL ,
54
63
};
55
64
56
- return new ConsoleLogger ( new ConsoleOutput ($ verbosity) );
65
+ return new ConsoleOutput ($ verbosity );
57
66
}
58
67
59
68
function print_token_usage (Metadata $ metadata ): void
@@ -62,23 +71,29 @@ function print_token_usage(Metadata $metadata): void
62
71
63
72
assert ($ tokenUsage instanceof TokenUsage);
64
73
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 ();
74
89
}
75
90
76
91
function print_vectors (ResultPromise $ result ): void
77
92
{
78
93
assert ([] !== $ result ->asVectors ());
79
94
assert (array_key_exists (0 , $ result ->asVectors ()));
80
95
81
- echo 'Dimensions: ' . $ result ->asVectors ()[0 ]->getDimensions ().\ PHP_EOL ;
96
+ output ()-> writeln ( sprintf ( 'Dimensions: %d ' , $ result ->asVectors ()[0 ]->getDimensions ())) ;
82
97
}
83
98
84
99
function perplexity_print_search_results (Metadata $ metadata ): void
@@ -138,3 +153,17 @@ function print_stream(ResultPromise $result): void
138
153
}
139
154
echo \PHP_EOL ;
140
155
}
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