Up
This commit is contained in:
parent
237d0c65ba
commit
930af8d9e9
5 changed files with 31 additions and 11 deletions
|
|
@ -10,6 +10,9 @@ PORT=3000
|
|||
# Logging configuration
|
||||
LOG_DIR=./logs
|
||||
|
||||
# Rate limit configuration (duration in milliseconds)
|
||||
RATE_LIMIT_DURATION_MS=60000
|
||||
|
||||
# Cloudflare Tunnel configuration (for Docker deployment)
|
||||
# Get your tunnel token from: https://dash.cloudflare.com/
|
||||
CLOUDFLARE_TUNNEL_TOKEN=your_cloudflare_tunnel_token_here
|
||||
|
|
|
|||
|
|
@ -3,15 +3,16 @@ import dotenv from 'dotenv';
|
|||
dotenv.config();
|
||||
|
||||
export const config = {
|
||||
apiPassword: process.env.API_PASSWORD || 'default_password',
|
||||
apiPassword: process.env.API_PASSWORD,
|
||||
geminiApiKeys: (process.env.GEMINI_API_KEYS || '').split(',').filter(key => key.trim()),
|
||||
port: parseInt(process.env.PORT || '3000', 10),
|
||||
logDir: process.env.LOG_DIR || './logs',
|
||||
model: 'gemini-flash-lite-latest'
|
||||
model: 'gemini-flash-lite-latest',
|
||||
rateLimitDurationMs: parseInt(process.env.RATE_LIMIT_DURATION_MS || '60000', 10)
|
||||
};
|
||||
|
||||
if (!config.apiPassword || config.apiPassword === 'default_password') {
|
||||
console.warn('WARNING: Using default API password. Please set API_PASSWORD in .env file');
|
||||
if (!config.apiPassword) {
|
||||
throw new Error('API_PASSWORD is required. Please set it in .env file');
|
||||
}
|
||||
|
||||
if (config.geminiApiKeys.length === 0) {
|
||||
|
|
|
|||
|
|
@ -29,8 +29,8 @@ class KeyRotationManager {
|
|||
return Date.now() < keyUsage.rateLimitedUntil;
|
||||
}
|
||||
|
||||
private markRateLimited(keyUsage: KeyUsage, durationMs: number = 60000): void {
|
||||
keyUsage.rateLimitedUntil = Date.now() + durationMs;
|
||||
private markRateLimited(keyUsage: KeyUsage, durationMs?: number): void {
|
||||
keyUsage.rateLimitedUntil = Date.now() + (durationMs || config.rateLimitDurationMs);
|
||||
}
|
||||
|
||||
getNextAvailableKey(): string {
|
||||
|
|
@ -71,9 +71,12 @@ class KeyRotationManager {
|
|||
return [...this.keys];
|
||||
}
|
||||
|
||||
createGenerativeAI(): GoogleGenerativeAI {
|
||||
createGenerativeAI(): { client: GoogleGenerativeAI; apiKey: string } {
|
||||
const apiKey = this.getNextAvailableKey();
|
||||
return new GoogleGenerativeAI(apiKey);
|
||||
return {
|
||||
client: new GoogleGenerativeAI(apiKey),
|
||||
apiKey
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ class RequestLogger {
|
|||
}
|
||||
|
||||
generateRequestId(): string {
|
||||
return `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
return `req_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ function convertToOpenAIResponse(geminiResponse: any, model: string, requestId:
|
|||
// POST /v1/chat/completions
|
||||
router.post('/v1/chat/completions', async (req: Request, res: Response) => {
|
||||
const requestId = requestLogger.generateRequestId();
|
||||
let apiKey = '';
|
||||
|
||||
try {
|
||||
const body: OpenAIChatRequest = req.body;
|
||||
|
|
@ -68,7 +69,8 @@ router.post('/v1/chat/completions', async (req: Request, res: Response) => {
|
|||
}
|
||||
|
||||
// Get Gemini client with rotated key
|
||||
const genAI = keyRotationManager.createGenerativeAI();
|
||||
const { client: genAI, apiKey: currentApiKey } = keyRotationManager.createGenerativeAI();
|
||||
apiKey = currentApiKey;
|
||||
const model = genAI.getGenerativeModel({ model: config.model });
|
||||
|
||||
// Convert messages
|
||||
|
|
@ -115,7 +117,18 @@ router.post('/v1/chat/completions', async (req: Request, res: Response) => {
|
|||
});
|
||||
|
||||
// Check if it's a rate limit error
|
||||
if (error.message?.includes('RATE_LIMIT_EXCEEDED') || error.status === 429) {
|
||||
const isRateLimitError =
|
||||
error.status === 429 ||
|
||||
error.message?.includes('RATE_LIMIT_EXCEEDED') ||
|
||||
error.message?.includes('quota') ||
|
||||
error.message?.includes('limit') ||
|
||||
error.code === 429 ||
|
||||
error.statusCode === 429;
|
||||
|
||||
if (isRateLimitError) {
|
||||
// Mark the key as rate limited
|
||||
keyRotationManager.markKeyAsRateLimited(apiKey);
|
||||
|
||||
res.status(429).json({
|
||||
error: {
|
||||
message: 'Rate limit exceeded. Please try again later.',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue