Skip to content

Let an AI plan the steps (LLM Planner)

Replace the rule-based planner in Run the regional safety assistant with one based on a large language model (LLM), in three stages.

What you'll do: Swap planner via stub provider first, check it in the frontend demo, then use a real LLM

Prerequisites: Run the regional safety assistant

Keep running: nothing needs to be running. Stage 1 starts the assistant with uvicorn on the host. If the assistant from the previous page is running, stop it first (both use port 8090)

What you need: PC (real LLM step needs an API key or a local LLM)

Time required: approx. 60 min

Where this page belongs

  • This is 3.4 in Part 3 and the last hands-on page (Hands-on).
  • "Phase 3" on this page is a development stage of the platform, not the learning order (Hands-on). "Stage 1–3" on this page are the steps within this page.
  • Previous page: Run the regional safety assistant
  • Next page: none. After this, the Design section explains the mechanisms and the reasons behind the design decisions.

This page is organized in three stages:

  1. reproduce the flow locally with the stub provider
  2. check how planner_diagnostics is shown in the frontend demo (assistant-demo)
  3. switch to an actual OpenAI-compatible API if available

Shortest path

For a first pass, these five steps are enough.

  1. start the assistant
  2. call /assistant/plan with the stub provider
  3. inspect planner_diagnostics
  4. if needed, start assistant-demo and inspect the same result from the UI
  5. finally switch to openai_compatible

Branches after that:

  • If you only need the API path: the stub provider and curl are enough
  • If you also want the UI path: start assistant-demo
  • If you want to connect to a real API: continue to assistant-llm with .env.local.example

What this page covers

  • what to keep fixed when replacing the rule-based planner with an LLM planner
  • the roles of planner_factory, validator, and fallback
  • how to read results and failures using planner_diagnostics

Common pitfalls

  • it is easy to assume that adding an LLM also requires changing the evaluator and actuator
  • the configuration difference between stub and openai_compatible is concentrated in environment variables
  • on failure, the cause is hard to find without looking at planner_diagnostics, not just the plan body

Required environment

  • Docker / Docker Compose [1]
  • curl
  • Python 3.11 or later (to run uvicorn in Stage 1 and 5.2 and pytest in 5.5 on the host). Create a virtual environment (venv) at the top of the Blockchain_IoT_Marketplace repository and install the packages into it. With Homebrew Python and similar setups, pip install outside a venv fails with an externally-managed-environment error. If python3 --version is older than 3.11, run the python3 -m venv line below with a specific version, such as python3.11 -m venv

    cd ~/program/Blockchain_IoT_Marketplace
    python3 -m venv .venv && source .venv/bin/activate
    pip install fastapi uvicorn pydantic pydantic-settings httpx pytest
    

    pip install -e . pytest also installs the required packages (along with the publisher's). In every newly opened terminal ("another terminal"), run source ~/program/Blockchain_IoT_Marketplace/.venv/bin/activate before using uvicorn, python, or pytest. With the venv active, python points to the venv's Python 3 - the main branch of the Blockchain_IoT_Marketplace repository

Matching source files

The exercise programs focus on the minimum request body for /assistant/plan and on how to summarize the returned plan.

Architecture Diagram

flowchart LR
  A["request_text"] --> B["planner_factory"]
  B --> C["RuleBasedPlanner or LLMPlanner"]
  C --> D["llm_prompt / llm_provider"]
  C --> E["plan_validator"]
  E --> F["ExecutionPlan"]
  F --> G["Evaluator / Actuator"]

Rather than embedding the LLM directly in main.py, the replacement is isolated within planner_factory.

Stage 1: Understand the planner shape with the stub provider

1. Start with the stub provider

Run the following in the Blockchain_IoT_Marketplace repository.

cd ~/program/Blockchain_IoT_Marketplace
source .venv/bin/activate
ASSISTANT_PLANNER_MODE=llm \
ASSISTANT_PLANNER_NAME=llm-planner-stub-v1 \
ASSISTANT_LLM_PROVIDER=stub \
uvicorn assistant.app.main:app --host 0.0.0.0 --port 8090

uvicorn runs on the host Python (the venv created in "Required environment"). The Docker Compose assistant service starts with the rule-based planner and returns planner_name rule-based-planner-v1, so use the host for this stage.

In another terminal:

cd ~/program/Blockchain_IoT_Marketplace
source .venv/bin/activate
curl http://localhost:8090/health

Expected:

{"status":"ok","service":"assistant"}

2. Inspect a Japanese request

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": {
    "planner_name": "llm-planner-stub-v1",
    "target_area": "park-north",
    "watch_events": ["possible_littering", "suspicious_activity"]
  }
}

