1
0
Files
jecnarozvrh/scrape/parse/v3/call.js

74 lines
1.9 KiB
JavaScript

/*
* Copyright (C) 2025 Jakub Žitník
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
import { GoogleGenAI } from "@google/genai";
import fs from "fs/promises";
const TIMETABLE_PATH = "db/persistent/timetables.json";
export async function setup() {
const timetable = JSON.parse(
await fs.readFile(TIMETABLE_PATH, { encoding: "utf8" })
);
const ai = new GoogleGenAI({
apiKey: process.env.GEMINI_API_KEY
});
const systemPrompt = await fs.readFile("./prompt.txt", "utf-8");
/**
* @param {Object} changesByClass
* {
* "1A": [ ...changes... ],
* "2B": [ ...changes... ]
* }
* @param {number} dayIndex
*/
return async (changesByClass, dayIndex) => {
const input = {};
for (const cls of Object.keys(changesByClass)) {
input[cls] = {
stableSchedule: timetable[cls][dayIndex],
changes: changesByClass[cls]
};
}
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
config: {
systemInstruction: {
parts: [{ text: systemPrompt }]
},
temperature: 0
},
contents: [
{
role: "user",
parts: [{ text: JSON.stringify(input) }]
}
]
});
const aiOutput = response.text ?? "";
try {
return JSON.parse(aiOutput);
} catch {
return { invalid: true, reason: "AI output could not be parsed" };
}
};
}