File size: 13,803 Bytes
071512e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""A local HTTP server for a DecisionNode Edge model, on the standard library only.

    python -m decisionnode_edge serve path/to/model [--host 127.0.0.1] [--port 8000] [--api-key FILE] [--backend ...]

    POST /v1/systemone   the /v1/systemone format used by public decision-model leaderboards:
                         {"model", "state", "questions", "assets"}; noul, choice and score questions; a noul is
                         answered {"type": "noul", "noul": p}, a choice and a score as on /v1/decide
    POST /v1/decide      the DecisionNode API's call: the runtime's own request and answers (EdgeModel.decide)
    GET  /v1/models      {"models": [{"name", "description", "release_date"}]}
    GET  /healthz        {"status": "ok", "model", "backend"} (no key needed)

Pictures: `images` as the runtime takes them, base64 `data` only (a local `path` is refused over HTTP: a caller must
never make the server read its files), and on /v1/systemone the `assets` map of that format, {"<ref>": {"type": "image",
"format": "png" | "jpg" | "jpeg", "data_base64": ...}}, each asset a picture whose id is its reference (the state names
it), in the map's order.

Errors carry a `detail` body: {"detail": {"error_type", "message"}} (a 422 as a list of field errors). A
request over the model's length limit says "maximum context length", a choice over 255 options "options per choice",
the words leaderboard harnesses look for to count a request as beyond the model rather than as a failure.

