Imagine you have an AI-powered personal alerting chat assistant that interacts using up-to-date data. Whether it's a big move in the stock market that affects your investments, any significant change on your shared SharePoint documents, or discounts on Amazon you were waiting for, the application is designed to keep you informed and alert you about any significant changes based on the criteria you set in advance using your natural language. In this tutorial, we'll learn how to build a real-time weather alert chat application using GlassFlow SDK and Streamlit.
This application collects real-time weather data, interprets user-defined alert criteria using OpenAI's GPT models, and notifies the user through a live chat interface whenever conditions are met. This piece of content and code samples can be invaluable for those who love technology or developers to understand how modern real-time alerting systems work with Larger Language Models (LLMs) and how to implement one.
You can also quickly jump on the source code hosted on our GitHub and try it yourself.
The Power Behind the Scenes
Let’s take a closer look at how the AI weather alert chat application works and transforms raw data into actionable alerts, keeping you updated about weather conditions.
Using GlassFlow Python SDK, we set up an event-driven stream processing pipeline for real-time weather alerts. Unlike traditional setups requiring message brokers and manual management, GlassFlow offers a fully managed serverless infrastructure for stream processing. This allows us to:
- Process and transform real-time weather data.
- Apply user-defined criteria dynamically.
- Integrate with Streamlit for a seamless, interactive frontend.
Integrating OpenAI's GPT model brings a conversational AI to life, capable of understanding alerts’ nature and responding to user queries. Users can specify their alert criteria in natural language, then the GPT model will interpret them.
Setting Up the GlassFlow Pipeline
The below Python code sets up a real-time data pipeline using GlassFlow. By leveraging just a few intuitive SDK methods, such as initializing a GlassFlow client, defining spaces, and deploying pipelines, you can set up a fully functional data processing pipeline in minutes.
Once you run the above Python script, a new pipeline is created with the data transformation function and returns the Pipeline ID to use in the next steps.
Connecting to the Pipeline for Data Processing
After successfully creating the pipeline, the below code snippet from (alert_streamlit_app.py
) Python script connects to the existing pipeline on the client side. We retrieve the pipeline's source (data_source
) and sink (data_sink
) objects. These objects facilitate sending data to and receiving transformed data from the pipeline. The data_source
is used to publish weather and criteria data, while the data_sink
consumes the processed alerts generated by the transformation function in the pipeline.
Creating an Interactive Streamlit Application
The second part of the code integrates the GlassFlow pipeline into a Streamlit web application. It creates an interactive UI for users to set their weather alert criteria and view real-time weather alerts. Users input their alert criteria through a text box, and a button confirms the criteria. The app stores this input in Streamlit's session state to persist user preferences across interactions.
The fetch_weather_data
function retrieves the latest weather data from the Weather API. This weather data includes temperature, humidity, precipitation, and more, covering locations worldwide. This snippet asynchronously fetches current weather data for Estonia but the app can be improved to set the location from user input dynamically. If the user has set an alert, both the weather data and the criteria are sent to the GlassFlow pipeline via the data_source
.
Listening for Alerts in Real-Time
The code then continuously listens for responses from the GlassFlow pipeline using the data_sink
. If the pipeline generates an alert based on the user's criteria, the response is consumed and displayed in the Streamlit UI as part of a conversation. The app uses a polling mechanism with a short delay to check for new alerts regularly. This ensures that users receive real-time updates without requiring manual refreshes. The combination of Streamlit for the UI and GlassFlow for the backend processing simplifies the development of real-time event-driven applications.
Next steps
You can further improve the sample app. In the app, we are just fetching live raw weather data and sending it straightaway to OpenAI or the frontend. Sometimes you need to transform this data to filter, enrich, aggregate, or normalize it in real-time before it reaches the external services. You start to think about creating a stream processing pipeline with several stages.
For example, not all the data fetched from the API will be relevant to every user and you can filter out unnecessary information at an initial stage. Also, data can come in various formats, especially if you're sourcing information from multiple APIs for comprehensive alerting and you need to normalize this data. At the next stage, you enrich the data with extra context or information to the raw data to make it more useful. This could include comparing current weather conditions against historical data to identify unusual patterns or adding location-based insights using another external API, such as specific advice for weather conditions in a particular area. At later stages, you might aggregate hourly temperature data to give an average daytime temperature or to highlight the peak temperature reached during the day.
Try It Out
To explore the real-time weather alert chat application in detail and try it out for yourself, please visit our GitHub repository. The repository contains all the necessary code, detailed setup instructions, and additional documentation to help you get started. Once the setup is complete, you can start the Streamlit frontend and the Python backend. Set your weather alert criteria, and see how the system processes real-time weather data to keep you informed.