|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +/* | 
|  | 4 | + * This file is part of the Symfony package. | 
|  | 5 | + * | 
|  | 6 | + * (c) Fabien Potencier <[email protected]> | 
|  | 7 | + * | 
|  | 8 | + * For the full copyright and license information, please view the LICENSE | 
|  | 9 | + * file that was distributed with this source code. | 
|  | 10 | + */ | 
|  | 11 | + | 
|  | 12 | +namespace Symfony\AI\Platform\Tests\Bridge\OpenAi\Gpt; | 
|  | 13 | + | 
|  | 14 | +use PHPUnit\Framework\TestCase; | 
|  | 15 | +use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter; | 
|  | 16 | +use Symfony\AI\Platform\Metadata\TokenUsage; | 
|  | 17 | +use Symfony\AI\Platform\Result\RawHttpResult; | 
|  | 18 | +use Symfony\AI\Platform\Result\StreamResult; | 
|  | 19 | +use Symfony\AI\Platform\Result\ToolCallResult; | 
|  | 20 | +use Symfony\Component\HttpClient\EventSourceHttpClient; | 
|  | 21 | +use Symfony\Component\HttpClient\MockHttpClient; | 
|  | 22 | +use Symfony\Component\HttpClient\Response\MockResponse; | 
|  | 23 | + | 
|  | 24 | +final class ResultConverterStreamTest extends TestCase | 
|  | 25 | +{ | 
|  | 26 | +    public function testStreamTextDeltas() | 
|  | 27 | +    { | 
|  | 28 | +        $sseBody = '' | 
|  | 29 | +            ."data: {\"choices\":[{\"delta\":{\"role\":\"assistant\"},\"index\":0}]}\n\n" | 
|  | 30 | +            ."data: {\"choices\":[{\"delta\":{\"content\":\"Hello \"},\"index\":0}]}\n\n" | 
|  | 31 | +            ."data: {\"choices\":[{\"delta\":{\"content\":\"world\"},\"index\":0}]}\n\n" | 
|  | 32 | +            ."data: {\"choices\":[{\"delta\":{},\"index\":0,\"finish_reason\":\"stop\"}]}\n\n" | 
|  | 33 | +            ."data: [DONE]\n\n"; | 
|  | 34 | + | 
|  | 35 | +        $mockClient = new MockHttpClient([ | 
|  | 36 | +            new MockResponse($sseBody, [ | 
|  | 37 | +                'http_code' => 200, | 
|  | 38 | +                'response_headers' => [ | 
|  | 39 | +                    'content-type' => 'text/event-stream', | 
|  | 40 | +                ], | 
|  | 41 | +            ]), | 
|  | 42 | +        ]); | 
|  | 43 | +        $esClient = new EventSourceHttpClient($mockClient); | 
|  | 44 | +        $asyncResponse = $esClient->request('GET', 'http://localhost/stream'); | 
|  | 45 | + | 
|  | 46 | +        $converter = new ResultConverter(); | 
|  | 47 | +        $result = $converter->convert(new RawHttpResult($asyncResponse), ['stream' => true]); | 
|  | 48 | + | 
|  | 49 | +        $this->assertInstanceOf(StreamResult::class, $result); | 
|  | 50 | +        $chunks = []; | 
|  | 51 | +        foreach ($result->getContent() as $delta) { | 
|  | 52 | +            $chunks[] = $delta; | 
|  | 53 | +        } | 
|  | 54 | + | 
|  | 55 | +        // Only text deltas are yielded; role and finish chunks are ignored | 
|  | 56 | +        $this->assertSame(['Hello ', 'world'], $chunks); | 
|  | 57 | +    } | 
|  | 58 | + | 
|  | 59 | +    public function testStreamToolCallsAreAssembledAndYielded() | 
|  | 60 | +    { | 
|  | 61 | +        // Simulate a tool call that is streamed in multiple argument parts | 
|  | 62 | +        $sseBody = '' | 
|  | 63 | +            ."data: {\"choices\":[{\"delta\":{\"role\":\"assistant\"},\"index\":0}]}\n\n" | 
|  | 64 | +            ."data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"id\":\"call_123\",\"type\":\"function\",\"function\":{\"name\":\"test_function\",\"arguments\":\"{\\\"arg1\\\": \\\"value1\\\"}\"}}]},\"index\":0}]}\n\n" | 
|  | 65 | +            ."data: {\"choices\":[{\"delta\":{},\"index\":0,\"finish_reason\":\"tool_calls\"}]}\n\n" | 
|  | 66 | +            ."data: [DONE]\n\n"; | 
|  | 67 | + | 
|  | 68 | +        $mockClient = new MockHttpClient([ | 
|  | 69 | +            new MockResponse($sseBody, [ | 
|  | 70 | +                'http_code' => 200, | 
|  | 71 | +                'response_headers' => [ | 
|  | 72 | +                    'content-type' => 'text/event-stream', | 
|  | 73 | +                ], | 
|  | 74 | +            ]), | 
|  | 75 | +        ]); | 
|  | 76 | +        $esClient = new EventSourceHttpClient($mockClient); | 
|  | 77 | +        $asyncResponse = $esClient->request('GET', 'http://localhost/stream'); | 
|  | 78 | + | 
|  | 79 | +        $converter = new ResultConverter(); | 
|  | 80 | +        $result = $converter->convert(new RawHttpResult($asyncResponse), ['stream' => true]); | 
|  | 81 | + | 
|  | 82 | +        $this->assertInstanceOf(StreamResult::class, $result); | 
|  | 83 | + | 
|  | 84 | +        $yielded = []; | 
|  | 85 | +        foreach ($result->getContent() as $delta) { | 
|  | 86 | +            $yielded[] = $delta; | 
|  | 87 | +        } | 
|  | 88 | + | 
|  | 89 | +        // Expect only one yielded item and it should be a ToolCallResult | 
|  | 90 | +        $this->assertCount(1, $yielded); | 
|  | 91 | +        $this->assertInstanceOf(ToolCallResult::class, $yielded[0]); | 
|  | 92 | +        /** @var ToolCallResult $toolCallResult */ | 
|  | 93 | +        $toolCallResult = $yielded[0]; | 
|  | 94 | +        $toolCalls = $toolCallResult->getContent(); | 
|  | 95 | + | 
|  | 96 | +        $this->assertCount(1, $toolCalls); | 
|  | 97 | +        $this->assertSame('call_123', $toolCalls[0]->getId()); | 
|  | 98 | +        $this->assertSame('test_function', $toolCalls[0]->getName()); | 
|  | 99 | +        $this->assertSame(['arg1' => 'value1'], $toolCalls[0]->getArguments()); | 
|  | 100 | +    } | 
|  | 101 | + | 
|  | 102 | +    public function testStreamTokenUsage() | 
|  | 103 | +    { | 
|  | 104 | +        $sseBody = '' | 
|  | 105 | +            ."data: {\"choices\":[{\"delta\":{\"role\":\"assistant\"},\"index\":0}]}\n\n" | 
|  | 106 | +            ."data: {\"choices\":[{\"delta\":{\"content\":\"Hello \"},\"index\":0}]}\n\n" | 
|  | 107 | +            ."data: {\"choices\":[{\"delta\":{\"content\":\"world\"},\"index\":0}]}\n\n" | 
|  | 108 | +            ."data: {\"choices\":[{\"delta\":{},\"index\":0,\"finish_reason\":\"stop\"}]}\n\n" | 
|  | 109 | +            ."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" | 
|  | 110 | +            ."data: [DONE]\n\n"; | 
|  | 111 | + | 
|  | 112 | +        $mockClient = new MockHttpClient([ | 
|  | 113 | +            new MockResponse($sseBody, [ | 
|  | 114 | +                'http_code' => 200, | 
|  | 115 | +                'response_headers' => [ | 
|  | 116 | +                    'content-type' => 'text/event-stream', | 
|  | 117 | +                ], | 
|  | 118 | +            ]), | 
|  | 119 | +        ]); | 
|  | 120 | +        $esClient = new EventSourceHttpClient($mockClient); | 
|  | 121 | +        $asyncResponse = $esClient->request('GET', 'http://localhost/stream'); | 
|  | 122 | + | 
|  | 123 | +        $converter = new ResultConverter(); | 
|  | 124 | +        $result = $converter->convert(new RawHttpResult($asyncResponse), ['stream' => true]); | 
|  | 125 | + | 
|  | 126 | +        $this->assertInstanceOf(StreamResult::class, $result); | 
|  | 127 | + | 
|  | 128 | +        $yielded = []; | 
|  | 129 | +        foreach ($result->getContent() as $delta) { | 
|  | 130 | +            $yielded[] = $delta; | 
|  | 131 | +        } | 
|  | 132 | +        $this->assertCount(3, $yielded); | 
|  | 133 | +        $this->assertInstanceOf(TokenUsage::class, $yielded[2]); | 
|  | 134 | +        $this->assertSame(1039, $yielded[2]->promptTokens); | 
|  | 135 | +        $this->assertSame(10, $yielded[2]->completionTokens); | 
|  | 136 | +        $this->assertSame(1049, $yielded[2]->totalTokens); | 
|  | 137 | +        $this->assertSame(0, $yielded[2]->cachedTokens); | 
|  | 138 | +    } | 
|  | 139 | +} | 
0 commit comments