-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini-start.js
More file actions
66 lines (54 loc) · 2.26 KB
/
Copy pathgemini-start.js
File metadata and controls
66 lines (54 loc) · 2.26 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
import dotenv from "dotenv";
import { GoogleGenerativeAI } from "@google/generative-ai";
dotenv.config();
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const today = new Date();
const month = today.getMonth(); // 0-indexed (January = 0, December = 11)
const year = today.getFullYear();
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const currentMonth = monthNames[month];
async function runWeather(data) {
const model = genAI.getGenerativeModel({ model: "gemini-3.5-flash" });
const location = data.location;
const dataString = JSON.stringify(data);
// const prompt =
// `Based on the below forecast data, can you analyse and tell me how the weather will be mostly in ${currentMonth},${year} month in ${location} and what can be the potential natural risks? Please provide me only short conclusion` +
// dataString;
const prompt = `Based on the below forecast data, can you analyse and tell me how the weather will be mostly in ${currentMonth},${year} month in ${location} and what can be the potential natural risks? Please provide me only a short conclusion and avoid using **.\n\n**Analysis:** ${dataString}`;
try {
const result = await model.generateContent(prompt);
const response = await result.response;
return response.text();
} catch (error) {
console.error("Gemini Execution Error:", error.message);
// Fallback to flash-lite if primary flash is somehow unavailable
if (error.status === 404) {
console.log("Attempting fallback to gemini-3.1-flash-lite...");
const fallbackModel = genAI.getGenerativeModel({ model: "gemini-3.1-flash-lite" });
const fallbackResult = await fallbackModel.generateContent(prompt);
return fallbackResult.response.text();
}
throw error;
}
// const prompt2 =
// "Give me short conclusion part about prediction from the following text. Also, don't give me any headings like '*Conclusions*' just give me paragarph" +
// text;
// const result2 = await model.generateContent(prompt2);
// const response2 = await result2.response;
// const text2 = response2.text();
// return text;
}
export { runWeather };