-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (60 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const express = require("express");
const cors = require("cors");
require('dotenv').config();
require('express-zip');
const bodyParser = require("body-parser");
const app = express();
const _ = require('lodash');
const docx = require("docx");
const OpenAI = require('openai');
const openai = new OpenAI();
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.static("frontend"));
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.render("index");
});
app.post('/submit', async (req, res) => {
const header = req.body.header
const prompt = req.body.prompt
const footer = req.body.footer
let files = []
for (const key of _.keys(prompt)) {
const value = prompt[key]
const completion = await openai.chat.completions.create({
messages: [
{
role: 'user',
content: `Generate a cover letter for ${key}. Use this information - ${value}`
}
],
model: 'gpt-3.5-turbo',
});
const aiGeneratedCoverLetter = completion.choices[0].message.content
const doc = new docx.Document({
sections: [
{
properties: {},
children: [
new docx.Paragraph({
children: [
new docx.TextRun(header + '\n\n'),
new docx.TextRun(aiGeneratedCoverLetter + '\n\n'),
new docx.TextRun(footer),
],
}),
],
},
],
});
const buffer = await docx.Packer.toBuffer(doc)
files.push({ name: `${key}-cover-letter.docx`, buffer: buffer, path: '' })
}
// Render Docs
res.zip(files, 'cover-letter.zip')
return
});
app.listen(3000, () => console.log("Server running on port 3000"));