Skip to content

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_littering and others), so the former makes it easier to follow

Keep running: nothing needs to be running. §1 starts the assistant

What you need: PC only

Time required: approx. 60 min

Where this page belongs

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.

  1. start the assistant service
  2. call /assistant/plan and confirm the park-north plan
  3. call /assistant/execute and confirm triggered: true
  4. inspect /assistant/executions and 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 execute and executions
  • If you want to extend this into LLM-based planning: continue to the LLM Planner hands-on next

What this page covers

  • how Phase 2 event sharing expands into decision and control in Phase 3
  • the reason for separating plan and execute
  • the meaning of designing planner, evaluator, and actuator as separate modules

Common pitfalls

  • being able to build a plan is different from having the conditions met at execute
  • 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 main branch of the Blockchain_IoT_Marketplace repository

Matching source files

This hands-on uses the following files. assistant/app/main.py is an API server built with FastAPI [2].

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:

  1. target area: park-north
  2. events of interest: possible_littering and suspicious_activity
  3. actions to run when the threshold is exceeded: light_on and send_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 Up with docker 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_area is park-north
  • watch_events includes possible_littering
  • actions includes light_on and send_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_littering count crosses the threshold
  • the target location matches park-north
  • therefore triggered becomes true
  • as a result, light_on and send_notification are 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 / deny by 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/plan converts the human request into a plan
  • /assistant/execute returns triggered: true
  • light_on and send_notification appear in actions_executed
  • /assistant/executions shows 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.json may be below threshold
  • check both the possible_littering count and the location

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 uvicorn on the host.

Related pages:

References

[1] Docker, "Get Docker," Docker Docs, https://docs.docker.com/get-started/get-docker/

[2] FastAPI, "FastAPI," https://fastapi.tiangolo.com/