-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
420 lines (362 loc) · 12.3 KB
/
Copy pathindex.js
File metadata and controls
420 lines (362 loc) · 12.3 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// 1. Imports and Setup
import { createPartFromUri, createUserContent, GoogleGenAI } from '@google/genai';
import cors from 'cors';
import dotenv from 'dotenv';
import express from 'express';
import fs from 'fs';
import multer from 'multer';
import path from 'path';
import { fileURLToPath } from 'url';
import { runEval } from './evals.utils.js';
import { prependImportantToDescriptions } from './utils.js';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(cors());
// 2. Constants and Configs
const MAX_FILE_SIZE = Number(process.env.MAX_FILE_SIZE || 10 * 1024 * 1024); // 10MB default
const RETRY_LIMIT = Number(process.env.RETRY_LIMIT ?? 1);
const RETRY_DELAY_MS = Number(process.env.RETRY_DELAY_MS ?? 800);
const allowedMimeTypes = new Set([
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'text/plain',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'text/csv',
'text/html',
'text/markdown',
'application/json',
'application/xml',
'text/xml',
'image/png',
'image/jpeg',
'image/webp',
'audio/mpeg',
'audio/wav',
'video/mp4',
'video/quicktime',
]);
const upload = multer({
dest: 'uploads/',
limits: { fileSize: MAX_FILE_SIZE },
fileFilter: (req, file, cb) => {
if (!allowedMimeTypes.has(file.mimetype)) {
const err = new Error('INVALID_FILE_TYPE');
err.code = 'INVALID_FILE_TYPE';
return cb(err);
}
cb(null, true);
},
});
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
// 3. Middlewares
if (process.env.NODE_ENV !== 'production') {
const livereload = (await import('livereload')).default;
const connectLivereload = (await import('connect-livereload')).default;
const liveReloadServer = livereload.createServer();
liveReloadServer.watch(path.join(__dirname, 'client/dist'));
app.use(connectLivereload());
liveReloadServer.server.once('connection', () => {
setTimeout(() => liveReloadServer.refresh('/'), 100);
});
}
const uploadMiddleware = (req, res, next) => {
upload.array('files')(req, res, (err) => {
if (err instanceof multer.MulterError) {
let errorResponse = {
status: 'error',
message: 'Parsing error',
errorCode: err.code,
error: err.message,
shortError: 'Upload error.',
tip: 'Check file size and type.',
};
if (err.code === 'LIMIT_FILE_SIZE') {
errorResponse = {
...errorResponse,
error: 'File is too large.',
shortError: 'File too large.',
tip: `Max size is ${Math.floor(MAX_FILE_SIZE / (1024 * 1024))}MB. Try a smaller file.`,
};
}
return res.status(400).json(errorResponse);
}
next(err);
});
};
const clientDistPath = path.join(__dirname, 'client', 'dist');
app.use(express.static(clientDistPath));
app.use('/downloads', express.static(path.join(process.cwd(), 'downloads')));
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const generateAndParse = async ({ ai, uploadedFiles, pastedText, systemInstructions }) => {
const response = await ai.models.generateContent({
model: 'models/gemini-2.5-flash-lite',
contents: createUserContent([
`Please extract the relevant information from the following:\n\nPasted text:\n${pastedText || '[none]'}`,
...uploadedFiles.map((f) => createPartFromUri(f.uri, f.mimeType)),
]),
config: { systemInstruction: { role: 'system', parts: [{ text: systemInstructions }] } },
});
const rawText = response?.candidates?.[0]?.content?.parts?.[0]?.text?.trim() || '{}';
let args;
try {
const jsonMatch = rawText.match(/```json([\s\S]*?)```/i);
args = JSON.parse(jsonMatch ? jsonMatch[1] : rawText);
} catch {
args = {};
}
const filteredArgs = Object.fromEntries(
Object.entries(args).filter(([_, v]) => {
if (!v || v?.length === 0) return false;
if (typeof v === 'string')
return !['null', 'undefined', 'none', 'n/a', 'unknown'].includes(v.toLowerCase());
return true;
}),
);
return { filteredArgs, tokens: response?.usageMetadata };
};
const withRetry = async (taskFn, { limit = RETRY_LIMIT, delayMs = RETRY_DELAY_MS } = {}) => {
let lastErr;
for (let attempt = 0; attempt <= limit; attempt++) {
try {
return await taskFn();
} catch (err) {
lastErr = err;
if (attempt < limit) await sleep(delayMs);
}
}
throw lastErr;
};
// 5. Endpoints
app.post(
'/api/formfill',
uploadMiddleware,
express.urlencoded({ extended: true }),
async (req, res) => {
const start = Date.now();
const files = req.files || [];
let formFields = JSON.parse(req.body.formFields || '{}');
const instruction = req.body.instruction?.trim() || '';
const important = req.body.important?.trim() || '';
const pastedText = req.body.pastedText?.trim() || '';
if (Object.keys(formFields).length === 0) {
return res.status(400).json({
status: 'error',
message: 'Parsing error',
errorCode: 'NO_FORM_FIELDS_PROVIDED',
error: 'No form fields were submitted.',
shortError: 'No form fields provided.',
tip: 'Please provide the necessary form fields before submitting.',
});
}
if (files.length === 0 && !pastedText) {
return res.status(400).json({
status: 'error',
message: 'Parsing error',
errorCode: 'NO_INPUT_PROVIDED',
error: 'No file or pasted text was submitted.',
shortError: 'No input provided.',
tip: 'Upload a file or paste some text before submitting.',
});
}
formFields = prependImportantToDescriptions(formFields, important);
console.log(formFields);
// TO-DO: ADD TO S3 AND SAVE REFERENCE IN A DB CONNECTED TO A CONTEXT TO BE PASSED BY THE FRONTEND
const uploadedFiles = await Promise.all(
files.map(async (file) => {
const uploaded = await ai.files.upload({
file: path.join(file.destination, file.filename),
config: { mimeType: file.mimetype },
});
return {
name: uploaded.name,
uri: uploaded.uri,
mimeType: uploaded.mimeType,
size: file.size,
};
}),
);
files.forEach((file) => fs.unlink(file.path, () => {}));
const systemInstructions = `You are an AI that fills form fields.
You cannot reply or assist directly.
You must read the user inputs and return only the values needed to fill the form fields.
You'll receive
- an instruction on how to fill the form fields
- a list of fields accepted by the form with descriptions
- optional text content pasted by the user
- optional files uploaded by the user
The content provided by the user may have information to entities unrelated to your instruction.
Be careful not to mix up entities.
<INSTRUCTION>
${instruction} ${important}
</INSTRUCTION>
<FORM_FIELDS>
${JSON.stringify(formFields, null, 2)}
</FORM_FIELDS>
Your task:
1. Analyze the content provided (text or files).
2. Use the FORM_FIELDS to determine what data to extract.
3. Convert the extracted data to the format required in the FORM_FIELDS.
4. Return a json with the extracted data as values to each form field name.
Do not explain or respond to the user. Just return the json with filled fields.
Do not ask questions. Only act.
Do not fill fields with "null", "undefined", "none", "N/A", "unknown" or similar.
Only fill in the fields with the extracted data. Don't use your own knowledge or assumptions.
When in doubt, don't mention the field in your response.
Today is ${new Date().toLocaleDateString()}`;
try {
const { filteredArgs, tokens } = await withRetry(() =>
generateAndParse({ ai, uploadedFiles, pastedText, systemInstructions }),
);
const duration = Date.now() - start;
res.json({ status: 'ok', data: filteredArgs, duration, tokens });
} catch (err) {
console.dir(err, { depth: null });
res.status(500).json({
status: 'error',
message: 'Gemini API request failed',
errorCode: 'GEMINI_API_ERROR',
error: err.message,
shortError: 'Processing failed.',
tip: 'Try again or check your API key.',
});
}
},
);
app.post(
'/api/eval',
uploadMiddleware,
express.urlencoded({ extended: true }),
async (req, res) => {
const file = req.files?.[0];
const { instruction = '', important = '' } = req.body;
let formFields, expected;
try {
formFields = JSON.parse(req.body.formFields || '{}');
expected = JSON.parse(req.body.expected || '{}');
} catch {
return res.status(400).json({ error: 'Invalid JSON in formFields or expected' });
}
if (!formFields || !file) {
return res.status(400).json({ error: 'Missing formFields or file' });
}
try {
const fileBuffer = fs.readFileSync(file.path);
const result = await runEval({
file,
fileBuffer,
formFields,
instruction,
important,
expected,
});
res.json(result);
} catch (err) {
res
.status(500)
.json({ file: file.originalname, ok: false, status: 'ERR', error: String(err) });
} finally {
fs.unlinkSync(file.path);
}
},
);
app.post(
'/api/evals',
uploadMiddleware,
express.urlencoded({ extended: true }),
async (req, res) => {
const { instruction = '', important = '' } = req.body;
const times = Number(req.query.times || 3);
const files = req.files || [];
let formFields, expectedMap;
try {
formFields = JSON.parse(req.body.formFields || '{}');
expectedMap = JSON.parse(req.body.expected || '{}');
} catch {
return res.status(400).json({ error: 'Invalid JSON in formFields or expectedMap' });
}
if (!formFields || !files.length) {
return res.status(400).json({ error: 'Missing formFields or files' });
}
const input = {
instruction,
important,
times,
files,
formFields,
expected: expectedMap,
};
const results = [];
for (const file of files) {
const fileBuffer = fs.readFileSync(file.path);
const expected = expectedMap[file.originalname];
for (let i = 0; i < times; i++) {
try {
const result = await runEval({
file,
fileBuffer,
formFields,
instruction,
important,
expected,
});
results.push({ ...result, run: i + 1 });
} catch (err) {
results.push({
file: file.originalname,
run: i + 1,
ok: false,
status: 'ERR',
error: String(err),
});
}
}
fs.unlinkSync(file.path);
}
const summary = {
totalFiles: files.length,
totalRuns: results.length,
avgLatencyMs: null,
avgPrecision: null,
avgRecall: null,
avgF1: null,
totalCostUsd: 0,
};
let latencySum = 0;
let precisionSum = 0;
let recallSum = 0;
let f1Sum = 0;
let metricCount = 0;
for (const r of results) {
if (r.latency_ms != null) latencySum += r.latency_ms;
if (r.total_cost) summary.totalCostUsd += parseFloat(r.total_cost);
if (r.precision != null && r.recall != null && r.f1 != null) {
precisionSum += r.precision;
recallSum += r.recall;
f1Sum += r.f1;
metricCount++;
}
}
summary.avgLatencyMs = results.length ? Math.round(latencySum / results.length) : null;
summary.avgPrecision = metricCount ? +(precisionSum / metricCount).toFixed(4) : null;
summary.avgRecall = metricCount ? +(recallSum / metricCount).toFixed(4) : null;
summary.avgF1 = metricCount ? +(f1Sum / metricCount).toFixed(4) : null;
summary.totalCostUsd = +summary.totalCostUsd.toFixed(6);
res.json({ input, totalRuns: results.length, summary, results });
},
);
// 6. Frontend Fallback
app.get('/', (req, res) => {
res.sendFile(path.join(clientDistPath, 'index.html'));
});
app.get('/:param', (req, res) => {
res.sendFile(path.join(clientDistPath, 'index.html'));
});
// 7. Start Server
app.listen(3001, () => console.log('http://localhost:3001'));
export default app;