chroe: support error reason for gemini pro

This commit is contained in:
Fred Liang 2023-12-24 04:32:25 +08:00
parent 7026bd926a
commit 75057f9a91
No known key found for this signature in database
GPG Key ID: 4DABDA85EF70EC71
1 changed files with 11 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import { getServerSideConfig } from "@/app/config/server";
export class GeminiProApi implements LLMApi { export class GeminiProApi implements LLMApi {
extractMessage(res: any) { extractMessage(res: any) {
console.log("[Response] gemini-pro response: ", res); console.log("[Response] gemini-pro response: ", res);
return ( return (
res?.candidates?.at(0)?.content?.parts.at(0)?.text || res?.candidates?.at(0)?.content?.parts.at(0)?.text ||
res?.error?.message || res?.error?.message ||
@ -176,6 +177,16 @@ export class GeminiProApi implements LLMApi {
clearTimeout(requestTimeoutId); clearTimeout(requestTimeoutId);
const resJson = await res.json(); const resJson = await res.json();
if (resJson?.promptFeedback?.blockReason) {
// being blocked
options.onError?.(
new Error(
"Message is being blocked for reason: " +
resJson.promptFeedback.blockReason,
),
);
}
const message = this.extractMessage(resJson); const message = this.extractMessage(resJson);
options.onFinish(message); options.onFinish(message);
} }