#am i passing in the correct value here to the eleven labs generate funciton? I want to generate aud
1 messages · Page 1 of 1 (latest)
AI-generated Answer for dussk_
To generate audio as the text from ChatGPT comes in, you should use the WebSocket API for real-time text-to-speech conversion. Here's an example of how you can achieve this in JavaScript:
import { WebSocket } from 'ws';
import { createReadStream } from 'fs';
import { openai } from 'openai'; // Ensure you have the correct import for OpenAI
const ELEVENLABS_API_KEY = 'your_elevenlabs_api_key';
const VOICE_ID = 'your_voice_id';
const MODEL_ID = 'eleven_english_sts_v2';
const OUTPUT_FORMAT = 'mp3_44100'; // or your desired format
async function streamChatGPTToElevenLabs() {
const chatgptStream = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: conversationChatgpt,
stream: true,
});
const uri = `wss://api.elevenlabs.io/v1/text-to-speech/${VOICE_ID}/stream-input?model_id=${MODEL_ID}`;
const websocket = new WebSocket(uri);
websocket.on('open', async () => {
websocket.send(JSON.stringify({
text: " ",
voice_settings: { stability: 0.5, similarity_boost: 0.8 },
xi_api_key: ELEVENLABS_API_KEY,
}));
for await (const chunk of chatgptStream) {
const text = chunk.choices[0].delta.content;
websocket.send(JSON.stringify({ text, try_trigger_generation: true }));
}
websocket.send(JSON.stringify({ text: "" })); // End of Sequence
});
websocket.on('message', (data) => {
const response = JSON.parse(data);
if (response.audio) {
const audioChunk = Buffer.from(response.audio, 'base64');
// Handle the audio chunk (e.g., save to file, stream to player, etc.)
}
});
websocket.on('close', () => {
console.log('WebSocket connection closed');
});
}
streamChatGPTToElevenLabs();
This script sets up a WebSocket connection to ElevenLabs and streams text from ChatGPT to generate audio in real-time. Make sure to replace placeholders like your_elevenlabs_api_key and your_voice_id with your actual API key and voice ID.
im confused as to what gets passes into the text value though, it says its looking for a string but online its using a generator function?
const elevenLabsRealTimeConversion = await elevenlabs.generate({
text: completionChoice,
voice: voices.female,
model_id: "eleven_english_sts_v2",
output_format: outputFormat,
stream: true,
});