The model answers one request at a time (a lock); the server listens on the loopback address unless told otherwise.
`model` is required on /v1/systemone, as on the hosted API, and any name is accepted: the answer names the model that
answered.
"""

from __future__ import annotations

import argparse
import hmac
import json
import logging
import sys
import threading
import time
import traceback
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from typing import Any

from .request import RequestError

MAX_BODY_BYTES = 64 << 20  # 16 pictures as base64 with room to spare
SYSTEMONE_TYPES = ("noul", "choice", "score")
ASSET_MEDIA_TYPES = {"png": "image/png", "jpg": "image/jpeg", "jpeg": "image/jpeg"}
AUTH_MESSAGE = "Cannot authenticate with the server. Please check your API key and try again."
ROUTES = {"/v1/systemone": "POST", "/v1/decide": "POST", "/v1/models": "GET", "/healthz": "GET"}

log = logging.getLogger("decisionnode_edge.server")


class HTTPError(Exception):
    def __init__(self, status: int, error_type: str, message: str, headers: dict | None = None):
        super().__init__(message)
        self.status, self.error_type, self.message, self.headers = status, error_type, message, headers or {}


def error_body(status: int, error_type: str, message: str) -> dict:
    if status == 422:
        return {"detail": [{"type": "value_error", "loc": ["body"], "msg": message, "input": None}]}
    return {"detail": {"error_type": error_type, "message": message}}


def _images_over_http(images: Any) -> list:
    if images is None:
        return []
    if not isinstance(images, list):
        raise RequestError("images must be a list", 422)
    for image in images:
        if isinstance(image, dict) and image.get("path") is not None:
            raise RequestError("Over HTTP a picture is sent as base64 `data` (or an asset), never as a local `path`.")
    return list(images)


def _asset_images(assets: Any) -> list[dict]:
    """The /v1/systemone `assets` map as the runtime's images: the reference is the picture's id."""
    if assets is None:
        return []
    if not isinstance(assets, dict):
        raise RequestError("assets must be an object of references", 422)
    out = []
    for ref, asset in assets.items():
        if not isinstance(asset, dict) or asset.get("type", "image") not in ("image", None):
            raise RequestError(f"Asset {ref!r}: only image assets are taken.")
        media_type = ASSET_MEDIA_TYPES.get(str(asset.get("format", "")).lower())
        if media_type is None:
            raise RequestError(f"Asset {ref!r}: format must be one of png, jpg, jpeg.")
        if not isinstance(asset.get("data_base64"), str) or not asset["data_base64"]:
            raise RequestError(f"Asset {ref!r} needs data_base64.", 422)
        out.append({"id": ref, "data": asset["data_base64"], "media_type": media_type})
    return out


class Service:
    """The routes, without the socket: handle(method, path, headers, body) gives (status, JSON body, extra headers)."""

    def __init__(self, model: Any, api_key: str | None = None, description: str | None = None, release_date: str | None = None):
        self.model = model
        self.api_key = api_key
        self.name = model.config["name"]
        self.description = description or model.config.get("description") or f"{self.name}: typed, calibrated decisions, run locally"
        self.release_date = release_date or model.config.get("release_date")
        self.lock = threading.Lock()

    def handle(self, method: str, path: str, headers: dict[str, str], body: bytes) -> tuple[int, dict, dict]:
        try:
            return 200, self._route(method, path.split("?", 1)[0], headers, body), {}
        except HTTPError as error:
            return error.status, error_body(error.status, error.error_type, error.message), error.headers
        except RequestError as error:
            return error.status, error_body(error.status, error.error_type, str(error)), {}
        except Exception:  # a fault of the model or the backend, never the caller's
            log.error("the model could not answer:\n%s", traceback.format_exc())
            return 500, error_body(500, "internal_error", "The model could not answer this request."), {}

    def _route(self, method: str, path: str, headers: dict[str, str], body: bytes) -> dict:
        if path not in ROUTES:
            raise HTTPError(404, "not_found_error", "Not found.")
        if method != ROUTES[path]:
            raise HTTPError(405, "api_usage_error", "Method not allowed.", {"Allow": ROUTES[path]})
        if path == "/healthz":
            return {"status": "ok", "model": self.name, "backend": self.model.backend}
        self._authenticate(headers)
        if path == "/v1/models":
            return {"models": [{"name": self.name, "description": self.description, "release_date": self.release_date}]}
        try:
            request = json.loads(body)
        except (UnicodeDecodeError, json.JSONDecodeError) as error:
            raise RequestError(f"the body is not JSON: {error}", 422) from None
        if path == "/v1/decide":
            if isinstance(request, dict) and request.get("images") is not None:
                request = {**request, "images": _images_over_http(request["images"])}
            with self.lock:
                return self.model.decide(request)
        return self._systemone(request)

    def _authenticate(self, headers: dict[str, str]) -> None:
        if self.api_key is None:
            return
        given = headers.get("authorization", "")
        scheme, _, token = given.partition(" ")
        if scheme.lower() != "bearer" or not hmac.compare_digest(token.strip().encode(), self.api_key.encode()):
            raise HTTPError(401, "authentication_error", AUTH_MESSAGE, {"WWW-Authenticate": "Bearer"})

    def _systemone(self, request: Any) -> dict:
        if not isinstance(request, dict):
            raise RequestError("the request must be a JSON object", 422)
        if not isinstance(request.get("model"), str):
            raise RequestError("model: Field required, a string", 422)
        questions = request.get("questions")
        if isinstance(questions, dict):
            for qid, question in questions.items():
                kind = question.get("type") if isinstance(question, dict) else None
                if kind == "number":
                    raise RequestError(f"question {qid!r}: the number type is not available on /v1/systemone; send it to /v1/decide")
                if isinstance(question, dict) and "type" in question and kind not in SYSTEMONE_TYPES:
                    raise RequestError(f"question {qid!r}: unknown question type {kind!r}")
        images = _images_over_http(request.get("images")) + _asset_images(request.get("assets"))
        body = {"state": request.get("state"), "questions": questions}
        if images:
            body["images"] = images
        with self.lock:
            response = self.model.decide(body)
        answers = {}
        for qid, answer in response["answers"].items():  # a noul is answered under its own name, as the format has it
            answers[qid] = {"type": "noul", "noul": answer["truth"]} if answer["type"] == "truth" else answer
        return {"model": response["model"], "answers": answers, "usage": response["usage"]}


class Handler(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"  # keep-alive: every response carries Content-Length
    disable_nagle_algorithm = True  # headers and body are two writes: without it a kept-alive client waits for a delayed ACK
    server_version = "decisionnode-edge"
    service: Service = None  # set by make_server

    def _answer(self, method: str) -> None:
        started = time.perf_counter()
        body = b""
        if method == "POST":
            length = self.headers.get("Content-Length")
            if length is None or not length.isdigit():
                return self._send(411, error_body(411, "api_usage_error", "Content-Length is required."), {"Connection": "close"}, started)
            if int(length) > MAX_BODY_BYTES:
                return self._send(413, error_body(413, "api_usage_error", "Request body too large."), {"Connection": "close"}, started)
            body = self.rfile.read(int(length))
        headers = {k.lower(): v for k, v in self.headers.items()}
        status, payload, extra = self.service.handle(method, self.path, headers, body)
        self._send(status, payload, extra, started)

    def _send(self, status: int, payload: dict, extra: dict, started: float) -> None:
        data = json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
        self.send_response(status)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(data)))
        self.send_header("X-Latency-Ms", f"{(time.perf_counter() - started) * 1000:.2f}")
        for name, value in extra.items():
            self.send_header(name, value)
        self.end_headers()
        self.wfile.write(data)
        if extra.get("Connection") == "close":
            self.close_connection = True

    def do_GET(self) -> None:  # noqa: N802 (the standard library's names)
        self._answer("GET")

    def do_POST(self) -> None:  # noqa: N802
        self._answer("POST")

    def do_PUT(self) -> None:  # noqa: N802
        self._answer("PUT")

    def do_DELETE(self) -> None:  # noqa: N802
        self._answer("DELETE")

    def log_message(self, format: str, *args: Any) -> None:  # noqa: A002
        log.info("%s %s", self.address_string(), format % args)


def make_server(model: Any, host: str = "127.0.0.1", port: int = 8000, api_key: str | None = None, **service: Any) -> ThreadingHTTPServer:
    handler = type("EdgeHandler", (Handler,), {"service": Service(model, api_key=api_key, **service)})
    server = ThreadingHTTPServer((host, port), handler)
    server.daemon_threads = True
    return server


def main(argv: list[str] | None = None) -> int:
    from . import EdgeModel

    parser = argparse.ArgumentParser(prog="python -m decisionnode_edge serve", description="Serve a DecisionNode Edge model over HTTP: /v1/systemone, /v1/decide, /v1/models.")
    parser.add_argument("model", help="the model directory")
    parser.add_argument("--host", default="127.0.0.1", help="the address to listen on (default 127.0.0.1, this machine only)")
    parser.add_argument("--port", type=int, default=8000)
    parser.add_argument("--api-key", default=None, help="a file holding the key callers must send as `Authorization: Bearer <key>`")
    parser.add_argument("--device", default=None)
    parser.add_argument("--dtype", default=None)
    parser.add_argument("--backend", default=None, help="auto, torch, vllm, openvino, mlx or llamacpp (default: auto)")
    parser.add_argument("--artifacts", default=None, help="the directory python -m decisionnode_edge.build wrote")
    parser.add_argument("--description", default=None, help="the description GET /v1/models gives")
    parser.add_argument("--release-date", default=None, help="the release date GET /v1/models gives (YYYY-MM-DD)")
    args = parser.parse_args(argv)
    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(message)s")
    api_key = None
    if args.api_key:
        with open(args.api_key, encoding="utf-8") as f:
            api_key = f.read().strip()
        if not api_key:
            raise SystemExit(f"{args.api_key} is empty")
    model = EdgeModel.load(args.model, device=args.device, dtype=args.dtype, backend=args.backend, artifacts=args.artifacts)
    model.decide({"state": "The color is red.", "questions": {"warmup": {"type": "choice", "instructions": "Which color is named?", "criteria": {"red": "red", "blue": "blue"}}}})
    server = make_server(model, args.host, args.port, api_key, description=args.description, release_date=args.release_date)
    log.info("serving %s (%s backend) on http://%s:%d%s", model.config["name"], model.backend, args.host, server.server_address[1], ", key required" if api_key else "")
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.server_close()
    return 0


if __name__ == "__main__":
    sys.exit(main())