yau-plant-assistant/api/main.py
Claude 885d8e31e2 Add NO_LLM_STUB: the whole chain, working, without a model
Azure OpenAI is pending and imh is pending, so POST /ask could not return
anything at all - which left the entire chain either side of the model
unproven: the browser, the API, entity resolution, Cube, retrieval, the
contracts, the banners, the error paths. All of it is testable now, and waiting
for a key to find out whether it works is a choice to find out later.

NO_LLM_STUB=true substitutes the two steps that need a model and nothing else.

  - Classification: the caller supplies the class, from a dropdown in the UI.
    NOT a keyword classifier. A crude keyword classifier produces a PLAUSIBLE
    label, and a plausible wrong label is the exact failure this system exists
    to prevent - "how do I reset it" landing in Historical is how a synthesised
    procedure reaches an operator. Choosing by hand is honest about what is
    happening and drives each branch deliberately. apply_safety_rules() still
    runs over the result.

  - Prose: a fixed placeholder per class, in stub.py.

Everything else is the real path. This is possible because generate() already
kept the factual fields away from the model: rows, counts, citations, the
fixture flag and the class are attached from evidence, and only prose comes
from the generator. Splitting that into _generate_prose() and _assemble() makes
the seam explicit - the stub feeds _assemble() exactly as the model does, so
this is a fair test of the assembly path rather than a mock of it.

The contracts are the point. A stub payload goes through enforce_contract()
unchanged, and it FAILED first time on two classes: the "nothing found" wording
did not match the not-found detectors, so Reference and Procedural returned 422
rather than an uncited answer. That is the contract doing its job against text
no model wrote. Retries are pointless on deterministic output, and a 422 is a
real result here, not a stub bug.

Retrieval is lexical (retrieval.lexical_search), because embedding the question
needs the model. Kept beside search() and never called on the normal path, so
nobody reads a trace and mistakes a lexical hit for a semantic one. It matches
what the operator typed, not what they meant.

What it does not prove: whether the classifier would have labelled correctly -
a person did; whether retrieval finds the RIGHT chunk; and nothing about prose.
It also cannot fill prerequisites_verbatim - extracting them with a regex would
be the "synthesised from fragments" failure the Procedural contract forbids, so
the list is empty and the answer says so.

Every answer carries stub_mode: true in the contract, not decorated on by the
UI, and a banner beside the fixture banner. Same reasoning: an answer nobody
generated must not be indistinguishable from one that was.

Also here:
  - demo/ai-docs: three fabricated documents, numbered WRPS-DEMO-00x so header
    extraction is genuinely exercised against a number no real WRPS document
    can have. Their setpoints contradict tags.csv on purpose.
  - VITE_API_BASE build arg, for a tunnelled build before DNS exists. The
    tunnel origin is allowed in CORS only while NO_LLM_STUB is on, so it
    disappears with the flag. Proxying /api through ai-web's nginx would have
    been easier and was rejected: it creates a second route to the API that
    bypasses the api.yokogawa.tech Caddy block, where the Phase 9 publisher
    rule lives.

Verified on lin001 with no Azure key set at all: all five classes return 200
through the real UI in a browser, over an SSH tunnel, with citations from the
demo documents, real Cube numbers, and both banners showing.

Turning it off: NO_LLM_STUB=false in ~/ai/api.env, restart ai-api.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 15:24:25 +10:00

199 lines
7 KiB
Python

