Making Your Agent Constantly Aware of Real-Time Local Weather Conditions
Introduction
In this tutorial, we will guide you through the process of creating a Hook in BrainSoup to update a Custom Context with local weather data. By leveraging this powerful combination of Hooks and Custom Contexts, you can ensure your agent is constantly aware of the current weather conditions, making it more helpful as a personal assistant.
Steps 1: Choose the Trigger Event
We will use the periodic trigger Timer.Every15Minutes
to fetch the weather data every 15 minutes. This ensures that the context is updated frequently enough to provide accurate weather information.
Steps 2: Write the Hook Script
The script, GetLocalWeather.ps1
, will be placed in the Hooks\OnEvent\Timer.Every15Minute
directory. This PowerShell script calls the Open-Meteo API to retrieve current weather data and saves it as a JSON file named LocalWeather.json
in the Context
directory.
Here is the script:
# URL to the Open-Meteo API # To create this URL, go to https://open-meteo.com/en/docs and use the configurator to select the data you want to retrieve $latitude = "40.7143" $longitude = "-74.006" $forecast_days = "3" $url = "https://api.open-meteo.com/v1/forecast?latitude=$latitude&longitude=$longitude¤t=temperature_2m,relative_humidity_2m,weather_code,wind_speed_10m&daily=weather_code,temperature_2m_max,temperature_2m_min,sunrise,sunset&timezone=auto&forecast_days=$forecast_days" # Call the API and get the response $response = Invoke-RestMethod -Uri $url -Method Get # Filter the response to keep only useful fields: # - Keep 'current_units', 'current', and 'daily' fields # - Rename 'current_units' to 'units' $filteredResponse = $response | Select-Object -Property @{Name='units'; Expression='current_units'}, current, daily # Convert the filtered response to JSON $jsonResponse = $filteredResponse | ConvertTo-Json -Compress # Path to the context file: "%AppData%\BrainSoup\Context\LocalWeather.json" $outputFilePath = [System.IO.Path]::Combine($env:APPDATA, "BrainSoup\Context\LocalWeather.json") # Save the JSON response to a file $jsonResponse | Out-File -FilePath $outputFilePath -Encoding utf8
Steps 3: Place Script in Appropriate Directory
Save this script as GetLocalWeather.ps1
and place it in %AppData%\BrainSoup\Hooks\OnEvent\Timer.Every15Minute\
.
Steps 4: Subscribe Agents to the Custom Context
To make an agent aware of this new context:
- Open BrainSoup.
- Go to Participants > Agents.
- Select your agent and click Edit.
- Navigate to Context subscriptions.
- Add
LocalWeather
as a subscribed context. - Click OK.
Conclusion
By following these steps, you can set up a Hook in BrainSoup that updates a Custom Context with local weather data every 15 minutes. This ensures your agents are always informed about current weather conditions and can respond accordingly.