1
mirror of https://github.com/cocktailpeanut/dalai synced 2024-11-20 23:07:32 +01:00

update the BE to accomodate for custom and default prompts, so future updates to default prompts won't overwrite the custom ones

This commit is contained in:
keldenl 2023-03-21 13:10:38 -07:00
parent 3ac56856d6
commit 9766862997
3 changed files with 38 additions and 12 deletions

View File

@ -17,21 +17,46 @@ const start = (port, home) => {
res.render("index") res.render("index")
}) })
app.get("/prompts", (req, res) => { app.get("/prompts", (req, res) => {
const promptsDir = path.join(__dirname, "prompts"); const promptsDirs = [
fs.readdir(promptsDir, (err, files) => { path.join(__dirname, "prompts"),
if (err) { path.join(__dirname, "prompts", "custom")
console.error(err); ];
return res.status(500).json({ error: "Internal server error" }); const allPrompts = [];
let numDirsChecked = 0;
promptsDirs.forEach(promptsDir => {
if (fs.existsSync(promptsDir) && fs.statSync(promptsDir).isDirectory()) {
fs.readdir(promptsDir, (err, files) => {
if (err) {
console.error(err);
return res.status(500).json({ error: "Internal server error" });
}
const prompts = files.filter(file => !fs.statSync(path.join(promptsDir, file)).isDirectory())
.map((file) => {
const promptName = path.basename(file, ".txt");
const promptValue = fs.readFileSync(path.join(promptsDir, file), "utf8");
return { name: promptName, value: promptValue };
});
allPrompts.push(...prompts);
console.log(`Added ${prompts.length} prompts from ${promptsDir}`);
numDirsChecked++;
if (numDirsChecked === promptsDirs.length) {
res.json(allPrompts);
console.log(`Returning ${allPrompts.length} prompts`);
}
});
} else {
console.error(`Directory not found: ${promptsDir}`);
numDirsChecked++;
if (numDirsChecked === promptsDirs.length) {
res.json(allPrompts);
console.log(`Returning ${allPrompts.length} prompts`);
}
} }
const prompts = files.map((file) => {
const promptName = path.basename(file, ".txt");
const promptValue = fs.readFileSync(path.join(promptsDir, file), "utf8");
return { name: promptName, value: promptValue };
});
res.json(prompts);
}); });
}); });
httpServer.listen(port, () => { httpServer.listen(port, () => {
console.log(`Server running on http://localhost:${port}/`) console.log(`Server running on http://localhost:${port}/`)
}) })

View File

@ -1 +1,2 @@
The expected response for a highly intelligent chatbot to "<PROMPT>" is " The expected response for a highly intelligent chatbot to "<PROMPT>" is
"