feat(matching): rank with semantic embeddings and explain matches by evidence

This commit is contained in:
shira committed 2026-10-11 10:39:46 +03:00
1 parent facc1789b1
commit 24b64d698a
215 files changed
+16951 -327

No files matched your search

+40 -4
View File
@@ -89,7 +89,7 @@ The eight-minute step-by-step guide with links to the screens and the logins is
### 5. FSP, ML, and data
- FSP: there is no integration in any mode. This build has no off or mock switch, no fixture provider, and no call to an external API. The profile is filled in manually, linking an FSP ID is optional and not available yet, and the path to a category goes through the questionnaire and the test. The project has no official access to FSP data and does not promise it; an FSP link through a fixture provider is planned (see [ARCHITECTURE.md](ARCHITECTURE.md#identity-fsp-and-privacy)).
- ML: the core and demo stands do not start ML and do not call it. Search uses the neutral semantic similarity 0.5 and marks it with the `ml_unavailable` flag. `make -C deploy ml-up` starts a separate service without a model: `/healthz` answers 200 and `/readyz` 503. The core needs no GPU or external inference key and reads none.
- ML: by default the core and demo stands do not start ML and do not call it; search uses the neutral semantic similarity 0.5 with the `ml_unavailable` flag. Semantic matching is enabled separately: the ML project with the Giga-Embeddings-instruct-480M model on CPU, `SEMANTIC_MATCHING_ENABLED`, the semantic policy, and a backfill (see [Semantic matching](#semantic-matching)). No GPU or external inference keys are needed or read.
- Data: the demo stand holds synthetic data only. Addresses on the reserved domain `@demo.lincodex.test` never receive mail, names are marked as synthetic, categories have the source `synthetic_demo` and are uncalibrated, and the item families are synthetic and had no subject review. Do not register real people on this stand. Synthetic categories are enabled (`ASSESSMENT_DEMO_CATEGORIES_ENABLED=true`) only by the demo overlay and by the overlay of the isolated registration browser suite (see [TESTING.md](TESTING.md#minimal-path-from-registration-to-disclosed-contacts)); `.env.example` sets `false`.
### 6. Checks
@@ -474,6 +474,7 @@ make -C backend policy-publish ARGS='matching --actor operator:<handle>'
- It needs only `DATABASE_URL`, reads no dotenv file, validates its arguments first, and prints the version number only. The author of the version is the `operator:<handle>` label, an audit label and not authentication: local database access is the operator boundary.
- The new version takes effect at once for new searches and changes no stored result. It is also a new version of the whole product policy, so a category opening pinned to the earlier version has to be opened again (and attested again on a demonstration stand, see above) before new assessment attempts are created. Publish it before the candidates start attempts, or repeat the opening and the attestation afterwards.
- The demo values (the coefficients of the PRD, the limit of 10 entries per group, and the small specialization-to-technology taxonomy) are in [architecture](ARCHITECTURE.md#product-policy-versions); they are not calibrated. The isolated browser fixture publishes the same section itself.
- `policy-publish semantic` publishes the next version with the matching section replaced by the semantic one (`DemoSemanticMatchingPolicy`: Fit weights 0.4167 / 0.0833 / 0.5, the model, the calibration, and the explanation thresholds; see [Semantic matching](#semantic-matching)); when the latest version already has a semantic section, nothing changes. The version with the PRD point stays the previous one.
## Isolated demo stand
@@ -508,16 +509,51 @@ What the seed creates (all names carry a synthetic marker; addresses are on `@de
## Separate ML Compose project
ML uses a separate Compose file and project; it does not require or start Redis or SeaweedFS in the current scaffold. Its default host bind is 127.0.0.1:8000. Set ML_BIND_ADDRESS only after choosing a private address and preparing firewall rules. Prometheus does not scrape remote ML while ML_METRICS_TARGET is empty.
ML uses a separate Compose file and project; it needs no Redis or SeaweedFS. It joins the external network of the core project (`APP_NETWORK`) with the alias `ml`, so the backend reaches `http://ml:8000` without a published port; start the core project first. The host port binds only to 127.0.0.1:8000 by default for diagnostics; change ML_BIND_ADDRESS only after preparing a private address and firewall rules. Prometheus does not scrape remote ML while ML_METRICS_TARGET is empty.
The ML image uses `python:alpine` and upgrades system packages during the build. Dependencies are installed with uv only in the build stage; the final runtime contains the prepared virtual environment without pip or ensurepip. Current dependencies support musl. Check Alpine compatibility before adding inference or GPU libraries; glibc-dependent libraries will require a different validated runtime.
The image is based on `python:3.14-slim` (PyTorch publishes glibc wheels only, so Alpine cannot be used; the minor version is fixed for `requires-python` and the lock). Dependencies, including the CPU build of PyTorch from the `download.pytorch.org/whl/cpu` index, are installed with uv only in the build stage; the runtime has no pip. A separate stage fetches the Giga-Embeddings-instruct-480M files at the pinned revision from `ml/model/giga-embeddings-480m.lock.json` and checks the size and SHA-256 of every file (about 0.98 GB of safetensors weights); at startup the service checks the SHA-256 again (`ML_VERIFY_WEIGHTS=true`, a few seconds) and runs with `HF_HUB_OFFLINE=1`. The expected image size is about 2.5 GB (Python and PyTorch CPU about 1.4 GB, the model about 1 GB); the image was not built on this machine because of free disk space.
| Variable | Default | Purpose |
| --- | --- | --- |
| `ML_EMBEDDER` | `giga` in the image and Compose, `none` outside the image | `giga` - the model of the image, `fake` - a deterministic hash encoder for stands without the model (its model id does not match the policy, so its vectors are never used), `none` - the service stays not ready |
| `ML_TORCH_THREADS` | 4 | torch threads; with 4 the study meets the CPU targets |
| `ML_MICRO_BATCH` | 1 | micro-batch size; 1 gives every text the same vector whatever the request holds |
| `ML_MAX_TOKENS` | 1024 | tokens a text, as in the study |
| `ML_VERIFY_WEIGHTS` | true | check the SHA-256 of the weights at startup |
Container limits: 4 CPUs and 5 GiB (the float32 model takes about 3.6 GiB resident). Readiness turns 200 only after the load and the warm-up.
make -C deploy ml-config
make -C deploy ml-build
make -C deploy ml-up
make -C deploy ml-logs
make -C deploy ml-down
GET /healthz checks the process. GET /readyz checks inference readiness and returns 503 while a model is unavailable. The current scaffold has no working model. Readiness does not measure model quality or inference behavior.
For development without Docker: `make -C ml model-fetch` fetches and checks the same files into `ML_MODEL_DIR` (default `~/.cache/lincodex-ml/giga-480m`), and `make -C ml test-model` checks that the vectors match the study. Measured on an Apple M5 (CPU, 4 threads, over HTTP, 20 requests): a need view of 887 characters p50 296 ms / p95 707 ms, a profile of 500 characters 207 / 237 ms, a short requirement 74 / 166 ms, a card job batch of 13 texts 2.2 / 5.4 s. This is one laptop with other load, not a stand measurement.
## Semantic matching
The search never calls ML: it reads the vectors that background jobs computed (see [ARCHITECTURE.md](ARCHITECTURE.md#semantic-layer-and-explanations)). To enable it on a stand:
1. `make -C deploy up`, then `make -C deploy ml-up`, and wait for `/readyz` 200.
2. Set `SEMANTIC_MATCHING_ENABLED=true` (and `ML_SERVICE_URL` if it differs) in the stand `.env` and restart the backend (`make -C deploy up`).
3. Publish the semantic policy section: `make -C backend policy-publish ARGS='semantic --actor operator:<handle>'` (with the stand `DATABASE_URL`). Like the `matching` section, a new policy version requires opening and attesting the category again on the demo stand.
4. Compute vectors for existing data: `make -C backend semantic-backfill ARGS='all --run <label>'`. The command needs only `DATABASE_URL`; it schedules jobs page by page and prints the cursor to resume with (`--after`). Repeating the same label schedules nothing twice; a new label recomputes, and unchanged texts cost no ML call. The backend worker runs the jobs.
| Backend variable | Default | Purpose |
| --- | --- | --- |
| `SEMANTIC_MATCHING_ENABLED` | false | schedule and run vector jobs |
| `ML_SERVICE_URL` | `http://ml:8000` in Compose | ML origin without a path or credentials |
| `ML_REQUEST_TIMEOUT` | 30s | timeout of one ML request |
| `ML_CIRCUIT_FAILURES`, `ML_CIRCUIT_OPEN` | 3, 30s | after that many consecutive failures the client does not call ML for that long |
| `GITHUB_REPOSITORY_CHECKS_ENABLED` | false | run the checks of project repositories on `api.github.com` |
| `GITHUB_TOKEN` | empty | optional token for reading public data only; raises the GitHub limit from 60 to 5000 requests an hour; a secret |
| `GITHUB_TIMEOUT` | 10s | timeout of a GitHub request |
| `REPOSITORY_CHECK_INTERVAL` | 10m | how often a candidate may ask for a new check |
Without ML, or while it is unavailable, the search works as before: the neutral `Sem` 0.5 with the `ml_unavailable` flag; jobs retry up to 20 times and then wait for an operator (a new backfill). The isolated browser stand (e2e) does not start ML and uses policy V4; the demo stand also runs without ML by default, and enabling it takes the same steps with the project `lincodex-demo`. Metrics: `lincodex_scheduled_jobs_total{kind="semantic.candidate"|"semantic.need"|"profile.repository_check"}` in the backend, `lincodex_ml_embedding_request_seconds` and `lincodex_ml_embedding_texts_total` in ML.
The model ensemble (Giga + Qwen3-Embedding-0.6B) is not enabled; the steps to enable it - a second model in the ML service, 8 threads, a calibration of the fused score, a new policy with two `members`, and a backfill - are in [ARCHITECTURE.md](ARCHITECTURE.md#semantic-layer-and-explanations). The search never reads vectors of a model that the policy no longer names; they stay for a rollback to the previous policy version.
## Separate monitoring Compose project