-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessFile.js
71 lines (59 loc) · 2.01 KB
/
processFile.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
require('dotenv').config()
const fs = require("fs/promises")
const fileType = require('file-type');
const { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash"});
const generationConfig = {
temperature: 1, // Controls the "creativity" or randomness of the generated text // 0 = most predictable, deterministic output and 2 = most creative, unpredictable output
topP: 0.95, // The model selects words from the top 95% most likely words
topK: 10, // The model selects the next word from the top 10 most likely words
maxOutputTokens: 8192, // The maxmium amount of text generated
responseMimeType: 'application/json',
};
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
];
async function processFile(filePath, prompt) {
const buffer = await fs.readFile(filePath)
const mimeType = (await fileType.fromBuffer(buffer)).mime
const result = await model.generateContent({
contents: [
{ role: 'user',
parts: [
{ text: prompt },
{ text: 'input: '},
{
inlineData: {
data: buffer.toString('base64'),
mimeType
}
}
]
}
],
generationConfig,
safetySettings,
});
// console.log(result.response.usageMetadata); // to check the input and output token consumption for tracking
const jsonData = JSON.parse(result.response.text())
return jsonData
}
module.exports = {
processFile
}