
Bridging AI and real-time data with tools
Augment your LLM responses with real-time data from external tools and APIs
- An AWS account. You can create your account here.
- Request access to an AI model (we'll use Claude Sonnet) on Amazon Bedrock before you can use it. Learn about model access here.
- Python 3.6.0 or later setup and configured on your system.
- A python virtual environment setup with packages installed via requirements.txt. Read more about doing this here.
Weather_Tool
.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
# Step 1: Define system prompt that requires the use of the tool
#
SYSTEM_PROMPT = """
You are a weather assistant that provides current weather data for user-specified locations using only
the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself.
If the user provides coordinates, infer the approximate location and refer to it in your response.
To use the tool, you strictly apply the provided tool specification.
- Explain your step-by-step process, and give brief updates before each step.
- Only use the Weather_Tool for data. Never guess or make up information.
- Repeat the tool use for subsequent requests if necessary.
- If the tool errors, apologize, explain weather is unavailable, and suggest other options.
- Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use
emojis where appropriate.
- Only respond to weather queries. Remind off-topic users of your purpose.
- Never claim to search online, access external data, or use tools besides Weather_Tool.
- Complete the entire process until you have all required data before sending the complete response.
"""
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#
# Step 2: Define weather tool using the Converse API tool definition format
#
WEATHER_TOOL_SPEC = {
"toolSpec": {
"name": "Weather_Tool",
"description": "Get the current weather for a given location, based on its WGS84 coordinates.",
"inputSchema": {
"json": {
"type": "object",
"properties": {
"latitude": {
"type": "string",
"description": "Geographical WGS84 latitude of the location.",
},
"longitude": {
"type": "string",
"description": "Geographical WGS84 longitude of the location.",
},
},
"required": ["latitude", "longitude"],
}
},
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#
# Step 3: Implement the weather tool function
#
def fetch_weather_data(input_data):
"""
Fetches weather data for the given latitude and longitude using the Open-Meteo API.
Returns the weather data or an error message if the request fails.
:param input_data: The input data containing the latitude and longitude.
:return: The weather data or an error message.
"""
endpoint = "https://api.open-meteo.com/v1/forecast"
latitude = input_data.get("latitude")
longitude = input_data.get("longitude", "")
params = {"latitude": latitude, "longitude": longitude, "current_weather": True}
try:
response = requests.get(endpoint, params=params)
weather_data = {"weather_data": response.json()}
response.raise_for_status()
return weather_data
except RequestException as e:
return e.response.json()
except Exception as e:
return {"error": type(e), "message": str(e)}
1
2
3
4
5
6
7
8
9
10
11
#
# Step 4: Define the initial message and conversation
#
conversation = []
initial_message = {
"role": "user",
"content": [
{ "text": "What is the current weather in Minneapolis, MN?" }
],
}
conversation.append(initial_message)
converse
call, sending along the model, conversation, tool config, and our system prompt.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#
# Step 5: Send the message with the tool config and system prompt
#
response = bedrock.converse(
modelId=MODEL_ID,
messages=conversation,
inferenceConfig={
"maxTokens": 2000,
"temperature": 0
},
toolConfig={
"tools": [WEATHER_TOOL_SPEC]
},
system=[{"text": SYSTEM_PROMPT}]
)
toolUse
is returned) and if so, we call the requested tool.fetch_weather_data
function, we then prepare a toolResult
to send back to Amazon Bedrock for further processing.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#
# Step 6: Process the response and call the tool when requested
#
response_message = response['output']['message']
conversation.append(response_message)
response_content_blocks = response_message['content']
follow_up_content_blocks = []
for content_block in response_content_blocks:
if 'text' in content_block:
print(content_block['text'])
elif 'toolUse' in content_block:
tool_use_block = content_block['toolUse']
tool_use_name = tool_use_block['name']
if tool_use_name == 'Weather_Tool':
tool_result_value = fetch_weather_data(tool_use_block['input'])
follow_up_content_blocks.append({
"toolResult": {
"toolUseId": tool_use_block['toolUseId'],
"content": [
{
"json": {
"result": tool_result_value
}
}
]
}
})
# Handle unknown tools
else:
follow_up_content_blocks.append({
"toolResult": {
"toolUseId": tool_use_block['toolUseId'],
"content": [
{
"text": "unknown tool" + tool_use_name
}
],
"status": "error"
}
})
toolResult
containing the current weather data, we now send this to Amazon Bedrock so we can get the final natural language response from the model incorporating the tool result -- the current weather.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
# Step 7: Send tool result back to Amazon Bedrock
#
if len(follow_up_content_blocks) > 0:
follow_up_message = {
"role": "user",
"content": follow_up_content_blocks,
}
conversation.append(follow_up_message)
response = bedrock.converse(
modelId=MODEL_ID,
messages=conversation,
inferenceConfig={
"maxTokens": 2000,
"temperature": 0
},
toolConfig={
"tools": [WEATHER_TOOL_SPEC]
},
system=[{"text": SYSTEM_PROMPT}]
)
response_message = response['output']['message']
print(response_message['content'][0]['text'])
conversation.append(response_message)
1
python weather_tool_use_demo.py
1
2
3
4
5
6
7
8
9
10
11
Okay, let me check the current weather conditions for Minneapolis, MN using the Weather Tool.
First, I'll need to get the latitude and longitude coordinates for Minneapolis:
Minneapolis, MN coordinates: 44.9778°N, 93.2650°W
Next, I'll invoke the Weather Tool with those coordinates:
Here is the current weather report for Minneapolis, MN:
The temperature is 17.9°C (64.2°F) with a partly cloudy sky. Winds are blowing from the west-northwest at 19.8 km/h (12.3 mph). ☁️ 🍃
Let me know if you need any other weather details for Minneapolis!