Finally, the LLM for analysing the alerts and taking action based on them. It
will be a simple service that listens to the alerts_climatecore
stream and
takes action based on the alerts posted there.
When there are alerts, the service needs to pick them up and analyse them, while also taking into consideration the current context (time of the day, for instance), as well as its previous action recommendations.
Will use a NATS stream as the memory for the LLM (“when all you’ve got is a hammer…”), with a retention policy that keeps older recommendations around for a couple of days.
1nats stream add memory_climatecore \
2 --subjects "memory.climatecore" \
3 --storage file \
4 --retention limits \
5 --discard old \
6 --dupe-window 2m \
7 --max-msgs 50 \
8 --max-msgs-per-subject 50 \
9 --max-age 2d \
10 --max-msg-size 2048 \
11 --defaults
I’m using a local LLM ("mlx-community/Mistral-Small-24B-Instruct-2501-4bit") for this, which runs fairly well on my Macbook Pro. The LLM is invoked using the Simon Willison’s excellent llm library, with the llm-mlx plugin.
The LLM receives a prompt containing the current time, the list of active
alerts, and its previous recommendations. It is asked to generate a new
recommendation only if strictly necessary. If a new recommendation is made, it
is posted to the notifications
stream (which currently acts as a stand-in for
a real actuator), published to the memory stream, and also sent to an upstream
notification stream for analysis by a higher-level module.
The upstream notification stream is set up as follows:
1nats stream add synapsteward_cerebellum \
2 --subjects "upstream.climatecore.*" \
3 --storage file \
4 --retention limits \
5 --discard old \
6 --dupe-window 2m \
7 --max-msgs 1000 \
8 --max-msgs-per-subject 800 \
9 --max-age 15d \
10 --max-msg-size 2048 \
11 --defaults
(I know… “cerebellum”. Well, you come up with a better name!)
The plumbing is now in place. And here is the code to plug the LLM in: https://github.com/venkytv/synapsteward-climatecore/blob/046ddbf/actuator.py
It is working decently so far. The recommendations are like this:
Investigate anomalies: Humidity sensor in the living room has
consistently reported values slightly above the upper bound.
This could indicate a malfunctioning sensor or an actual
environmental issue that needs attention.
And the times when it is not recommending anything, on these lines:
No action suggested; reason: The humidity sensor in the
living room continues to report values slightly above the
upper bound, but since this issue has been previously noted
and investigated, no further immediate action is required,
especially at this early hour.