2026-03-27 13:58:20 +01:00
|
|
|
export async function generateGemini(prompt: string, systemPrompt: string): Promise<string> {
|
2026-03-27 14:06:21 +01:00
|
|
|
// Try all possible ways to get the API key
|
|
|
|
|
const apiKey =
|
|
|
|
|
(window as any).GEMINI_API_KEY ||
|
|
|
|
|
localStorage.getItem('GEMINI_API_KEY') ||
|
|
|
|
|
import.meta.env.VITE_GEMINI_API_KEY ||
|
|
|
|
|
import.meta.env.GEMINI_API_KEY ||
|
|
|
|
|
(process.env as any).GEMINI_API_KEY ||
|
|
|
|
|
'';
|
|
|
|
|
|
2026-03-27 13:58:20 +01:00
|
|
|
if (!apiKey) {
|
2026-03-27 14:06:21 +01:00
|
|
|
console.error('No Gemini API Key found in env or storage');
|
|
|
|
|
throw new Error('Gemini API Key not found. Please set VITE_GEMINI_API_KEY in .env or GEMINI_API_KEY in localStorage.');
|
2026-03-27 13:58:20 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 14:07:27 +01:00
|
|
|
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}`, {
|
2026-03-27 13:58:20 +01:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
contents: [{
|
|
|
|
|
parts: [{
|
|
|
|
|
text: `${systemPrompt}\n\nTask: ${prompt}`
|
|
|
|
|
}]
|
|
|
|
|
}],
|
|
|
|
|
generationConfig: {
|
|
|
|
|
maxOutputTokens: 1000,
|
|
|
|
|
temperature: 0.7
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const errorData = await response.json().catch(() => ({}));
|
|
|
|
|
throw new Error(errorData.error?.message || 'Failed to generate text from Gemini API');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
return data.candidates[0].content.parts[0].text;
|
|
|
|
|
}
|