From b5ba807c48ad3127fbd545e051c367bb0424c651 Mon Sep 17 00:00:00 2001 From: Sam Sneed <163201376+sam-sneed@users.noreply.github.com> Date: Tue, 30 Apr 2024 04:03:13 +0000 Subject: [PATCH] Update index.mjs --- index.mjs | 88 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 33 deletions(-) diff --git a/index.mjs b/index.mjs index b5a1e6a..3817838 100644 --- a/index.mjs +++ b/index.mjs @@ -2,20 +2,43 @@ import readline from 'readline'; import Ollama from 'ollama-js-client'; import fs from 'fs'; -let DEBUG_MODE = true +let DEBUG_MODE = true; async function ollamaInteraction() { const rl = await readline.createInterface({ input: process.stdin, - output: process.stdout + output: process.stdout, }); + function isUndefined(value) { + if (typeof value == undefined || value == null) { + return true; + } else { + return false; + } + } + + function ifUndefinedReturnBlankStr(value) { + if (isUndefined(value)) { + return ""; + } else { + return value; + } + } + + function ifUndefinedReturnBlankFunction(value) { + if (isUndefined(value)) { + return Function(); + } else { + return value; + } + } + function extractFunctionName(response) { const match = response.match(/([^<]+)<\/functioncall>/); return match ? match[1] : ''; } - function generateRandomString(length = 16) { const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; let result = ""; @@ -25,21 +48,22 @@ async function ollamaInteraction() { return result; } - //INTERNAL FUNCTIONS MAGIC BEGIN - async function jitsi() { - const id = generateRandomString() - const jitsiURL = `https://meet.jit.si/${id}`; - console.log(jitsiURL); - return jitsiURL; - } - - async function search(q) { - q = q.replaceAll(" ", "+") - const searchURL = `https://www.google.com/search?q=${q}&sca_upv=1` - console.log(searchURL); - return searchURL; - } - //END OF INTERNAL FUNCTIONS MAGIC + // INTERNAL FUNCTIONS MAGIC BEGIN + let internalFunctions = { + jitsi: function() { + const id = generateRandomString(); + const jitsiURL = `https://meet.jit.si/${id}`; + console.log(jitsiURL); + return jitsiURL; + }, + search: function(q) { + q = q.replaceAll(" ", "+"); + const searchURL = `https://www.google.com/search?q=${q}&sca_upv=1`; + console.log(searchURL); + return searchURL; + } + }; + // END OF INTERNAL FUNCTIONS MAGIC return new Promise(async (resolve) => { rl.question("User: ", async (userInput) => { @@ -50,16 +74,15 @@ async function ollamaInteraction() { url: "http://127.0.0.1:11434/api/", }); // Ensure the model name is correct - const responsePreParse = await ollama.prompt(userInput) + const responsePreParse = await ollama.prompt(userInput); const response = responsePreParse.response; - const functionName = extractFunctionName(response); - const responseWithoutFunctionCall = response.replace(/.*?<\/functioncall>/, ''); + const functionName = extractFunctionName(response).replaceAll("\n", ''); + const responseWithoutFunctionCall = await response.replace(/.*?<\/functioncall>/, ''); console.log(responseWithoutFunctionCall); let contentToAppend = `: ${userInput} - - : ${responseWithoutFunctionCall}`; + : ${responseWithoutFunctionCall}`; await fs.appendFile('journal.txt', contentToAppend, async (err) => { if (err) { @@ -69,17 +92,16 @@ async function ollamaInteraction() { } }); - if (DEBUG_MODE) { - console.log(`DEBUG: RUN ${functionName}`) - } + if (DEBUG_MODE) { console.log(`DEBUG: RUN ${functionName}`); } - eval(function() { - if (typeof functionName == 'undefined' || functionName == null) { - return ""; - } else { - return functionName; - } - }) + // Call the function by name if it exists in internalFunctions + try { + if (DEBUG_MODE) { console.log(`DEBUG: ${functionName} => internalFunctions[${(functionName)})];`) } + internalFunctions[functionName](); + console.log("OK") + } catch (err) { + if (DEBUG_MODE) { console.log(`Error: ${err}`) } + } resolve(); // Resolve the promise after processing });