diff --git a/src/agent/speak.js b/src/agent/speak.js index 2402647..7f453df 100644 --- a/src/agent/speak.js +++ b/src/agent/speak.js @@ -37,18 +37,29 @@ $s.Speak('${txt.replace(/'/g,"''")}'); $s.Dispose()"` }); } else { + + function getModelUrl(prov) { + if (prov === 'pollinations') { + return 'https://text.pollinations.ai/openai' + } else if (prov === 'openai') { + return 'https://api.openai.com/v1/audio/speech' + } else { + // fallback + return 'https://api.openai.com/v1/audio/speech' + } + } + // remote audio provider let prov, mdl, voice, url; if (typeof model === "string") { [prov, mdl, voice] = model.split('/'); - url = "https://text.pollinations.ai/openai"; + url = getModelUrl(prov); } else { prov = model.api; mdl = model.model; voice = model.voice; - url = model.url || "https://text.pollinations.ai/openai"; + url = model.url || getModelUrl(prov); } - if (prov !== 'pollinations') throw new Error(`Unknown provider: ${prov}`); try { let audioData = await sendAudioRequest(txt, mdl, voice, url); diff --git a/src/models/gpt.js b/src/models/gpt.js index e8e5c5c..c7d2e86 100644 --- a/src/models/gpt.js +++ b/src/models/gpt.js @@ -89,5 +89,30 @@ export class GPT { } +export async function sendAudioRequest(text, model, voice, url) { + const payload = { + model: model, + voice: voice, + input: text + } + let audioData = null; + + let config = {}; + + if (url) + config.baseURL = url; + + if (hasKey('OPENAI_ORG_ID')) + config.organization = getKey('OPENAI_ORG_ID'); + + config.apiKey = getKey('OPENAI_API_KEY'); + + openai = new OpenAIApi(config); + + const mp3 = await openai.audio.speech.create(payload); + const buffer = Buffer.from(await mp3.arrayBuffer()); + const base64 = buffer.toString("base64"); + return base64; +}