-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (101 loc) · 3.63 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
import { stringify } from 'csv';
import { createWriteStream } from 'fs';
import dotenv from 'dotenv';
import fs from 'fs';
import { VertexAI } from "@google-cloud/vertexai";
import { tests, models, formats, outputFile, itemCount } from "./config.js";
import { readFileAsBase64, sleep, parseOutput, ensureDirectoryExistence } from "./utils.js";
dotenv.config();
async function runImageTest(modelName, image, prompt, outputFormat, itemCount, retry = false) {
try {
const vertexAI = new VertexAI({
project: process.env.GCP_PROJECT_NAME,
location: process.env.GCP_REGION
});
const model = vertexAI.getGenerativeModel({
model: modelName,
});
const data = readFileAsBase64(image);
const imagePart = {
inlineData: {
data,
'mimeType': 'image/jpeg'
}
}
const countPrompt = itemCount === -1 ? '' : `Get only ${itemCount} items.`
const promptPart = {
text: `
${prompt}.
${countPrompt}
Even if there's 1 item, treat it like an array.
VERY IMPORTANT: Output the result in ${outputFormat} format.
`,
}
const startTime = performance.now();
const result = await model.generateContent({
contents: [{
role: 'user',
parts: [
promptPart,
imagePart
],
}]
});
const content = result.response.candidates[0].content.parts[0].text;
const parsedContent = parseOutput(content, outputFormat);
const resultStats = {
content,
reqItemCount: itemCount,
itemCount: parsedContent?.length,
contentLength: content.length,
executionTime: performance.now() - startTime
}
return resultStats;
} catch (error) {
const isQuotaError = error.message.indexOf('429') > -1;
if (isQuotaError) {
if (retry) {
throw error;
}
await sleep(60);
return runImageTest(modelName, image, prompt, outputFormat, itemCount, true);
} else {
throw error;
}
}
}
async function main() {
const output = createWriteStream(outputFile);
const stringifier = stringify({ header: true });
stringifier.pipe(output);
for (let test of tests) {
const statsArr = [];
const { name, fileName, prompt } = test;
for (let modelName of models) {
for (let count of itemCount) {
for (let format of formats) {
console.log(`Testing ${name} with ${modelName} model in ${format} mode and ${count} items.`)
const stats = await runImageTest(modelName, fileName, prompt, format, count);
const statsObj = {
format,
model: modelName,
count,
...test,
...stats,
};
statsArr.push(statsObj);
console.log(`Ran in ${statsObj.executionTime}.`)
const { content, ...rest } = statsObj;
const outPath = `./output/${name}/${modelName}/${format}/output_${count}.${format}`;
ensureDirectoryExistence(outPath);
fs.writeFileSync(
outPath,
content);
stringifier.write(rest);
}
}
}
}
stringifier.end();
}
main()