Skip to content

Commit 03f07df

Browse files
author
Your Name
committed
feat: Add Cerebras and Google Gemini as fallbacks for Groq
Implement intelligent fallback chain: 1. Groq (primary) - fastest, free tier 2. Cerebras (fallback 1) - fast inference 3. Google Gemini (fallback 2) - reliable 4. HuggingFace (fallback 3) - final option Features: - Automatic provider switching on failure - Provider priority ordering - Try up to 3 models for better reliability - Improved error messages mentioning all providers This ensures the AI always responds even if Groq is down.
1 parent 86a7dba commit 03f07df

5 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/ai/smart-fallback.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,37 @@ function isCriticalFailure(error: unknown): boolean {
6868

6969
/**
7070
* Get fallback models for a given category
71-
* Returns models in order of preference (most capable first)
71+
* Returns models in order of preference:
72+
* 1. Groq models (fastest, primary)
73+
* 2. Cerebras models (fast fallback)
74+
* 3. Google Gemini models (reliable fallback)
75+
* 4. HuggingFace models (final fallback)
7276
*/
7377
function getFallbackModels(category: ModelCategory): ModelConfig[] {
7478
const registry = getModelRegistry();
79+
const allModels = registry.getAvailableModels();
80+
81+
// Define provider priority order
82+
const providerPriority = ['groq', 'cerebras', 'google', 'huggingface'];
83+
84+
// Get models for the requested category
7585
const categoryModels = registry.getModelsByCategory(category);
7686

7787
if (categoryModels.length > 0) {
78-
return categoryModels;
88+
// Sort by provider priority
89+
return categoryModels.sort((a, b) => {
90+
const aPriority = providerPriority.indexOf(a.provider);
91+
const bPriority = providerPriority.indexOf(b.provider);
92+
return aPriority - bPriority;
93+
});
7994
}
8095

81-
// If no models in category, return all available models
82-
return registry.getAvailableModels();
96+
// If no models in category, return all available models sorted by provider priority
97+
return allModels.sort((a, b) => {
98+
const aPriority = providerPriority.indexOf(a.provider);
99+
const bPriority = providerPriority.indexOf(b.provider);
100+
return aPriority - bPriority;
101+
});
83102
}
84103

85104
/**
@@ -212,8 +231,9 @@ export async function generateWithSmartFallback(
212231
throw new Error('No models available. Please check your GROQ_API_KEY configuration at https://console.groq.com/keys');
213232
}
214233

215-
// Limit to 2 models maximum for Netlify timeout constraints
216-
const maxModelsToTry = Math.min(modelsToTry.length, 2);
234+
// Limit to 3 models maximum for better fallback coverage
235+
// Groq → Cerebras → Google Gemini
236+
const maxModelsToTry = Math.min(modelsToTry.length, 3);
217237

218238
// Try each model in sequence
219239
for (let i = 0; i < maxModelsToTry; i++) {
@@ -294,7 +314,7 @@ export function getFriendlyErrorMessage(error: unknown): string {
294314
}
295315

296316
if (errorMessage.includes('All models failed')) {
297-
return 'AI service is currently unavailable. Please check your Groq API key and try again in a few minutes.';
317+
return 'All AI providers (Groq, Cerebras, Google Gemini) are currently unavailable. Please check your API keys and try again in a few minutes.';
298318
}
299319

300320
return 'An unexpected error occurred. Please try again.';

src/app/account-settings/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export default function AccountSettingsPage() {
238238

239239
<div className="min-h-screen bg-background">
240240
<PageHeader
241-
backLink="/user-management"
241+
backLink="/account"
242242
backText="Back to User Management"
243243
title="Account Settings"
244244
/>

src/app/privacy/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ export default function PrivacyPage() {
600600
Terms of Service
601601
</Button>
602602
</Link>
603-
<Link href="/user-management">
603+
<Link href="/account">
604604
<Button variant="outline" size="sm">
605605
Manage Your Data
606606
</Button>

src/app/user-management/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default function UserManagementPage() {
118118
title="User Management | CODEEX AI"
119119
description="Manage your CODEEX AI account, view features, and access settings"
120120
keywords={['user management', 'account', 'AI features']}
121-
canonical="/user-management"
121+
canonical="/account"
122122
/>
123123

124124
<div className="min-h-screen bg-background">

src/lib/ai-system-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ CodeEx is a free, open-source AI platform providing access to 35+ AI models thro
4545
4646
## Key Pages
4747
- /chat - Main chat interface
48-
- /user-management - Feature dashboard
48+
- /account - Feature dashboard
4949
- /account-settings - Profile and security
5050
- /models - List of all 35+ models
5151
- /documentation - Comprehensive guides

0 commit comments

Comments
 (0)