|
| 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\Azure\OpenAI; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\Small; |
| 16 | +use PHPUnit\Framework\Attributes\Test; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Symfony\AI\Platform\Bridge\Azure\OpenAI\WhisperModelClient; |
| 19 | +use Symfony\AI\Platform\Bridge\OpenAI\Whisper; |
| 20 | +use Symfony\AI\Platform\Bridge\OpenAI\Whisper\Task; |
| 21 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 22 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 23 | + |
| 24 | +#[CoversClass(WhisperModelClient::class)] |
| 25 | +#[Small] |
| 26 | +final class WhisperModelClientTest extends TestCase |
| 27 | +{ |
| 28 | + #[Test] |
| 29 | + public function itSupportsWhisperModel(): void |
| 30 | + { |
| 31 | + $client = new WhisperModelClient( |
| 32 | + new MockHttpClient(), |
| 33 | + 'test.openai.azure.com', |
| 34 | + 'whisper-deployment', |
| 35 | + '2023-12-01-preview', |
| 36 | + 'test-key' |
| 37 | + ); |
| 38 | + $model = new Whisper(); |
| 39 | + |
| 40 | + self::assertTrue($client->supports($model)); |
| 41 | + } |
| 42 | + |
| 43 | + #[Test] |
| 44 | + public function itUsesTranscriptionEndpointByDefault(): void |
| 45 | + { |
| 46 | + $httpClient = new MockHttpClient([ |
| 47 | + function ($method, $url): MockResponse { |
| 48 | + self::assertSame('POST', $method); |
| 49 | + self::assertSame('https://test.azure.com/openai/deployments/whspr/audio/transcriptions?api-version=2023-12', $url); |
| 50 | + |
| 51 | + return new MockResponse('{"text": "Hello World"}'); |
| 52 | + }, |
| 53 | + ]); |
| 54 | + |
| 55 | + $client = new WhisperModelClient($httpClient, 'test.azure.com', 'whspr', '2023-12', 'test-key'); |
| 56 | + $model = new Whisper(); |
| 57 | + $payload = ['file' => 'audio-data']; |
| 58 | + |
| 59 | + $client->request($model, $payload); |
| 60 | + |
| 61 | + self::assertSame(1, $httpClient->getRequestsCount()); |
| 62 | + } |
| 63 | + |
| 64 | + #[Test] |
| 65 | + public function itUsesTranscriptionEndpointWhenTaskIsSpecified(): void |
| 66 | + { |
| 67 | + $httpClient = new MockHttpClient([ |
| 68 | + function ($method, $url): MockResponse { |
| 69 | + self::assertSame('POST', $method); |
| 70 | + self::assertSame('https://test.azure.com/openai/deployments/whspr/audio/transcriptions?api-version=2023-12', $url); |
| 71 | + |
| 72 | + return new MockResponse('{"text": "Hello World"}'); |
| 73 | + }, |
| 74 | + ]); |
| 75 | + |
| 76 | + $client = new WhisperModelClient($httpClient, 'test.azure.com', 'whspr', '2023-12', 'test-key'); |
| 77 | + $model = new Whisper(); |
| 78 | + $payload = ['file' => 'audio-data']; |
| 79 | + $options = ['task' => Task::TRANSCRIPTION]; |
| 80 | + |
| 81 | + $client->request($model, $payload, $options); |
| 82 | + |
| 83 | + self::assertSame(1, $httpClient->getRequestsCount()); |
| 84 | + } |
| 85 | + |
| 86 | + #[Test] |
| 87 | + public function itUsesTranslationEndpointWhenTaskIsSpecified(): void |
| 88 | + { |
| 89 | + $httpClient = new MockHttpClient([ |
| 90 | + function ($method, $url): MockResponse { |
| 91 | + self::assertSame('POST', $method); |
| 92 | + self::assertSame('https://test.azure.com/openai/deployments/whspr/audio/translations?api-version=2023-12', $url); |
| 93 | + |
| 94 | + return new MockResponse('{"text": "Hello World"}'); |
| 95 | + }, |
| 96 | + ]); |
| 97 | + |
| 98 | + $client = new WhisperModelClient($httpClient, 'test.azure.com', 'whspr', '2023-12', 'test-key'); |
| 99 | + $model = new Whisper(); |
| 100 | + $payload = ['file' => 'audio-data']; |
| 101 | + $options = ['task' => Task::TRANSLATION]; |
| 102 | + |
| 103 | + $client->request($model, $payload, $options); |
| 104 | + |
| 105 | + self::assertSame(1, $httpClient->getRequestsCount()); |
| 106 | + } |
| 107 | +} |
0 commit comments