Checkpoints:

  • planner_name is llm-planner-stub-v1
  • target_area is park-north
  • watch_events is returned as JSON array

Sending the same request from the exercise program (answer version) prints only the key parts of plan and planner_diagnostics:

python examples/hands_on/phase3_llm_planner/answer_program.py --base-url http://localhost:8090

3. Inspect an English request

curl -X POST http://localhost:8090/assistant/plan \
  -H 'Content-Type: application/json' \
  -d @examples/phase3_request_station_warning.json

Example expected output:

{
  "status": "planned",
  "plan": {
    "planner_name": "llm-planner-stub-v1",
    "target_area": "station-front",
    "watch_events": ["suspicious_activity"],
    "actions": [
      {"action_type": "send_notification"},
      {"action_type": "show_warning"}
    ]
  }
}

This confirms that the planner can also map an English request to station-front.

At this point, the minimum path from natural-language request to plan is working. The next step is to inspect how the same planner output should be read from the frontend side.

Stage 2: Inspect the frontend demo and diagnostics

3.5 Start the React frontend demo

There is also a minimal React screen for calling the Phase 3 assistant from a browser.

First stop the uvicorn from Stage 1 with Ctrl+C (it uses the same port 8090).

docker compose -f infra/docker-compose.yml --profile assistant up --build -d assistant
docker compose -f infra/docker-compose.yml --profile assistant-ui up --build -d assistant-ui

Open:

  • http://localhost:4173

This screen lets you check:

  • editing the natural-language request
  • POST /assistant/plan
  • POST /assistant/execute
  • GET /assistant/executions
  • badge / alert rendering for planner_diagnostics

Screen image:

assistant-demo overview

If you want a one-command path including the local mock LLM, stop the assistant above first (assistant-demo also uses port 8090):

docker compose -f infra/docker-compose.yml stop assistant
docker compose -f infra/docker-compose.yml --profile assistant-demo up --build -d assistant-demo llm-mock assistant-ui

This profile starts:

  • assistant-demo
  • llm-mock
  • assistant-ui

4. Inspect the real-API environment template

To use an actual OpenAI-compatible API, inspect the example environment file:

cat .env.local.example

Main fields:

  • ASSISTANT_PLANNER_MODE=llm
  • ASSISTANT_LLM_PROVIDER=openai_compatible
  • ASSISTANT_LLM_API_BASE_URL
  • ASSISTANT_LLM_API_KEY
  • ASSISTANT_LLM_MODEL

How to read planner_diagnostics:

  • status
  • ok or fallback
  • severity
  • info, warning, or error
  • label
  • short text that can be shown directly in a badge
  • color_hint
  • color hint such as green, amber, or red
  • code
  • stable code that the frontend can branch on
  • category
  • broad group such as success, provider, validation, or planner
  • user_message
  • short message that can be shown directly to the end user
  • summary
  • short explanation of what happened
  • suggestion
  • next troubleshooting step

Example for frontend use:

type PlannerDiagnostics = {
  status: "ok" | "fallback";
  severity: "info" | "warning" | "error";
  label: string;
  color_hint: "green" | "amber" | "red";
  code: string;
  category: "success" | "provider" | "validation" | "planner";
  user_message: string;
  summary?: string | null;
  suggestion?: string | null;
};

type BadgeViewModel = {
  label: string;
  tone: "success" | "warning" | "danger" | "neutral";
  detail: string;
  code: string;
  category: string;
};

