Run the regional safety assistant¶
The basic Phase 3 sample: using a sample file of events like those shared in Phase 2 (possible_littering etc.), an AI breaks a person's free-form request into a plan and responds.
What you'll do: Free-form request → plan → execute
Prerequisites: one of the 2.1 pages (Share detections, not raw frames or Share disaster events). This page uses a sample event file (
possible_litteringand others), so the former makes it easier to followKeep running: nothing needs to be running. §1 starts the assistant
What you need: PC only
Time required: approx. 60 min
Where this page belongs
- This is 3.3 in Part 3 (Hands-on).
- "Phase 2" and "Phase 3" on this page are development stages of the platform, not the learning order (Hands-on).
- Previous page: Protect camera images before handing them to AI (local vs cloud)
- Next page: Let an AI plan the steps (LLM Planner)
As an extension of Share detections, not raw frames, this covers the flow where an AI interprets a human request and breaks it down into event collection, condition evaluation, and device control.
In Phase 2, the focus was on sharing events such as possible_littering.
In Phase 3, the flow becomes:
human request -> planner -> execution plan -> event evaluation -> device actions
Shortest path¶
For a first pass, these four steps are enough.
- start the
assistantservice - call
/assistant/planand confirm thepark-northplan - call
/assistant/executeand confirmtriggered: true - inspect
/assistant/executionsand confirm that request, plan, and execution result are connected
Branches after that:
- If you only want the planning part: stop after
/assistant/plan - If you want the full execution path: continue through
executeandexecutions - If you want to extend this into LLM-based planning: continue to the
LLM Plannerhands-on next
What this page covers¶
- how Phase 2 event sharing expands into decision and control in Phase 3
- the reason for separating
planandexecute - the meaning of designing planner, evaluator, and actuator as separate modules
Common pitfalls¶
- being able to build a
planis different from having the conditions met atexecute - treating event-count evaluation and device control as one process makes the flow hard to see
- this page has you read the minimum implementation itself rather than a problem program, so the module boundaries deserve attention
Required environment¶
- Docker / Docker Compose [1]
curl- the
mainbranch of theBlockchain_IoT_Marketplacerepository
Matching source files¶
This hands-on uses the following files. assistant/app/main.py is an API server built with FastAPI [2].
- assistant/app/main.py
- assistant/app/rule_based_planner.py
- assistant/app/planner_factory.py
- assistant/app/evaluator.py
- assistant/app/actuator.py
- examples/phase3_request_park_safety.json
- examples/phase3_events_park_safety.json
Unlike the earlier workshop samples, this page points directly to the minimum implementation itself rather than a separate problem/answer pair.
The reason is that the module boundaries between planner, evaluator, and actuator are themselves what you learn here.
Scenario¶
The user asks:
If littering or risky behavior is increasing on the north side of the park, tell me. If needed, turn on the lights and notify the manager.
The assistant then interprets the request as:
- target area:
park-north - events of interest:
possible_litteringandsuspicious_activity - actions to run when the threshold is exceeded:
light_onandsend_notification
Sequence Diagram¶
sequenceDiagram
participant User
participant API as Assistant API
participant Planner
participant Evaluator
participant Actuator
participant Log as Execution Store
User->>API: POST /assistant/plan
API->>Planner: pass request_text
Planner-->>API: return plan
API-->>User: plan
User->>API: POST /assistant/execute
API->>Planner: convert request_text into plan
Planner-->>API: plan
API->>Evaluator: pass plan and observed_events
Evaluator-->>API: triggered / reason
alt triggered == true
API->>Actuator: execute actions
Actuator-->>API: executed actions
end
API->>Log: store execution record
API-->>User: execution result
Note that plan and execute are separated, and that if the request has no observed_events, execute loads the sample events file and evaluates that.
Phase 3: Inspect how the planner interprets the request¶
1. Start the assistant service¶
Run the following in the Blockchain_IoT_Marketplace repository.
cd ~/program/Blockchain_IoT_Marketplace
docker compose -f infra/docker-compose.yml --profile assistant up --build -d assistant
Check:
curl http://localhost:8090/health
Expected:
{"status":"ok","service":"assistant"}
If you see Connection refused:
- the container may still be starting
- check whether the assistant service is
Upwithdocker ps - inspect logs with
docker compose -f infra/docker-compose.yml --profile assistant logs assistant
2. Inspect the generated plan¶
First, inspect how the natural-language request is turned into a plan.
curl -X POST http://localhost:8090/assistant/plan \
-H 'Content-Type: application/json' \
-d @examples/phase3_request_park_safety.json
Example expected output:
{
"status": "planned",
"plan": {
"intent": "monitor_public_safety",
"target_area": "park-north",
"watch_events": ["possible_littering", "suspicious_activity"],
"actions": [
{"action_type": "light_on", "target": "park-north-light-1", "parameters": {"brightness": 80}},
{"action_type": "send_notification", "target": "park-north-manager", "parameters": {"channel": "mobile_push"}}
]
}
}
Checkpoints:
target_areaispark-northwatch_eventsincludespossible_litteringactionsincludeslight_onandsend_notification
The request has been converted into a structured plan. Next, see how event evaluation and device control proceed based on that plan.
Phase 3: Inspect evaluator and actuator behavior¶
3. Execute the plan¶
Now execute the request against the sample event file.
curl -X POST http://localhost:8090/assistant/execute \
-H 'Content-Type: application/json' \
-d @examples/phase3_request_park_safety.json
Example expected output (the sample request file is in Japanese, so request_text comes back in Japanese; the rule-based planner looks for 公園北側, ポイ捨て, 照明 and 通知):
{
"status": "executed",
"execution": {
"request_text": "公園北側でポイ捨てや危険行動が増えていたら教えて。必要なら照明をつけて管理者に通知して。",
"evaluation": {
"triggered": true,
"matched_counts": {"possible_littering": 3, "suspicious_activity": 0},
"reason": "possible_littering=3 >= 3"
},
"actions_executed": [
{"action_type": "light_on", "target": "park-north-light-1", "parameters": {"brightness": 80}},
{"action_type": "send_notification", "target": "park-north-manager", "parameters": {"channel": "mobile_push"}}
]
}
}
This result means the threshold is met, because the sample event file contains three possible_littering events.
How to read the result:
- the
possible_litteringcount crosses the threshold - the target location matches
park-north - therefore
triggeredbecomestrue - as a result,
light_onandsend_notificationare executed
4. Inspect execution history¶
curl http://localhost:8090/assistant/executions
Checkpoints:
- original
request_text - generated
plan - evaluation result in
evaluation - executed actions in
actions_executed
are all kept together as one execution record.
The final section organizes the difference from Phase 2.
Phase 3: Organize the difference from Phase 2¶
5. Difference between Phase 2 and Phase 3¶
In Phase 2, the focus was mainly on:
- sharing events
- deciding
allow/denyby consent or policy
Phase 3 adds:
- interpreting a human request
- selecting relevant events
- assembling evaluation conditions
- issuing device actions when conditions are met
Phase 3 is the stage that moves from "sharing events" to "deciding and acting in response to a request."
6. Success criteria¶
This hands-on is successful if you can confirm the following:
/assistant/planconverts the human request into a plan/assistant/executereturnstriggered: truelight_onandsend_notificationappear inactions_executed/assistant/executionsshows the recorded history
7. Common issues¶
examples/phase3_request_park_safety.json cannot be found¶
- make sure your current directory is the root of
Blockchain_IoT_Marketplace
Expected actions do not run¶
- the event count in
examples/phase3_events_park_safety.jsonmay be below threshold - check both the
possible_litteringcount and thelocation
It is unclear where the AI part is¶
In this minimum implementation, the planner is still rule-based.
However, the module boundary that converts a natural-language request into a structured plan is built first, which makes it easy to later replace with an LLM or an external planner.
8. Stop services¶
cd ~/program/Blockchain_IoT_Marketplace
docker compose -f infra/docker-compose.yml --profile assistant stop assistant
Next steps¶
Next is Let an AI plan the steps (LLM Planner). You replace the rule-based planner of this page with a planner that uses an LLM.
- First stop the assistant with the command in 8. Stop services. Stage 1 of the next page starts the assistant on the same port 8090 with
uvicornon the host.
Related pages:
- To connect this back to Phase 2: Share detections, not raw frames
- To review the Phase 1 foundation: Pull from Home Assistant (HA SSI Publisher)
References¶
[1] Docker, "Get Docker," Docker Docs, https://docs.docker.com/get-started/get-docker/
[2] FastAPI, "FastAPI," https://fastapi.tiangolo.com/