mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 16:35:24 +02:00
fix(gemini): implement model fallback system for reliability
This commit is contained in:
+24
-2
@@ -13,7 +13,19 @@ export async function generateGemini(prompt: string, systemPrompt: string): Prom
|
|||||||
throw new Error('Gemini API Key not found. Please set VITE_GEMINI_API_KEY in .env or GEMINI_API_KEY in localStorage.');
|
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}`, {
|
// Model fallback list - based on March 2026 status
|
||||||
|
const modelOptions = [
|
||||||
|
'gemini-2.0-flash',
|
||||||
|
'gemini-1.5-flash',
|
||||||
|
'gemini-pro'
|
||||||
|
];
|
||||||
|
|
||||||
|
let lastError: any = null;
|
||||||
|
|
||||||
|
for (const modelName of modelOptions) {
|
||||||
|
try {
|
||||||
|
console.log(`Trying Gemini model: ${modelName}...`);
|
||||||
|
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/${modelName}:generateContent?key=${apiKey}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@@ -33,9 +45,19 @@ export async function generateGemini(prompt: string, systemPrompt: string): Prom
|
|||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json().catch(() => ({}));
|
const errorData = await response.json().catch(() => ({}));
|
||||||
throw new Error(errorData.error?.message || 'Failed to generate text from Gemini API');
|
console.warn(`Model ${modelName} failed:`, errorData);
|
||||||
|
lastError = errorData;
|
||||||
|
continue; // Try next model
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
console.log(`Successfully used model: ${modelName}`);
|
||||||
return data.candidates[0].content.parts[0].text;
|
return data.candidates[0].content.parts[0].text;
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Fetch error with ${modelName}:`, err);
|
||||||
|
lastError = err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(lastError?.error?.message || lastError?.message || 'All Gemini models failed. Check console for details.');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user