-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjobRecommendationSystem.js
More file actions
212 lines (184 loc) · 6.38 KB
/
jobRecommendationSystem.js
File metadata and controls
212 lines (184 loc) · 6.38 KB
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
require('dotenv').config();
const { MongoClient, ServerApiVersion} = require("mongodb");
const { HfInference } = require("@huggingface/inference");
const { MONGO_HOST, MONGO_USER, MONGO_PASS, MONGO_DB , MONGO_COLLECTION } = process.env;
const uri = `mongodb+srv://${MONGO_USER}:${MONGO_PASS}@${MONGO_HOST}/?retryWrites=true&w=majority`;
const hf = new HfInference("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");
const jobPosts = require("./jobPostings");
let client;
async function connectToMongoDB() {
if (!client) {
client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: false,
deprecationErrors: true,
}
});
try {
await client.connect();
console.log('Connected to MongoDB Atlas');
} catch (err) {
console.error('Error connecting to MongoDB:', err);
throw err;
}
}
return client.db(MONGO_DB);
}
async function closeMongoDBConnection() {
if (client) {
await client.close();
console.log('MongoDB connection closed');
client = null;
}
}
async function generateEmbeddings(text) {
try {
return await hf.featureExtraction({
model: "sentence-transformers/all-MiniLM-L6-v2",
inputs: text,
});
} catch (err) {
console.error('Error generating Embeddings:', err);
}
}
async function storeEmbeddings (collection, jobPostings, embeddings) {
const jobsWithEmbeddings = jobPostings.map((job, index) => ({
...job,
embedding: embeddings[index],
}));
await collection.insertMany(jobsWithEmbeddings);
}
async function classifyText(text, labels){
const response = await hf.request({
model: "facebook/bart-large-mnli",
inputs: text,
parameters: {
candidate_labels: labels
}
});
return response;
}
async function extractFilterCriteria (query) {
const criteria = { location: null, jobTitle: null, company: null, jobType: null };
const labels = ["location", "job title", "company", "job type"];
const words = query.split(" ");
for(const word of words) {
const result = await classifyText(word, labels);
console.log(result);
const highestScoreLabel = result.labels[0];
const score = result.scores[0];
if(score > 0.4) {
switch (highestScoreLabel) {
case "location":
criteria.location = word;
break;
case "job title":
criteria.jobTitle = word;
break;
case "company":
criteria.company = word;
break;
case "job type":
criteria.jobType = word;
break;
default:
break;
}
}
}
return criteria;
}
async function performSimilaritySearch(collection, queryTerm, filteredCriteria) {
try {
const queryEmbedding = await generateEmbeddings([queryTerm]);
const pipeline = [
{
'$vectorSearch': {
'index': 'job_vector_search',
'path': 'embedding',
'queryVector': queryEmbedding[0],
'numCandidates': 38,
'limit': 5
}
},
{
'$set': {
'score': {
'$meta': 'vectorSearchScore'
}
}
},
{
'$match': {
'$or': []
}
},
{
'$sort': {
'score': -1
}
}
];
// // Add filter conditions based on filteredCriteria
// if (filteredCriteria.location) {
// pipeline[2]['$match']['$or'].push({ 'location': { '$regex': filteredCriteria.location } });
// }
// if (filteredCriteria.jobTitle) {
// pipeline[2]['$match']['$or'].push({ 'jobTitle': { '$regex': filteredCriteria.jobTitle } });
// }
// if (filteredCriteria.company) {
// pipeline[2]['$match']['$or'].push({ 'company': { '$regex': filteredCriteria.company, } });
// }
// if (filteredCriteria.jobType) {
// pipeline[2]['$match']['$or'].push({ 'jobType': { '$regex': filteredCriteria.jobType, } });
// }
// If no criteria were added, remove the $match stage
if (pipeline[2]['$match']['$or'].length === 0) {
pipeline.splice(2, 1);
}
const results = await collection.aggregate(pipeline).toArray();
if (!results || results.length === 0) {
console.log(`No Job items found similar to "${queryTerm}" with given criteria`);
return [];
}
let topJobPosts = results.map(result => {
return {
jobId: result.jobId,
score: result.score,
job_name: result.jobTitle,
job_description: result.jobDescription,
location: result.location,
job_type: result.jobType,
company: result.company
};
});
return topJobPosts;
} catch (error) {
console.log(error);
}
}
async function main() {
const query = "Python";
try {
await connectToMongoDB();
const db = client.db(MONGO_DB);
const collection = db.collection(MONGO_COLLECTION);
//
// const jobTexts = jobPosts.map(jobPost => `${jobPost.jobTitle}, ${jobPost.jobDescription}, ${jobPost.jobType}, ${jobPost.location}`)
// const jobDataEmbeddings = [];
// for (let jobText of jobTexts) {
// const embedding = await generateEmbeddings(jobText);
// jobDataEmbeddings.push(embedding);
// }
// await storeEmbeddings(collection, jobPosts, jobDataEmbeddings);
const filteredCriteria = await extractFilterCriteria(query);
const initialResults = await performSimilaritySearch(collection, query, filteredCriteria );
initialResults.forEach((item, index) => {
console.log(`Top ${index + 1} Recommended Job Name: ${item.job_name}`);
});
}catch (error) {
console.log(error);
}
}
main();