Skip to content

Commit

Permalink
fix(anthropic, bedrock): Remove message merging logic (#7196)
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul authored Nov 13, 2024
1 parent 1a1b7a6 commit 474994f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 56 deletions.
36 changes: 8 additions & 28 deletions libs/langchain-anthropic/src/utils/message_inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ function _formatImage(imageUrl: string) {
} as any;
}

function _mergeMessages(
function _ensureMessageContents(
messages: BaseMessage[]
): (SystemMessage | HumanMessage | AIMessage)[] {
// Merge runs of human/tool messages into single human messages with content blocks.
const merged = [];
const updatedMsgs = [];
for (const message of messages) {
if (message._getType() === "tool") {
if (typeof message.content === "string") {
const previousMessage = merged[merged.length - 1];
const previousMessage = updatedMsgs[updatedMsgs.length - 1];
if (
previousMessage?._getType() === "human" &&
Array.isArray(previousMessage.content) &&
Expand All @@ -58,7 +58,7 @@ function _mergeMessages(
});
} else {
// If not, we create a new human message with the tool result.
merged.push(
updatedMsgs.push(
new HumanMessage({
content: [
{
Expand All @@ -71,7 +71,7 @@ function _mergeMessages(
);
}
} else {
merged.push(
updatedMsgs.push(
new HumanMessage({
content: [
{
Expand All @@ -84,30 +84,10 @@ function _mergeMessages(
);
}
} else {
const previousMessage = merged[merged.length - 1];
if (
previousMessage?._getType() === "human" &&
message._getType() === "human"
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let combinedContent: Record<string, any>[];
if (typeof previousMessage.content === "string") {
combinedContent = [{ type: "text", text: previousMessage.content }];
} else {
combinedContent = previousMessage.content;
}
if (typeof message.content === "string") {
combinedContent.push({ type: "text", text: message.content });
} else {
combinedContent = combinedContent.concat(message.content);
}
previousMessage.content = combinedContent;
} else {
merged.push(message);
}
updatedMsgs.push(message);
}
}
return merged;
return updatedMsgs;
}

export function _convertLangChainToolCallToAnthropic(
Expand Down Expand Up @@ -202,7 +182,7 @@ function _formatContent(content: MessageContent) {
export function _convertMessagesToAnthropicPayload(
messages: BaseMessage[]
): AnthropicMessageCreateParams {
const mergedMessages = _mergeMessages(messages);
const mergedMessages = _ensureMessageContents(messages);
let system;
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === "system") {
system = messages[0].content;
Expand Down
46 changes: 18 additions & 28 deletions libs/langchain-community/src/utils/bedrock/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ function _formatImage(imageUrl: string) {
} as any;
}

function _mergeMessages(
function _ensureMessageContents(
messages: BaseMessage[]
): (SystemMessage | HumanMessage | AIMessage)[] {
// Merge runs of human/tool messages into single human messages with content blocks.
const merged = [];
const updatedMsgs = [];
for (const message of messages) {
if (message._getType() === "tool") {
if (typeof message.content === "string") {
const previousMessage = merged[merged.length - 1];
const previousMessage = updatedMsgs[updatedMsgs.length - 1];
if (
previousMessage?._getType() === "human" &&
Array.isArray(previousMessage.content) &&
Expand All @@ -70,7 +70,7 @@ function _mergeMessages(
});
} else {
// If not, we create a new human message with the tool result.
merged.push(
updatedMsgs.push(
new HumanMessage({
content: [
{
Expand All @@ -83,33 +83,23 @@ function _mergeMessages(
);
}
} else {
merged.push(new HumanMessage({ content: message.content }));
updatedMsgs.push(
new HumanMessage({
content: [
{
type: "tool_result",
content: _formatContent(message.content),
tool_use_id: (message as ToolMessage).tool_call_id,
},
],
})
);
}
} else {
const previousMessage = merged[merged.length - 1];
if (
previousMessage?._getType() === "human" &&
message._getType() === "human"
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let combinedContent: Record<string, any>[];
if (typeof previousMessage.content === "string") {
combinedContent = [{ type: "text", text: previousMessage.content }];
} else {
combinedContent = previousMessage.content;
}
if (typeof message.content === "string") {
combinedContent.push({ type: "text", text: message.content });
} else {
combinedContent = combinedContent.concat(message.content);
}
previousMessage.content = combinedContent;
} else {
merged.push(message);
}
updatedMsgs.push(message);
}
}
return merged;
return updatedMsgs;
}

export function _convertLangChainToolCallToAnthropic(
Expand Down Expand Up @@ -170,7 +160,7 @@ export function formatMessagesForAnthropic(messages: BaseMessage[]): {
system?: string;
messages: Record<string, unknown>[];
} {
const mergedMessages = _mergeMessages(messages);
const mergedMessages = _ensureMessageContents(messages);
let system: string | undefined;
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === "system") {
if (typeof messages[0].content !== "string") {
Expand Down

0 comments on commit 474994f

Please sign in to comment.