-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
242 lines (204 loc) · 6.75 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Imports
import config from 'config';
import fs from 'fs';
import path from 'path';
import { PdfReader } from 'pdfreader';
import Docxtemplater from 'docxtemplater';
import mammoth from 'mammoth';
import { Configuration, OpenAIApi } from "openai";
// Read Configuration
const ChatGPT_API_Key = config.get("ChatGPT_API_Key");
const GPTOrgId = config.get("GPTOrgId");
const File_Folder = config.get("File_Folder");
const Max_GPT_Version = config.get("Max_GPT_Version");
const Text_Separator = config.get("Text_Separator");
const ChatGPT_Specs = config.get("ChatGPT_Specs");
const Prompts = config.get("Prompts");
// Set your OpenAI API key here
const configuration = new Configuration({
organization: GPTOrgId,
apiKey: ChatGPT_API_Key,
});
const openai = new OpenAIApi(configuration);
//Sanity Check
// dumpConfiguration();
// Lets find and loop through folders
readFilesInFolder(File_Folder);
async function readTextFile(filePath, callback) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.log('Error reading file:', err);
return callback(err, null);
}
callback(null, data);
});
}
async function readPDFFile(filePath, callback) {
const textLines = [];
const pdfReader = new PdfReader();
pdfReader.parseFileItems(filePath, (err, item) => {
if (err) {
console.error('Error reading PDF file:', err);
return callback(err, null);
}
if (!item) {
const content = textLines.join('\n');
callback(null, content);
} else if (item.text) {
textLines.push(item.text);
}
});
}
function readWordFile(filePath, callback) {
try {
const data = fs.readFileSync(filePath, 'binary');
const doc = new Docxtemplater();
doc.loadZip(data);
const content = doc.getFullText();
callback(null, content);
} catch (err) {
console.error('Error reading Word file:', err);
callback(err, null);
}
}
function readDocFile(filePath, callback) {
fs.readFile(filePath, 'binary', (err, data) => {
if (err) {
console.error('Error reading .doc file:', err);
return callback(err, null);
}
mammoth.extractRawText({ buffer: data })
.then(result => {
const content = result.value;
callback(null, content);
})
.catch(error => {
console.error('Error extracting .doc contents:', error);
callback(error, null);
});
});
}
function readFilesInFolder(folderPath) {
fs.readdir(folderPath, (err, files) => {
if (err) {
console.error('Error reading folder:', err);
return;
}
files.forEach(file => {
const filePath = path.join(folderPath, file);
const fileType = path.extname(file).toLowerCase();
console.log(`File: ${file}`);
console.log(`Type: ${fileType}`);
switch (fileType) {
case '.txt':
/*
readTextFile(filePath, (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
//processFile(data,filePath);
});
*/
console.log(`${fileType} file detected. Supported but not processing. Skipping reading the file.`);
break;
case '.pdf':
readPDFFile(filePath, (err, data) => {
if (err) {
console.error('Error:', err);
return;
}
processFile(data,filePath);
});
break;
case '.doc':
/*
readDocFile(filePath, (err, data) => {
if (err) {
console.error('Error:', err);
return;
}
console.log(`Content of ${file}:`);
console.log(data);
});
*/
console.log(`${fileType} file detected. Currently unsupported. Skipping reading the file.`);
break;
case '.docx':
/*
readWordFile(filePath, (err, data) => {
if (err) {
console.error('Error:', err);
return;
}
console.log(`Content of ${file}:`);
console.log(data);
});
*/
console.log(`${fileType} file detected. Currently unsupported. Skipping reading the file.`);
break;
default:
console.log('Unsupported file type. Skipped reading the file.');
}
});
});
}
async function generateChatResponse(system, userPrompt) {
console.log();
console.log("entering generateChatResponse");
try {
const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo', // Change the model if needed
temperature: 0.0,
messages: [
{ role: 'system', content: system },
{ role: 'user', content: userPrompt }
],
max_tokens: 400 // Adjust as needed - NOTE. This is the tokens to reserve for the RESpONSE!!!!
});
console.log("made chat call");
console.log(response);
console.log();
console.log(response.data.choices[0]);
return response.data.choices[0].message.content;
} catch (error) {
console.error('Error generating response:', error.message);
console.error(JSON.stringify(error,null,2));
return '';
}
}
async function processFile(file, filePath) {
file = file.replace(/(\r?\n)/g, " "); //Remove Carriage return / line feed
file = file.replace(/(\r)/g, " "); // Remove Carriage return
file = file.replace(/(\t)/g, " "); // Remove tabs
file = file.replace(/‘/g, ""); // Remove ticks
file = file.replace(/•/g, ""); // Remove bullets
file = file.replace(/¨/g, ""); // Remove ¨
//Reduce character count so we will have around 3500 tokens max
file = file.substring(0, 14000);
// check file size (tokens) and reduce size if needed
for (const prompt of Prompts) {
const userPrompt = "Resume: " + file + " " + prompt;
const response = await generateChatResponse(prompt.System, userPrompt);
console.log("Write Response " + response);
// Save the response to a file
const fileName = filePath + "_" + prompt.Name + ".txt";
fs.writeFile(fileName, response, (err) => {
if (err) {
console.error('Error saving response to file:', err);
} else {
console.log('Response saved to', fileName);
}
});
}
}
//Helper function
function dumpConfiguration() {
console.log("******** Config **************");
console.log("ChatGPT_API_Key: " + ChatGPT_API_Key);
console.log("File_Folder: " + File_Folder);
console.log("Max_GPT_Version: " + Max_GPT_Version);
console.log("Text_Separator: " + Text_Separator);
console.log("ChatGPT_Specs: " + ChatGPT_Specs[0].Version + " " + ChatGPT_Specs[0].MaxTokens);
console.log("Prompts: " + Prompts[0].Name + " " + Prompts[0].Prompt);
}