Skip to content

Commit

Permalink
community[patch]: Fix handling parallel tool call results in bedrock (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul authored Jul 30, 2024
1 parent 216295f commit 5ed8753
Showing 2 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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",
@@ -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();
38 changes: 27 additions & 11 deletions libs/langchain-community/src/utils/bedrock/anthropic.ts
Original file line number Diff line number Diff line change
@@ -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 }));
}

0 comments on commit 5ed8753

Please sign in to comment.