correctly decompresses gzip/deflate payloads

This commit is contained in:
nai-degen
2023-04-09 02:30:59 -05:00
committed by nai-degen
parent 326def06a3
commit 7bb70ddfdc
5 changed files with 52 additions and 15 deletions
+11 -1
View File
@@ -14,7 +14,8 @@
"express": "^4.18.2",
"http-proxy-middleware": "^3.0.0-beta.1",
"pino-http": "^8.3.3",
"showdown": "^2.1.0"
"showdown": "^2.1.0",
"zlib": "^1.0.5"
},
"devDependencies": {
"@types/cors": "^2.8.13",
@@ -1624,6 +1625,15 @@
"engines": {
"node": ">=6"
}
},
"node_modules/zlib": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/zlib/-/zlib-1.0.5.tgz",
"integrity": "sha512-40fpE2II+Cd3k8HWTWONfeKE2jL+P42iWJ1zzps5W51qcTsOUKM5Q5m2PFb0CLxlmFAaUuUdJGc3OfZy947v0w==",
"hasInstallScript": true,
"engines": {
"node": ">=0.2.0"
}
}
}
}
+2 -1
View File
@@ -15,7 +15,8 @@
"express": "^4.18.2",
"http-proxy-middleware": "^3.0.0-beta.1",
"pino-http": "^8.3.3",
"showdown": "^2.1.0"
"showdown": "^2.1.0",
"zlib": "^1.0.5"
},
"devDependencies": {
"@types/cors": "^2.8.13",
+21 -8
View File
@@ -1,5 +1,7 @@
import { Request, Response } from "express";
import * as http from "http";
import util from "util";
import zlib from "zlib";
import * as httpProxy from "http-proxy";
import { logger } from "../logger";
import { keys } from "../keys";
@@ -20,9 +22,20 @@ export const handleDownstreamErrors = (
return resolve();
}
let body = "";
proxyRes.on("data", (chunk) => (body += chunk));
proxyRes.on("end", () => {
let chunks: Buffer[] = [];
proxyRes.on("data", (chunk) => chunks.push(chunk));
proxyRes.on("end", async () => {
let body = Buffer.concat(chunks);
const contentEncoding = proxyRes.headers["content-encoding"];
if (contentEncoding === "gzip") {
body = await util.promisify(zlib.gunzip)(body);
} else if (contentEncoding === "deflate") {
body = await util.promisify(zlib.inflate)(body);
}
const bodyString = body.toString();
let errorPayload: any = {
error: "Proxy couldn't parse error from OpenAI",
};
@@ -30,14 +43,14 @@ export const handleDownstreamErrors = (
? "You can try again to get a different key."
: "There are no more keys available.";
try {
errorPayload = JSON.parse(body);
errorPayload = JSON.parse(bodyString);
} catch (parseError: any) {
const errorObject = {
error: parseError.message,
trace: parseError.stack,
body: body,
}
trace: parseError.stack,
body: bodyString,
};
logger.error(errorObject, "Unparseable error from OpenAI");
res.json(errorObject);
return reject(parseError.message);
+17 -5
View File
@@ -4,6 +4,8 @@ requests to OpenAI API equivalents. */
import { Request, Response, Router } from "express";
import http from "http";
import { createProxyMiddleware } from "http-proxy-middleware";
import util from "util";
import zlib from "zlib";
import { logger } from "../logger";
import {
copyHttpHeaders,
@@ -17,8 +19,8 @@ import {
finalizeBody,
languageFilter,
limitOutputTokens,
transformKoboldPayload,
} from "./rewriters";
import { transformKoboldPayload } from "./rewriters/transform-kobold-payload";
export const handleModelRequest = (_req: Request, res: Response) => {
res.status(200).json({ result: "Connected to OpenAI reverse proxy" });
@@ -68,10 +70,20 @@ const handleProxiedResponse = async (
// For Kobold we need to consume the response body to turn it into a KoboldAI
// response payload.
let body = "";
proxyRes.on("data", (chunk) => (body += chunk));
proxyRes.on("end", () => {
const response = JSON.parse(body);
let chunks: Buffer[] = [];
proxyRes.on("data", (chunk) => chunks.push(chunk));
proxyRes.on("end", async () => {
let body = Buffer.concat(chunks);
const contentEncoding = proxyRes.headers["content-encoding"];
if (contentEncoding === "gzip") {
body = await util.promisify(zlib.gunzip)(body);
} else if (contentEncoding === "deflate") {
body = await util.promisify(zlib.inflate)(body);
}
const response = JSON.parse(body.toString());
const koboldResponse = {
results: [{ text: response.choices[0].message.content }],
};
+1
View File
@@ -7,6 +7,7 @@ export { languageFilter } from "./language-filter";
export { disableStream } from "./disable-stream";
export { limitOutputTokens } from "./limit-output-tokens";
export { finalizeBody } from "./finalize-body";
export { transformKoboldPayload } from "./transform-kobold-payload";
export type ExpressHttpProxyReqCallback = ProxyReqCallback<
ClientRequest,