Repository navigation
Expand file tree
/
Copy pathmodelRouter.js
More file actions
194 lines (157 loc) · 6.03 KB
/
Copy pathmodelRouter.js
File metadata and controls
194 lines (157 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const fetch = require("node-fetch");
const { v4: uuidv4 } = require("uuid");
module.exports = (app, db) => {
const rateLimits = new Map();
const MINUTE_LIMIT = 10;
const MINUTE_WINDOW = 60 * 1000;
const DAILY_LIMIT = 60;
const DAILY_WINDOW = 24 * 60 * 60 * 1000;
function checkRateLimit(apiKey) {
const now = Date.now();
if (!rateLimits.has(apiKey)) {
rateLimits.set(apiKey, { minute: [], day: [] });
}
const limits = rateLimits.get(apiKey);
limits.minute = limits.minute.filter(ts => now - ts < MINUTE_WINDOW);
limits.day = limits.day.filter(ts => now - ts < DAILY_WINDOW);
if (limits.minute.length >= MINUTE_LIMIT) {
return { allowed: false, error: "minute" };
}
if (limits.day.length >= DAILY_LIMIT) {
return { allowed: false, error: "day" };
}
limits.minute.push(now);
limits.day.push(now);
return { allowed: true };
}
async function handleChat({ apiKey, model, messages, temperature, max_tokens }) {
// Validate API key
const keyResult = await db.execute({
sql: "SELECT * FROM api_keys WHERE api_key = ?",
args: [apiKey]
});
if (keyResult.rows.length === 0) {
throw { status: 401, message: "Invalid API key" };
}
// Rate limit
const limitCheck = checkRateLimit(apiKey);
if (!limitCheck.allowed) {
throw { status: 429, message: "Rate limit exceeded" };
}
let provider, modelName;
if (model.startsWith("nvidia/")) {
provider = "nvidia";
modelName = model.replace("nvidia/", "");
} else if (model.startsWith("cerebras/")) {
provider = "cerebras";
modelName = model.replace("cerebras/", "");
} else {
throw { status: 400, message: "Unsupported model provider" };
}
const payload = {
model: modelName,
messages,
temperature,
max_tokens
};
let upstream;
if (provider === "nvidia") {
upstream = await fetch("https://integrate.api.nvidia.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.NVIDIA_API_KEY}`
},
body: JSON.stringify(payload)
});
} else {
upstream = await fetch("https://api.cerebras.ai/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.CEREBRAS_API_KEY}`
},
body: JSON.stringify(payload)
});
}
if (!upstream.ok) {
const text = await upstream.text();
throw { status: 502, message: text };
}
return await upstream.json();
}
// -------------------------
// 🔹 YOUR ORIGINAL ENDPOINT
// -------------------------
app.post("/chat/completions", async (req, res) => {
try {
const { apiKey, model, messages, temperature, max_tokens } = req.body;
if (!apiKey || !model || !messages) {
return res.status(400).json({ error: "apiKey, model, messages required" });
}
const data = await handleChat({ apiKey, model, messages, temperature, max_tokens });
res.json(data);
} catch (err) {
res.status(err.status || 500).json({
error: err.message || "request failed"
});
}
});
// -------------------------
// 🔹 OPENAI COMPAT ENDPOINT
// -------------------------
app.post("/oai/chat/completions", async (req, res) => {
try {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return res.status(401).json({
error: {
message: "Missing Authorization header",
type: "invalid_request_error"
}
});
}
const apiKey = authHeader.split(" ")[1];
const { model, messages, temperature, max_tokens } = req.body;
if (!model || !messages) {
return res.status(400).json({
error: {
message: "model and messages required",
type: "invalid_request_error"
}
});
}
const data = await handleChat({ apiKey, model, messages, temperature, max_tokens });
// Normalize response
const response = {
id: `chatcmpl-${uuidv4()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model,
choices: [
{
index: 0,
message: {
role: "assistant",
content: data.choices?.[0]?.message?.content || ""
},
finish_reason: data.choices?.[0]?.finish_reason || "stop"
}
],
usage: data.usage || {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0
}
};
res.json(response);
} catch (err) {
res.status(err.status || 500).json({
error: {
message: err.message || "server_error",
type: "server_error"
}
});
}
});
};