demo / test_gemini.js
samiee2213's picture
Upload 55 files
65a8bf3 verified
Raw
History Blame Contribute Delete
1.34 kB
const fs = require('fs');
function load_env_key() {
if (fs.existsSync('.env')) {
const env = fs.readFileSync('.env', 'utf-8');
const match = env.match(/^GEMINI_API_KEY=(.+)$/m);
if (match) return match[1].trim().replace(/['"]/g, '');
}
return '';
}
const apiKey = load_env_key();
console.log(`Loaded API Key prefix: ${apiKey.substring(0, 8)}...`);
const payload = {
contents: [{ parts: [{ text: "Say 'Gemini API is working!'" }] }],
generationConfig: { temperature: 0.2 }
};
const models = ["gemini-2.0-flash", "gemini-1.5-flash-latest", "gemini-1.5-flash", "gemini-1.5-pro"];
async function testModels() {
for (const model of models) {
const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`;
console.log(`\nTrying model: ${model}...`);
try {
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
const data = await res.json();
if (res.ok) {
console.log(`Success! Response: ${data.candidates[0].content.parts[0].text.trim()}`);
} else {
console.log(`HTTP Error ${res.status}: ${JSON.stringify(data)}`);
}
} catch (err) {
console.log(`Error: ${err.message}`);
}
}
}
testModels();