docs: finalise data-pipeline.md with tiebreak, pipeline.db, reports, sync

This commit is contained in:
lila 2026-05-03 17:21:02 +02:00
parent 6007fe1e38
commit 74cfc82bdd
2 changed files with 122 additions and 33 deletions

View file

@ -7,7 +7,7 @@
> (huggingface.co/UniversalCEFR) AND CEFR-J > (huggingface.co/UniversalCEFR) AND CEFR-J
> (github.com/openlanguageprofiles/olp-en-cefrj) AS STARTING POINTS.** > (github.com/openlanguageprofiles/olp-en-cefrj) AS STARTING POINTS.**
This pipeline extracts vocabulary data from the Open Multilingual Wordnet (OMW), annotates it with CEFR levels from curated source files, verifies and enriches annotations using local LLMs, and produces authoritative JSON files per language. These files are consumed by the seeder in `packages/db` to populate the database with terms, translations, glosses, CEFR levels, difficulty ratings, and LLM-generated descriptions. This pipeline extracts vocabulary data from the Open Multilingual Wordnet (OMW), annotates it with CEFR levels from curated source files, verifies and enriches annotations using local LLMs, and produces authoritative output in `pipeline.db`. This database is consumed by the sync script to populate the production database with terms, translations, glosses, CEFR levels, difficulty ratings, and LLM-generated descriptions.
## Overview ## Overview
@ -20,6 +20,7 @@ flowchart LR
enrich[Enrich] enrich[Enrich]
pipelinedb[(pipeline.db)] pipelinedb[(pipeline.db)]
merge[Merge] merge[Merge]
tiebreak[Tiebreak]
compare[Compare] compare[Compare]
sync[Sync] sync[Sync]
db[(PostgreSQL)] db[(PostgreSQL)]
@ -31,6 +32,8 @@ flowchart LR
enrich --> pipelinedb enrich --> pipelinedb
pipelinedb --> merge pipelinedb --> merge
merge --> pipelinedb merge --> pipelinedb
pipelinedb --> tiebreak
tiebreak --> pipelinedb
pipelinedb --> compare pipelinedb --> compare
pipelinedb --> sync pipelinedb --> sync
sync --> db sync --> db
@ -42,7 +45,26 @@ Stage 1 is a manual prerequisite and is not run by the pipeline orchestrator. Se
The enrich stage is designed to run overnight, one model at a time. Each model processes every word and writes results to `pipeline.db` atomically per record — interrupted runs resume from the last unprocessed record. The enrich stage is designed to run overnight, one model at a time. Each model processes every word and writes results to `pipeline.db` atomically per record — interrupted runs resume from the last unprocessed record.
Only fully resolved records reach the database. Records where LLMs could not reach a majority vote are marked `flagged` in `pipeline.db` and wait for manual review before syncing. Only fully resolved records reach the production database. Records where LLMs could not reach a majority vote are handled automatically by the tiebreaker stage before seeding.
## pipeline.db
All pipeline state from stage 3 onwards is stored in `pipeline.db` — a SQLite
database in `data-pipeline/db/`. It is created automatically on first run and
is not committed to git.
The database serves three purposes:
- **Resumability** — every record is written atomically with a status. Interrupted
overnight runs resume from the last pending record without losing work.
- **Vote tracking** — all model votes for CEFR levels and generated text are
stored per model per record, giving full auditability of how every decision
was reached.
- **Resolved output** — the final resolved records live here and are read by
the sync script to seed the production database.
The schema is defined in `data-pipeline/db/schema.sql`. Never edit `pipeline.db`
directly — all writes go through the pipeline scripts.
## Data sources ## Data sources
@ -77,7 +99,9 @@ CEFR levels are determined by a majority vote combining all available sources:
The LLMs verify existing annotations as well as filling gaps — a source file entry does not automatically win. Majority vote across all sources determines the final level. The LLMs verify existing annotations as well as filling gaps — a source file entry does not automatically win. Majority vote across all sources determines the final level.
If no majority is reached, the word is flagged for manual review and excluded from the database until resolved. Words appearing in the CEFR source file multiple times with different CEFR levels are written to `conflicts.json` and excluded from `cefr_source_votes`. They are still present in `translations` and the LLMs vote on them like any other unannotated word — the conflict is resolved by majority vote.
If no majority is reached after all model runs, the word is handled automatically by the tiebreaker stage.
## Setup ## Setup
@ -99,19 +123,21 @@ to git.
### LLM setup ### LLM setup
See `LLM-SETUP.md`. See `llm-setup.md`.
## Pipeline stages ## Pipeline stages
The pipeline runs in five stages. Each stage is independent and can be re-run without affecting the others. The pipeline runs in six stages plus a tiebreaker. Each stage is independent and can be re-run without affecting the others.
| Stage | What it does | | Stage | What it does |
| ----------- | -------------------------------------------------------------------- | | ------------ | -------------------------------------------------------------------- |
| 1. Extract | Reads OMW SQLite database, outputs normalized JSON per language | | 1. Extract | Reads OMW SQLite database, outputs normalized JSON per language |
| 2. Annotate | Merges CEFR source files into extracted data, adds source file votes | | 2. Annotate | Merges CEFR source files into extracted data, adds source file votes |
| 3. Enrich | Runs local LLMs in two rounds — generation then voting | | 3. Enrich | Runs local LLMs in two rounds — generation then voting |
| 4. Merge | Resolves votes, derives difficulty, splits into final and flagged | | 4. Merge | Resolves votes, derives difficulty, splits into final and flagged |
| 5. Compare | Generates COVERAGE.md with detailed quality report | | 4b. Tiebreak | Runs unused models on flagged translations until majority is reached |
| 5. Compare | Generates COVERAGE.md with detailed quality report |
| 6. Sync | Upserts resolved records into production PostgreSQL |
### 1. Extract ### 1. Extract
@ -156,18 +182,17 @@ Note: glosses and examples are not available for all languages. French and Spani
### 2. Annotate ### 2. Annotate
Reads the combined OMW extract and merges CEFR source data into it. Each translation in each language is matched against the corresponding CEFR source Reads the combined OMW extract and merges CEFR source data into it. Each translation in each language is matched against the corresponding CEFR source file by word text and part of speech. Matched translations receive a `cefr_source` vote which carries into the enrich stage. Unmatched translations proceed without a vote.
file by word text and part of speech. Matched translations receive a `cefr_source` vote which carries into the enrich stage. Unmatched translations proceed without a vote.
This stage also extracts native example sentences from the CEFR source files and adds them to the record alongside OMW examples, with `source: "cefr"` to distinguish them. This stage also extracts native example sentences from the CEFR source files and adds them to the record alongside OMW examples, with `source: "cefr"` to distinguish them.
Words appearing in the CEFR source file multiple times with different CEFR levels are written to `conflicts.json` for manual review and excluded from voting until resolved. Words appearing in the CEFR source file multiple times with different CEFR levels are written to `conflicts.json` and excluded from source voting. The LLMs handle these words like any other unannotated word.
**Input:** `stage-1-extract/output/omw.json` + `stage-2-annotate/sources/cefr/{lang}.json` **Input:** `stage-1-extract/output/omw.json` + `stage-2-annotate/sources/cefr/{lang}.json`
**Output:** **Output:**
- `stage-2-annotate/output/{lang}.json` — one per language - `stage-2-annotate/output/{lang}.json` — one per language
- `stage-2-annotate/output/conflicts.json` — cross-language conflicts for review - `stage-2-annotate/output/conflicts.json` — cross-language conflicts for reference
```bash ```bash
pnpm --filter @lila/pipeline annotate pnpm --filter @lila/pipeline annotate
@ -293,11 +318,10 @@ every field. Updates each record in `pipeline.db` with status `final` or
- OMW data wins automatically and is never overridden - OMW data wins automatically and is never overridden
- For CEFR levels: the level with the most votes wins. If no majority is - For CEFR levels: the level with the most votes wins. If no majority is
reached, that translation is flagged reached, that translation is flagged for the tiebreaker
- For LLM-generated text fields (gloss, examples, descriptions): the - For LLM-generated text fields (gloss, examples, descriptions): the
candidate with the most votes wins candidate with the most votes wins. If no majority is reached, the
tiebreaker runs for that record as well
<!-- TODO: decide fallback strategy when no majority is reached for text fields -->
**Difficulty mapping:** **Difficulty mapping:**
@ -314,11 +338,26 @@ every field. Updates each record in `pipeline.db` with status `final` or
pnpm --filter @lila/pipeline merge pnpm --filter @lila/pipeline merge
``` ```
**Resolving flagged words:** ### 4b. Tiebreak
Query `pipeline.db` for all records with status `flagged`, manually set the Runs automatically after merge if any translations remain flagged. The script
correct `cefr_level` and `difficulty` for each flagged translation, and update queries `pipeline.db` for flagged translations, identifies which configured
the record status to `final`. Re-run the sync script after resolving. models have not yet voted on each word, and runs those models on the flagged
subset only. Merge is re-run after each tiebreaker pass. This repeats until
all flagged translations are resolved or no unused models remain.
If unused models are exhausted and flagged translations remain, the script
logs a detailed report showing the exact vote split for each unresolved word
and lists available models from OpenRouter that have not been used. Seeding
is blocked until all translations are resolved. To continue, add one or more
models to the config and re-run the pipeline — the tiebreaker will pick up
automatically.
**Input:** `pipeline.db` — flagged translations from merge
**Output:** `pipeline.db` — flagged translations resolved to `final`
> **Note:** The tiebreaker is not a standalone script. It runs automatically
> as part of the pipeline orchestrator after merge completes.
### 5. Compare / QA ### 5. Compare / QA
@ -326,7 +365,7 @@ Read-only. Generates `COVERAGE.md` with a full breakdown of the pipeline
output quality per language. Run this after merge to verify output before output quality per language. Run this after merge to verify output before
syncing to the database. syncing to the database.
**Input:** `pipeline.db` — records with status `final` and `flagged` **Input:** `pipeline.db` — records with status `final`
**Output:** `COVERAGE.md` **Output:** `COVERAGE.md`
```bash ```bash
@ -363,7 +402,7 @@ already exists in the target database:
- **Present but changed** → update - **Present but changed** → update
- **Present and unchanged** → skip - **Present and unchanged** → skip
Run this after merge and after manually resolving any flagged entries. Run this after all records are resolved and Compare / QA has been reviewed.
```bash ```bash
pnpm --filter @lila/pipeline sync pnpm --filter @lila/pipeline sync
@ -372,6 +411,41 @@ pnpm --filter @lila/pipeline sync
The sync script requires a connection string to the target database. Set The sync script requires a connection string to the target database. Set
`DATABASE_URL` in your `.env` file before running. `DATABASE_URL` in your `.env` file before running.
## Reports
The pipeline generates a report at the end of every run. Reports are written
to `data-pipeline/reports/` as a JSON file and a markdown file with the same
name. The markdown is generated from the JSON and contains identical data.
```
data-pipeline/reports/
2026-05-03_night-1.json
2026-05-03_night-1.md
```
The report name is provided when starting the pipeline:
```bash
pnpm --filter @lila/pipeline run --name "night-1"
```
**Nightly report** contains:
- Records processed this run vs total
- Records remaining per stage
- Average processing speed and estimated nights remaining
- `needs_review` count — records that failed structural validation
- Per-model progress breakdown
**Final report** (generated when all records are processed) additionally contains:
- Full vote breakdown per model
- Flagged translations with exact vote splits
- Available unused models from OpenRouter for tiebreaking
- Per-model quality metrics — CEFR agreement rate, field coverage, JSON parse rate
Reports are not committed to git.
## Adding a new language ## Adding a new language
1. Add the language code to `SUPPORTED_LANGUAGE_CODES` in `packages/shared/src/constants.ts` 1. Add the language code to `SUPPORTED_LANGUAGE_CODES` in `packages/shared/src/constants.ts`
@ -424,9 +498,9 @@ dataset matures:
## Roadmap ## Roadmap
**Current state:** Stages 1 and 2 are complete and output has been reviewed **Current state:** Stages 1 and 2 are complete and output has been reviewed
for all five languages. Architecture for stages 35 and the sync script is for all five languages. Architecture for stages 36, the tiebreaker, and the
finalised. Stage 3 scripts have not been written yet and llama.cpp is not report system are finalised. Stage 3 scripts have not been written yet and
installed. llama.cpp is not installed.
**Next action:** Write the stage 3 round 1 script. **Next action:** Write the stage 3 round 1 script.
@ -436,7 +510,9 @@ installed.
| 2. Annotate | ✅ complete | | 2. Annotate | ✅ complete |
| 3. Enrich | 🔲 not started | | 3. Enrich | 🔲 not started |
| 4. Merge | 🔲 not started | | 4. Merge | 🔲 not started |
| 4b. Tiebreak | 🔲 not started |
| 5. Compare / QA | 🔲 not started | | 5. Compare / QA | 🔲 not started |
| 6. Sync | 🔲 not started |
### Stage 1 — Extract `✅ complete` ### Stage 1 — Extract `✅ complete`
@ -461,14 +537,20 @@ installed.
- [ ] Run full 100-record sample, collect metrics - [ ] Run full 100-record sample, collect metrics
- [ ] Compare providers (local vs OpenRouter free models) - [ ] Compare providers (local vs OpenRouter free models)
- [ ] Production run — all records, all models - [ ] Production run — all records, all models
- [ ] Compile candidates → `stage-3-enrich/output/candidates/{lang}_candidates.json` - [ ] Compile candidates → `pipeline.db`
- [ ] Compile votes → `stage-3-enrich/output/votes/{lang}_votes.json` - [ ] Compile votes → `pipeline.db`
### Stage 4 — Merge `🔲 not started` ### Stage 4 — Merge `🔲 not started`
- [ ] Write merge script - [ ] Write merge script
- [ ] Run merge → `final/` and `flagged/` - [ ] Run merge → `pipeline.db`
- [ ] Manually resolve flagged entries - [ ] Confirm tiebreaker resolves all flagged translations
### Stage 4b — Tiebreak `🔲 not started`
- [ ] Write tiebreak logic
- [ ] Run tiebreaker for all flagged translations
- [ ] Confirm no flagged translations remain before seeding
### Stage 5 — Compare / QA `🔲 not started` ### Stage 5 — Compare / QA `🔲 not started`
@ -476,6 +558,13 @@ installed.
- [ ] Run compare → `COVERAGE.md` - [ ] Run compare → `COVERAGE.md`
- [ ] Review output quality before seeding - [ ] Review output quality before seeding
### Stage 6 — Sync `🔲 not started`
- [ ] Write sync script
- [ ] Configure `DATABASE_URL` in `.env`
- [ ] Run sync → production PostgreSQL
- [ ] Verify seeded data in production
### Utilities ### Utilities
**`test/`** — Runs the pipeline against a small sample to produce human-readable output for a quick sanity check before committing to a full run. Run this after any script change before running the full pipeline. **`test/`** — Runs the pipeline against a small sample to produce human-readable output for a quick sanity check before committing to a full run. Run this after any script change before running the full pipeline.