kode/index.js

115 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-03-15 13:48:14 -05:00
import readline from 'readline';
import Ollama from 'ollama-js-client';
import spawn from 'child_process';
2024-03-15 15:06:44 -05:00
let potentialAnswers = [];
2024-03-15 13:48:14 -05:00
function prompt(q) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(`${q}`, function (a) {
rl.close();
resolve(a);
});
});
}
2024-03-15 16:53:55 -05:00
let problem = await prompt("<What's the project? (no external libs or reqs)>: ");
2024-03-15 13:48:14 -05:00
2024-03-15 16:53:55 -05:00
let lang = await prompt("<What's the lang? (js, python, ppython [panda3d python])>: ");
2024-03-15 13:48:14 -05:00
2024-03-15 16:53:55 -05:00
let generations = await prompt("<How many generations? (more generations = more memory used and more time!)>: ");
generations = Math.ceil(Number(generations))
console.log("coding, this will take a bit of time!");
2024-03-15 13:48:14 -05:00
function langExec(langCode) {
if (lang == "js") {
return eval(langCode);
} else if (lang == "python") {
const pythonProcess = spawn('python', ['-c', langCode]);
// Handle stderr data from the Python process
return pythonProcess.stderr.on('data', (data) => {
return Error(`${data}`);
});
} else if (lang == "ppython") {
const ppythonProcess = spawn('ppython', ['-c', langCode]);
// Handle stderr data from the Python process
return ppythonProcess.stderr.on('data', (data) => {
return Error(`${data}`);
});
} else {
console.error("Language command not found!")
}
}
function getLangID() {
if (lang == "ppython") {
return "panda3d python"
} else {
return lang;
}
}
2024-03-15 16:38:50 -05:00
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
2024-03-15 13:48:14 -05:00
async function main() {
2024-03-15 16:38:50 -05:00
const instance = new Ollama({
model: "codellama",
url: "http://127.0.0.1:11434/api/",
});
2024-03-15 16:40:11 -05:00
let answer = await instance.prompt(`${problem} - This must be coded in pure ${getLangID()}, no external libraries or requirements. Please provide the code, the full code, and nothing but the code. No chit-chat, no markdown, just code.`);
2024-03-15 13:48:14 -05:00
let problemSolved = false;
while (problemSolved == false) {
try {
2024-03-15 16:45:10 -05:00
let answerParsed = replaceAll(answer.response, "```javascript", "")
2024-03-15 16:38:50 -05:00
answerParsed = replaceAll(answerParsed, "```", "")
2024-03-15 13:48:14 -05:00
langExec(answerParsed);
problemSolved = true;
2024-03-15 16:38:50 -05:00
console.log(answerParsed)
2024-03-15 16:45:10 -05:00
return answerParsed;
2024-03-15 13:48:14 -05:00
} catch (error) {
answer = await instance.prompt(`There was an error: ${error.message}. Please only provide the code, the full code, and nothing but the code. No chit-chat, no markdown, just code. Also, make sure it's written in ${getLangID()} without any libraries besides included.`)
}
}
2024-03-15 16:45:10 -05:00
return 1;
2024-03-15 13:48:14 -05:00
}
2024-03-15 15:06:44 -05:00
async function aThousand() {
2024-03-15 16:40:59 -05:00
const instance = new Ollama({
model: "codellama",
url: "http://127.0.0.1:11434/api/",
});
2024-03-15 15:06:44 -05:00
let potentialAnswersQuestion = `Which answer is best suited for ${problem}?
If there are two or more answers that are about as equal, but one has lower quality code, choose the one with higher quality code.
2024-03-15 15:46:28 -05:00
Pick ONLY ONE ANSWER. MUST BE PROGRAMMED IN THE LANGUAGE ${getLangID}!
2024-03-15 15:06:44 -05:00
Answers:
`
2024-03-15 16:53:55 -05:00
for (let i = 0; i < generations; i++) {
2024-03-15 16:45:10 -05:00
console.log(`Generation ${i + 1}`)
2024-03-15 15:06:44 -05:00
let potentialAnswer = await main();
potentialAnswers.push(potentialAnswer)
}
potentialAnswers.forEach((answer, index) => {
potentialAnswersQuestion += `
----
Answer ${index + 1}:
${answer}
----
`;
});
let finalAnswer = await instance.prompt(`${potentialAnswersQuestion}`)
let finalAnswerParsed = finalAnswer.response;
return finalAnswerParsed;
}
let a = await aThousand();
console.log(a)