Skip to content

How to use Speech.stream_format=sse #672

@linyongfu2013

Description

@linyongfu2013

Hi there, how to save the response data to a audio file when setting stream_format=sse, my code as below, the audio file can't be played.

And what's the meaning of stream_format=sse, does it like the steaming response with chat completion api ?

    static void speech() throws IOException {
        SpeechCreateParams createParams = SpeechCreateParams.builder()
            .model(SpeechModel.GPT_4O_MINI_TTS)
            .input("金十数据12月31日讯,商务部新闻发言人就荷兰经济大臣卡雷曼斯接受采访答记者问表示,中方已多次强调,荷方对安世半导体企业内部事务的不当行政干预,已导致全球半导体供应链危机,荷方必须对此承担全部责任。")
            .responseFormat(SpeechCreateParams.ResponseFormat.WAV)
            .streamFormat(SpeechCreateParams.StreamFormat.SSE)
            .voice(SpeechCreateParams.Voice.ALLOY)
            .build();

        InputStream inputStream = buildClient().audio()
            .withRawResponse()
            .speech()
            .create(createParams)
            .body();

        Path tempDir = Path.of("temp");
        Files.createDirectories(tempDir);
        Path outputFile = tempDir.resolve(System.currentTimeMillis() + ".wav");
        Files.copy(inputStream, outputFile, StandardCopyOption.REPLACE_EXISTING);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsdk

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions