-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathor_proxy.js
More file actions
187 lines (180 loc) · 6.3 KB
/
or_proxy.js
File metadata and controls
187 lines (180 loc) · 6.3 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
const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");
const bodyParser = require("body-parser");
const axios = require("axios");
const app = express();
const PORT = process.env.PORT || 3001;
function applyCaching(messageArray, ttl1h) {
for (let i = messageArray.length - 1; i >= 0; i--) {
if (typeof messageArray[i]["content"] == "string") {
messageArray[i]["content"] = [
{
type: "text",
text: messageArray[i]["content"],
cache_control: ttl1h
? {
type: "ephemeral",
ttl: "1h",
}
: {
type: "ephemeral",
},
},
];
return;
} else if (Array.isArray(messageArray[i]["content"])) {
if (typeof messageArray[i]["content"].length != "number") {
return; // not sure how this could happen but just to be safe
}
for (let j = messageArray[i]["content"].length; j >= 0; j--) {
if (messageArray[i]["content"][j]["type"] == "text") {
messageArray[i]["content"][j]["cache_control"] = {
type: "ephemeral",
};
return;
}
}
} else {
// Unknown message type
}
}
}
app.use(express.json({ limit: "10gb" }));
app.use(
express.urlencoded({
limit: "10gb",
extended: true,
parameterLimit: 10000000,
})
);
app.use("/v1/chat/completions", bodyParser.raw({ type: "application/json" }));
app.post("/v1/chat/completions", async (req, res) => {
try {
const requestBody = req.body;
let modifiedBody = { ...requestBody };
const model = requestBody.model;
let slug = model;
let params = [];
const parts = model.split("$");
if (parts.length > 1) {
slug = parts[0];
params = parts[1].split(",");
}
for (let param of params) {
if (
[
"int4",
"int8",
"fp4",
"fp6",
"fp8",
"fp16",
"bf16",
"fp32",
].includes(param)
) {
// Quantization lock
if (!("provider" in modifiedBody)) {
modifiedBody["provider"] = {};
}
if (!("quantizations" in modifiedBody.provider)) {
modifiedBody.provider["quantizations"] = [];
}
modifiedBody.provider.quantizations.push(param);
} else if (param.startsWith("think")) {
// Thinking options
if (param.includes(".")) {
let thinking_option = param.split(".")[1];
if (["no", "off"].includes(thinking_option)) {
modifiedBody["reasoning"] = { enabled: false };
} else if (isNaN(thinking_option)) {
modifiedBody["reasoning"] = {
enabled: true,
effort: thinking_option,
};
} else {
modifiedBody["reasoning"] = {
enabled: true,
max_tokens: +thinking_option,
};
}
} else {
modifiedBody["reasoning"] = { enabled: true };
}
} else if (param == "cache") {
applyCaching(modifiedBody["messages"], false);
} else if (param == "cache1h") {
applyCaching(modifiedBody["messages"], true);
} else if (param == "zdr") {
// Zero Data Retention endpoint requirement
if (!("provider" in modifiedBody)) {
modifiedBody["provider"] = {};
}
modifiedBody["provider"]["zdr"] = true;
} else {
// If nothing else matches, its a provider name
if (!("provider" in modifiedBody)) {
modifiedBody["provider"] = {};
}
if (!("only" in modifiedBody.provider)) {
modifiedBody.provider["only"] = [];
}
modifiedBody.provider.only.push(param);
}
}
modifiedBody.model = slug;
modifiedBody.usage = {
include: true,
};
const headers = { ...req.headers };
headers["host"] = "openrouter.ai";
delete headers["content-length"];
const response = await axios({
method: "POST",
url: "https://openrouter.ai/api/v1/chat/completions",
data: modifiedBody,
headers,
responseType: "stream",
});
Object.keys(response.headers).forEach((key) => {
res.setHeader(key, response.headers[key]);
});
response.data.on("data", (chunk) => {
res.write(chunk);
});
response.data.on("end", async () => {
res.end();
});
response.data.on("error", async (err) => {
console.error("Stream error:", err);
res.status(500).send(`Internal Server Error: ${err.message}`);
});
} catch (err) {
console.error(err.message);
return res.status(500).send({
error: `Internal Server Error: ${err.message}`,
details: err.message,
});
}
});
app.use(
"/v1",
createProxyMiddleware({
target: "https://www.openrouter.ai",
changeOrigin: true,
pathRewrite: (path, req) => {
return "api/" + path;
},
onError: (err, req, res) => {
console.error("Proxy error:", err);
res.status(502).json({ error: "Proxy request failed" });
},
})
);
app.listen(PORT, () => {
console.log(`Proxy server running on port ${PORT}`);
});
process.on("SIGINT", async () => {
console.log("Shutting down...");
process.exit(0);
});