Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
246 lines (216 loc) · 6.65 KB
/
Copy pathindex.js
File metadata and controls
246 lines (216 loc) · 6.65 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const express = require("express");
const Anthropic = require("@anthropic-ai/sdk");
const cors = require("cors");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const fs = require("fs");
const https = require("https");
require("dotenv").config();
const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
const EXPECTED_STARTS = String(process.env.EXPECTED_STARTS || "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const HOST = process.env.HOST || "localhost";
const PORT = process.env.PORT || 3000;
const SSL_KEY_PATH = process.env.SSL_KEY_PATH || null;
const SSL_PEM_PATH = process.env.SSL_PEM_PATH || null;
const ANTHROPIC_MODEL = process.env.ANTHROPIC_MODEL || "claude-sonnet-5-5";
const MAX_COMPLETION_TOKENS = Number(process.env.MAX_COMPLETION_TOKENS) || 4096;
const app = express();
const anthropic = new Anthropic({ apiKey: ANTHROPIC_API_KEY });
app.use(cors());
app.use(bodyParser.json({ limit: "2mb" }));
app.use(morgan("dev"));
app.get("/", (req, res) => {
res.status(200).send("Hello, World!");
});
function toTextContent(content) {
if (typeof content === "string") return content;
if (Array.isArray(content)) {
return content
.map((part) => {
if (typeof part === "string") return part;
if (part && typeof part.text === "string") return part.text;
return "";
})
.join("");
}
return content == null ? "" : String(content);
}
function mapResponseFormat(responseFormat) {
if (!responseFormat || typeof responseFormat !== "object") return null;
if (responseFormat.type === "json_schema") {
const jsonSchema = responseFormat.json_schema || {};
const schema = jsonSchema.schema || responseFormat.schema;
if (!schema || typeof schema !== "object") return null;
return {
format: {
type: "json_schema",
schema,
},
};
}
return null;
}
function buildAnthropicRequest(body) {
const { temperature, messages = [], response_format, output_config, ...rest } =
body || {};
const systemParts = [];
const anthropicMessages = [];
for (const message of messages) {
if (!message || !message.role) continue;
const text = toTextContent(message.content);
if (message.role === "system") {
if (text) systemParts.push(text);
continue;
}
if (message.role !== "user" && message.role !== "assistant") continue;
anthropicMessages.push({ role: message.role, content: text });
}
const request = {
model: ANTHROPIC_MODEL,
max_tokens: MAX_COMPLETION_TOKENS,
messages: anthropicMessages,
};
if (systemParts.length) {
request.system = systemParts.join("\n\n");
}
// Client model / max_tokens / n / stop / aiAPIKey are intentionally ignored.
for (const key of ["stop_sequences", "top_p", "top_k", "metadata"]) {
if (rest[key] !== undefined) request[key] = rest[key];
}
if (output_config && typeof output_config === "object") {
request.output_config = output_config;
} else {
const mapped = mapResponseFormat(response_format);
if (mapped) {
request.output_config = mapped;
}
}
// Anthropic rejects temperature (and similar sampling knobs) with structured outputs.
if (!request.output_config && temperature !== undefined) {
request.temperature = temperature;
}
return request;
}
function toOpenAiStyleResponse(message) {
const text = (message.content || [])
.filter((block) => block.type === "text")
.map((block) => block.text)
.join("");
return {
id: message.id,
object: "chat.completion",
model: message.model,
choices: [
{
index: 0,
message: {
role: "assistant",
content: text,
},
finish_reason: message.stop_reason || "stop",
},
],
usage: {
prompt_tokens: message.usage && message.usage.input_tokens,
completion_tokens: message.usage && message.usage.output_tokens,
total_tokens:
message.usage &&
(message.usage.input_tokens || 0) + (message.usage.output_tokens || 0),
},
anthropic: message,
};
}
function normalizeError(error) {
const status = error.status || error.statusCode || 500;
const message =
(error.error && error.error.message) ||
error.message ||
"Unexpected error from AI provider.";
const type =
(error.error && error.error.type) || error.type || error.name || "api_error";
return {
status,
body: {
error: {
message,
type,
status,
},
},
};
}
app.post("/openai-completion", async (req, res) => {
const rawContent =
(req.body &&
req.body.messages &&
req.body.messages[0] &&
req.body.messages[0].content) ||
"";
const messageContent = String(rawContent)
.replace(/<[^>]*>/g, " ")
.replace(/ /gi, " ")
.replace(/\s+/g, " ")
.trim();
const IS_DEBUG =
process.env.npm_lifecycle_event === "debug" || process.env.DEBUG === "true";
if (IS_DEBUG) {
try {
console.log("DEBUG rawContent:", rawContent);
console.log("DEBUG normalizedContent:", messageContent);
console.log("DEBUG EXPECTED_STARTS:", EXPECTED_STARTS);
const debugIncludes = EXPECTED_STARTS.map((start) => ({
start,
includes: messageContent.includes(start),
}));
console.log("DEBUG includes per start:", debugIncludes);
} catch (_) {
// no-op if console/logging fails for any reason
}
}
const isValidStart = EXPECTED_STARTS.some((start) =>
messageContent.includes(start)
);
if (!isValidStart) {
return res.status(400).json({
error: {
message: "Invalid prompt content.",
type: "invalid_request_error",
status: 400,
},
});
}
try {
const requestBody = buildAnthropicRequest(req.body);
const response = await anthropic.messages.create(requestBody);
res.status(200).send(toOpenAiStyleResponse(response));
} catch (error) {
console.error("Error in /openai-completion route:", error);
const { status, body } = normalizeError(error);
res.status(status).json(body);
}
});
app.use((req, res, next) => {
res.status(404).json({
error: {
message: "Not Found",
type: "not_found",
status: 404,
},
});
});
if (SSL_KEY_PATH && SSL_PEM_PATH) {
const privateKey = fs.readFileSync(SSL_KEY_PATH, "utf8");
const certificate = fs.readFileSync(SSL_PEM_PATH, "utf8");
const credentials = { key: privateKey, cert: certificate };
const httpsServer = https.createServer(credentials, app);
httpsServer.listen(PORT, HOST, () => {
console.log(`Server is running on https://${HOST}:${PORT}`);
});
} else {
app.listen(PORT, HOST, () => {
console.log(`Server is running on http://${HOST}:${PORT}`);
});
}