forked from PawanOsman/ChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
139 lines (132 loc) · 3.94 KB
/
index.ts
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
import { randomUUID } from "node:crypto";
import { OpenAI } from "openai";
const TOKEN_TIMEOUT_MS = 10 * 60 * 1000;
const DEBUG = true;
const baseUrl = "https://chat.openai.com";
const baseHeaders = {
accept: "*/*",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"content-type": "application/json",
"oai-language": "en-US",
origin: baseUrl,
pragma: "no-cache",
referer: baseUrl,
"sec-ch-ua":
'"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
};
const randomSessionId = (function () {
let last = 0;
let deviceId = "",
token = "";
return async () => {
const current = Date.now();
if (current - last > TOKEN_TIMEOUT_MS) {
deviceId = randomUUID();
const response = await fetch(
`${baseUrl}/backend-anon/sentinel/chat-requirements`,
{
keepalive: true,
verbose: DEBUG,
method: "POST",
headers: { "oai-device-id": deviceId, ...baseHeaders },
}
);
token = await response.json().then((data) => (data as any)?.token);
last = current;
}
return { deviceId, token };
};
})();
async function* chunksToLines(chunksAsync: any) {
let previous = "";
for await (const chunk of chunksAsync) {
const bufferChunk = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
previous += bufferChunk;
let eolIndex: number;
while ((eolIndex = previous.indexOf("\n")) >= 0) {
// line includes the EOL
const line = previous.slice(0, eolIndex + 1).trimEnd();
if (line === "data: [DONE]") break;
if (line.startsWith("data: ")) yield line;
previous = previous.slice(eolIndex + 1);
}
}
}
async function* linesToMessages(linesAsync: any) {
for await (const line of linesAsync) {
const message = line.substring("data :".length);
yield message;
}
}
Bun.serve({
port: process.env.PORT || 3000,
async fetch(request) {
const { deviceId, token } = await randomSessionId();
const chatCompletionCreate = await request
.json()
.then((data) => data as OpenAI.ChatCompletionCreateParams);
const body = {
action: "next",
messages: chatCompletionCreate.messages.map((message) => ({
author: { role: message.role },
content: { content_type: "text", parts: [message.content] },
})),
parent_message_id: randomUUID(),
model: chatCompletionCreate.model,
timezone_offset_min: -180,
suggestions: [],
history_and_training_disabled: true,
conversation_mode: { kind: "primary_assistant" },
websocket_request_id: randomUUID(),
};
const response = await fetch(`${baseUrl}/backend-api/conversation`, {
verbose: DEBUG,
method: "POST",
keepalive: true,
headers: {
"oai-device-id": deviceId,
"openai-sentinel-chat-requirements-token": token,
...baseHeaders,
},
body: JSON.stringify(body),
});
let id, created, content;
for await (const message of linesToMessages(chunksToLines(response.body))) {
const parsed = JSON.parse(message);
id = parsed?.message?.id;
created = parsed?.message?.create_time;
content = parsed?.message?.content?.parts[0] ?? "";
}
return new Response(
JSON.stringify({
id,
created,
model: "gpt-3.5-turbo",
object: "chat.completion",
choices: [
{
finish_reason: "stop",
index: 0,
message: {
content,
role: "assistant",
},
},
],
usage: {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
},
})
);
},
});