-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrenderer.js
223 lines (191 loc) · 6.97 KB
/
renderer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const SERVER_URL = "http://127.0.0.1:5000";
const apiSubmitButton = document.getElementById("api-key-btn");
var currentWriting = null;
var coverLetterButton = null;
var whyUsButton = null;
var uploadResumeButton = null;
var uploadResumeForm = null;
var generateButton = null;
var goBackButton = null;
var API_KEY_INFO = null;
var COMPANY_NAME = null;
var ROLE_DESCRIPTION = null;
var USER_BACKGROUND = null;
var JOB_POSITION = null;
apiSubmitButton.addEventListener("click", async event => {
// Get the API key from the user
API_KEY_INFO = document.getElementById("api-key-txt").value;
uploadApiKey();
document.getElementById("api-key-txt").value = '';
let mainPage = document.getElementById("main-page");
let apiKeyPage = document.getElementById("api-key-page");
mainPage.style.display = "block";
apiKeyPage.style.display = "none";
// Cover letter button click
coverLetterButton = waitUntilLoad('cover-letter-btn');
coverLetterButton.addEventListener('click', selectCoverLetterBtn);
// Why button click
whyUsButton = waitUntilLoad('why-us-btn');
whyUsButton.addEventListener('click', selectWhyUsBtn);
// Default
selectCoverLetterBtn();
// Upload resume submit
uploadResumeForm = waitUntilLoad('upload-resume-form');
uploadResumeForm.addEventListener('submit', uploadResume);
// Generate button click
generateButton = waitUntilLoad('generate-btn');
generateButton.addEventListener("click", onClickGenerateButton);
// Go back button click
goBackButton = waitUntilLoad('go-back-btn');
goBackButton.addEventListener("click", onClickGoBackButton);
});
function selectCoverLetterBtn() {
coverLetterButton.style.backgroundColor = "#9cc0f7"; // selected
whyUsButton.style.backgroundColor = "#d2e4ff";
currentWriting = 'cover-letter';
}
function selectWhyUsBtn() {
coverLetterButton.style.backgroundColor = "#d2e4ff";
whyUsButton.style.backgroundColor = "#9cc0f7"; // selected
currentWriting = 'why-us';
}
async function uploadApiKey() {
axios.post(`${SERVER_URL}/apikey`, { 'OPENAI_API_KEY': API_KEY_INFO })
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.error(error);
});
}
async function uploadResume(event) {
event.preventDefault();
let formData = new FormData();
formData.append("file", this.file.files[0]);
axios.post(`${SERVER_URL}/resume`, formData, {
headers: {
"Content-Type": "multipart/form-data"
}
})
.then((response) => {
console.log(response.data);
let text = response.data.choices[0].text.trim();
let userBackground = document.getElementById("user-background");
userBackground.value = text;
})
.catch((error) => {
console.error(error);
});
}
function waitUntilLoad(id) {
let ele = null;
do {
ele = document.getElementById(id);
} while (ele == null);
return ele;
}
async function onClickGoBackButton() {
let mainPage = document.getElementById("main-page");
let apiKeyPage = document.getElementById("api-key-page");
mainPage.style.display = "none";
apiKeyPage.style.display = "block";
}
async function onClickGenerateButton() {
// Prep company & user info
COMPANY_NAME = document.getElementById("company-info").value;
ROLE_DESCRIPTION = document.getElementById("role-description").value;
USER_BACKGROUND = document.getElementById("user-background").value;
JOB_POSITION = document.getElementById("job-position").value;
if (!COMPANY_NAME) {
alert('⚠️ You did not specify the company name! Please write the name of the company.');
} else if (!ROLE_DESCRIPTION) {
alert('⚠️ The role description section is missing! Please tell us more about the role you are applying for.');
} else if (!USER_BACKGROUND) {
alert('⚠️ You forgot to tell me your background! I cannot generate text without knowing who you are. Please write a brief summary of your background.');
} else if (!JOB_POSITION) {
alert('⚠️ You did not specify which position you are applying for! Please write down the job position.');
} else {
// Get response from OpenAPI
let prompt = null;
if (currentWriting == "cover-letter") {
prompt = generateCoverLetterPrompt();
} else if (currentWriting == "why-us") {
prompt = generateWhyUsPrompt();
} else {
alert('⚠️ Unknown writing format!');
}
let response = await requestCompletion(prompt);
createParagraphs(response);
console.log(`Prompt: ${prompt}`);
}
}
async function requestCompletion(prompt) {
try {
let requestOptions = getRequestOptions(prompt);
let response = await fetch("https://api.openai.com/v1/completions", requestOptions);
if (response.ok) {
let data = await response.json();
return data.choices[0].text;
} else {
handleErrors(response);
}
} catch (e) {
console.log(e);
alert(e);
}
}
function getRequestOptions(prompt) {
const params = {
"prompt": prompt,
"model": "text-davinci-003",
"max_tokens": 700,
"temperature": 0.2,
"n": 1,
}
const requestOptions = {
method: "POST",
headers: {
"Authorization": "Bearer " + API_KEY_INFO,
"Content-Type": "application/json"
},
body: JSON.stringify(params)
}
return requestOptions
}
function createParagraphs(text) {
let answerDiv = document.getElementById('answer');
answerDiv.innerHTML = "";
answerDiv.style.color = "#123693";
let texts = text.split(/\r\n|\r|\n/);
for (let i in texts) {
let p = document.createElement("p");
p.innerHTML = texts[i];
answerDiv.appendChild(p);
}
}
function handleErrors(response) {
switch (response.status) {
case 401: // 401: Unauthorized: API key is wrong
throw new Error('Please double-check your API key.');
case 429: // 429: Too Many Requests: Need to pay
throw new Error('You exceeded your current quota, please check your plan and billing details.');
case 405: // 405: Wrong HTTP method
throw new Error('Your current HTTP method is not compatible with the request.');
default:
throw new Error(`Error occurred (error code: ${response.status})`);
}
}
function generateCoverLetterPrompt() {
return `Write a professional cover letter for the ${JOB_POSITION} position at ${COMPANY_NAME}.` +
"\n\nHere is the role description:\n" +
`${ROLE_DESCRIPTION}` +
"\n\nThis is my background:\n" +
`${USER_BACKGROUND}`
}
function generateWhyUsPrompt() {
return `Explain why I would be a great fit for the ${JOB_POSITION} position at ${COMPANY_NAME}.` +
"\n\nThis is the role description:\n" +
`${ROLE_DESCRIPTION}` +
"\n\nHere is my background:\n" +
`${USER_BACKGROUND}`
}