-
Notifications
You must be signed in to change notification settings - Fork 238
/
worker.mjs
355 lines (336 loc) · 11 KB
/
worker.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
export default {
async fetch (request) {
if (request.method === "OPTIONS") {
return handleOPTIONS();
}
try {
const auth = request.headers.get("Authorization");
let apiKey = auth && auth.split(" ")[1];
if (!apiKey) {
throw new HttpError("Bad credentials", 401);
}
const url = new URL(request.url);
if (url.pathname.endsWith("/models") && request.method === "GET") {
return handleModels(apiKey);
} else if (url.pathname.endsWith("/chat/completions") && request.method === "POST") {
const json = await request.json();
if (!Array.isArray(json.messages)) {
throw new HttpError(".messages array required", 400);
}
return handleRequest(json, apiKey);
} else {
throw new HttpError("404 Not Found", 404);
}
} catch (err) {
console.error(err.toString());
return new Response(err, { status: err.status ?? 500, headers: fixCors() });
}
}
};
class HttpError extends Error {
constructor(message, status) {
super(message);
this.name = this.constructor.name;
this.status = status;
}
}
const fixCors = (headers) => {
headers = new Headers(headers);
headers.set("Access-Control-Allow-Origin", "*");
return headers;
};
const handleOPTIONS = async () => {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "*",
}
});
};
const BASE_URL = "https://generativelanguage.googleapis.com";
const API_VERSION = "v1beta";
async function handleModels(apiKey) {
const response = await fetch(`${BASE_URL}/${API_VERSION}/models`, {
headers: {
"x-goog-api-key": apiKey,
"x-goog-api-client": API_CLIENT,
},
});
let body = response.body;
if (response.ok) {
body = await response.text();
body = processModels(JSON.parse(body));
}
return new Response(body, { status: response.status, statusText: response.statusText, headers: fixCors(response.headers) });
}
const processModels = (data) => {
return JSON.stringify({
object: "list",
data: data.models.map((model) => ({
id: model.name.replace("models/", ""),
object: "model",
created: 0,
owned_by: "",
})),
}, null, " ");
};
const DEFAULT_MODEL = "gemini-1.5-pro-latest";
// https://github.com/google-gemini/generative-ai-js/blob/cf223ff4a1ee5a2d944c53cddb8976136382bee6/src/requests/request.ts#L71
const API_CLIENT = "genai-js/0.19.0"; // npm view @google/generative-ai version
async function handleRequest (req, apiKey) {
const model = req.model?.startsWith("gemini-") ? req.model : DEFAULT_MODEL;
const TASK = req.stream ? "streamGenerateContent" : "generateContent";
let url = `${BASE_URL}/${API_VERSION}/models/${model}:${TASK}`;
if (req.stream) { url += "?alt=sse"; }
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-goog-api-key": apiKey,
"x-goog-api-client": API_CLIENT,
},
body: JSON.stringify(await transformRequest(req)), // try
});
let body = response.body;
if (response.ok) {
let id = generateChatcmplId(); //"chatcmpl-8pMMaqXMK68B3nyDBrapTDrhkHBQK";
if (req.stream) {
body = response.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TransformStream({
transform: parseStream,
flush: parseStreamFlush,
buffer: "",
}))
.pipeThrough(new TransformStream({
transform: toOpenAiStream,
flush: toOpenAiStreamFlush,
model, id, last: [],
}))
.pipeThrough(new TextEncoderStream());
} else {
body = await response.text();
body = processResponse(JSON.parse(body), model, id);
}
}
return new Response(body, { status: response.status, statusText: response.statusText, headers: fixCors(response.headers) });
}
const harmCategory = [
"HARM_CATEGORY_HATE_SPEECH",
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
"HARM_CATEGORY_DANGEROUS_CONTENT",
"HARM_CATEGORY_HARASSMENT",
"HARM_CATEGORY_CIVIC_INTEGRITY",
];
const safetySettings = harmCategory.map((category) => ({
category,
threshold: "BLOCK_NONE",
}));
const fieldsMap = {
stop: "stopSequences",
n: "candidateCount", // { "error": { "code": 400, "message": "Only one candidate can be specified", "status": "INVALID_ARGUMENT" } }
max_tokens: "maxOutputTokens",
temperature: "temperature",
top_p: "topP",
//..."topK"
};
const transformConfig = (req) => {
let cfg = {};
//if (typeof req.stop === "string") { req.stop = [req.stop]; } // no need
for (let key in req) {
const matchedKey = fieldsMap[key];
if (matchedKey) {
cfg[matchedKey] = req[key];
}
}
if (req.response_format?.type === "json_object") {
cfg.response_mime_type = "application/json";
}
return cfg;
};
const parseImg = async (url) => {
if (url.startsWith("http://") || url.startsWith("https://")) {
try {
const response = await fetch(`https://wsrv.nl/?url=${url}&w=512&we&encoding=base64`);
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText} (${url})`);
}
url = await response.text();
} catch (err) {
throw new Error("Error fetching image: " + err.toString());
}
}
const match = url.match(/^data:(?<mimeType>.*?)(;base64)?,(?<data>.*)$/);
if (!match) {
throw new Error("Invalid image data: " + url);
}
const { mimeType, data } = match.groups;
return {
inlineData: {
mimeType,
data,
},
};
};
const transformMsg = async ({ role, content }) => {
const parts = [];
if (!Array.isArray(content)) {
// system, user: string
// assistant: string or null (Required unless tool_calls is specified.)
parts.push({ text: content });
return { role, parts };
}
// OpenAI "model": "gpt-4-vision-preview"
// user:
// An array of content parts with a defined type, each can be of type text or image_url when passing in images.
// You can pass multiple images by adding multiple image_url content parts.
// Image input is only supported when using the gpt-4-visual-preview model.
for (const item of content) {
switch (item.type) {
case "text":
parts.push({ text: item.text });
break;
case "image_url":
parts.push(await parseImg(item.image_url.url));
break;
default:
throw new TypeError(`Unknown "content" item type: "${item.type}"`);
}
}
return { role, parts };
};
const transformMessages = async (messages) => {
const contents = [];
let system_instruction;
for (const item of messages) {
if (item.role === "system") {
delete item.role;
system_instruction = await transformMsg(item);
} else {
item.role = item.role === "assistant" ? "model" : "user";
contents.push(await transformMsg(item));
}
}
if (system_instruction && contents.length === 0) {
contents.push({ role: "model", parts: { text: " " } });
}
//console.info(JSON.stringify(contents, 2));
return { system_instruction, contents };
};
const transformRequest = async (req) => ({
...await transformMessages(req.messages),
safetySettings,
generationConfig: transformConfig(req),
});
const generateChatcmplId = () => {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const randomChar = () => characters[Math.floor(Math.random() * characters.length)];
return "chatcmpl-" + Array.from({ length: 29 }, randomChar).join("");
};
const reasonsMap = { //https://ai.google.dev/api/rest/v1/GenerateContentResponse#finishreason
//"FINISH_REASON_UNSPECIFIED": // Default value. This value is unused.
"STOP": "stop",
"MAX_TOKENS": "length",
"SAFETY": "content_filter",
"RECITATION": "content_filter",
//"OTHER": "OTHER",
// :"function_call",
};
const transformCandidates = (key, cand) => ({
index: cand.index || 0, // 0-index is absent in new -002 models response
[key]: { role: "assistant", content: cand.content?.parts[0].text },
logprobs: null,
finish_reason: reasonsMap[cand.finishReason] || cand.finishReason,
});
const transformCandidatesMessage = transformCandidates.bind(null, "message");
const transformCandidatesDelta = transformCandidates.bind(null, "delta");
const transformUsage = (data) => ({
completion_tokens: data.candidatesTokenCount,
prompt_tokens: data.promptTokenCount,
total_tokens: data.totalTokenCount
});
const processResponse = (data, model, id) => {
return JSON.stringify({
id,
choices: data.candidates.map(transformCandidatesMessage),
created: Math.floor(Date.now()/1000),
model,
//system_fingerprint: "fp_69829325d0",
object: "chat.completion",
usage: transformUsage(data.usageMetadata),
});
};
const responseLineRE = /^data: (.*)(?:\n\n|\r\r|\r\n\r\n)/;
async function parseStream (chunk, controller) {
chunk = await chunk;
if (!chunk) { return; }
this.buffer += chunk;
do {
const match = this.buffer.match(responseLineRE);
if (!match) { break; }
controller.enqueue(match[1]);
this.buffer = this.buffer.substring(match[0].length);
} while (true); // eslint-disable-line no-constant-condition
}
async function parseStreamFlush (controller) {
if (this.buffer) {
console.error("Invalid data:", this.buffer);
controller.enqueue(this.buffer);
}
}
function transformResponseStream (data, stop, first) {
const item = transformCandidatesDelta(data.candidates[0]);
if (stop) { item.delta = {}; } else { item.finish_reason = null; }
if (first) { item.delta.content = ""; } else { delete item.delta.role; }
const output = {
id: this.id,
choices: [item],
created: Math.floor(Date.now()/1000),
model: this.model,
//system_fingerprint: "fp_69829325d0",
object: "chat.completion.chunk",
};
if (stop && data.usageMetadata) {
output.usage = transformUsage(data.usageMetadata);
}
return "data: " + JSON.stringify(output) + delimiter;
}
const delimiter = "\n\n";
async function toOpenAiStream (chunk, controller) {
const transform = transformResponseStream.bind(this);
const line = await chunk;
if (!line) { return; }
let data;
try {
data = JSON.parse(line);
} catch (err) {
console.error(line);
console.error(err);
const length = this.last.length || 1; // at least 1 error msg
const candidates = Array.from({ length }, (_, index) => ({
finishReason: "error",
content: { parts: [{ text: err }] },
index,
}));
data = { candidates };
}
const cand = data.candidates[0]; // !!untested with candidateCount>1
cand.index = cand.index || 0; // absent in new -002 models response
if (!this.last[cand.index]) {
controller.enqueue(transform(data, false, "first"));
}
this.last[cand.index] = data;
if (cand.content) { // prevent empty data (e.g. when MAX_TOKENS)
controller.enqueue(transform(data));
}
}
async function toOpenAiStreamFlush (controller) {
const transform = transformResponseStream.bind(this);
if (this.last.length > 0) {
for (const data of this.last) {
controller.enqueue(transform(data, "stop"));
}
controller.enqueue("data: [DONE]" + delimiter);
}
}