1313
1414use PHPUnit \Framework \TestCase ;
1515use Symfony \AI \Platform \Bridge \OpenAi \Gpt \ResultConverter ;
16- use Symfony \AI \Platform \Metadata \TokenUsage ;
1716use Symfony \AI \Platform \Result \RawHttpResult ;
18- use Symfony \AI \Platform \Result \StreamChunk ;
1917use Symfony \AI \Platform \Result \StreamResult ;
18+ use Symfony \AI \Platform \Result \TextChunk ;
2019use Symfony \AI \Platform \Result \ToolCallResult ;
2120use Symfony \Component \HttpClient \EventSourceHttpClient ;
2221use Symfony \Component \HttpClient \MockHttpClient ;
@@ -49,7 +48,7 @@ public function testStreamTextDeltas()
4948
5049 $ this ->assertInstanceOf (StreamResult::class, $ result );
5150
52- /** @var StreamChunk [] $chunks */
51+ /** @var TextChunk [] $chunks */
5352 $ chunks = [];
5453 $ content = '' ;
5554
@@ -107,6 +106,8 @@ public function testStreamToolCallsAreAssembledAndYielded()
107106 $ this ->assertSame ('call_123 ' , $ toolCalls [0 ]->getId ());
108107 $ this ->assertSame ('test_function ' , $ toolCalls [0 ]->getName ());
109108 $ this ->assertSame (['arg1 ' => 'value1 ' ], $ toolCalls [0 ]->getArguments ());
109+
110+ // Get the token usage metadata from the result
110111 $ this ->assertSame (
111112 [
112113 'prompt_tokens ' => 1039 ,
@@ -123,18 +124,18 @@ public function testStreamToolCallsAreAssembledAndYielded()
123124 'rejected_prediction_tokens ' => 0 ,
124125 ],
125126 ],
126- $ toolCallResult ->getMetadata ()->get ('usage ' )
127+ $ result ->getMetadata ()->get ('usage ' )
127128 );
128129 }
129130
130131 public function testStreamTokenUsage ()
131132 {
132133 $ sseBody = ''
133- ."data: { \"choices \":[{ \"delta \":{ \"role \": \"assistant \"}, \"index \":0}]} \n\n"
134- ."data: { \"choices \":[{ \"delta \":{ \"content \": \"Hello \"}, \"index \":0}]} \n\n"
135- ."data: { \"choices \":[{ \"delta \":{ \"content \": \"world \"}, \"index \":0}]} \n\n"
136- ."data: { \"choices \":[{ \"delta \":{}, \"index \":0, \"finish_reason \": \"stop \"}]} \n\n"
137- ."data: { \"usage \":{ \"prompt_tokens \":1039, \"completion_tokens \":10, \"total_tokens \":1049, \"prompt_tokens_details \":{ \"cached_tokens \":0, \"audio_tokens \":0}, \"completion_tokens_details \":{ \"reasoning_tokens \":0, \"audio_tokens \":0, \"accepted_prediction_tokens \":0, \"rejected_prediction_tokens \":0}}} \n\n"
134+ ."data: { \"id \" : \" chatcmpl-123 \" , \" choices \":[{ \"delta \":{ \"role \": \"assistant \"}, \"index \":0}]} \n\n"
135+ ."data: { \"id \" : \" chatcmpl-123 \" , \" choices \":[{ \"delta \":{ \"content \": \"Hello \"}, \"index \":0}]} \n\n"
136+ ."data: { \"id \" : \" chatcmpl-123 \" , \" choices \":[{ \"delta \":{ \"content \": \"world \"}, \"index \":0}]} \n\n"
137+ ."data: { \"id \" : \" chatcmpl-123 \" , \" choices \":[{ \"delta \":{}, \"index \":0, \"finish_reason \": \"stop \"}]} \n\n"
138+ ."data: { \"id \" : \" chatcmpl-123 \" , \" usage \":{ \"prompt_tokens \":1039, \"completion_tokens \":10, \"total_tokens \":1049, \"prompt_tokens_details \":{ \"cached_tokens \":0, \"audio_tokens \":0}, \"completion_tokens_details \":{ \"reasoning_tokens \":0, \"audio_tokens \":0, \"accepted_prediction_tokens \":0, \"rejected_prediction_tokens \":0}}} \n\n"
138139 ."data: [DONE] \n\n" ;
139140
140141 $ mockClient = new MockHttpClient ([
@@ -157,11 +158,10 @@ public function testStreamTokenUsage()
157158 foreach ($ result ->getContent () as $ delta ) {
158159 $ yielded [] = $ delta ;
159160 }
160- $ this ->assertCount (3 , $ yielded );
161- $ this ->assertInstanceOf (TokenUsage::class, $ yielded [2 ]);
162- $ this ->assertSame (1039 , $ yielded [2 ]->promptTokens );
163- $ this ->assertSame (10 , $ yielded [2 ]->completionTokens );
164- $ this ->assertSame (1049 , $ yielded [2 ]->totalTokens );
165- $ this ->assertSame (0 , $ yielded [2 ]->cachedTokens );
161+ $ this ->assertCount (2 , $ yielded );
162+ /** @var TextChunk $chunk */
163+ $ chunk = $ yielded [0 ];
164+ $ this ->assertInstanceOf (TextChunk::class, $ chunk );
165+ $ this ->assertEquals ('chatcmpl-123 ' , $ chunk ->getMetadata ()->get ('id ' ));
166166 }
167167}
0 commit comments