|
16 | 16 | use PHPUnit\Framework\TestCase;
|
17 | 17 | use Symfony\AI\Platform\Bridge\Ollama\Ollama;
|
18 | 18 | use Symfony\AI\Platform\Bridge\Ollama\OllamaClient;
|
| 19 | +use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory; |
19 | 20 | use Symfony\AI\Platform\Model;
|
| 21 | +use Symfony\AI\Platform\Result\StreamResult; |
20 | 22 | use Symfony\Component\HttpClient\MockHttpClient;
|
21 | 23 | use Symfony\Component\HttpClient\Response\JsonMockResponse;
|
| 24 | +use Symfony\Component\HttpClient\Response\MockResponse; |
22 | 25 |
|
23 | 26 | #[CoversClass(OllamaClient::class)]
|
24 | 27 | #[UsesClass(Ollama::class)]
|
@@ -87,4 +90,89 @@ public function testOutputStructureIsSupported()
|
87 | 90 | 'done' => true,
|
88 | 91 | ], $response->getData());
|
89 | 92 | }
|
| 93 | + |
| 94 | + public function testStreamingIsSupported() |
| 95 | + { |
| 96 | + $httpClient = new MockHttpClient([ |
| 97 | + new JsonMockResponse([ |
| 98 | + 'capabilities' => ['completion'], |
| 99 | + ]), |
| 100 | + new MockResponse('data: '.json_encode([ |
| 101 | + 'model' => 'llama3.2', |
| 102 | + 'created_at' => '2025-08-23T10:00:00Z', |
| 103 | + 'message' => ['role' => 'assistant', 'content' => 'Hello world'], |
| 104 | + 'done' => true, |
| 105 | + ])."\n\n", [ |
| 106 | + 'response_headers' => [ |
| 107 | + 'content-type' => 'text/event-stream', |
| 108 | + ], |
| 109 | + ]), |
| 110 | + ], 'http://127.0.0.1:1234'); |
| 111 | + |
| 112 | + $platform = PlatformFactory::create('http://127.0.0.1:1234', $httpClient); |
| 113 | + $response = $platform->invoke(new Ollama(), [ |
| 114 | + 'messages' => [ |
| 115 | + [ |
| 116 | + 'role' => 'user', |
| 117 | + 'content' => 'Say hello world', |
| 118 | + ], |
| 119 | + ], |
| 120 | + 'model' => 'llama3.2', |
| 121 | + ], [ |
| 122 | + 'stream' => true, |
| 123 | + ]); |
| 124 | + |
| 125 | + $result = $response->getResult(); |
| 126 | + |
| 127 | + $this->assertInstanceOf(StreamResult::class, $result); |
| 128 | + $this->assertInstanceOf(\Generator::class, $result->getContent()); |
| 129 | + $this->assertSame(2, $httpClient->getRequestsCount()); |
| 130 | + } |
| 131 | + |
| 132 | + public function testStreamingConverterWithDirectResponse() |
| 133 | + { |
| 134 | + $streamingData = 'data: '.json_encode([ |
| 135 | + 'model' => 'llama3.2', |
| 136 | + 'created_at' => '2025-08-23T10:00:00Z', |
| 137 | + 'message' => ['role' => 'assistant', 'content' => 'Hello'], |
| 138 | + 'done' => false, |
| 139 | + ])."\n\n". |
| 140 | + 'data: '.json_encode([ |
| 141 | + 'model' => 'llama3.2', |
| 142 | + 'created_at' => '2025-08-23T10:00:01Z', |
| 143 | + 'message' => ['role' => 'assistant', 'content' => ' world'], |
| 144 | + 'done' => true, |
| 145 | + ])."\n\n"; |
| 146 | + |
| 147 | + $mockHttpClient = new MockHttpClient([ |
| 148 | + new MockResponse($streamingData, [ |
| 149 | + 'response_headers' => [ |
| 150 | + 'content-type' => 'text/event-stream', |
| 151 | + ], |
| 152 | + ]), |
| 153 | + ]); |
| 154 | + |
| 155 | + $mockResponse = $mockHttpClient->request('GET', 'http://test.example'); |
| 156 | + $rawResult = new \Symfony\AI\Platform\Result\RawHttpResult($mockResponse); |
| 157 | + $converter = new \Symfony\AI\Platform\Bridge\Ollama\OllamaResultConverter(); |
| 158 | + |
| 159 | + $result = $converter->convert($rawResult, ['stream' => true]); |
| 160 | + |
| 161 | + $this->assertInstanceOf(StreamResult::class, $result); |
| 162 | + $this->assertInstanceOf(\Generator::class, $result->getContent()); |
| 163 | + |
| 164 | + $regularMockHttpClient = new MockHttpClient([ |
| 165 | + new JsonMockResponse([ |
| 166 | + 'model' => 'llama3.2', |
| 167 | + 'message' => ['role' => 'assistant', 'content' => 'Hello world'], |
| 168 | + 'done' => true, |
| 169 | + ]), |
| 170 | + ]); |
| 171 | + |
| 172 | + $regularMockResponse = $regularMockHttpClient->request('GET', 'http://test.example'); |
| 173 | + $regularRawResult = new \Symfony\AI\Platform\Result\RawHttpResult($regularMockResponse); |
| 174 | + $regularResult = $converter->convert($regularRawResult, ['stream' => false]); |
| 175 | + |
| 176 | + $this->assertNotInstanceOf(StreamResult::class, $regularResult); |
| 177 | + } |
90 | 178 | }
|
0 commit comments