2025-02-12 15:15:52 -05:00
|
|
|
const promises = require('fs/promises');
|
2025-02-11 17:00:36 -05:00
|
|
|
const pdf = require('pdf-parse');
|
2025-02-12 15:15:52 -05:00
|
|
|
const Anthropic = require('@anthropic-ai/sdk');
|
2025-02-12 15:41:29 -05:00
|
|
|
const { Document, Packer, Paragraph, TextRun } = require('docx');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
2025-02-12 15:15:52 -05:00
|
|
|
require('dotenv').config();
|
|
|
|
|
|
|
|
const today = new Date();
|
|
|
|
let jobDescription = '';
|
2025-02-12 15:41:29 -05:00
|
|
|
let coverLetterRawText = '';
|
2025-02-12 15:15:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
const anthropic = new Anthropic({
|
|
|
|
apiKey: process.env.ANTHROPIC_API_KEY
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2025-02-11 17:00:36 -05:00
|
|
|
|
|
|
|
// Path to the PDF file you want to extract text from
|
|
|
|
const pdfPath = 'data/kyle_resume.pdf';
|
|
|
|
|
|
|
|
// Function to extract text from the PDF and return it as a string
|
|
|
|
async function extractTextFromPDF(pdfPath) {
|
|
|
|
try {
|
2025-02-12 15:15:52 -05:00
|
|
|
const data = await promises.readFile(pdfPath); // Use promises with fs
|
2025-02-11 17:00:36 -05:00
|
|
|
const pdfData = await pdf(data); // Parse the PDF
|
|
|
|
return pdfData.text; // Return the extracted text
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error('Error extracting text from PDF: ' + error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-12 15:15:52 -05:00
|
|
|
async function readJobDescription(filePath) {
|
|
|
|
try {
|
|
|
|
const data = await promises.readFile(filePath, 'utf8');
|
|
|
|
jobDescription = data; // Save content to the global variable
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error reading the file:', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-12 15:41:29 -05:00
|
|
|
function generateCoverLetter(rawText, outputFilename) {
|
|
|
|
// Parse the raw markup
|
|
|
|
const header = rawText.match(/<header>(.*?)<\/header>/s)[1].trim();
|
|
|
|
const greeting = rawText.match(/<greeting>(.*?)<\/greeting>/s)[1].trim();
|
|
|
|
const introduction = rawText.match(/<introduction>(.*?)<\/introduction>/s)[1].trim();
|
|
|
|
const body = rawText.match(/<body>(.*?)<\/body>/s)[1].trim();
|
|
|
|
const conclusion = rawText.match(/<conclusion>(.*?)<\/conclusion>/s)[1].trim();
|
|
|
|
const signature = rawText.match(/<signature>(.*?)<\/signature>/s)[1].trim();
|
|
|
|
|
|
|
|
// Create a new document using docx
|
|
|
|
const doc = new Document({
|
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
properties: {},
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun(header),
|
|
|
|
new TextRun("\n\n"), // Add line breaks between sections
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun(greeting),
|
|
|
|
new TextRun("\n\n"),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun(introduction),
|
|
|
|
new TextRun("\n\n"),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun(body),
|
|
|
|
new TextRun("\n\n"),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun(conclusion),
|
|
|
|
new TextRun("\n\n"),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun(signature),
|
|
|
|
new TextRun("\n\n"),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create a packer to generate the .docx file
|
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
|
|
|
// Save the document as a .docx file
|
|
|
|
fs.writeFileSync(outputFilename, buffer);
|
|
|
|
console.log(`${outputFilename} created successfully!`);
|
|
|
|
}).catch(error => {
|
|
|
|
console.error("Error generating the document:", error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-12 15:15:52 -05:00
|
|
|
|
|
|
|
async function main() {
|
2025-02-11 17:00:36 -05:00
|
|
|
try {
|
2025-02-12 15:15:52 -05:00
|
|
|
const resume_parser_api = require('./data/resume_parser_api.json');
|
|
|
|
const cover_letter_api = require('./data/cover_letter_api.json');
|
|
|
|
const extractedResumeText = await extractTextFromPDF(pdfPath); // Waits for PDF extraction to complete
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// used to replace the variables in the resume parser api
|
|
|
|
resume_parser_api.messages[0].content[0].text = resume_parser_api.messages[0].content[0].text.replace("{{resume}}", extractedResumeText)
|
|
|
|
|
|
|
|
const resume_response = await anthropic.messages.create(resume_parser_api);
|
|
|
|
const candidateProfile = resume_response.content[0].text.split('```json')[1].split('```')[0].trim(); // pulls just the json structure out of LLM response
|
|
|
|
|
|
|
|
// replace variables in cover_letter_api
|
|
|
|
cover_letter_api.messages[0].content[0].text = cover_letter_api.messages[0].content[0].text
|
|
|
|
.replace('{{resume_json}}', candidateProfile)
|
|
|
|
.replace('{{job_description}}', jobDescription)
|
|
|
|
.replace('{{date}}', today);
|
|
|
|
|
|
|
|
const cover_letter_response = await anthropic.messages.create(cover_letter_api);
|
|
|
|
|
2025-02-12 15:41:29 -05:00
|
|
|
coverLetterRawText = cover_letter_response.content[0].text.split('<cover_letter>')[1].split('</cover_letter>')[0].trim()
|
|
|
|
// console.log(coverLetterRawText)
|
|
|
|
|
|
|
|
generateCoverLetter(coverLetterRawText, 'test.docx')
|
|
|
|
|
|
|
|
|
2025-02-12 15:15:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-11 17:00:36 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message); // Catches and logs any errors that occurred
|
|
|
|
}
|
2025-02-12 15:15:52 -05:00
|
|
|
}
|
2025-02-11 17:00:36 -05:00
|
|
|
|
2025-02-12 15:41:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-12 15:15:52 -05:00
|
|
|
readJobDescription('./data/job_description.txt')
|
|
|
|
// Call the main function
|
|
|
|
main();
|