#Hi. How do I stream openAI chat completions output to elvenlabs text to speech in typescript from t
1 messages · Page 1 of 1 (latest)
I'm sorry, but the information provided doesn't include a specific example of streaming OpenAI chat completions output to ElevenLabs text-to-speech in TypeScript from the browser-side. However, I can provide a general guide on how you might approach this based on the information available.
Firstly, you would need to establish a connection to the ElevenLabs text-to-speech API via WebSockets. Here's a basic example of how you might do this:
import * as WebSocket from 'ws';
const voiceId = 'your_voice_id_here';
const modelId = 'eleven_turbo_v2_5';
const uri = `wss://api.elevenlabs.io/v1/text-to-speech/${voiceId}/stream-input?model_id=${modelId}`;
const ws = new WebSocket(uri);
ws.on('open', function open() {
// Connection is established
});
Next, you would need to send the text to the API. You can do this by sending a JSON stringified object with the text and other necessary parameters:
ws.send(JSON.stringify({
"text": "Your text here",
"voice_settings": {"stability": 0.5, "similarity_boost": 0.8},
"xi_api_key": "your_elevenlabs_api_key_here",
}));
You would then listen for incoming messages from the WebSocket connection, which would contain the audio data:
ws.on('message', function incoming(data) {
const message = JSON.parse(data);
if (message.audio) {
// This is the audio data
}
});
To stream the output from OpenAI's GPT model to the ElevenLabs API, you would need to make a request to the OpenAI API to get the chat completions, and then send each completion to the ElevenLabs API as it comes in. Unfortunately, the information provided doesn't include a specific example of how to do this in TypeScript.
Remember to close the WebSocket connection when you're done:
ws.close();
Please note that this is a simplified example and you may need to adjust it to fit your specific use case. Also, be aware that you would need to handle errors and edge cases in your actual implementation.