async function processcommandsquery(voiceConnection, clientopenai, messages, mapKey) {…
- User
async function process_commands_query(voice_Connection, client_openai, messages, mapKey) { // if (txt && txt.length) { // let val = guildMap.get(mapKey); // val.text_Channel.send(user.username + ': ' + txt); try {
let val = guildMap.get(mapKey); console.log(messages); const completion = await client_openai.chat.completions.create({ model: "gemma2-9b-it", messages: messages, temperature: 0.6, max_tokens: 50, top_p: 1, stream: false, }); const response = completion.choices[0].message.content; console.log('Completion: ' + completion.choices[0].message.content); messages.push({"role": "assistant", "content": response}); const speaker = JSON.parse(fs.readFileSync('./default_speaker.json', 'utf8')); if (response === "[null]") { return null; }; if (response && response !== "[null]") { val.text_Channel.send('Олег: ' + response); await tts( voice_Connection, completion.choices[0].message.content, ); }; } catch (e) { console.log('Error processing commands: ' + e); }}
async function tts(connection, text, speaker, language, serverUrl, streamChunkSize) { const start = process.hrtime();
// Update speaker object speaker.text = text; speaker.language = language; speaker.stream_chunk_size = streamChunkSize; // reduce size for faster response, but may degrade quality // Send POST request with streaming response await axios({ method: 'post', url: ${serverUrl}/tts_stream, data: speaker, responseType: 'stream', }) .then(response => { const [seconds, nanoseconds] = process.hrtime(start); console.log(Time to make POST: ${seconds + nanoseconds / 1e9}s); const audioStream = new Readable().wrap(response.data); const audioResource = createAudioResource(audioStream); const player = createAudioPlayer(); player.play(audioResource); connection.subscribe(player); // player.on(AudioPlayerStatus.Idle, () => { // connection.destroy(); // }); }) .catch(error => { console.error('Error streaming audio:', error); });It is necessary to create a queue after each call of the tts function, in case the stream has not been played yet
- Assistant