Spaces:
Build error
Build error
Initial deployment
Browse files- .gitignore +4 -0
- Dockerfile +22 -0
- README.md +43 -4
- package.json +18 -0
- src/index.js +131 -0
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
node_modules/
|
| 2 |
+
.env
|
| 3 |
+
*.log
|
| 4 |
+
.DS_Store
|
Dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:18-alpine
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Copy package files
|
| 6 |
+
COPY package*.json ./
|
| 7 |
+
|
| 8 |
+
# Install dependencies
|
| 9 |
+
RUN npm ci --only=production
|
| 10 |
+
|
| 11 |
+
# Copy source code
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# Expose port (Spaces uses 7860)
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Health check
|
| 18 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
| 19 |
+
CMD node -e "require('http').get('http://localhost:7860/health', (r) => { process.exit(r.statusCode === 200 ? 0 : 1); })"
|
| 20 |
+
|
| 21 |
+
# Start server
|
| 22 |
+
CMD ["node", "src/index.js"]
|
README.md
CHANGED
|
@@ -1,10 +1,49 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: anthropic-gemini-proxy
|
| 3 |
+
emoji: 🤖
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Anthropic-Compatible API Powered by Gemini 2.5
|
| 12 |
+
|
| 13 |
+
A production-grade API proxy providing Claude-compatible endpoints with Gemini 2.5 intelligence.
|
| 14 |
+
|
| 15 |
+
## Features
|
| 16 |
+
|
| 17 |
+
- Claude API Compatible endpoints
|
| 18 |
+
- Streaming support (SSE)
|
| 19 |
+
- Token accounting & usage tracking
|
| 20 |
+
- Rate limiting per API key
|
| 21 |
+
- Production-ready with health checks
|
| 22 |
+
|
| 23 |
+
## Usage
|
| 24 |
+
|
| 25 |
+
### Set Your API Keys
|
| 26 |
+
|
| 27 |
+
Go to Space Settings → Variables and add:
|
| 28 |
+
|
| 29 |
+
- `GEMINI_API_KEY` - Your Google Gemini API key
|
| 30 |
+
- `PROXY_API_KEY` - Secret key for proxy authentication (e.g., `sk-proxy-yoursecret`)
|
| 31 |
+
|
| 32 |
+
### Make Requests
|
| 33 |
+
|
| 34 |
+
```bash
|
| 35 |
+
curl -X POST https://YOUR-SPACE-URL/anthropic/v1/messages \
|
| 36 |
+
-H "x-api-key: sk-proxy-yoursecret" \
|
| 37 |
+
-H "Content-Type: application/json" \
|
| 38 |
+
-d '{
|
| 39 |
+
"model": "claude-3-5-sonnet-20241022",
|
| 40 |
+
"max_tokens": 1024,
|
| 41 |
+
"messages": [{"role": "user", "content": "Hello!"}]
|
| 42 |
+
}'
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
## Endpoints
|
| 46 |
+
|
| 47 |
+
- `POST /anthropic/v1/messages` - Create completion
|
| 48 |
+
- `GET /anthropic/v1/models` - List models
|
| 49 |
+
- `GET /health` - Health check
|
package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "anthropic-gemini-proxy",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "Claude-compatible API powered by Gemini",
|
| 5 |
+
"main": "src/index.js",
|
| 6 |
+
"scripts": {
|
| 7 |
+
"start": "node src/index.js"
|
| 8 |
+
},
|
| 9 |
+
"dependencies": {
|
| 10 |
+
"express": "^4.18.2",
|
| 11 |
+
"helmet": "^7.1.0",
|
| 12 |
+
"cors": "^2.8.5",
|
| 13 |
+
"express-rate-limit": "^7.1.5",
|
| 14 |
+
"@google/generative-ai": "^0.1.3",
|
| 15 |
+
"better-sqlite3": "^9.2.2",
|
| 16 |
+
"dotenv": "^16.3.1"
|
| 17 |
+
}
|
| 18 |
+
}
|
src/index.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const helmet = require('helmet');
|
| 3 |
+
const cors = require('cors');
|
| 4 |
+
const rateLimit = require('express-rate-limit');
|
| 5 |
+
const { GoogleGenerativeAI } = require('@google/generative-ai');
|
| 6 |
+
const Database = require('better-sqlite3');
|
| 7 |
+
|
| 8 |
+
const app = express();
|
| 9 |
+
const PORT = process.env.PORT || 7860;
|
| 10 |
+
|
| 11 |
+
// Security
|
| 12 |
+
app.use(helmet());
|
| 13 |
+
app.use(cors());
|
| 14 |
+
app.use(express.json());
|
| 15 |
+
|
| 16 |
+
// Rate limiting
|
| 17 |
+
const limiter = rateLimit({
|
| 18 |
+
windowMs: 15 * 60 * 1000, // 15 minutes
|
| 19 |
+
max: 100, // Limit each IP to 100 requests per windowMs
|
| 20 |
+
message: { error: { type: 'rate_limit_error', message: 'Too many requests' }}
|
| 21 |
+
});
|
| 22 |
+
app.use('/anthropic', limiter);
|
| 23 |
+
|
| 24 |
+
// Database for usage tracking
|
| 25 |
+
const db = new Database(':memory:');
|
| 26 |
+
db.exec(`CREATE TABLE IF NOT EXISTS usage ( id INTEGER PRIMARY KEY AUTOINCREMENT, api_key TEXT, input_tokens INTEGER, output_tokens INTEGER, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP )`);
|
| 27 |
+
|
| 28 |
+
// Gemini client
|
| 29 |
+
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || '');
|
| 30 |
+
|
| 31 |
+
// Auth middleware
|
| 32 |
+
const authenticate = (req, res, next) => {
|
| 33 |
+
const apiKey = req.headers['x-api-key'];
|
| 34 |
+
if (!apiKey || apiKey !== process.env.PROXY_API_KEY) {
|
| 35 |
+
return res.status(401).json({
|
| 36 |
+
error: { type: 'authentication_error', message: 'Invalid API key' }
|
| 37 |
+
});
|
| 38 |
+
}
|
| 39 |
+
req.apiKey = apiKey;
|
| 40 |
+
next();
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
// Health check
|
| 44 |
+
app.get('/health', (req, res) => {
|
| 45 |
+
res.json({ status: 'healthy', timestamp: new Date().toISOString() });
|
| 46 |
+
});
|
| 47 |
+
|
| 48 |
+
// Models endpoint
|
| 49 |
+
app.get('/anthropic/v1/models', authenticate, (req, res) => {
|
| 50 |
+
res.json({
|
| 51 |
+
data: [
|
| 52 |
+
{
|
| 53 |
+
id: 'claude-3-5-sonnet-20241022',
|
| 54 |
+
name: 'Claude 3.5 Sonnet',
|
| 55 |
+
type: 'model'
|
| 56 |
+
}
|
| 57 |
+
]
|
| 58 |
+
});
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
// Messages endpoint
|
| 62 |
+
app.post('/anthropic/v1/messages', authenticate, async (req, res) => {
|
| 63 |
+
try {
|
| 64 |
+
const { messages, max_tokens = 1024, stream = false } = req.body;
|
| 65 |
+
|
| 66 |
+
if (!messages || !Array.isArray(messages)) {
|
| 67 |
+
return res.status(400).json({
|
| 68 |
+
error: { type: 'invalid_request_error', message: 'messages is required' }
|
| 69 |
+
});
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
// Convert to Gemini format
|
| 73 |
+
const prompt = messages.map(m => `${m.role}: ${m.content}`).join('\n');
|
| 74 |
+
|
| 75 |
+
const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash-exp' });
|
| 76 |
+
|
| 77 |
+
if (stream) {
|
| 78 |
+
res.setHeader('Content-Type', 'text/event-stream');
|
| 79 |
+
res.setHeader('Cache-Control', 'no-cache');
|
| 80 |
+
res.setHeader('Connection', 'keep-alive');
|
| 81 |
+
|
| 82 |
+
const result = await model.generateContentStream(prompt);
|
| 83 |
+
|
| 84 |
+
for await (const chunk of result.stream) {
|
| 85 |
+
const text = chunk.text();
|
| 86 |
+
res.write(`event: content_block_delta\n`);
|
| 87 |
+
res.write(`data: ${JSON.stringify({
|
| 88 |
+
type: 'content_block_delta',
|
| 89 |
+
delta: { type: 'text_delta', text }
|
| 90 |
+
})}\n\n`);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
res.write(`event: message_stop\n`);
|
| 94 |
+
res.write(`data: {}\n\n`);
|
| 95 |
+
res.end();
|
| 96 |
+
} else {
|
| 97 |
+
const result = await model.generateContent(prompt);
|
| 98 |
+
const text = result.response.text();
|
| 99 |
+
|
| 100 |
+
// Track usage
|
| 101 |
+
const inputTokens = Math.ceil(prompt.length / 4);
|
| 102 |
+
const outputTokens = Math.ceil(text.length / 4);
|
| 103 |
+
|
| 104 |
+
db.prepare('INSERT INTO usage (api_key, input_tokens, output_tokens) VALUES (?, ?, ?)')
|
| 105 |
+
.run(req.apiKey, inputTokens, outputTokens);
|
| 106 |
+
|
| 107 |
+
res.json({
|
| 108 |
+
id: `msg_${Date.now()}`,
|
| 109 |
+
type: 'message',
|
| 110 |
+
role: 'assistant',
|
| 111 |
+
content: [{ type: 'text', text }],
|
| 112 |
+
model: 'claude-3-5-sonnet-20241022',
|
| 113 |
+
stop_reason: 'end_turn',
|
| 114 |
+
usage: {
|
| 115 |
+
input_tokens: inputTokens,
|
| 116 |
+
output_tokens: outputTokens
|
| 117 |
+
}
|
| 118 |
+
});
|
| 119 |
+
}
|
| 120 |
+
} catch (error) {
|
| 121 |
+
console.error('Error:', error);
|
| 122 |
+
res.status(500).json({
|
| 123 |
+
error: { type: 'api_error', message: error.message }
|
| 124 |
+
});
|
| 125 |
+
}
|
| 126 |
+
});
|
| 127 |
+
|
| 128 |
+
app.listen(PORT, () => {
|
| 129 |
+
console.log(`🚀 Server running on port ${PORT}`);
|
| 130 |
+
console.log(`📊 Health: http://localhost:${PORT}/health`);
|
| 131 |
+
});
|