export function diagnosticsToBadge(diagnostics: PlannerDiagnostics): BadgeViewModel {
  const toneMap: Record<PlannerDiagnostics["color_hint"], BadgeViewModel["tone"]> = {
    green: "success",
    amber: "warning",
    red: "danger",
  };

  return {
    label: diagnostics.label,
    tone: toneMap[diagnostics.color_hint] ?? "neutral",
    detail: diagnostics.user_message,
    code: diagnostics.code,
    category: diagnostics.category,
  };
}

For example, a fallback case can be rendered like this:

const badge = diagnosticsToBadge({
  status: "fallback",
  severity: "warning",
  label: "Fallback",
  color_hint: "amber",
  code: "llm_provider_error",
  category: "provider",
  user_message: "The LLM API could not be used, so the rule-based planner was used instead.",
});

Minimal React example:

type PlannerDiagnostics = {
  label: string;
  color_hint: "green" | "amber" | "red";
  code: string;
  category: string;
  user_message: string;
  summary?: string | null;
  suggestion?: string | null;
};

function DiagnosticsBadge({ diagnostics }: { diagnostics: PlannerDiagnostics }) {
  return (
    <span className={`badge badge-${diagnostics.color_hint}`}>
      {diagnostics.label}
    </span>
  );
}

function DiagnosticsAlertPanel({ diagnostics }: { diagnostics: PlannerDiagnostics }) {
  return (
    <div className={`alert alert-${diagnostics.color_hint}`}>
      <strong>{diagnostics.label}</strong>
      <p>{diagnostics.user_message}</p>
      <small>
        code={diagnostics.code} / category={diagnostics.category}
      </small>
      {diagnostics.suggestion ? <p>Next: {diagnostics.suggestion}</p> : null}
    </div>
  );
}

Minimal Vue example:

<script setup lang="ts">
type PlannerDiagnostics = {
  label: string;
  color_hint: "green" | "amber" | "red";
  code: string;
  category: string;
  user_message: string;
  summary?: string | null;
  suggestion?: string | null;
};

defineProps<{
  diagnostics: PlannerDiagnostics;
}>();
</script>

<template>
  <span :class="`badge badge-${diagnostics.color_hint}`">
    {{ diagnostics.label }}
  </span>

  <div :class="`alert alert-${diagnostics.color_hint}`">
    <strong>{{ diagnostics.label }}</strong>
    <p>{{ diagnostics.user_message }}</p>
    <small>code={{ diagnostics.code }} / category={{ diagnostics.category }}</small>
    <p v-if="diagnostics.suggestion">Next: {{ diagnostics.suggestion }}</p>
  </div>
</template>

Minimal CSS example:

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.2rem 0.65rem;
  font-size: 0.85rem;
  font-weight: 700;
}

.badge-green {
  background: #e8f7ee;
  color: #146c43;
}

.badge-amber {
  background: #fff3cd;
  color: #9a6700;
}

.badge-red {
  background: #fbe9e7;
  color: #b42318;
}

.alert {
  margin-top: 0.75rem;
  border-left: 4px solid currentColor;
  border-radius: 0.5rem;
  padding: 0.75rem 1rem;
  background: #f8f9fa;
}

.alert-green {
  color: #146c43;
  background: #eefbf3;
}

.alert-amber {
  color: #9a6700;
  background: #fff8e1;
}

.alert-red {
  color: #b42318;
  background: #fef3f2;
}

At this point, planner_diagnostics can be read from both the API and the UI. The last step is to replace the provider while keeping the same structure.

Stage 3: Switch to an OpenAI-compatible API

5. Switch to an OpenAI-compatible API

This step is optional. It needs an API key or a local LLM running on your PC. If you have neither, skip this step and try the same HTTP path with the mock server in §5.2.

Copy .env.local.example, set ASSISTANT_LLM_API_KEY (and, if needed, ASSISTANT_LLM_MODEL and ASSISTANT_LLM_API_BASE_URL) in .env.local to your own values, then start the server. Stop any other assistant on port 8090 first. If you run uvicorn on the host, press Ctrl+C in that terminal.

