|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpClient\OpenAI\Requests\Audio; |
| 6 | + |
| 7 | +use PhpClient\OpenAI\Helpers\MultipartValueHelper; |
| 8 | +use Saloon\Contracts\Body\HasBody; |
| 9 | +use Saloon\Data\MultipartValue; |
| 10 | +use Saloon\Enums\Method; |
| 11 | +use Saloon\Http\Request; |
| 12 | +use Saloon\Traits\Body\HasMultipartBody; |
| 13 | + |
| 14 | +use function array_filter; |
| 15 | + |
| 16 | +/** |
| 17 | + * Transcribes audio into the input language. |
| 18 | + * |
| 19 | + * @see https://platform.openai.com/docs/api-reference/audio/createTranscription |
| 20 | + * @version Relevant for 2025-02-13, OpenAI API v1 |
| 21 | + */ |
| 22 | +final class CreateTranscriptionRequest extends Request implements HasBody |
| 23 | +{ |
| 24 | + use HasMultipartBody; |
| 25 | + |
| 26 | + protected Method $method = Method::POST; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param MultipartValue|string $file The audio file (object or path) to transcribe, in one of these formats: |
| 30 | + * `flac`, `mp3`, `mp4`, `mpeg`, `mpga`, `m4a`, `ogg`, `wav`, or `webm`. |
| 31 | + * |
| 32 | + * @param string $model ID of the model to use. Only `whisper-1` is currently available. |
| 33 | + * |
| 34 | + * @param string|null $language The language of the input audio. Supplying the input language in ISO-639-1 |
| 35 | + * (e.g. en) format will improve accuracy and latency. |
| 36 | + * |
| 37 | + * @param string|null $prompt An optional text to guide the model's style or continue a previous audio segment. |
| 38 | + * The prompt should match the audio language. |
| 39 | + * |
| 40 | + * @param string|null $responseFormat The format of the output, in one of these options: |
| 41 | + * `json`, `text`, `srt`, `verbose_json`, or `vtt`. |
| 42 | + * |
| 43 | + * @param float|null $temperature The sampling temperature, between 0 and 1. Higher values like 0.8 will make |
| 44 | + * the output more random, while lower values like 0.2 will make it more focused and deterministic. |
| 45 | + * If set to 0, the model will use log probability to automatically increase the temperature until certain |
| 46 | + * thresholds are hit. |
| 47 | + * |
| 48 | + * @param array|null $timestampGranularities The timestamp granularities to populate for this transcription. |
| 49 | + * `$responseFormat` must be set `verbose_json` to use timestamp granularities. Either or both of these options |
| 50 | + * are supported: `word`, or `segment`. Note: There is no additional latency for segment timestamps, but generating |
| 51 | + * word timestamps incurs additional latency. |
| 52 | + */ |
| 53 | + public function __construct( |
| 54 | + public readonly MultipartValue|string $file, |
| 55 | + public readonly string $model, |
| 56 | + public readonly null|string $language = null, |
| 57 | + public readonly null|string $prompt = null, |
| 58 | + public readonly null|string $responseFormat = null, |
| 59 | + public readonly null|float $temperature = null, |
| 60 | + public readonly null|array $timestampGranularities = null, |
| 61 | + ) {} |
| 62 | + |
| 63 | + public function resolveEndpoint(): string |
| 64 | + { |
| 65 | + return '/v1/audio/transcriptions'; |
| 66 | + } |
| 67 | + |
| 68 | + protected function defaultBody(): array |
| 69 | + { |
| 70 | + return array_filter( |
| 71 | + array: [ |
| 72 | + 'file' => MultipartValueHelper::ensureFile(file: $this->file), |
| 73 | + 'model' => $this->model, |
| 74 | + 'language' => $this->language, |
| 75 | + 'prompt' => $this->prompt, |
| 76 | + 'response_format' => $this->responseFormat, |
| 77 | + 'temperature' => $this->temperature, |
| 78 | + 'timestamp_granularity' => $this->timestampGranularities, |
| 79 | + ], |
| 80 | + callback: static fn(mixed $value): bool => $value !== null, |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
0 commit comments