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

community[patch]: Fix handling parallel tool call results in bedrock #6266

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BedrockChatStandardIntegrationTests extends ChatModelIntegrationTests<
Cls: BedrockChat,
chatModelHasToolCalling: true,
chatModelHasStructuredOutput: true,
supportsParallelToolCalls: true,
constructorArgs: {
region,
model: "anthropic.claude-3-sonnet-20240229-v1:0",
Expand Down Expand Up @@ -57,6 +58,13 @@ class BedrockChatStandardIntegrationTests extends ChatModelIntegrationTests<
"Flaky test with Bedrock not consistently returning tool calls. TODO: Fix prompting."
);
}

async testParallelToolCalling() {
// Anthropic is very flaky when calling multiple tools in parallel.
// Because of this, we pass `true` as the second arg to only verify
// it can handle parallel tools in the message history.
await super.testParallelToolCalling(undefined, true);
}
}

const testClass = new BedrockChatStandardIntegrationTests();
Expand Down
38 changes: 27 additions & 11 deletions libs/langchain-community/src/utils/bedrock/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,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
Loading