Skip to content

Commit

Permalink
Merge pull request cocktailpeanut#199 from keldenl/soft-prompts
Browse files Browse the repository at this point in the history
[Web-UI] Add support for prompt templates via UI dropdown
  • Loading branch information
cocktailpeanut committed Mar 22, 2023
2 parents d63771b + 4a66b5d commit 9702ba4
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 240 deletions.
79 changes: 63 additions & 16 deletions bin/web/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,72 @@
const express = require('express')
const os = require("os");
const http = require('http')
const path = require('path')
const os = require('os')
const fs = require('fs')
const Dalai = require("../../index")
const app = express()
const httpServer = http.Server(app);
const start = (port, home) => {
const dalai = new Dalai(home)
dalai.http(httpServer)
app.use(express.static(path.resolve(__dirname, 'public')))
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.set('views', path.resolve(__dirname, "views"))
app.get("/", (req, res) => {
res.render("index", {
threads: os.cpus().length
const dalai = new Dalai(home)
dalai.http(httpServer)
app.use(express.static(path.resolve(__dirname, 'public')))
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.set('views', path.resolve(__dirname, "views"))
app.get("/", (req, res) => {
res.render("index")
})

const defaultDir = path.join(__dirname, "prompts");
const customDir = path.join(os.homedir(), "dalai", "config", "prompts");

if (!fs.existsSync(customDir)) {
fs.mkdirSync(customDir, { recursive: true });
console.log(`Created custom directory: ${customDir}`);
}

app.get("/prompts", (req, res) => {
let prompts = [];

const filesToPrompts = (files, directory, editable) =>
files.flatMap((file) => {
const filePath = path.join(directory, file);
const stats = fs.statSync(filePath, "utf8");

// Filter out directories and non .txt files
if (!stats.isFile() || !file.endsWith('.txt')) {
return []
}
const promptName = path.basename(file, ".txt");
const promptValue = fs.readFileSync(filePath, "utf8");
return { name: promptName, value: promptValue, editable };
});

// Read default prompts
fs.readdir(defaultDir, (err, files) => {
if (err) {
console.error(err);
return res.status(500).json({ error: "Internal server error" });
}
prompts = filesToPrompts(files, defaultDir, false);

// Read custom prompts
fs.readdir(customDir, (err, files) => {
if (err) {
console.error(err);
return res.status(500).json({ error: "Internal server error" });
}
prompts = prompts.concat(filesToPrompts(files, customDir, true));
res.json(prompts);
});
});
});



httpServer.listen(port, () => {
console.log(`Server running on https://localhost:${port}/`)
})
})
httpServer.listen(port, () => {
console.log(`Server running on https://localhost:${port}/`)
})
}
module.exports = start
module.exports = start
12 changes: 12 additions & 0 deletions bin/web/prompts/ai-dialog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Below is a dialog, where User interacts with AI. AI is helpful, kind, obedient, honest, and knows its own limits.

Instruction
Write the last AI response to complete the dialog.

Dialog
User: Hello, AI.
AI: Hello! How can I assist you today?
User: >PROMPT

Response
AI:
2 changes: 2 additions & 0 deletions bin/web/prompts/chatbot.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The expected response for a highly intelligent chatbot to ">PROMPT" is
"
6 changes: 6 additions & 0 deletions bin/web/prompts/default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
>PROMPT

### Response:
9 changes: 9 additions & 0 deletions bin/web/prompts/instruction-alpaca.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
>PROMPT

### Input:
>PROMPT

### Response:
9 changes: 9 additions & 0 deletions bin/web/prompts/rewrite-alpaca.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
Rephrase the sentence.

### Input:
>PROMPT

### Response:
4 changes: 4 additions & 0 deletions bin/web/prompts/translate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Here is how a professional Translator accurately translates the following English text to a grammatically correct >LANGUAGE text:

English: >PROMPT
>LANGUAGE:
11 changes: 11 additions & 0 deletions bin/web/prompts/tweet-sentiment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Tweet: 'I hate it when my phone battery dies.'
Sentiment: Negative
###
Tweet: 'My day has been 👍'
Sentiment: Positive
###
Tweet: 'This is the link to the article'
Sentiment: Neutral
###
Tweet: '>PROMPT'
Sentiment:
Loading

0 comments on commit 9702ba4

Please sign in to comment.