Update route.ts

This commit is contained in:
yorkeking 2023-04-04 01:57:05 +08:00 committed by GitHub
parent 34c2f77790
commit fe3654a030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,15 @@ async function createStream(req: NextRequest) {
const res = await requestOpenai(req); const res = await requestOpenai(req);
const contentType = res.headers.get("Content-Type") ?? "";
if (!contentType.includes("stream")) {
const content = await (
await res.text()
).replace(/provided:.*. You/, "provided: ***. You");
console.log("[Stream] error ", content);
return "```json\n" + content + "```";
}
const stream = new ReadableStream({ const stream = new ReadableStream({
async start(controller) { async start(controller) {
function onParse(event: any) { function onParse(event: any) {