From 39004fe8b377a1c405bd31e74c0300f73513adbb Mon Sep 17 00:00:00 2001 From: MrlolDev Date: Fri, 15 Dec 2023 21:48:36 +0100 Subject: [PATCH] Fix handling of empty candidates and parts in Google text model --- src/models/text/google.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/models/text/google.ts b/src/models/text/google.ts index eb6200f..2c0a03d 100644 --- a/src/models/text/google.ts +++ b/src/models/text/google.ts @@ -142,6 +142,8 @@ export default { const cost = 0; let resultLength = 0; for await (const item of streamingResp.stream) { + if (item.candidates.length == 0) continue; + if (item.candidates[0]?.content?.parts.length == 0) continue; res.result = item.candidates[0]?.content?.parts[0]?.text || ""; resultLength = item.usageMetadata?.candidates_token_count || 0; promptLength = item.usageMetadata?.prompt_token_count || 0; @@ -161,7 +163,15 @@ export default { ...res, }; event.emit("data", res); - }); + }).catch((err) => { + console.log(err); + console.log(request); + event.emit("data", { + done: true, + + }) + }) + return event; }, };