From 27cf734d35ced8f19bee45107f8c945f0c75b5cf Mon Sep 17 00:00:00 2001 From: bracesproul Date: Mon, 29 Jul 2024 15:44:49 -0700 Subject: [PATCH] google-common[patch]: Merge adjacent function results in google common --- .../src/chat_models.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/libs/langchain-google-common/src/chat_models.ts b/libs/langchain-google-common/src/chat_models.ts index 7f3146fb2b20..207490cd3077 100644 --- a/libs/langchain-google-common/src/chat_models.ts +++ b/libs/langchain-google-common/src/chat_models.ts @@ -111,11 +111,20 @@ class ChatConnection extends AbstractGoogleLLMConnection< baseMessageToContent(msg, input[i - 1], this.useSystemInstruction) ) .reduce((acc, cur) => { - // Filter out the system content, since those don't belong - // in the actual content. - const hasNoSystem = cur.every((content) => content.role !== "system"); - return hasNoSystem ? [...acc, ...cur] : acc; - }, []); + // Filter out the system content + if (cur.every((content) => content.role === "system")) { + return acc; + } + + // Combine adjacent function messages + if (cur[0]?.role === "function" && acc.length > 0 && acc[acc.length - 1].role === "function") { + acc[acc.length - 1].parts = [...acc[acc.length - 1].parts, ...cur[0].parts]; + } else { + acc.push(...cur); + } + + return acc; + }, [] as GeminiContent[]); } formatSystemInstruction(