|
| 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\Cerebras; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\Small; |
| 16 | +use PHPUnit\Framework\Attributes\TestWith; |
| 17 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Symfony\AI\Platform\Bridge\Cerebras\Model; |
| 20 | +use Symfony\AI\Platform\Bridge\Cerebras\ModelClient; |
| 21 | +use Symfony\AI\Platform\Exception\InvalidArgumentException; |
| 22 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 23 | +use Symfony\Component\HttpClient\Response\JsonMockResponse; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Junaid Farooq <[email protected]> |
| 27 | + */ |
| 28 | +#[CoversClass(ModelClient::class)] |
| 29 | +#[UsesClass(Model::class)] |
| 30 | +#[Small] |
| 31 | +class ModelClientTest extends TestCase |
| 32 | +{ |
| 33 | + public function testItDoesNotAllowAnEmptyKey() |
| 34 | + { |
| 35 | + $this->expectException(InvalidArgumentException::class); |
| 36 | + $this->expectExceptionMessage('The API key must not be empty.'); |
| 37 | + |
| 38 | + new ModelClient(new MockHttpClient(), ''); |
| 39 | + } |
| 40 | + |
| 41 | + #[TestWith(['api-key-without-prefix'])] |
| 42 | + #[TestWith(['pk-api-key'])] |
| 43 | + #[TestWith(['SK-api-key'])] |
| 44 | + #[TestWith(['skapikey'])] |
| 45 | + #[TestWith(['sk api-key'])] |
| 46 | + #[TestWith(['sk'])] |
| 47 | + public function testItVerifiesIfTheKeyStartsWithCsk(string $invalidApiKey) |
| 48 | + { |
| 49 | + $this->expectException(InvalidArgumentException::class); |
| 50 | + $this->expectExceptionMessage('The API key must start with "csk-".'); |
| 51 | + |
| 52 | + new ModelClient(new MockHttpClient(), $invalidApiKey); |
| 53 | + } |
| 54 | + |
| 55 | + public function testItSupportsTheCorrectModel() |
| 56 | + { |
| 57 | + $client = new ModelClient(new MockHttpClient(), 'csk-1234567890abcdef'); |
| 58 | + |
| 59 | + self::assertTrue($client->supports(new Model(Model::GPT_OSS_120B))); |
| 60 | + } |
| 61 | + |
| 62 | + public function testItSuccessfullyInvokesTheModel() |
| 63 | + { |
| 64 | + $expectedResponse = [ |
| 65 | + 'model' => 'llama-3.3-70b', |
| 66 | + 'input' => [ |
| 67 | + 'messages' => [ |
| 68 | + ['role' => 'user', 'content' => 'Hello, world!'], |
| 69 | + ], |
| 70 | + ], |
| 71 | + 'temperature' => 0.5, |
| 72 | + ]; |
| 73 | + $httpClient = new MockHttpClient( |
| 74 | + new JsonMockResponse($expectedResponse), |
| 75 | + ); |
| 76 | + |
| 77 | + $client = new ModelClient($httpClient, 'csk-1234567890abcdef'); |
| 78 | + |
| 79 | + $payload = [ |
| 80 | + 'messages' => [ |
| 81 | + ['role' => 'user', 'content' => 'Hello, world!'], |
| 82 | + ], |
| 83 | + ]; |
| 84 | + |
| 85 | + $result = $client->request(new Model(Model::LLAMA_3_3_70B), $payload); |
| 86 | + $data = $result->getData(); |
| 87 | + $info = $result->getObject()->getInfo(); |
| 88 | + |
| 89 | + self::assertNotEmpty($data); |
| 90 | + self::assertNotEmpty($info); |
| 91 | + self::assertSame('POST', $info['http_method']); |
| 92 | + self::assertSame('https://api.cerebras.ai/v1/chat/completions', $info['url']); |
| 93 | + self::assertSame($expectedResponse, $data); |
| 94 | + } |
| 95 | +} |
0 commit comments