Spaces:
Running
Running
File size: 9,889 Bytes
ca5b3b3 |
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 |
/**
* This file included ported funtion from @PawanOsman
*/
import { Fetcher } from "./fetch";
import { ENV } from "../env";
import { randomUUID, randomInt, createHash } from "node:crypto";
import { encode } from "gpt-3-encoder";
import Stream from "@elysiajs/stream";
import { AgentManager } from "./agent";
import { AppLogger } from "./tools";
import { sha3_512 } from "js-sha3";
const apiUrl = `${ENV.BASE_URL}/backend-anon/conversation`;
/**
* Generates a unique completion ID with the given prefix.
* @param prefix The prefix to use for the completion ID. Defaults to "cmpl-".
* @returns The generated completion ID.
*/
function generateCompletionId(prefix: string = "cmpl-") {
const characters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const length = 28;
for (let i = 0; i < length; i++) {
prefix += characters.charAt(Math.floor(Math.random() * characters.length));
}
return prefix;
}
/**
* Converts chunks of data into lines.
* @param chunksAsync An async iterable that yields chunks of data.
* @returns An async generator that yields lines of data.
*/
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);
}
}
}
/**
* Converts lines from an asynchronous iterator to messages.
* @param linesAsync - An asynchronous iterator that yields lines.
* @returns An asynchronous generator that yields messages.
*/
async function* linesToMessages(linesAsync: any) {
for await (const line of linesAsync) {
const message = line.substring("data :".length);
yield message;
}
}
/**
* Asynchronously generates a stream of completions from the provided data.
*
* @param data - The data to process for completions.
* @returns An asynchronous generator that yields completions.
*/
async function* streamCompletion(data: any) {
yield* linesToMessages(chunksToLines(data));
}
/**
* Generates a new session ID and token for the OpenAI API.
* @returns An object containing the new device ID and token.
*/
async function getNewSession(): Promise<Session> {
let newDeviceId = randomUUID();
const session = await new Fetcher(
`${ENV.BASE_URL}/backend-anon/sentinel/chat-requirements`
)
.json()
.post<Session>(
{},
{
headers: { "oai-device-id": newDeviceId },
}
);
AppLogger.i(
"getNewSession",
`System: Successfully refreshed session ID and token. ${
!session.token ? "(Now it's ready to process requests)" : ""
}`
);
session.deviceId = newDeviceId;
return session;
}
/**
* Generates a proof token for the OpenAI API.
*/
function GenerateProofToken(
seed: string,
diff: string,
userAgent: string
): string {
const cores: number[] = [8, 12, 16, 24];
const screens: number[] = [3000, 4000, 6000];
const core = cores[randomInt(0, cores.length)];
const screen = screens[randomInt(0, screens.length)];
const now = new Date(Date.now() - 8 * 3600 * 1000);
const parseTime = now.toUTCString().replace("GMT", "GMT-0500 (Eastern Time)");
const config = [core + screen, parseTime, 4294705152, 0, userAgent];
const diffLen = diff.length / 2;
for (let i = 0; i < 100000; i++) {
config[3] = i;
const jsonData = JSON.stringify(config);
const base = Buffer.from(jsonData).toString("base64");
const hashValue = sha3_512.create().update(seed + base);
if (hashValue.hex().substring(0, diffLen) <= diff) {
const result = "gAAAAAB" + base;
return result;
}
}
const fallbackBase = Buffer.from(`"${seed}"`).toString("base64");
return "gAAAAABwQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + fallbackBase;
}
/**
* Handles chat completion by sending messages to the OpenAI API and processing the response.
*/
async function handleChatCompletion({
messages,
streamRes,
}: {
/**
* The messages to process for completion.
*/
messages: { role: string; content: string }[];
/**
* The stream response to send the completion to.
*/
streamRes?: Stream<string | number | boolean | object>;
}) {
const session = await AgentManager.getInstance()
.roll()
.catch((e) => {
AppLogger.w("handleChatCompletion", "Failed to get a new session", e);
return null;
});
if (!session) {
const resp = {
status: false,
error: {
message: `Error getting a new session, please try again later, if the issue persists, please open an issue on the GitHub repository, https://github.com/PawanOsman/ChatGPT`,
type: "invalid_request_error",
},
support: "https://discord.pawan.krd",
};
if (streamRes) {
streamRes.send(JSON.stringify(resp));
streamRes.close();
} else {
return resp;
}
return;
}
let proofToken = GenerateProofToken(
session.proofofwork.seed,
session.proofofwork.difficulty,
AgentManager.getInstance().userAgentString
);
let promptTokens = 0;
let completionTokens = 0;
const body = {
action: "next",
messages: messages.map((message: { role: string; content: string }) => ({
author: { role: message.role },
content: { content_type: "text", parts: [message.content] },
})),
parent_message_id: randomUUID(),
model: "text-davinci-002-render-sha",
timezone_offset_min: -180,
suggestions: [],
history_and_training_disabled: true,
conversation_mode: { kind: "primary_assistant" },
websocket_request_id: randomUUID(),
};
for (let message of messages) {
promptTokens += encode(message.content).length;
}
try {
let fullContent = "";
let finish_reason = null;
let created = Math.floor(Date.now() / 1000); // Unix timestamp in seconds
let requestId = generateCompletionId("chatcmpl-");
const response = await new Fetcher(apiUrl).post<Response>(body, {
headers: {
...AgentManager.getInstance().openAiHeaders,
"openai-sentinel-proof-token": proofToken,
},
});
if (!response.ok || !response.body) {
throw new Error("An error occurred while processing the request.");
}
for await (const message of streamCompletion(response.body as any)) {
if (message.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{6}$/)) {
// Skip heartbeat detection
continue;
}
const parsed = JSON.parse(message);
let content = parsed?.message?.content?.parts[0] ?? "";
let status = parsed?.message?.status ?? "";
for (let message of messages) {
if (message.content === content) {
content = "";
break;
}
}
switch (status) {
case "in_progress":
finish_reason = null;
break;
case "finished_successfully":
let finish_reason_data =
parsed?.message?.metadata?.finish_details?.type ?? null;
switch (finish_reason_data) {
case "max_tokens":
finish_reason = "length";
break;
case "stop":
default:
finish_reason = "stop";
}
break;
default:
finish_reason = null;
}
if (content === "") continue;
let completionChunk = content.replace(fullContent, "");
completionTokens += encode(completionChunk).length;
if (streamRes) {
let response = {
id: requestId,
created: created,
object: "chat.completion.chunk",
model: "gpt-3.5-turbo",
choices: [
{
delta: {
content: completionChunk,
},
index: 0,
finish_reason: finish_reason,
},
],
};
streamRes.send(JSON.stringify(response));
}
fullContent = content.length > fullContent.length ? content : fullContent;
}
if (streamRes) {
streamRes.send(
JSON.stringify({
id: requestId,
created: created,
object: "chat.completion.chunk",
model: "gpt-3.5-turbo",
choices: [
{
delta: {
content: "",
},
index: 0,
finish_reason: finish_reason,
},
],
})
);
streamRes.close();
} else {
return {
id: requestId,
created: created,
model: "gpt-3.5-turbo",
object: "chat.completion",
choices: [
{
finish_reason: finish_reason,
index: 0,
message: {
content: fullContent,
role: "assistant",
},
},
],
usage: {
prompt_tokens: promptTokens,
completion_tokens: completionTokens,
total_tokens: promptTokens + completionTokens,
},
};
}
} catch (error) {
const content = {
status: false,
error: {
message:
"An error occurred. Please check the server console to confirm it is ready and free of errors. Additionally, ensure that your request complies with OpenAI's policy.",
type: "invalid_request_error",
},
support: "https://discord.pawan.krd",
};
if (streamRes) {
streamRes.send(JSON.stringify(content));
streamRes.close();
} else {
return content;
}
}
}
export {
generateCompletionId,
chunksToLines,
linesToMessages,
streamCompletion,
getNewSession,
handleChatCompletion,
};
|