From bde81c0a1752ca7dbcbbabb7c42e09538dc3683c Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Mon, 17 Feb 2025 23:54:08 +0800 Subject: [PATCH] fix: MALFORMED_FUNCTION_CALL --- client/CustomGoogleGeminiClient.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/client/CustomGoogleGeminiClient.js b/client/CustomGoogleGeminiClient.js index ff2b629..b127baa 100644 --- a/client/CustomGoogleGeminiClient.js +++ b/client/CustomGoogleGeminiClient.js @@ -111,9 +111,10 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient { * search: boolean, * codeExecution: boolean, * }} opt + * @param {number} retryTime 重试次数 * @returns {Promise<{conversationId: string?, parentMessageId: string, text: string, id: string}>} */ - async sendMessage (text, opt = {}) { + async sendMessage (text, opt = {}, retryTime = 3) { let history = await this.getHistory(opt.parentMessageId) let systemMessage = opt.system // if (systemMessage) { @@ -269,7 +270,7 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient { */ let responseContent /** - * @type {{candidates: Array<{content: Content, groundingMetadata: GroundingMetadata}>}} + * @type {{candidates: Array<{content: Content, groundingMetadata: GroundingMetadata, finishReason: string}>}} */ let response = await result.json() if (this.debug) { @@ -277,6 +278,11 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient { } responseContent = response.candidates[0].content let groundingMetadata = response.candidates[0].groundingMetadata + if (response.candidates[0].finishReason === 'MALFORMED_FUNCTION_CALL') { + logger.warn('遇到MALFORMED_FUNCTION_CALL,进行重试。') + return this.sendMessage(text, opt, retryTime--) + } + // todo 空回复也可以重试 if (responseContent.parts.filter(i => i.functionCall).length > 0) { // functionCall const functionCall = responseContent.parts.filter(i => i.functionCall).map(i => i.functionCall)