问题所在
Gemini API频繁返回429(限流)和503(服务器错误)响应,尤其在高峰时段。依赖Gemini搜索的代理在这些时段要么产生幻觉要么完全停止响应。
Scavio 解决方案
添加中间件层检测Gemini错误并将搜索查询路由到搜索API。中间件包装Gemini调用,检测429/503响应,自动切换到Scavio,Gemini恢复后自动回切。
之前
降级前,支持代理使用Gemini搜索告诉客户错误的产品价格(因为搜索失败时回退到过时的训练数据)。客户投诉导致退款。
之后
添加降级后,Gemini宕机期间搜索查询路由到Scavio。3小时宕机期间零幻觉,用户体验无感知降级。
适用人群
使用Gemini API进行搜索且需要在Gemini限流和宕机期间可靠降级的代理开发者。
核心优势
- Gemini宕机期间零幻觉
- 自动降级和恢复无需代码修改
- 搜索API降级每小时宕机成本仅几分钱
- 健康追踪提前识别Gemini性能退化
- 兼容任何基于Gemini的代理框架
Python 示例
Python
import requests, os, time
H = {'x-api-key': os.environ['SCAVIO_API_KEY']}
class GeminiFallback:
def __init__(self):
self.gemini_healthy = True
self.last_check = 0
def search(self, query: str) -> dict:
if self.gemini_healthy:
try:
# Try Gemini grounding first
# gemini_result = call_gemini(query)
# return gemini_result
pass
except Exception:
self.gemini_healthy = False
self.last_check = time.time()
# Fallback to Scavio
r = requests.post('https://api.scavio.dev/api/v1/search', headers=H,
json={'platform': 'google', 'query': query}, timeout=10).json()
# Health check every 5 minutes
if time.time() - self.last_check > 300:
self.gemini_healthy = True
self.last_check = time.time()
return {'source': 'scavio_fallback',
'results': r.get('organic_results', [])[:3]}
fb = GeminiFallback()
result = fb.search('current mortgage rate 2026')
print(f"Source: {result['source']}, Results: {len(result['results'])}")JavaScript 示例
JavaScript
const H = { 'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json' };
class GeminiFallback {
constructor() { this.healthy = true; this.lastCheck = 0; }
async search(query) {
if (this.healthy) {
try { /* const r = await callGemini(query); return r; */ } catch {
this.healthy = false; this.lastCheck = Date.now();
}
}
const r = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST', headers: H,
body: JSON.stringify({ platform: 'google', query })
}).then(r => r.json());
if (Date.now() - this.lastCheck > 300000) { this.healthy = true; this.lastCheck = Date.now(); }
return { source: 'scavio_fallback', results: (r.organic_results || []).slice(0, 3) };
}
}
const fb = new GeminiFallback();
const r = await fb.search('current mortgage rate 2026');
console.log(`${r.source}: ${r.results.length} results`);使用的平台
包含知识图谱、PAA和AI概览的网页搜索