Merge pull request #1585 from Yidadaa/bugfix-0518

feat: handle non-stream response
This commit is contained in:
Yifei Zhang 2023-05-18 02:06:22 +08:00 committed by GitHub
commit b4faf3faad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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();
}
},