cd ~/program/Blockchain_IoT_Marketplace
git pull --ff-only
# stop only the Docker assistants started in §3.5 etc., the publisher and others keep running
docker compose -f infra/docker-compose.yml stop assistant assistant-demo llm-mock assistant-ui
cp .env.local.example .env.local

Edit ASSISTANT_LLM_API_KEY and the rest (and ASSISTANT_LLM_MODEL and ASSISTANT_LLM_API_BASE_URL if needed) in .env.local, save it, and then start the server. For a local LLM running on the PC, localhost does not reach it from inside the container, so set ASSISTANT_LLM_API_BASE_URL to http://host.docker.internal:<port>/v1. host.docker.internal works on Docker Desktop (macOS / Windows). On Linux with Docker Engine, the assistant-llm service has no host.docker.internal entry (no extra_hosts with host-gateway), so the name does not resolve. On Linux, use the PC's LAN IP instead (http://<PC LAN IP>:<port>/v1) and start the local LLM so that it accepts connections from the LAN, not only from localhost.

cd ~/program/Blockchain_IoT_Marketplace
docker compose -f infra/docker-compose.yml --profile assistant-llm up --build -d assistant-llm

Reference screenshot:

assistant-llm startup

Then call the same endpoint:

curl -X POST http://localhost:8090/assistant/plan \
  -H 'Content-Type: application/json' \
  -d @examples/phase3_request_park_safety.json

The expected JSON shape is documented here:

Reference screenshot:

LLM planner response

5.2 Try the HTTP path with the local mock server

If you do not want to call a real API yet, you can check just the HTTP path with an OpenAI-compatible mock server.

If assistant-demo from §3.5 is still running, its llm-mock already holds port 18000. Stop it first. Also stop here the Docker assistants that use port 8090, including assistant-llm from §5:

cd ~/program/Blockchain_IoT_Marketplace
docker compose -f infra/docker-compose.yml stop assistant assistant-demo assistant-llm llm-mock assistant-ui

Start the mock server:

cd ~/program/Blockchain_IoT_Marketplace
source .venv/bin/activate
uvicorn examples.phase3_llm_mock_server:app --host 127.0.0.1 --port 18000

Docker Compose example:

docker compose -f infra/docker-compose.yml --profile llm-mock up --build -d llm-mock

Either way, check from another terminal that the mock server responds. A JSON reply such as {"status":"ok"} means it is up.

curl http://127.0.0.1:18000/health

In another terminal, start the assistant (the commands below move to the repository top and activate the venv). Stop any other assistant on port 8090 first (the Docker assistant, assistant-demo, or assistant-llm):

cd ~/program/Blockchain_IoT_Marketplace
source .venv/bin/activate
source examples/phase3_llm_mock.env.example
uvicorn assistant.app.main:app --host 0.0.0.0 --port 8090

In yet another terminal, call the same endpoint:

cd ~/program/Blockchain_IoT_Marketplace
source .venv/bin/activate
curl -X POST http://localhost:8090/assistant/plan \
  -H 'Content-Type: application/json' \
  -d @examples/phase3_request_park_safety.json

Checkpoints:

  • planner_name is llm-planner-mock-http-v1
  • planner_diagnostics.provider_name is openai_compatible
  • planner_diagnostics.severity is info
  • planner_diagnostics.label is OK
  • planner_diagnostics.color_hint is green
  • planner_diagnostics.code is llm_plan_generated
  • planner_diagnostics.category is success
  • planner_diagnostics.user_message is readable enough to show in the UI
  • planner_diagnostics.used_fallback is false

5.5 Use the exercise pytest

This hands-on also includes a reusable pytest file for both the problem and answer programs. In the problem program (problem_program.py) you implement build_plan_request() (TODO 1), which builds the JSON to send, and summarize_plan() (TODO 2), which summarizes the response.

First, validate the answer program:

cd ~/program/Blockchain_IoT_Marketplace
source .venv/bin/activate
pytest -q tests/test_phase3_llm_hands_on_program.py

