feat: handle non-stream response

This commit is contained in:
Yidadaa 2023-05-18 02:04:12 +08:00
parent 736c66f46a
commit 5f2745c32a
1 changed files with 20 additions and 8 deletions

View File

@ -86,27 +86,39 @@ export class ChatGPTApi implements LLMApi {
...chatPayload,
async onopen(res) {
clearTimeout(requestTimeoutId);
const contentType = res.headers.get("content-type");
console.log(
"[OpenAI] request response content type: ",
contentType,
);
if (contentType?.startsWith("text/plain")) {
responseText = await res.clone().text();
return finish();
}
if (
!res.ok ||
res.headers.get("content-type") !== EventStreamContentType ||
res.status !== 200
) {
let extraInfo = { error: undefined };
const responseTexts = [responseText];
let extraInfo = await res.clone().text();
try {
extraInfo = await res.clone().json();
const resJson = await res.clone().json();
extraInfo = prettyObject(resJson);
} catch {}
if (res.status === 401) {
if (responseText.length > 0) {
responseText += "\n\n";
}
responseText += Locale.Error.Unauthorized;
responseTexts.push(Locale.Error.Unauthorized);
}
if (extraInfo.error) {
responseText += "\n\n" + prettyObject(extraInfo);
if (extraInfo) {
responseTexts.push(extraInfo);
}
responseText = responseTexts.join("\n\n");
return finish();
}
},