Skip to content

Instantly share code, notes, and snippets.

@jb0gie
Last active September 8, 2023 13:11
Show Gist options
  • Save jb0gie/e72b1ee8b3566ccf2dd5254c82a4eec0 to your computer and use it in GitHub Desktop.
Save jb0gie/e72b1ee8b3566ccf2dd5254c82a4eec0 to your computer and use it in GitHub Desktop.
lanmaoa gist #1
// WRITTEN BY LANMAOA THE ILLUSTRIOUS, praise jeeves.
const fs = require('fs');
const { Configuration, OpenAIApi } = require("openai");
const path = require('path');
require('dotenv').config()
const tokens = require('gpt3-tokenizer').default
const tokenizer = new tokens({ type: 'gpt3' });
const systemPrompt = "the prompt contains files, modify the source code in the input files, dont omit anything and respond only with all the modified files without comments in the following format: filename\nfilecontents\n\n";
const transformationInstruction = process.argv[2] + ' \n also make it easy to read ';
console.log(transformationInstruction)
async function generateJsonData() {
try {
const srcDirectory = './src'; // Specify the source directory path
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Read all files in the source directory
const jsonEntries = {};
const readsrcdirectory = async (srcDirectory) => {
const files = fs.readdirSync(srcDirectory);
for (const filename of files) {
const filePath = path.join(srcDirectory, filename);
const fileContent = fs.readFileSync(filePath, 'utf8');
let result;
if (filePath.endsWith('.js')) {
const Terser = require('terser');
result = (await Terser.minify(fileContent, {
mangle: false,
compress: false,
format:{comments:'all'}
})).code;
}
if (filePath.endsWith('.ejs') || filePath.endsWith('.html')) {
var minify = require('html-minifier').minify;
const options = {
includeAutoGeneratedTags: true,
removeAttributeQuotes: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortClassName: true,
useShortDoctype: true,
collapseWhitespace: true,
minifyJS: true
};
result = minify(fileContent, options);
}
jsonEntries[srcDirectory + '/' + filename] = result
}
}
await readsrcdirectory(srcDirectory);
//await readsrcdirectory('./views')
// Save the generated JSON data to a file
const generatedJsonData = Object.keys(jsonEntries).map(a => `${a}:\n${jsonEntries[a]}\n\n`).join(''); // Pretty-print JSON
let total = 1
const message = `${transformationInstruction}\n\n${generatedJsonData}`;
total += tokenizer.encode(message).bpe.length + tokenizer.encode(systemPrompt).bpe.length + 15
const messages = [
{ "role": "system", "content": systemPrompt },
{ "role": "user", "content": message },
]
console.log(messages);
const question = {
model: 'gpt-4',
messages,
temperature: 0.1,
max_tokens: 8192 - total,
top_p: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
}
const response = await openai.createChatCompletion(
question
)
const text = response.data.choices[0].message.content.trim();
fs.writeFileSync('transformed.out', text)
function writeFilesFromStr(str) {
const regex = /([\.\/\w]*):\n```(?:[a-z]*\n)?([^`]*?)```/g;
let result;
while ((result = regex.exec(str)) !== null) {
const file = result[1];
const content = result[2];
// Extract the directory from the file path
const directory = path.dirname(file);
// Create the directory, if it doesn't already exist
fs.mkdirSync(directory, { recursive: true });
fs.writeFile(file.replace('./','./out/'), content, (err) => {
if (err) {
console.error(`Error writing file ${file}:`, err);
} else {
console.log(`File ${file} written successfully.`);
}
});
}
}
writeFilesFromStr(text)
} catch (error) {
console.error(error)
console.error('Error:', error.response.data);
}
}
generateJsonData();
@jb0gie
Copy link
Author

jb0gie commented Sep 8, 2023

Lanmaoa — Today at 09:27
I made a 'transform an app using a prompt in its entirety' tool for openai lol
you can point it to your folders and tell it what you want your program to do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment