|
| 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\LiteLlm; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\AI\Platform\Bridge\LiteLlm\ModelClient; |
| 16 | +use Symfony\AI\Platform\Model; |
| 17 | +use Symfony\Component\HttpClient\EventSourceHttpClient; |
| 18 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 19 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 20 | + |
| 21 | +class ModelClientTest extends TestCase |
| 22 | +{ |
| 23 | + public function testItIsSupportingTheCorrectModel() |
| 24 | + { |
| 25 | + $client = new ModelClient(new MockHttpClient(), 'http://localhost:4000'); |
| 26 | + |
| 27 | + $this->assertTrue($client->supports(new Model('test-model'))); |
| 28 | + } |
| 29 | + |
| 30 | + public function testItIsExecutingTheCorrectRequest() |
| 31 | + { |
| 32 | + $resultCallback = static function (string $method, string $url, array $options): MockResponse { |
| 33 | + self::assertSame('POST', $method); |
| 34 | + self::assertSame('http://localhost:4000/v1/chat/completions', $url); |
| 35 | + self::assertSame( |
| 36 | + '{"model":"test-model","messages":[{"role":"user","content":"Hello, world!"}]}', |
| 37 | + $options['body'] |
| 38 | + ); |
| 39 | + |
| 40 | + return new MockResponse(); |
| 41 | + }; |
| 42 | + |
| 43 | + $httpClient = new MockHttpClient([$resultCallback]); |
| 44 | + $client = new ModelClient($httpClient, 'http://localhost:4000'); |
| 45 | + |
| 46 | + $payload = [ |
| 47 | + 'model' => 'test-model', |
| 48 | + 'messages' => [ |
| 49 | + ['role' => 'user', 'content' => 'Hello, world!'], |
| 50 | + ], |
| 51 | + ]; |
| 52 | + |
| 53 | + $client->request(new Model('test-model'), $payload); |
| 54 | + } |
| 55 | + |
| 56 | + public function testItMergesOptionsWithPayload() |
| 57 | + { |
| 58 | + $resultCallback = static function (string $method, string $url, array $options): MockResponse { |
| 59 | + self::assertSame('POST', $method); |
| 60 | + self::assertSame('http://localhost:4000/v1/chat/completions', $url); |
| 61 | + self::assertSame( |
| 62 | + '{"temperature":0.7,"model":"test-model","messages":[{"role":"user","content":"Hello, world!"}]}', |
| 63 | + $options['body'] |
| 64 | + ); |
| 65 | + |
| 66 | + return new MockResponse(); |
| 67 | + }; |
| 68 | + |
| 69 | + $httpClient = new MockHttpClient([$resultCallback]); |
| 70 | + $client = new ModelClient($httpClient, 'http://localhost:4000'); |
| 71 | + |
| 72 | + $payload = [ |
| 73 | + 'model' => 'test-model', |
| 74 | + 'messages' => [ |
| 75 | + ['role' => 'user', 'content' => 'Hello, world!'], |
| 76 | + ], |
| 77 | + ]; |
| 78 | + |
| 79 | + $client->request(new Model('test-model'), $payload, ['temperature' => 0.7]); |
| 80 | + } |
| 81 | + |
| 82 | + public function testItUsesEventSourceHttpClient() |
| 83 | + { |
| 84 | + $httpClient = new MockHttpClient(); |
| 85 | + $client = new ModelClient($httpClient, 'http://localhost:4000'); |
| 86 | + |
| 87 | + $reflection = new \ReflectionProperty($client, 'httpClient'); |
| 88 | + |
| 89 | + $this->assertInstanceOf(EventSourceHttpClient::class, $reflection->getValue($client)); |
| 90 | + } |
| 91 | + |
| 92 | + public function testItKeepsExistingEventSourceHttpClient() |
| 93 | + { |
| 94 | + $eventSourceHttpClient = new EventSourceHttpClient(new MockHttpClient()); |
| 95 | + $client = new ModelClient($eventSourceHttpClient, 'http://localhost:4000'); |
| 96 | + |
| 97 | + $reflection = new \ReflectionProperty($client, 'httpClient'); |
| 98 | + |
| 99 | + $this->assertSame($eventSourceHttpClient, $reflection->getValue($client)); |
| 100 | + } |
| 101 | +} |
0 commit comments