Skip to content

Commit

Permalink
fix: 🐛 Fix Bedrock Converse's tool use blocks, when there are multipl…
Browse files Browse the repository at this point in the history
…e consecutive function calls (#14386)
  • Loading branch information
AndreCNF authored Jul 6, 2024
1 parent 2a43f06 commit 11cde0f
Showing 1 changed file with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,39 @@ def messages_to_converse_messages(
status = message.additional_kwargs.get("status")
if status:
content["toolResult"]["status"] = status
converse_message = {
"role": "user",
"content": [content],
}
converse_messages.append(converse_message)
converse_messages.append(
{
"role": "user",
"content": [content],
}
)
else:
content = []
if message.content:
# get the text of the message
content.append({"text": message.content})
# convert tool calls to the AWS Bedrock Converse format
tool_calls = message.additional_kwargs.get("tool_calls", [])
for tool_call in tool_calls:
assert "toolUseId" in tool_call, f"`toolUseId` not found in {tool_call}"
assert "input" in tool_call, f"`input` not found in {tool_call}"
assert "name" in tool_call, f"`name` not found in {tool_call}"
content.append({"toolUse": tool_call})
converse_message = {
"role": message.role.value,
"content": content,
}
converse_messages.append(converse_message)
converse_messages.append(
{
"role": message.role.value,
"content": [{"text": message.content}],
}
)
# convert tool calls to the AWS Bedrock Converse format
# NOTE tool calls might show up within any message,
# e.g. within assistant message or in consecutive tool calls,
# thus this tool call check is done for all messages
tool_calls = message.additional_kwargs.get("tool_calls", [])
content = []
for tool_call in tool_calls:
assert "toolUseId" in tool_call, f"`toolUseId` not found in {tool_call}"
assert "input" in tool_call, f"`input` not found in {tool_call}"
assert "name" in tool_call, f"`name` not found in {tool_call}"
content.append({"toolUse": tool_call})
if len(content) > 0:
converse_messages.append(
{
"role": "assistant", # tool calls are always from the assistant
"content": content,
}
)

return __merge_common_role_msgs(converse_messages), system_prompt.strip()

Expand Down

0 comments on commit 11cde0f

Please sign in to comment.