Ugeen
Ugeen

Reputation: 11

how to interrupt audio playback in voximplant script

example with chatgpt voximplant. chatgpt says something to the caller for a long time, without waiting for him to finish, how to interrupt him, stop him? for next processing. I can't interrupt GPT during a conversation and I didn't find anything in the documentation either

Upvotes: 1

Views: 80

Answers (1)

Avicus Delacroix
Avicus Delacroix

Reputation: 151

There are two ways of fixing long chatgpt phrases.

The easiest way, you can make the answers shorter, by specifying the max_tokens parameter in the scenario example in the original article.

Or you can actually implement the possibility to interrupt speech via common Voximplant's tools.

Take the original scenario from the article. First declare a variable to know if the speech is playing somewhere in the beginning:

var playing = false

Then modify the original scenario code. You need to keep on voice recognition and process the InterimResults. Here is the example. It's not the full scenario, it's only the modified part:

asr = VoxEngine.createASR({
        profile: ASRProfileList.Google.en_US,
        interimResults: true,
        singleUtterance: true
    })
    // Process InterimResult from ASR
    asr.addEventListener(ASREvents.InterimResult, async(e) => {
        if (playing) {
            player.stop()
            playing = false 
        }
    })
    // Process ASR result
    asr.addEventListener(ASREvents.Result, async (e) => {
        // Messages array is used for the conversation context according to the OpenAI API
        messages.push({ "role": "user", "content": e.text })
        Logger.write("Sending data to the OpenAI endpoint")
        // Add some "telemetry" to understand how long it took OpenAI to process the request
        let ts1 = Date.now();
        var res = await requestCompletion()
        let ts2 = Date.now();
        Logger.write("Request complete in " + (ts2 - ts1) + " ms")

        if (res.code == 200) {
            let jsData = JSON.parse(res.text)
            player = VoxEngine.createTTSPlayer(jsData.choices[0].message.content,
                {
                    language: defaultVoice,
                    progressivePlayback: true
                })
            player.sendMediaTo(call)
            player.addMarker(-300)
            // Push the message to the conversation array
            messages.push({ role: "assistant", content: jsData.choices[0].message.content })
        } else {
            Logger.write(res.code + " : " + res.text)
            player = VoxEngine.createTTSPlayer('Sorry, something went wrong, can you repeat please?',
                {
                    language: defaultVoice,
                    progressivePlayback: true
                })
            player.sendMediaTo(call)
            player.addMarker(-300)
            playing = true
        }
        player.addEventListener(PlayerEvents.PlaybackMarkerReached, (ev) => {
            player.removeEventListener(PlayerEvents.PlaybackMarkerReached)
            call.sendMediaTo(asr)
            playing = false
        })
    })

Then interruption should work. If you provide a new request to the chatgpt, it should stop the playback and give you the response to your new request.

Upvotes: 0

Related Questions