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
|
# Logging configuration
|
||||||
LOG_DIR=./logs
|
LOG_DIR=./logs
|
||||||
|
|
||||||
|
# Rate limit configuration (duration in milliseconds)
|
||||||
|
RATE_LIMIT_DURATION_MS=60000
|
||||||
|
|
||||||
# Cloudflare Tunnel configuration (for Docker deployment)
|
# Cloudflare Tunnel configuration (for Docker deployment)
|
||||||
# Get your tunnel token from: https://dash.cloudflare.com/
|
# Get your tunnel token from: https://dash.cloudflare.com/
|
||||||
CLOUDFLARE_TUNNEL_TOKEN=your_cloudflare_tunnel_token_here
|
CLOUDFLARE_TUNNEL_TOKEN=your_cloudflare_tunnel_token_here
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,16 @@ import dotenv from 'dotenv';
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
export const 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()),
|
geminiApiKeys: (process.env.GEMINI_API_KEYS || '').split(',').filter(key => key.trim()),
|
||||||
port: parseInt(process.env.PORT || '3000', 10),
|
port: parseInt(process.env.PORT || '3000', 10),
|
||||||
logDir: process.env.LOG_DIR || './logs',
|
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') {
|
if (!config.apiPassword) {
|
||||||
console.warn('WARNING: Using default API password. Please set API_PASSWORD in .env file');
|
throw new Error('API_PASSWORD is required. Please set it in .env file');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.geminiApiKeys.length === 0) {
|
if (config.geminiApiKeys.length === 0) {
|
||||||
|
|
|
||||||
|
|
@ -29,8 +29,8 @@ class KeyRotationManager {
|
||||||
return Date.now() < keyUsage.rateLimitedUntil;
|
return Date.now() < keyUsage.rateLimitedUntil;
|
||||||
}
|
}
|
||||||
|
|
||||||
private markRateLimited(keyUsage: KeyUsage, durationMs: number = 60000): void {
|
private markRateLimited(keyUsage: KeyUsage, durationMs?: number): void {
|
||||||
keyUsage.rateLimitedUntil = Date.now() + durationMs;
|
keyUsage.rateLimitedUntil = Date.now() + (durationMs || config.rateLimitDurationMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
getNextAvailableKey(): string {
|
getNextAvailableKey(): string {
|
||||||
|
|
@ -71,9 +71,12 @@ class KeyRotationManager {
|
||||||
return [...this.keys];
|
return [...this.keys];
|
||||||
}
|
}
|
||||||
|
|
||||||
createGenerativeAI(): GoogleGenerativeAI {
|
createGenerativeAI(): { client: GoogleGenerativeAI; apiKey: string } {
|
||||||
const apiKey = this.getNextAvailableKey();
|
const apiKey = this.getNextAvailableKey();
|
||||||
return new GoogleGenerativeAI(apiKey);
|
return {
|
||||||
|
client: new GoogleGenerativeAI(apiKey),
|
||||||
|
apiKey
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ class RequestLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
generateRequestId(): string {
|
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
|
// POST /v1/chat/completions
|
||||||
router.post('/v1/chat/completions', async (req: Request, res: Response) => {
|
router.post('/v1/chat/completions', async (req: Request, res: Response) => {
|
||||||
const requestId = requestLogger.generateRequestId();
|
const requestId = requestLogger.generateRequestId();
|
||||||
|
let apiKey = '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const body: OpenAIChatRequest = req.body;
|
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
|
// 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 });
|
const model = genAI.getGenerativeModel({ model: config.model });
|
||||||
|
|
||||||
// Convert messages
|
// Convert messages
|
||||||
|
|
@ -115,7 +117,18 @@ router.post('/v1/chat/completions', async (req: Request, res: Response) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if it's a rate limit error
|
// 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({
|
res.status(429).json({
|
||||||
error: {
|
error: {
|
||||||
message: 'Rate limit exceeded. Please try again later.',
|
message: 'Rate limit exceeded. Please try again later.',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue