Skip to content

Commit

Permalink
updated tools logic in python (results is handled in the framework, n…
Browse files Browse the repository at this point in the history
…ot in the tool handler)
  • Loading branch information
brnaba-aws committed Nov 20, 2024
1 parent 181534a commit b7a4e4a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
10 changes: 5 additions & 5 deletions examples/python-demo/tools/weather_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"""


async def weather_tool_handler(response: ConversationMessage, conversation: List[Dict[str, Any]]):
async def weather_tool_handler(response: ConversationMessage, conversation: List[Dict[str, Any]]) -> ConversationMessage:
response_content_blocks = response.content

# Initialize an empty list of tool results
Expand All @@ -64,11 +64,11 @@ async def weather_tool_handler(response: ConversationMessage, conversation: List
tool_use_name = tool_use_block.get("name")

if tool_use_name == "Weather_Tool":
response = await fetch_weather_data(tool_use_block["input"])
tool_response = await fetch_weather_data(tool_use_block["input"])
tool_results.append({
"toolResult": {
"toolUseId": tool_use_block["toolUseId"],
"content": [{"json": {"result": response}}],
"content": [{"json": {"result": tool_response}}],
}
})

Expand All @@ -77,8 +77,8 @@ async def weather_tool_handler(response: ConversationMessage, conversation: List
role=ParticipantRole.USER.value,
content=tool_results)

# Append the new message to the ongoing conversation
conversation.append(message)
return message


async def fetch_weather_data(input_data):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ async def process_request(
conversation.append(bedrock_response)

if any('toolUse' in content for content in bedrock_response.content):
await self.tool_config['useToolHandler'](bedrock_response, conversation)
tool_response = await self.tool_config['useToolHandler'](bedrock_response, conversation)
conversation.append(tool_response)
else:
continue_with_tools = False
final_message = bedrock_response
Expand Down

0 comments on commit b7a4e4a

Please sign in to comment.