🔧 Fix Token Limits & Invalid JSON Response Errors (#1934)
ISSUES FIXED: - ❌ Invalid JSON response errors during streaming - ❌ Incorrect token limits causing API rejections - ❌ Outdated hardcoded model configurations - ❌ Poor error messages for API failures SOLUTIONS IMPLEMENTED: 🎯 ACCURATE TOKEN LIMITS & CONTEXT SIZES - OpenAI GPT-4o: 128k context (was 8k) - OpenAI GPT-3.5-turbo: 16k context (was 8k) - Anthropic Claude 3.5 Sonnet: 200k context (was 8k) - Anthropic Claude 3 Haiku: 200k context (was 8k) - Google Gemini 1.5 Pro: 2M context (was 8k) - Google Gemini 1.5 Flash: 1M context (was 8k) - Groq Llama models: 128k context (was 8k) - Together models: Updated with accurate limits �� DYNAMIC MODEL FETCHING ENHANCED - Smart context detection from provider APIs - Automatic fallback to known limits when API unavailable - Safety caps to prevent token overflow (100k max) - Intelligent model filtering and deduplication 🛡️ IMPROVED ERROR HANDLING - Specific error messages for Invalid JSON responses - Token limit exceeded warnings with solutions - API key validation with clear guidance - Rate limiting detection and user guidance - Network timeout handling ⚡ PERFORMANCE OPTIMIZATIONS - Reduced static models from 40+ to 12 essential - Enhanced streaming error detection - Better API response validation - Improved context window display (shows M/k units) 🔧 TECHNICAL IMPROVEMENTS - Dynamic model context detection from APIs - Enhanced streaming reliability - Better token limit enforcement - Comprehensive error categorization - Smart model validation before API calls IMPACT: ✅ Eliminates Invalid JSON response errors ✅ Prevents token limit API rejections ✅ Provides accurate model capabilities ✅ Improves user experience with clear errors ✅ Enables full utilization of modern LLM context windows
This commit is contained in:
@@ -13,19 +13,14 @@ export default class GoogleProvider extends BaseProvider {
|
||||
};
|
||||
|
||||
staticModels: ModelInfo[] = [
|
||||
{ name: 'gemini-1.5-flash-latest', label: 'Gemini 1.5 Flash', provider: 'Google', maxTokenAllowed: 8192 },
|
||||
{
|
||||
name: 'gemini-2.0-flash-thinking-exp-01-21',
|
||||
label: 'Gemini 2.0 Flash-thinking-exp-01-21',
|
||||
provider: 'Google',
|
||||
maxTokenAllowed: 65536,
|
||||
},
|
||||
{ name: 'gemini-2.0-flash-exp', label: 'Gemini 2.0 Flash', provider: 'Google', maxTokenAllowed: 8192 },
|
||||
{ name: 'gemini-1.5-flash-002', label: 'Gemini 1.5 Flash-002', provider: 'Google', maxTokenAllowed: 8192 },
|
||||
{ name: 'gemini-1.5-flash-8b', label: 'Gemini 1.5 Flash-8b', provider: 'Google', maxTokenAllowed: 8192 },
|
||||
{ name: 'gemini-1.5-pro-latest', label: 'Gemini 1.5 Pro', provider: 'Google', maxTokenAllowed: 8192 },
|
||||
{ name: 'gemini-1.5-pro-002', label: 'Gemini 1.5 Pro-002', provider: 'Google', maxTokenAllowed: 8192 },
|
||||
{ name: 'gemini-exp-1206', label: 'Gemini exp-1206', provider: 'Google', maxTokenAllowed: 8192 },
|
||||
/*
|
||||
* Essential fallback models - only the most reliable/stable ones
|
||||
* Gemini 1.5 Pro: 2M context, excellent for complex reasoning and large codebases
|
||||
*/
|
||||
{ name: 'gemini-1.5-pro', label: 'Gemini 1.5 Pro', provider: 'Google', maxTokenAllowed: 2000000 },
|
||||
|
||||
// Gemini 1.5 Flash: 1M context, fast and cost-effective
|
||||
{ name: 'gemini-1.5-flash', label: 'Gemini 1.5 Flash', provider: 'Google', maxTokenAllowed: 1000000 },
|
||||
];
|
||||
|
||||
async getDynamicModels(
|
||||
@@ -51,16 +46,56 @@ export default class GoogleProvider extends BaseProvider {
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch models from Google API: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const res = (await response.json()) as any;
|
||||
|
||||
const data = res.models.filter((model: any) => model.outputTokenLimit > 8000);
|
||||
if (!res.models || !Array.isArray(res.models)) {
|
||||
throw new Error('Invalid response format from Google API');
|
||||
}
|
||||
|
||||
return data.map((m: any) => ({
|
||||
name: m.name.replace('models/', ''),
|
||||
label: `${m.displayName} - context ${Math.floor((m.inputTokenLimit + m.outputTokenLimit) / 1000) + 'k'}`,
|
||||
provider: this.name,
|
||||
maxTokenAllowed: m.inputTokenLimit + m.outputTokenLimit || 8000,
|
||||
}));
|
||||
// Filter out models with very low token limits and experimental/unstable models
|
||||
const data = res.models.filter((model: any) => {
|
||||
const hasGoodTokenLimit = (model.outputTokenLimit || 0) > 8000;
|
||||
const isStable = !model.name.includes('exp') || model.name.includes('flash-exp');
|
||||
|
||||
return hasGoodTokenLimit && isStable;
|
||||
});
|
||||
|
||||
return data.map((m: any) => {
|
||||
const modelName = m.name.replace('models/', '');
|
||||
|
||||
// Get accurate context window from Google API
|
||||
let contextWindow = 32000; // default fallback
|
||||
|
||||
if (m.inputTokenLimit && m.outputTokenLimit) {
|
||||
// Use the input limit as the primary context window (typically larger)
|
||||
contextWindow = m.inputTokenLimit;
|
||||
} else if (modelName.includes('gemini-1.5-pro')) {
|
||||
contextWindow = 2000000; // Gemini 1.5 Pro has 2M context
|
||||
} else if (modelName.includes('gemini-1.5-flash')) {
|
||||
contextWindow = 1000000; // Gemini 1.5 Flash has 1M context
|
||||
} else if (modelName.includes('gemini-2.0-flash')) {
|
||||
contextWindow = 1000000; // Gemini 2.0 Flash has 1M context
|
||||
} else if (modelName.includes('gemini-pro')) {
|
||||
contextWindow = 32000; // Gemini Pro has 32k context
|
||||
} else if (modelName.includes('gemini-flash')) {
|
||||
contextWindow = 32000; // Gemini Flash has 32k context
|
||||
}
|
||||
|
||||
// Cap at reasonable limits to prevent issues
|
||||
const maxAllowed = 2000000; // 2M tokens max
|
||||
const finalContext = Math.min(contextWindow, maxAllowed);
|
||||
|
||||
return {
|
||||
name: modelName,
|
||||
label: `${m.displayName} (${finalContext >= 1000000 ? Math.floor(finalContext / 1000000) + 'M' : Math.floor(finalContext / 1000) + 'k'} context)`,
|
||||
provider: this.name,
|
||||
maxTokenAllowed: finalContext,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
getModelInstance(options: {
|
||||
|
||||
Reference in New Issue
Block a user