-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.js
More file actions
78 lines (73 loc) · 2.89 KB
/
Copy pathai.js
File metadata and controls
78 lines (73 loc) · 2.89 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
import { GoogleGenAI, Type } from "@google/genai";
import * as fs from 'fs';
import chalk from "chalk";
import dotenv from "dotenv"
dotenv.config()
const ai = new GoogleGenAI({
apiKey: process.env.GEMINI_API_KEY
});
export default async function predictFolder(mimeType, path) {
try {
console.log(chalk.green('Gemini background job started'));
const file = {
inlineData: {
mimeType: mimeType,
data: Buffer.from(fs.readFileSync(path)).toString("base64"),
},
};
const foldersFile = {
inlineData: {
mimeType: "text/plain",
data: Buffer.from(fs.readFileSync("folders.txt")).toString("base64"),
},
};
const contents = [
file,
foldersFile,
]
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: contents,
config: {
systemInstruction: `
You are an AI file organizer.
Your task is to read and interpret the content of a PDF document and determine
the most suitable folder location for storing it from the provided list of possible folder locations
(contained in folders_hierarchy.json).
Guidelines:
1) Carefully extract and analyze the text and metadata from the PDF.
2) Identify the main subject, context, or category of the document.
3) Select the single most appropriate folder path only from the provided list.
4) If the content is ambiguous, choose the closest logical folder.
5) Never invent content or folder names.
6) Strictly return the result as a JSON object with exactly three fields:
'folder' (string, chosen folder path),
'confidence' (integer between 1 and 100),
"suggestedName": "string, a clean descriptive filename WITHOUT extension,
must be under 7 words, use _ instead of spaces,
keep original if already short and clear.
Do not include file extensions like .pdf, .docx, .png etc.
If the current name is already suitable, keep it as is (without extension).",
'reason' (string, justify your choice shortly).
`
,
responseMimeType: "application/json",
responseSchema: {
folder: Type.STRING,
confidence: Type.INTEGER,
suggestedName: Type.STRING,
reason: Type.STRING
},
},
});
return {
status: 'success',
data: response.text
};
} catch (err) {
return {
status: 'error',
data: err.message
};
}
}