After the TODOs in the problem program are implemented, switch the target module like this:

PHASE3_LLM_HANDS_ON_MODULE=examples.hands_on.phase3_llm_planner.problem_program \
pytest -q tests/test_phase3_llm_hands_on_program.py

6. Check validator and fallback behavior

The LLM output is not adopted as-is; the following are always validated:

  • whether the events are allowed
  • whether the actions are allowed
  • whether the area is allowed

If any check fails, it falls back to the rule-based planner. The planner_diagnostics.code is then llm_output_invalid (category validation).

If the LLM response cannot be parsed as JSON, it also falls back, but this is treated as a provider failure: the planner_diagnostics.code is llm_provider_error (category provider), the same category as a wrong API key or a connection failure.

Points to check:

  • if planner_name differs from the expected value, a fallback may have occurred
  • actions outside the allow list are not executed as-is
  • the reason for a fallback is in planner_diagnostics.error_type and planner_diagnostics.error_message of the /assistant/plan response (plans run with POST /assistant/execute are also kept in /assistant/executions)

Success criteria

This hands-on is successful if you can confirm:

  • llm mode works with the stub provider
  • both Japanese and English requests return plans
  • you can explain the environment variables for an OpenAI-compatible API
  • you can explain the role of validator and fallback

Common issues

.env.local still has placeholder values

  • a real API call will not work while REPLACE_WITH_YOUR_API_KEY is still present

When the LLM API cannot be used, /assistant/plan does not fail; it returns the rule-based planner's plan (fallback). The reason is in code and error_message of planner_diagnostics in the response.

error_message is LLM API returned HTTP 401

  • check ASSISTANT_LLM_API_KEY. After editing .env.local, restart with the up command in §5

error_message is LLM API returned HTTP 404 or 405

  • check whether ASSISTANT_LLM_API_BASE_URL points to an OpenAI-compatible /chat/completions endpoint
  • check the model name in ASSISTANT_LLM_MODEL

error_message is could not reach LLM API (ConnectError) or similar

  • the name in parentheses is the exception type: ConnectError when the connection fails, ReadTimeout and the like when no reply comes in time

  • check that the host and port of ASSISTANT_LLM_API_BASE_URL are reachable. For the §5.2 mock server, use curl http://127.0.0.1:18000/health

  • when the Docker assistant-llm uses a local LLM on the PC, give host.docker.internal, not localhost

error_message is missing api_key or another missing ...

  • make sure ASSISTANT_LLM_API_KEY, ASSISTANT_LLM_API_BASE_URL and ASSISTANT_LLM_MODEL in .env.local are not empty

error_message is LLM API response is not JSON

  • the API reply itself is not JSON. Check that ASSISTANT_LLM_API_BASE_URL points to the API, not to a web page (HTML)

error_message is model response is not valid JSON

  • the model did not return a JSON plan. Use a model that supports JSON mode (response_format)

code is llm_output_invalid

  • the model returned JSON, but it failed the plan shape check (ExecutionPlan) or the allow-list check. error_message names the missing field or the area / event / action that is not allowed

An LLM error gives Internal Server Error (HTTP 500)

  • with an old course repository, HTTP and connection errors give a 500 instead of a fallback. Run git pull, then restart with --build for Docker or restart uvicorn on the host

docker compose --profile assistant-llm up fails

  • make sure .env.local exists (an empty key still starts; you then get missing api_key above)
  • if it says port is already allocated, stop the other assistants on port 8090 with the commands in §5

station-front does not appear

  • make sure your main branch is up to date (git pull)

Next steps

The hands-on ends here. You can look back on what you checked in "Done when…" for Part 3 on Hands-on.

  • After this, the Design section explains how each feature works and why it was designed that way.
  • If you stop here, stop the Docker assistants with the first docker compose ... stop command in 5.2 Try the HTTP path with the local mock server. In terminals running uvicorn or the mock server on the host, press Ctrl+C.

Related pages:

References

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

[2] HTTPX, "HTTPX," https://www.python-httpx.org/