Skip to content

Commit

Permalink
google-common[patch]: Merge adjacent function results in google common
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jul 29, 2024
1 parent 8718e3b commit 27cf734
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions libs/langchain-google-common/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,20 @@ class ChatConnection<AuthOptions> 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(
Expand Down

0 comments on commit 27cf734

Please sign in to comment.