Creating a Weather Agent with BedrockLLMAgent and Custom Tools
This guide demonstrates how to create a specialized weather agent using BedrockLLMAgent and a custom weather tool. We’ll walk through the process of defining the tool, setting up the agent, and integrating it into your Multi-Agent Orchestrator system.
Define the Weather Tool
Let’s break down the weather tool definition into its key components:
A. Tool Description
Explanation:
This describes the tool’s interface to the LLM.
name: Identifies the tool to the LLM.
description: Explains the tool’s purpose to the LLM.
inputSchema: Defines the expected input format.
Requires latitude and longitude as strings.
This schema helps the LLM understand how to use the tool correctly.
B. Custom Prompt
Explanation:
This prompt sets the behavior and limitations for the LLM.
It instructs the LLM to:
Use only the Weather_Tool for data.
Infer coordinates from location names.
Provide step-by-step explanations.
Handle errors gracefully.
Format responses consistently (units, conciseness).
Stay on topic and use only the provided tool.
C. Tool Handler
Explanation:
This handler processes the LLM’s request to use the Weather_Tool.
It iterates through the response content, looking for tool use blocks.
When it finds a Weather_Tool use:
It calls fetchWeatherData with the provided coordinates.
It formats the result into a tool result object.
Finally, it adds the tool results to the conversation as a new user message.
D. Data Fetching Function
Explanation:
This function makes the actual API call to get weather data.
It uses the Open-Meteo API (a free weather API service).
It constructs the API URL with the provided latitude and longitude.
It handles both successful responses and errors:
On success, it returns the weather data.
On failure, it returns an error object.
These components work together to create a functional weather tool:
The tool description tells the LLM how to use the tool.
The prompt guides the LLM’s behavior and response format.
The handler processes the LLM’s tool use requests.
The fetch function retrieves real weather data based on the LLM’s input.
This setup allows the BedrockLLMAgent to provide weather information by seamlessly integrating external data into its responses.
Create the Weather Agent
Now that we have our weather tool defined and the code above in a file called weatherTool.ts, let’s create a BedrockLLMAgent that uses this tool.
Add the Weather Agent to the Orchestrator
Now we can add our weather agent to the Multi-Agent Orchestrator:
4. Using the Weather Agent
Now that our weather agent is set up and added to the orchestrator, we can use it to get weather information:
How It Works
When a weather query is received, the orchestrator routes it to the Weather Agent.
The Weather Agent processes the query using the custom system prompt (WEATHER_PROMPT).
The agent uses the Weather_Tool to fetch weather data for the specified location.
The weatherToolHandler processes the tool use, fetches real weather data, and adds it to the conversation.
The agent then formulates a response based on the weather data and the original query.
This setup allows for a specialized weather agent that can handle various weather-related queries while using real-time data from an external API.
By following this guide, you can create a powerful, context-aware weather agent using BedrockLLMAgent and custom tools within your Multi-Agent Orchestrator system.