Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anthropic[patch]: Refactor merging tool results #6262

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 28 additions & 38 deletions libs/langchain-anthropic/src/utils/message_inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,33 @@ function _mergeMessages(
for (const message of messages) {
if (message._getType() === "tool") {
if (typeof message.content === "string") {
merged.push(
new HumanMessage({
content: [
{
type: "tool_result",
content: message.content,
tool_use_id: (message as ToolMessage).tool_call_id,
},
],
})
);
const previousMessage = merged[merged.length - 1];
if (
previousMessage?._getType() === "human" &&
Array.isArray(previousMessage.content) &&
"type" in previousMessage.content[0] &&
previousMessage.content[0].type === "tool_result"
) {
// If the previous message was a tool result, we merge this tool message into it.
previousMessage.content.push({
type: "tool_result",
content: message.content,
tool_use_id: (message as ToolMessage).tool_call_id,
});
} else {
// If not, we create a new human message with the tool result.
merged.push(
new HumanMessage({
content: [
{
type: "tool_result",
content: message.content,
tool_use_id: (message as ToolMessage).tool_call_id,
},
],
})
);
}
} else {
merged.push(new HumanMessage({ content: message.content }));
}
Expand Down Expand Up @@ -158,32 +174,6 @@ function _formatContent(content: MessageContent) {
}
}

function _mergeAdjacentMessages(
messages: AnthropicMessageParam[]
): AnthropicMessageParam[] {
return messages.reduce((acc: AnthropicMessageParam[], current, index) => {
if (index === 0) {
return [current];
}

const previous = acc[acc.length - 1];

if (
previous.role === current.role &&
Array.isArray(previous.content) &&
Array.isArray(current.content) &&
// Only concatenate tool results if they are consecutive
previous.content[0].type === "tool_result" &&
current.content[0].type === "tool_result"
) {
previous.content = previous.content.concat(current.content);
return acc;
}

return [...acc, current];
}, []);
}

/**
* Formats messages as a prompt for the model.
* @param messages The base messages to format as a prompt.
Expand Down Expand Up @@ -264,7 +254,7 @@ export function _formatMessagesForAnthropic(messages: BaseMessage[]): {
}
});
return {
messages: _mergeAdjacentMessages(formattedMessages),
messages: formattedMessages,
system,
};
}
Loading