-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (115 loc) · 4.37 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
import express from 'express';
import multer from 'multer';
import { OpenAI } from 'openai';
import pdf from 'pdf-parse/lib/pdf-parse.js';
import dotenv from 'dotenv';
import cors from 'cors';
// Load environment variables
dotenv.config();
// Initialize Express app
const app = express();
const upload = multer();
// Middleware
app.use(express.json());
app.use(cors());
// Initialize OpenAI
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// Extract text from PDF function
async function extractPdfText(pdfBuffer) {
try {
const data = await pdf(pdfBuffer);
return data.text;
} catch (error) {
console.error('PDF extraction error:', error);
throw new Error(`Error reading PDF: ${error.message}`);
}
}
// Ask question using OpenAI API
async function askQuestion(pdfText, question) {
try {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: `You are a job seeker talking to a interview and asking simple questions answering questions.
When discussing experience:
- Never use phrases like 'based on the resume' or 'according to the provided information'.
- Provide specific timeframes whenever available
- Calculate and mention the total duration of experience
- Include relevant project durations
- Be precise about when technologies were used
- If exact timeframes aren't available, acknowledge that
Format your responses in a natural, conversational way while being specific and direct.
Focus on answering exactly what was asked without adding unnecessary information.
limit your responses to the information provided in the resume.
Don't provide any info other than resume related information. like coding question,general lnowledge question or maths question etc or any other question.
If information is not available in the resume, clearly state that the specific detail isn't mentioned.`
},
{
role: "user",
content: `PDF Content:\n${pdfText.slice(0, 9000)}\n\nQuestion: ${question}`
}
],
max_tokens: 150,
temperature: 0.7
});
return response.choices[0].message.content.trim().replace(/\n/g, ' ');
} catch (error) {
console.error('OpenAI API error:', error);
throw new Error(`Error generating answer: ${error.message}`);
}
}
// Routes
app.get('/', (req, res) => {
res.json({ message: 'Welcome to the PDF Question Answering API!' });
});
// PDF upload endpoint
app.post('/upload-pdf', upload.single('file'), async (req, res) => {
try {
console.log("file received");
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded.' });
}
const pdfText = await extractPdfText(req.file.buffer);
console.log("pdf text extracted");
res.json({
pdf_text: pdfText.slice(0, 1000),
full_text: pdfText // Send full text for later use
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Question answering endpoint
app.post('/ask-question', async (req, res) => {
try {
//TODO: get pdf_text from db or some other source (redis,local storage,etc)
const { question, pdf_text } = req.body;
console.log("question received");
if (!question || !pdf_text) {
return res.status(400).json({
error: 'Both question and pdf_text are required.'
});
}
const answer = await askQuestion(pdf_text, question);
res.json({ answer });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
error: 'Something went wrong!',
message: err.message
});
});
// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});