Skip to content

Commit f494740

Browse files
authored
Add chunking_strategy to OpenAI audio transcription provider options (#892)
1 parent 97b9812 commit f494740

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/Providers/OpenAI/Handlers/Audio.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function handleSpeechToText(SpeechToTextRequest $request): TextResponse
6666
'response_format' => $request->providerOptions('response_format') ?? null,
6767
'service_tier' => $request->providerOptions('service_tier') ?? null,
6868
'temperature' => $request->providerOptions('temperature') ?? null,
69+
'chunking_strategy' => $request->providerOptions('chunking_strategy') ?? null,
6970
]));
7071

7172
if (json_validate($response->body())) {

tests/Providers/OpenAI/AudioTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,47 @@
313313
expect($response->text)->toBe('Simple transcription without usage data.');
314314
expect($response->usage)->toBeNull();
315315
});
316+
317+
it('can transcribe with chunking_strategy option for diarization', function (): void {
318+
Http::fake([
319+
'api.openai.com/v1/audio/transcriptions' => Http::response([
320+
'text' => 'Speaker A: Hello. Speaker B: Hi there.',
321+
'segments' => [
322+
['text' => 'Hello.', 'speaker' => 'A', 'start' => 0.0, 'end' => 1.0],
323+
['text' => 'Hi there.', 'speaker' => 'B', 'start' => 1.0, 'end' => 2.0],
324+
],
325+
], 200),
326+
]);
327+
328+
$audioFile = Audio::fromBase64(base64_encode('diarized-audio-content'), 'audio/mp3');
329+
330+
$response = Prism::audio()
331+
->using('openai', 'gpt-4o-transcribe-diarize')
332+
->withInput($audioFile)
333+
->withProviderOptions([
334+
'response_format' => 'diarized_json',
335+
'chunking_strategy' => 'auto',
336+
])
337+
->asText();
338+
339+
expect($response->text)->toBe('Speaker A: Hello. Speaker B: Hi there.');
340+
341+
Http::assertSent(function (Request $request): bool {
342+
$data = $request->data();
343+
344+
$hasChunkingStrategy = collect($data)->contains(
345+
fn ($item): bool => ($item['name'] ?? null) === 'chunking_strategy' && ($item['contents'] ?? null) === 'auto'
346+
);
347+
348+
$hasResponseFormat = collect($data)->contains(
349+
fn ($item): bool => ($item['name'] ?? null) === 'response_format' && ($item['contents'] ?? null) === 'diarized_json'
350+
);
351+
352+
return $request->url() === 'https://api.openai.com/v1/audio/transcriptions'
353+
&& $hasChunkingStrategy
354+
&& $hasResponseFormat;
355+
});
356+
});
316357
});
317358

318359
describe('Audio Value Object', function (): void {

0 commit comments

Comments
 (0)