From 01a1351cfb10b6c02de58749181bf6ff579c7ecf Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sun, 30 Apr 2023 20:06:14 -0400 Subject: [PATCH] Add: TTS=1 espeak with queue system to read back conversation --- converse-llama.js | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/converse-llama.js b/converse-llama.js index 6c27a67..02f06ca 100644 --- a/converse-llama.js +++ b/converse-llama.js @@ -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;