Files
Craze-Data-check/src/services/gemini.ts
T

42 lines
1.3 KiB
TypeScript
Raw Normal View History

export async function generateGemini(prompt: string, systemPrompt: string): Promise<string> {
// 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 ||
'';
if (!apiKey) {
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.');
}
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${apiKey}`, {
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;
}