Update index.mjs
This commit is contained in:
parent
f0c9179229
commit
b5ba807c48
1 changed files with 55 additions and 33 deletions
68
index.mjs
68
index.mjs
|
@ -2,20 +2,43 @@ import readline from 'readline';
|
||||||
import Ollama from 'ollama-js-client';
|
import Ollama from 'ollama-js-client';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
||||||
let DEBUG_MODE = true
|
let DEBUG_MODE = true;
|
||||||
|
|
||||||
async function ollamaInteraction() {
|
async function ollamaInteraction() {
|
||||||
const rl = await readline.createInterface({
|
const rl = await readline.createInterface({
|
||||||
input: process.stdin,
|
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) {
|
function extractFunctionName(response) {
|
||||||
const match = response.match(/<functioncall>([^<]+)<\/functioncall>/);
|
const match = response.match(/<functioncall>([^<]+)<\/functioncall>/);
|
||||||
return match ? match[1] : '';
|
return match ? match[1] : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function generateRandomString(length = 16) {
|
function generateRandomString(length = 16) {
|
||||||
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
let result = "";
|
let result = "";
|
||||||
|
@ -26,19 +49,20 @@ async function ollamaInteraction() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// INTERNAL FUNCTIONS MAGIC BEGIN
|
// INTERNAL FUNCTIONS MAGIC BEGIN
|
||||||
async function jitsi() {
|
let internalFunctions = {
|
||||||
const id = generateRandomString()
|
jitsi: function() {
|
||||||
|
const id = generateRandomString();
|
||||||
const jitsiURL = `https://meet.jit.si/${id}`;
|
const jitsiURL = `https://meet.jit.si/${id}`;
|
||||||
console.log(jitsiURL);
|
console.log(jitsiURL);
|
||||||
return jitsiURL;
|
return jitsiURL;
|
||||||
}
|
},
|
||||||
|
search: function(q) {
|
||||||
async function search(q) {
|
q = q.replaceAll(" ", "+");
|
||||||
q = q.replaceAll(" ", "+")
|
const searchURL = `https://www.google.com/search?q=${q}&sca_upv=1`;
|
||||||
const searchURL = `https://www.google.com/search?q=${q}&sca_upv=1`
|
|
||||||
console.log(searchURL);
|
console.log(searchURL);
|
||||||
return searchURL;
|
return searchURL;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
// END OF INTERNAL FUNCTIONS MAGIC
|
// END OF INTERNAL FUNCTIONS MAGIC
|
||||||
|
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve) => {
|
||||||
|
@ -50,15 +74,14 @@ async function ollamaInteraction() {
|
||||||
url: "http://127.0.0.1:11434/api/",
|
url: "http://127.0.0.1:11434/api/",
|
||||||
}); // Ensure the model name is correct
|
}); // Ensure the model name is correct
|
||||||
|
|
||||||
const responsePreParse = await ollama.prompt(userInput)
|
const responsePreParse = await ollama.prompt(userInput);
|
||||||
const response = responsePreParse.response;
|
const response = responsePreParse.response;
|
||||||
const functionName = extractFunctionName(response);
|
const functionName = extractFunctionName(response).replaceAll("\n", '');
|
||||||
const responseWithoutFunctionCall = response.replace(/<functioncall>.*?<\/functioncall>/, '');
|
const responseWithoutFunctionCall = await response.replace(/<functioncall>.*?<\/functioncall>/, '');
|
||||||
|
|
||||||
console.log(responseWithoutFunctionCall);
|
console.log(responseWithoutFunctionCall);
|
||||||
|
|
||||||
let contentToAppend = `<USER>: ${userInput}
|
let contentToAppend = `<USER>: ${userInput}
|
||||||
|
|
||||||
<AI AGENT>: ${responseWithoutFunctionCall}`;
|
<AI AGENT>: ${responseWithoutFunctionCall}`;
|
||||||
|
|
||||||
await fs.appendFile('journal.txt', contentToAppend, async (err) => {
|
await fs.appendFile('journal.txt', contentToAppend, async (err) => {
|
||||||
|
@ -69,17 +92,16 @@ async function ollamaInteraction() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (DEBUG_MODE) {
|
if (DEBUG_MODE) { console.log(`DEBUG: RUN ${functionName}`); }
|
||||||
console.log(`DEBUG: RUN ${functionName}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
eval(function() {
|
// Call the function by name if it exists in internalFunctions
|
||||||
if (typeof functionName == 'undefined' || functionName == null) {
|
try {
|
||||||
return "";
|
if (DEBUG_MODE) { console.log(`DEBUG: ${functionName} => internalFunctions[${(functionName)})];`) }
|
||||||
} else {
|
internalFunctions[functionName]();
|
||||||
return functionName;
|
console.log("OK")
|
||||||
|
} catch (err) {
|
||||||
|
if (DEBUG_MODE) { console.log(`Error: ${err}`) }
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
resolve(); // Resolve the promise after processing
|
resolve(); // Resolve the promise after processing
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue