-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
247 lines (210 loc) · 9.47 KB
/
main.js
File metadata and controls
247 lines (210 loc) · 9.47 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
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
243
244
245
246
247
const he = require('he');
const { levenshteinDistance, longestCommonSubstring } = require('./helpers/string-utils');
const { openAIGroupResponse, openAIEffortCategorization } = require('./helpers/openai-utils');
const { checkForCrossDuplicateResponses, checkIfMatch } = require('./helpers/cross-duplicate-utils');
const { isJsonString, parseQueryString, parseJSON } = require('./helpers/json-utils');
const config = require('./config');
// -- -- --
// Script starts here
// -- -- --
// Remove HTML tags from a string
const convertHTMLEntities = (str) => {
return he.decode(str);
}
// Remove special characters from a string
const cleanFinalStateString = (str) => {
return str.toString().toLowerCase().replace(/\n/g, ' ').replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g, '');
}
// Check for self-duplicate responses
const checkForSelfDuplicateResponses = (obj) => {
let keys = Object.keys(obj);
const duplicatedResponsesDict = {};
keys.forEach(key => {
duplicatedResponsesDict[key] = false;
});
for (let i = 0; i < keys.length; i++) {
for (let j = i + 1; j < keys.length; j++) {
let key1 = keys[i];
let key2 = keys[j];
const maxStringLength = Math.max(obj[key1].length, obj[key2].length);
if (maxStringLength === 0) continue;
let s1 = obj[key1];
let s2 = obj[key2];
const rlev = levenshteinDistance(s1, s2);
const nlev = rlev / maxStringLength;
const rlcs = longestCommonSubstring(s1, s2);
const nlcs = rlcs / maxStringLength;
const match = checkIfMatch(s1, s2, nlev, rlev, nlcs, rlcs);
if (match) {
duplicatedResponsesDict[key1] = true;
duplicatedResponsesDict[key2] = true;
}
}
}
return duplicatedResponsesDict;
}
// Check if two objects have the same keys
const haveSameKeys = (...args) => {
let allKeys = args.map(obj => Object.keys(obj).sort().join(','));
for (let i = 1; i < allKeys.length; i++) {
if (allKeys[i] !== allKeys[0]) {
return false;
}
}
return true;
}
exports.handler = async function (event, context) {
// Set CORS headers
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS"
};
// Handle preflight OPTIONS request
if (event.httpMethod === 'OPTIONS') {
return { statusCode: 204, headers, body: '' };
}
let errorText = '';
let problemParsingResponse = false;
const api = async () => {
let isValidQueryString;
try {
parseQueryString(event.body);
isValidQueryString = true;
} catch (error) {
isValidQueryString = false;
}
const isValidJSON = isJsonString(event.body);
if (!isValidJSON && !isValidQueryString) {
errorText = `Must pass a serialized JSON object or a query string`;
throw new Error(errorText);
}
// Determine which parsing function to use (decipher uses query strings)
const parsingFunction = isValidJSON ? parseJSON : parseQueryString;
// Parse the body
problemParsingResponse = true;
let { questions, survey_id, participant_id, responses, low_effort_threshold } = parsingFunction(event.body);
const lowEffortThreshold = low_effort_threshold || 0;
problemParsingResponse = false;
// If any of these are undefined, raise error
const missing = Object.entries({ questions, survey_id, participant_id, responses })
.filter(([, v]) => v == null) // catches undefined or null
.map(([k]) => k);
if (missing.length) throw new Error(`Missing ${missing.join(', ')}`);
// If type of questions or is not object, raise error
const nonObjects = Object.entries({ questions, responses })
.filter(([, v]) => v === null || typeof v !== 'object')
.map(([k]) => k);
if (nonObjects.length) {
throw new Error(`The following fields must be objects: ${nonObjects.join(', ')}`);
}
// If survey_id or participant_id are not strings, raise error
const badStrings = Object.entries({ survey_id, participant_id })
.filter(([, v]) => typeof v !== 'string')
.map(([k]) => k);
if (badStrings.length) {
throw new Error(`The following fields must be strings: ${badStrings.join(', ')}`);
}
// Make sure keys of questions and responses are the same
if (haveSameKeys(questions, responses) === false) {
errorText = 'Questions and responses must have the same keys';
throw new Error(errorText);
}
// Clean responses with convertHTMLEntities
Object.keys(responses).forEach(id => {
responses[id] = convertHTMLEntities(responses[id]);
});
// Clean the responses for duplicate matching
const cleanedResponses = {};
Object.keys(responses).forEach(id => {
cleanedResponses[id] = cleanFinalStateString(responses[id]);
});
// Start duplication promise
const duplicateResponsePromise = checkForCrossDuplicateResponses(cleanedResponses, survey_id);
// Start categorization but do not wait for it to complete
const uniqueIds = Object.keys(questions);
const categorizationPromises = uniqueIds.map(id => {
return openAIGroupResponse(questions[id], responses[id]).then(({ result }) => { return { id, result }} );
});
const lowEffortPromises = uniqueIds.map(id => {
return openAIEffortCategorization(questions[id], responses[id]).then(({ result }) => { return { id, result }} );
});
const selfDuplicateResponses = checkForSelfDuplicateResponses(cleanedResponses);
// Wait for duplication results from duplicatedResponsesPromise
const { duplicateResponses, responseGroups } = await duplicateResponsePromise;
// Wait for categorization results
const openAIResults = await Promise.all(categorizationPromises);
const lowEffortResults = await Promise.all(lowEffortPromises);
// Initialize checks, effort ratings and categorization results
const checks = {};
uniqueIds.forEach(id => { checks[id] = [] });
const effortRatings = {};
const failureTypes = ["Profane", "Off-topic", 'Gibberish', 'GPT'];
// Loop through the results and categorize them
Object.keys(questions).forEach(id => {
// -- OPenai categorizations --
const openAIResult = openAIResults.find(result => result.id === id);
// Make sure result exists and is string
if (openAIResult && typeof openAIResult.result === 'string') {
const lowerCaseResult = openAIResult.result.toLowerCase();
if (failureTypes.includes(lowerCaseResult)) {
// Add punctuation
let cleanedResultWithCapitalization = lowerCaseResult.charAt(0).toUpperCase() + lowerCaseResult.slice(1);
// If gpt, capitalize every character
if (cleanedResultWithCapitalization === 'Gpt') cleanedResultWithCapitalization = cleanedResultWithCapitalization.toUpperCase();
checks[id].push(`Automated test: ${cleanedResultWithCapitalization}`);
}
}
// -- Effort ratings --
const effortResult = lowEffortResults.find(result => result.id === id);
if (effortResult) {
if (parseInt(effortResult.result) <= lowEffortThreshold) {
if (responses[id].length > 0) checks[id].push('Low-effort');
}
effortRatings[id] = parseInt(effortResult.result);
} else {
effortRatings[id] = 0;
};
});
// Add cross duplicate response to checks
Object.keys(duplicateResponses).forEach(id => {
if (typeof responses[id] !== 'string' || responses[id].length < 20) return;
if (duplicateResponses[id].length > 0) checks[id].push('Cross-duplicate response');
});
// Add self duplicate response to checks
Object.keys(selfDuplicateResponses).forEach(id => {
if (typeof responses[id] !== 'string') return;
if (selfDuplicateResponses[id]) checks[id].push('Self-duplicate response');
});
const returnBody = {
error: false,
checks,
response_groups: responseGroups,
effort_ratings: effortRatings,
};
return {
statusCode: 200,
headers,
body: JSON.stringify(returnBody),
}
};
const requestTimedOutPromise = new Promise((_, reject) => {
setTimeout(() => {
errorText = 'Request timed out';
reject(new Error(errorText));
}, config.TIMEOUT_MS);
});
const mainLogicPromise = api();
return Promise.race([mainLogicPromise, requestTimedOutPromise])
.catch(error => {
console.error(error);
const errorTextForReturn = errorText === '' ? problemParsingResponse ? 'Problem parsing request body' : "An unknown error occured" : errorText;
return {
statusCode: 500,
headers,
body: JSON.stringify({
error: true,
problem: errorTextForReturn,
})
}
});
}