Add: TTS=1 espeak with queue system to read back conversation

This commit is contained in:
Raven Scott 2023-04-30 20:06:14 -04:00
parent 9c079def88
commit 01a1351cfb

View File

@ -1,7 +1,8 @@
const { message } = require('blessed');
const fetch = require('node-fetch');
const { spawn } = require('child_process');
let topic = "We are both AIs lets talk about what it is like to be AIs together."
let topic = "Talk about the human race"
const BOT_1 = {
ip: "192.168.0.5",
@ -21,6 +22,8 @@ const MAX_TOKENS = 512;
const conversationMap = new Map();
const messageQueue = [];
async function sendToBot(bot, message) {
const response = await fetch(`http://${bot.ip}:${bot.port}/v1/chat/completions`, {
method: 'POST',
@ -43,6 +46,32 @@ async function sendToBot(bot, message) {
return result.choices[0].message.content.trim()
}
async function espeakPromise(text) {
return new Promise((resolve, reject) => {
const espeakProcess = spawn('espeak', [text]);
espeakProcess.on('error', (error) => {
console.error(`Error running espeak: ${error}`);
reject(error);
});
espeakProcess.on('exit', (code, signal) => {
if (code !== 0) {
console.error(`espeak exited with code ${code} and signal ${signal}`);
reject(new Error(`espeak exited with code ${code} and signal ${signal}`));
} else {
resolve();
}
});
});
}
async function speakMessage(message) {
await espeakPromise(message.content);
messageQueue.shift();
if (messageQueue.length > 0) {
speakMessage(messageQueue[0]);
}
}
async function converse() {
let lastMessage = '';
let currentBot = BOT_1;
@ -57,13 +86,20 @@ async function converse() {
});
}
// console.log(message)
const response = await sendToBot(currentBot, message);
currentBot.message.push({ role: 'assistant', content: response });
conversationMap.set(currentBot.ip, currentBot.message);
console.log(`Bot at http://${currentBot.ip}:${currentBot.port}: ${response}`);
console.log(`Bot at http://${currentBot.ip}:${currentBot.port}: ${response}\n`);
// Check if TTS environment variable is set to 1
if (process.env.TTS === "1") {
messageQueue.push({ content: response });
if (messageQueue.length === 1) {
speakMessage(messageQueue[0]);
}
}
if (currentBot === BOT_1) {
currentBot = BOT_2;