"""FastAPI entrypoint for ai-api.
Reached at https://api.yokogawa.tech, behind Caddy and Authelia. There is no
authentication in this application because Authelia does it at the edge - which
also means this app must never be given a published host port, and never a
Caddyfile block without `import authelia`.
Every request is traced to Langfuse with its class, confidence, tool calls,
retrieved chunks, tokens, latency and contract result. Tracing failures never
break the request path.
"""
from __future__ import annotations
import logging
import time
import uuid
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
import agent
from config import settings
from contracts import ContractViolation
from guardrails import GuardrailViolation
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s %(message)s",
)
log = logging.getLogger("api")
app = FastAPI(
title="WRPS Plant Operations Assistant",
version="0.1.0",
description=(
"Information retrieval and analysis for the Waterloo Road Pump Station. "
"Not a control system, not an advisory controller, not a substitute for "
"a competent person."
),
)
# The browser reaches the API by its own public hostname, so a same-site
# origin is all that is ever needed.
#
# NO_LLM_STUB additionally allows a tunnelled origin, because the public
# hostnames do not resolve yet and the whole chain has to be exercisable
# without them. It is deliberately tied to the stub flag so it disappears when
# the flag does.
#
# The alternative - proxying /api through ai-web's nginx to make the page
# same-origin - was rejected: it creates a second route to the API that does
# not pass through the api.yokogawa.tech Caddy block, and that block is where
# the Phase 9 publisher rule for ^/docs/.* lives. A convenience path around an
# authorisation rule is how the rule stops meaning anything.
_ORIGINS = ["https://ai.yokogawa.tech"]
if settings().no_llm_stub:
_ORIGINS.append("http://localhost:8080")
app.add_middleware(
CORSMiddleware,
allow_origins=_ORIGINS,
# The UI sends credentials: "include" so the browser attaches the Authelia
# session cookie. Without this the preflight fails and NO cross-origin call
# succeeds - the browser refuses a credentialed request unless the response
# says Access-Control-Allow-Credentials: true. Found driving the real UI in
# a browser; it would have failed identically at ai.yokogawa.tech, and it
# cannot be found by calling the API directly with curl.
allow_credentials=True,
allow_methods=["GET", "POST"],
allow_headers=["Content-Type"],
)
def _langfuse():
"""Langfuse client, or None. Never let observability break the answer path."""
cfg = settings()
if not (cfg.langfuse_public_key and cfg.langfuse_secret_key):
return None
try:
from langfuse import Langfuse
return Langfuse(
public_key=cfg.langfuse_public_key,
secret_key=cfg.langfuse_secret_key,
host=cfg.langfuse_host,
)
except Exception:
log.exception("Langfuse unavailable - continuing untraced")
return None
class AskRequest(BaseModel):
question: str = Field(min_length=3, max_length=1000)
question_class: str | None = Field(
default=None,
description=(
"Force the class instead of classifying. NO-LLM STUB MODE ONLY - "
"ignored unless NO_LLM_STUB is on, because letting a caller choose "
"its own contract is exactly what the classifier exists to prevent."
),
)
class AskResponse(BaseModel):
request_id: str
question_class: str
confidence: float
downgraded_reason: str | None = None
answer: dict
latency_ms: int
@app.get("/healthz")
def healthz() -> dict:
"""Liveness plus configuration shape. No secrets, ever - values are
reported as set/unset."""
return {"status": "ok", "config": settings().redacted()}
@app.post("/ask", response_model=AskResponse)
def ask(request: AskRequest) -> AskResponse:
request_id = str(uuid.uuid4())
started = time.perf_counter()
client = _langfuse()
trace = None
if client is not None:
try:
trace = client.trace(
id=request_id, name="ask", input={"question": request.question}
)
except Exception:
log.exception("could not open a Langfuse trace")
try:
answer, classification = agent.answer(
request.question, trace=trace, forced_class=request.question_class
)
except ContractViolation as violation:
# The offending output has already gone to Langfuse with the whole
# generated text. What comes back here says nothing about it: an error
# message is not a side channel for content that failed a safety check.
log.warning("contract failure request_id=%s rule=%s", request_id, violation.rule)
raise HTTPException(
status_code=422,
detail={
"request_id": request_id,
"error": "contract_not_met",
"message": (
"I could not produce an answer that meets the safety contract "
"for this question. The attempt has been logged for review."
),
},
) from violation
except GuardrailViolation as violation:
log.warning("guardrail refusal request_id=%s rule=%s", request_id, violation.rule)
raise HTTPException(
status_code=400,
detail={
"request_id": request_id,
"error": "query_refused",
"message": f"The query was refused: {violation.rule}.",
},
) from violation
except Exception as exc:
log.exception("unhandled error request_id=%s", request_id)
raise HTTPException(
status_code=500,
detail={"request_id": request_id, "error": "internal_error"},
) from exc
latency_ms = int((time.perf_counter() - started) * 1000)
payload = answer.model_dump(mode="json")
if trace is not None:
try:
trace.update(
output=payload,
metadata={
"question_class": classification.question_class.value,
"confidence": classification.confidence,
"downgraded_reason": classification.downgraded_reason,
"contract_result": "passed",
"used_fixture_data": payload.get("used_fixture_data", False),
"latency_ms": latency_ms,
},
)
except Exception:
log.exception("could not finalise the Langfuse trace")
return AskResponse(
request_id=request_id,
question_class=classification.question_class.value,
confidence=classification.confidence,
downgraded_reason=classification.downgraded_reason,
answer=payload,
latency_ms=latency_ms,
)