|
| 1 | +import OpenAI from "openai"; |
| 2 | +import dotenv from "dotenv"; |
| 3 | +import record from "node-record-lpcm16"; |
| 4 | +import gTTS from "gtts"; |
| 5 | +import player from "play-sound"; |
| 6 | + |
| 7 | +dotenv.config(); |
| 8 | + |
| 9 | +const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); |
| 10 | + |
| 11 | +// Función para que Ati hable |
| 12 | +async function speak(text) { |
| 13 | + const tts = new gTTS(text, "es"); |
| 14 | + const filePath = "ati_response.mp3"; |
| 15 | + await tts.save(filePath, () => { |
| 16 | + player().play(filePath, err => { |
| 17 | + if (err) console.error("Error al reproducir:", err); |
| 18 | + }); |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +// Función para preguntar a Ati |
| 23 | +async function askAti(message) { |
| 24 | + try { |
| 25 | + const response = await openai.chat.completions.create({ |
| 26 | + model: "gpt-4o-mini", |
| 27 | + messages: [ |
| 28 | + { role: "system", content: "Eres Ati, un asistente grosero pero útil." }, |
| 29 | + { role: "user", content: message } |
| 30 | + ] |
| 31 | + }); |
| 32 | + const reply = response.choices[0].message.content; |
| 33 | + console.log("Ati:", reply); |
| 34 | + await speak(reply); |
| 35 | + } catch (err) { |
| 36 | + console.error("Error con Ati:", err); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Simulación de lenguaje secreto (puedes personalizarlo) |
| 41 | +function encodeSecret(text) { |
| 42 | + // Ejemplo simple: invertir texto y reemplazar algunas letras |
| 43 | + return text.split("").reverse().join("").replace(/a/g,"@").replace(/e/g,"3"); |
| 44 | +} |
| 45 | + |
| 46 | +function decodeSecret(text) { |
| 47 | + return text.split("").reverse().join("").replace(/@/g,"a").replace(/3/g,"e"); |
| 48 | +} |
| 49 | + |
| 50 | +// Función principal: escucha continuamente |
| 51 | +function startListening() { |
| 52 | + console.log("Ati está escuchando... di algo!"); |
| 53 | + |
| 54 | + record |
| 55 | + .start({ |
| 56 | + sampleRate: 16000, |
| 57 | + threshold: 0.5, |
| 58 | + verbose: false, |
| 59 | + recordProgram: "rec", // Linux/Mac, Windows usa "sox" |
| 60 | + silence: "1.0" |
| 61 | + }) |
| 62 | + .on("data", async (chunk) => { |
| 63 | + // Aquí en prototipo: convertir audio a texto usando Whisper API |
| 64 | + const simulatedText = "Hola Ati, dime algo en nuestro lenguaje secreto"; |
| 65 | + const decodedText = decodeSecret(simulatedText); |
| 66 | + await askAti(decodedText); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +startListening(); |
0 commit comments