mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 18:25:24 +02:00
33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
export async function generateGemini(prompt: string, systemPrompt: string): Promise<string> {
|
|||
|
|
const apiKey = import.meta.env.GEMINI_API_KEY || localStorage.getItem('GEMINI_API_KEY') || '';
|
||
|
|
if (!apiKey) {
|
||
|
|
throw new Error('Gemini API Key not found. Please set GEMINI_API_KEY in .env or GEMINI_API_KEY in localStorage.');
|
||
|
|
}
|
||
|
|
|
||
|
|
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp: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;
|
||
|
|
}
|