forked from zuisong/gemini-openai-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_deno.mjs
568 lines (553 loc) · 17.7 KB
/
main_deno.mjs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// node_modules/.deno/[email protected]/node_modules/itty-router/index.mjs
var t = ({ base: e = "", routes: t2 = [], ...r2 } = {}) => ({ __proto__: new Proxy({}, { get: (r3, o2, a2, s2) => (r4, ...c) => t2.push([o2.toUpperCase?.(), RegExp(`^${(s2 = (e + r4).replace(/\/+(\/|$)/g, "$1")).replace(/(\/?\.?):(\w+)\+/g, "($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g, "($1(?<$2>[^$1/]+?))").replace(/\./g, "\\.").replace(/(\/?)\*/g, "($1.*)?")}/*$`), c, s2]) && a2 }), routes: t2, ...r2, async fetch(e2, ...o2) {
let a2, s2, c = new URL(e2.url), n = e2.query = { __proto__: null };
for (let [e3, t3] of c.searchParams) n[e3] = n[e3] ? [].concat(n[e3], t3) : t3;
e: try {
for (let t3 of r2.before || []) if (null != (a2 = await t3(e2.proxy ?? e2, ...o2))) break e;
t: for (let [r3, n2, l, i] of t2) if ((r3 == e2.method || "ALL" == r3) && (s2 = c.pathname.match(n2))) {
e2.params = s2.groups || {}, e2.route = i;
for (let t3 of l) if (null != (a2 = await t3(e2.proxy ?? e2, ...o2))) break t;
}
} catch (t3) {
if (!r2.catch) throw t3;
a2 = await r2.catch(t3, e2.proxy ?? e2, ...o2);
}
try {
for (let t3 of r2.finally || []) a2 = await t3(a2, e2.proxy ?? e2, ...o2) ?? a2;
} catch (t3) {
if (!r2.catch) throw t3;
a2 = await r2.catch(t3, e2.proxy ?? e2, ...o2);
}
return a2;
} });
var r = (e = "text/plain; charset=utf-8", t2) => (r2, o2 = {}) => {
if (void 0 === r2 || r2 instanceof Response) return r2;
const a2 = new Response(t2?.(r2) ?? r2, o2.url ? void 0 : o2);
return a2.headers.set("content-type", e), a2;
};
var o = r("application/json; charset=utf-8", JSON.stringify);
var a = (e) => ({ 400: "Bad Request", 401: "Unauthorized", 403: "Forbidden", 404: "Not Found", 500: "Internal Server Error" })[e] || "Unknown Error";
var s = (e = 500, t2) => {
if (e instanceof Error) {
const { message: r2, ...o2 } = e;
e = e.status || 500, t2 = { error: r2 || a(e), ...o2 };
}
return t2 = { status: e, ..."object" == typeof t2 ? t2 : { error: t2 || a(e) } }, o(t2, { status: e });
};
var p = r("text/plain; charset=utf-8", String);
var f = r("text/html");
var u = r("image/jpeg");
var h = r("image/png");
var g = r("image/webp");
var y = (e = {}) => {
const { origin: t2 = "*", credentials: r2 = false, allowMethods: o2 = "*", allowHeaders: a2, exposeHeaders: s2, maxAge: c } = e, n = (e2) => {
const o3 = e2?.headers.get("origin");
return true === t2 ? o3 : t2 instanceof RegExp ? t2.test(o3) ? o3 : void 0 : Array.isArray(t2) ? t2.includes(o3) ? o3 : void 0 : t2 instanceof Function ? t2(o3) : "*" == t2 && r2 ? o3 : t2;
}, l = (e2, t3) => {
for (const [r3, o3] of Object.entries(t3)) o3 && e2.headers.append(r3, o3);
return e2;
};
return { corsify: (e2, t3) => e2?.headers?.get("access-control-allow-origin") || 101 == e2.status ? e2 : l(e2.clone(), { "access-control-allow-origin": n(t3), "access-control-allow-credentials": r2 }), preflight: (e2) => {
if ("OPTIONS" == e2.method) {
const t3 = new Response(null, { status: 204 });
return l(t3, { "access-control-allow-origin": n(e2), "access-control-allow-methods": o2?.join?.(",") ?? o2, "access-control-expose-headers": s2?.join?.(",") ?? s2, "access-control-allow-headers": a2?.join?.(",") ?? a2 ?? e2.headers.get("access-control-request-headers"), "access-control-max-age": c, "access-control-allow-credentials": r2 });
}
} };
};
// src/gemini-proxy.ts
async function geminiProxy(rawReq) {
const url = new URL(rawReq.url);
url.host = "generativelanguage.googleapis.com";
url.port = "";
url.protocol = "https:";
const req = new Request(url, rawReq);
const resp = await fetch(req);
return new Response(resp.body, resp);
}
// src/utils.ts
function getToken(headers) {
for (const [k, v] of headers.entries()) {
if (k.toLowerCase() !== "authorization") continue;
const rawApikey = v.substring(v.indexOf(" ") + 1);
if (!rawApikey.includes("#")) {
return {
apikey: rawApikey,
useBeta: false
};
}
const apikey = rawApikey.substring(0, rawApikey.indexOf("#"));
const params = new URLSearchParams(rawApikey.substring(rawApikey.indexOf("#") + 1));
return {
apikey,
useBeta: params.has("useBeta")
};
}
return null;
}
function parseBase64(base64) {
if (!base64.startsWith("data:")) {
return { text: "" };
}
const [m, data, ..._arr] = base64.split(",");
const mimeType = m.match(/:(?<mime>.*?);/)?.groups?.mime ?? "img/png";
return {
inlineData: {
mimeType,
data
}
};
}
function openAiMessageToGeminiMessage(messages) {
const result = messages.flatMap(({ role, content }) => {
if (role === "system") {
return [
{ role: "user", parts: [{ text: content }] },
{ role: "model", parts: [{ text: "" }] }
];
}
const parts = content == null || typeof content === "string" ? [{ text: content?.toString() ?? "" }] : content.map((item) => item.type === "text" ? { text: item.text } : parseBase64(item.image_url.url));
return [{ role: "user" === role ? "user" : "model", parts }];
}).flatMap((item, idx, arr) => {
if (item.role === arr.at(idx + 1)?.role && item.role === "user") {
return [item, { role: "model", parts: [{ text: "" }] }];
}
return [item];
});
return result;
}
function genModel(req) {
const model = ModelMapping[req.model] ?? "gemini-1.0-pro-latest";
let functions = req.tools?.filter((it) => it.type === "function")?.map((it) => it.function) ?? [];
functions = functions.concat(req.functions ?? []);
const generateContentRequest = {
contents: openAiMessageToGeminiMessage(req.messages),
generationConfig: {
maxOutputTokens: req.max_tokens ?? void 0,
temperature: req.temperature ?? void 0,
topP: req.top_p ?? void 0
},
tools: functions.length === 0 ? void 0 : [
{
functionDeclarations: functions
}
],
safetySettings: [
"HARM_CATEGORY_HATE_SPEECH",
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
"HARM_CATEGORY_DANGEROUS_CONTENT",
"HARM_CATEGORY_HARASSMENT"
].map((category) => ({
category,
threshold: "BLOCK_NONE"
}))
};
return [model, generateContentRequest];
}
var ModelMapping = {
"gpt-3.5-turbo": "gemini-1.0-pro-latest",
"gpt-4": "gemini-1.5-pro-latest",
"gpt-4o": "gemini-1.5-flash-latest",
"gpt-4-vision-preview": "gemini-1.0-pro-vision-latest",
"gpt-4-turbo": "gemini-1.5-pro-latest",
"gpt-4-turbo-preview": "gemini-1.5-pro-latest"
};
function getRuntimeKey() {
const global = globalThis;
if (global?.Deno !== void 0) {
return "deno";
}
if (global?.Bun !== void 0) {
return "bun";
}
if (typeof global?.WebSocketPair === "function") {
return "workerd";
}
if (typeof global?.EdgeRuntime === "string") {
return "edge-light";
}
if (global?.fastly !== void 0) {
return "fastly";
}
if (global?.process?.release?.name === "node") {
return "node";
}
return "other";
}
// src/hello.ts
function hello(req) {
const origin = new URL(req.url).origin;
return new Response(`
Hello Gemini-OpenAI-Proxy from ${getRuntimeKey()}!
You can try it with:
curl ${origin}/v1/chat/completions \\
-H "Authorization: Bearer $YOUR_GEMINI_API_KEY" \\
-H "Content-Type: application/json" \\
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello"}],
"temperature": 0.7
}'
`);
}
// src/log.ts
var LEVEL = ["debug", "info", "warn", "error"];
var Logger = class {
config;
debug;
info;
warn;
error;
constructor(config) {
const level = LEVEL.find((it) => it === config?.level) ?? "warn";
this.config = {
prefix: config?.prefix ?? "",
level
};
for (const m of LEVEL) {
this[m] = (...data) => this.#write(m, ...data);
}
}
#write(level, ...data) {
const { level: configLevel, prefix } = this.config;
if (LEVEL.indexOf(level) < LEVEL.indexOf(configLevel)) {
return;
}
console[level](`${(/* @__PURE__ */ new Date()).toISOString()} ${level.toUpperCase()}${prefix ? ` ${prefix}` : ""}`, ...data);
}
};
// src/gemini-api-client/errors.ts
var GoogleGenerativeAIError = class extends Error {
constructor(message) {
super(`[GoogleGenerativeAI Error]: ${message}`);
}
};
var GoogleGenerativeAIResponseError = class extends GoogleGenerativeAIError {
constructor(message, response) {
super(message);
this.response = response;
}
};
// src/gemini-api-client/response-helper.ts
function addHelpers(response) {
;
response.result = () => {
if (response.candidates && response.candidates.length > 0) {
if (response.candidates.length > 1) {
console.warn(
`This response had ${response.candidates.length} candidates. Returning text from the first candidate only. Access response.candidates directly to use the other candidates.`
);
}
if (hadBadFinishReason(response.candidates[0])) {
throw new GoogleGenerativeAIResponseError(
`${formatBlockErrorMessage(response)}`,
response
);
}
return getText(response);
}
if (response.promptFeedback) {
throw new GoogleGenerativeAIResponseError(
`Text not available. ${formatBlockErrorMessage(response)}`,
response
);
}
return "";
};
return response;
}
function getText(response) {
if (response.candidates?.[0].content?.parts?.[0]?.text) {
return response.candidates[0].content.parts[0].text;
}
if (response.candidates?.[0].content?.parts?.[0]?.functionCall) {
return response.candidates[0].content.parts[0].functionCall;
}
return "";
}
var badFinishReasons = ["RECITATION", "SAFETY"];
function hadBadFinishReason(candidate) {
return !!candidate.finishReason && badFinishReasons.includes(candidate.finishReason);
}
function formatBlockErrorMessage(response) {
let message = "";
if ((!response.candidates || response.candidates.length === 0) && response.promptFeedback) {
message += "Response was blocked";
if (response.promptFeedback?.blockReason) {
message += ` due to ${response.promptFeedback.blockReason}`;
}
if (response.promptFeedback?.blockReasonMessage) {
message += `: ${response.promptFeedback.blockReasonMessage}`;
}
} else if (response.candidates?.[0]) {
const firstCandidate = response.candidates[0];
if (hadBadFinishReason(firstCandidate)) {
message += `Candidate was blocked due to ${firstCandidate.finishReason}`;
if (firstCandidate.finishMessage) {
message += `: ${firstCandidate.finishMessage}`;
}
}
}
return message;
}
// src/gemini-api-client/gemini-api-client.ts
async function generateContent(apiParam, model, params, requestOptions) {
const url = new RequestUrl(model, "generateContent" /* GENERATE_CONTENT */, false, apiParam);
const response = await makeRequest(url, JSON.stringify(params), requestOptions);
const responseJson = await response.json();
const enhancedResponse = addHelpers(responseJson);
return {
response: enhancedResponse
};
}
async function makeRequest(url, body, requestOptions) {
let response;
try {
response = await fetch(url.toURL(), {
...buildFetchOptions(requestOptions),
method: "POST",
headers: {
"Content-Type": "application/json"
},
body
});
if (!response.ok) {
let message = "";
try {
const json = await response.json();
message = json.error.message;
if (json.error.details) {
message += ` ${JSON.stringify(json.error.details)}`;
}
} catch (_e) {
}
throw new Error(`[${response.status} ${response.statusText}] ${message}`);
}
} catch (e) {
const err = new GoogleGenerativeAIError(`Error fetching from google -> ${e.message}`);
err.stack = e.stack;
throw err;
}
return response;
}
var RequestUrl = class {
constructor(model, task, stream, apiParam) {
this.model = model;
this.task = task;
this.stream = stream;
this.apiParam = apiParam;
}
toURL() {
const api_version = "v1beta" /* v1beta */;
const url = new URL(`${BASE_URL}/${api_version}/models/${this.model}:${this.task}`);
url.searchParams.append("key", this.apiParam.apikey);
if (this.stream) {
url.searchParams.append("alt", "sse");
}
return url;
}
};
var BASE_URL = "https://generativelanguage.googleapis.com";
function buildFetchOptions(requestOptions) {
const fetchOptions = {};
if (requestOptions?.timeout) {
const abortController = new AbortController();
const signal = abortController.signal;
setTimeout(() => abortController.abort(), requestOptions.timeout);
fetchOptions.signal = signal;
}
return fetchOptions;
}
// src/openai/chat/completions/NonStreamingChatProxyHandler.ts
async function nonStreamingChatProxyHandler(req, apiParam, log) {
const [model, geminiReq] = genModel(req);
const geminiResp = await generateContent(apiParam, model, geminiReq).then((it) => it.response.result()).catch((err) => {
log?.error(req);
log?.error(err?.message ?? err.toString());
return err?.message ?? err.toString();
});
log?.debug(req);
log?.debug(geminiResp);
function genOpenAiResp(content) {
if (typeof content === "string") {
return {
id: "chatcmpl-abc123",
object: "chat.completion",
created: Math.floor(Date.now() / 1e3),
model: req.model,
choices: [
{
message: { role: "assistant", content },
finish_reason: "stop",
index: 0,
logprobs: null
}
]
};
}
return {
id: "chatcmpl-abc123",
object: "chat.completion",
created: Math.floor(Date.now() / 1e3),
model: req.model,
choices: [
{
message: {
role: "assistant",
content: null,
function_call: {
name: content.name ?? "",
arguments: JSON.stringify(content.args)
}
},
finish_reason: "function_call",
index: 0,
logprobs: null
}
]
};
}
return genOpenAiResp(geminiResp);
}
// src/openai/chat/completions/StreamingChatProxyHandler.ts
async function* streamingChatProxyHandler(req, apiParam) {
const [model, geminiReq] = genModel(req);
const geminiResp = await generateContent(apiParam, model, geminiReq).then((it) => it.response.result()).catch((e) => e.message ?? e?.toString());
function genOpenAiResp(content, stop) {
if (typeof content === "string") {
return {
id: "chatcmpl-abc123",
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1e3),
model: req.model,
choices: [
{
delta: { role: "assistant", content },
finish_reason: stop ? "stop" : null,
index: 0
}
]
};
}
return {
id: "chatcmpl-abc123",
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1e3),
model: req.model,
choices: [
{
delta: { role: "assistant", function_call: content },
finish_reason: stop ? "function_call" : null,
index: 0
}
]
};
}
yield genOpenAiResp(geminiResp, false);
yield genOpenAiResp("", true);
}
// src/openai/chat/completions/ChatProxyHandler.ts
async function chatProxyHandler(rawReq) {
const req = await rawReq.json();
const headers = rawReq.headers;
const apiParam = getToken(headers);
if (apiParam == null) {
return new Response("Unauthorized", { status: 401 });
}
rawReq.logger?.debug(req);
if (req.stream !== true) {
const resp = await nonStreamingChatProxyHandler(req, apiParam, rawReq.logger);
rawReq.logger?.debug(resp);
return Response.json(resp);
}
const respArr = streamingChatProxyHandler(req, apiParam);
return sseResponse(
async function* () {
for await (const data of respArr) {
rawReq.logger?.debug(data);
yield JSON.stringify(data);
}
yield "[DONE]";
}()
);
}
function sseResponse(dataStream) {
const { readable, writable } = new TransformStream();
const response = new Response(readable, {
status: 200,
headers: new Headers({
"Content-Type": "text/event-stream"
})
});
const encoder = new TextEncoder();
async function writer(data) {
const w = writable.getWriter();
await w.write(encoder.encode(data));
w.releaseLock();
}
;
(async () => {
for await (const data of dataStream) {
await writer(toSseMsg({ data }));
}
await writable.close();
})();
return response;
}
function toSseMsg(sseEvent) {
const { event, data, id } = sseEvent;
let result = `data: ${data}
`;
if (event) {
result += `event: ${event ?? ""}
`;
}
if (id) {
result += `id: ${id ?? ""}
`;
}
return `${result}
`;
}
// src/openai/models.ts
var modelData = Object.keys(ModelMapping).map((model) => ({
created: 1677610602,
object: "model",
owned_by: "openai",
id: model
}));
var models = () => {
return {
object: "list",
data: modelData
};
};
var modelDetail = (model) => {
return modelData.find((it) => it.id === model);
};
// src/app.ts
var { preflight, corsify } = y({ allowHeaders: "*" });
var app = t({
before: [
preflight,
(req) => {
req.logger = new Logger({ prefix: crypto.randomUUID().toString() });
req.logger.warn(`--> ${req.method} ${req.url}`);
}
],
catch: s,
finally: [
corsify,
(_, req) => {
req.logger?.warn(`<-- ${req.method} ${req.url}`);
},
o
]
});
app.get("/", (c) => hello(c));
app.post("/v1/chat/completions", chatProxyHandler);
app.get("/v1/models", () => Response.json(models()));
app.get("/v1/models/:model", (c) => Response.json(modelDetail(c.params.model)));
app.post(":model_version/models/:model_and_action", geminiProxy);
app.all("*", () => new Response("Page Not Found", { status: 404 }));
// main_deno.ts
Deno.serve({ port: 8e3 }, app.fetch);