Spaces:
Sleeping
Sleeping
File size: 4,620 Bytes
a5d6759 697e9a8 8829cca 697e9a8 |
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 |
// Sup 2ch /ai/
const express = require("express");
const app = express();
const port = 7860;
app.use(express.json());
app.get("//models", async (req, res) => {
try {
res.status(200).json({ object: "list", data: createFakeModelsList() });
} catch {}
});
app.post("//chat/completions", async (clientRequest, clientResponse) => {
try {
const {
frequency_penalty,
presence_penalty,
max_tokens,
stop,
temperature,
top_p,
} = clientRequest.body;
const apiRequestBody = {
model: "gpt-4",
prompt: convertChatMLPrompt(clientRequest.body.messages),
frequency_penalty,
presence_penalty,
max_tokens,
stop,
temperature,
top_p,
};
const apiResponse = await fetch(process.env.API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(apiRequestBody),
});
if (clientRequest.body.stream) {
handleResponseAsStream(clientResponse, apiResponse);
} else {
handleResponseAsNonStreamable(clientResponse, apiResponse);
}
} catch {}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
async function handleResponseAsNonStreamable(clientResponse, apiResponse) {
const apiText = await apiResponse.text();
const clientMessage = createClientMessage(apiText);
clientResponse.send(JSON.stringify(clientMessage));
}
async function handleResponseAsStream(clientResponse, apiResponse) {
const reader = apiResponse.body.getReader();
const nextDecoder = new TextDecoder();
clientResponse.write("data: " + JSON.stringify(createBeginChunk()) + "\n\n");
new ReadableStream({
start(controller) {
return pump();
function pump() {
return reader.read().then(({ done, value }) => {
const textData = nextDecoder.decode(value);
clientResponse.write(
"data: " + JSON.stringify(createMessageChunk(textData)) + "\n\n"
);
// When no more data needs to be consumed, close the stream
if (done) {
clientResponse.write(
"data: " + JSON.stringify(createEndChunk()) + "\n\n"
);
clientResponse.end();
controller.close();
return;
}
// Enqueue the next data chunk into our target stream
controller.enqueue(value);
return pump();
});
}
},
});
}
function getCurrentDate() {
return Math.floor(new Date().getTime());
}
function convertChatMLPrompt(messages) {
const messageStrings = [];
messages.forEach((m) => {
if (m.role === "system" && m.name === undefined) {
messageStrings.push("System: " + m.content);
} else if (m.role === "system" && m.name !== undefined) {
messageStrings.push(m.name + ": " + m.content);
} else {
messageStrings.push(m.role + ": " + m.content);
}
});
return messageStrings.join("\n") + "\nassistant:";
}
const createClientMessage = (text) => ({
id: "chatcmpl-123",
object: "chat.completion",
created: getCurrentDate(),
model: "gpt-4",
choices: [
{
index: 0,
message: { role: "assistant", content: text },
logprobs: null,
finish_reason: "stop",
},
],
});
const createBeginChunk = () => ({
id: "chatcmpl-123",
object: "chat.completion.chunk",
created: getCurrentDate(),
model: "gpt-4",
system_fingerprint: "",
choices: [
{
index: 0,
delta: { role: "assistant", content: "" },
logprobs: null,
finish_reason: null,
},
],
});
const createMessageChunk = (text) => ({
id: "chatcmpl-123",
object: "chat.completion.chunk",
created: getCurrentDate(),
model: "gpt-4",
system_fingerprint: "",
choices: [
{
index: 0,
delta: { content: text },
logprobs: null,
finish_reason: null,
},
],
});
const createEndChunk = () => ({
id: "chatcmpl-123",
object: "chat.completion.chunk",
created: getCurrentDate(),
model: "gpt-4",
system_fingerprint: "",
choices: [{ index: 0, delta: {}, logprobs: null, finish_reason: "stop" }],
});
function createFakeModelsList() {
return [
{
id: "gpt-4",
object: "model",
created: getCurrentDate(),
owned_by: "openai",
permission: [
{
id: "modelperm-gpt-4",
object: "model_permission",
created: getCurrentDate(),
organization: "*",
group: null,
is_blocking: false,
},
],
root: "gpt-4",
parent: null,
},
];
}
|