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
#6264)

* google-common[patch]: Merge adjacent function results in google common

* chore: lint files
  • Loading branch information
bracesproul authored Jul 29, 2024
1 parent 799e715 commit a1f4114
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 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,27 @@ 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 a1f4114

Please sign in to comment.