File size: 10,009 Bytes
5464613
9af7af9
5464613
 
 
 
 
 
9af7af9
5464613
 
9af7af9
5464613
9af7af9
 
 
 
 
 
 
5464613
9af7af9
5464613
 
9af7af9
 
 
5464613
9af7af9
5464613
 
9af7af9
5464613
 
 
 
 
9af7af9
5464613
 
 
9af7af9
 
 
5464613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9af7af9
 
 
 
5464613
 
 
9af7af9
 
 
5464613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9af7af9
 
5464613
 
9af7af9
 
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
 
 
 
9af7af9
5464613
9af7af9
5464613
 
9af7af9
 
5464613
9af7af9
5464613
 
 
9af7af9
5464613
9af7af9
5464613
 
 
 
 
 
9af7af9
5464613
9af7af9
 
5464613
 
 
 
9af7af9
 
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
 
 
 
9af7af9
5464613
9af7af9
5464613
 
9af7af9
 
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
 
 
9af7af9
5464613
9af7af9
5464613
 
 
 
 
 
 
 
 
 
 
 
 
9af7af9
5464613
 
 
9af7af9
 
 
5464613
 
 
9af7af9
5464613
 
 
9af7af9
5464613
9af7af9
5464613
 
 
9af7af9
5464613
 
 
 
 
 
 
 
 
 
 
 
9af7af9
 
 
5464613
9af7af9
5464613
9af7af9
5464613
 
 
 
 
 
 
 
9af7af9
5464613
9af7af9
5464613
9af7af9
 
 
5464613
9af7af9
5464613
9af7af9
5464613
 
 
 
 
 
 
 
 
 
 
 
9af7af9
 
5464613
9af7af9
 
5464613
 
 
 
 
9af7af9
5464613
 
 
9af7af9
 
 
5464613
 
 
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
 
 
 
9af7af9
5464613
9af7af9
 
5464613
 
 
 
 
 
 
 
9af7af9
 
5464613
9af7af9
 
5464613
 
 
 
9af7af9
 
5464613
9af7af9
5464613
9af7af9
5464613
 
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
 
 
 
 
 
9af7af9
 
5464613
9af7af9
5464613
 
 
9af7af9
5464613
9af7af9
 
 
5464613
9af7af9
5464613
 
 
9af7af9
5464613
9af7af9
5464613
 
 
 
 
 
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
 
9af7af9
 
 
5464613
9af7af9
5464613
9af7af9
5464613
 
 
 
 
 
9af7af9
5464613
9af7af9
5464613
 
9af7af9
5464613
9af7af9
5464613
 
 
 
 
 
 
 
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
 
 
 
 
 
 
 
 
 
9af7af9
5464613
9af7af9
5464613
 
 
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
 
 
9af7af9
5464613
 
 
 
 
 
 
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
9af7af9
5464613
 
 
 
 
9af7af9
 
 
5464613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9af7af9
5464613
9af7af9
5464613
 
 
 
 
9af7af9
5464613
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
Absolutely β€” here is a **fully updated, successor-ready `ARCHITECTURE.md`** that reflects your *current* codebase, including:

* the **new question generator subsystem**,
* the **multi-provider architecture**,
* the **modular pipeline** (generator/verifier/reward),
* the **retrieval stage**,
* the **batch orchestrator**,
* and the **modularization plan** you're moving toward.

It will not clobber anything.
It is aligned with your present repo after the latest commits and tests.

You can paste this directly as:

```
distill-pipeline/ARCHITECTURE.md
```

---

# **ARCHITECTURE.md**

*Distill-Pipeline β€” System Architecture & Successor Notes*
*(Node.js, ESM, Ollama/vLLM/OpenAI providers, Vitest-tested)*

---

# **1. Purpose**

`distill-pipeline` is a modular, retrieval-augmented LLM distillation engine.
It produces high-quality *gold data* by running each question through:

1. **retrieval** (hybrid RAG via distill-rag)
2. **generator** (teacher model)
3. **verifier** (alignment/format checker)
4. **reward model** (scoring)
5. **gold writer** (JSONL builder)

It also includes a **question generation** module to extract questions directly from RAG chunks, enabling true content-first distillation.

The system is built for offline, local distillation on consumer GPUs (your 3090 + 3060).

---

# **2. High-Level Flow**

```
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚  Chunk Source  β”‚ ← distill-rag index
              β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β–Ό
       (optional) Question Generation
                     β–Ό
             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
             β”‚   Retrieval    β”‚ (hybrid BM25 + dense)
             β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β–Ό
             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
             β”‚   Generator    β”‚ (LLM teacher)
             β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β–Ό
             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
             β”‚    Verifier    β”‚ (LLM)
             β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β–Ό
             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
             β”‚  Reward Model  β”‚ (LLM critic)
             β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β–Ό
             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
             β”‚   Gold Writer  β”‚
             β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

# **3. Directory Layout**

Your repo structure (as of now, after modularization):

```
distill-pipeline/
  prompts/
    generator_prompt.txt
    verifier_prompt.txt
    reward_prompt.txt
    question_prompt.txt

  src/
    pipeline/
      pipeline.mjs
      pipeline_cli.mjs
    providers/
      provider.mjs
      ollama_provider.mjs
      openai_provider.mjs
      http_provider.mjs
    retrieval/
      retrieval.mjs
    generator/
      generator_core.mjs
    verifier/
      verifier_core.mjs
    reward/
      reward_core.mjs
    question/
      question_core.mjs
      question_cli.mjs

  gold/
    (generated JSONL files)

  test_samples/
    seed_questions.jsonl   ← for static mode

  tests/
    generator_core.test.mjs
    verifier_core.test.mjs
    reward_core.test.mjs
    provider.mock.test.mjs
    pipeline.mock.test.mjs
    retrieval.real.test.mjs
    retrieval.mock.test.mjs
    gold_core.test.mjs
    question_core.test.mjs

  .env
  package.json
  ARCHITECTURE.md
  ROADMAP.md
```

Everything is now properly separated into **pure core modules**, each with **Vitest tests**.

---

# **4. Core Modules**

Below is a top-down view.

---

## **4.1 Provider System (src/providers/)**

This system routes each pipeline stage to a backend:

* `OllamaProvider`
* `OpenAIProvider`
* `HttpProvider`
* future: `vLLMProvider`

All providers expose:

```js
async generate(prompt, options?)
```

The dispatcher:

```js
loadProviderFor("generator" | "verifier" | "reward" | "question")
```

Selects backend using env:

```
GENERATOR_PROVIDER=ollama
VERIFIER_PROVIDER=ollama
REWARD_PROVIDER=ollama
QUESTION_PROVIDER=ollama
```

And uses stage-specific model names:

```
GENERATOR_MODEL=qwen3-vl:8b-thinking
VERIFIER_MODEL=patronus:8b
REWARD_MODEL=patronus:8b
QUESTION_MODEL=qwen2.5-7b-instruct
```

This architecture is clean, extensible, and fully testable.

---

## **4.2 Retrieval (src/retrieval/retrieval.mjs)**

Your retrieval layer connects to the **distill-rag** Elasticsearch index.

Supports:

* BM25
* Dense vector KNN
* Hybrid RRF
* optional future HyDE

The key export:

```js
export async function hybridSearch(query, k)
```

You already have real + mock tests for this module.

βœ” This module is stable.

---

## **4.3 Generator (src/generator/generator_core.mjs)**

Pure function:

```js
async function runGenerator(query, contextChunks, provider)
```

Pipeline:

* loads generator prompt template
* merges context chunks into a context string
* invokes provider.generate
* JSON-parses output
* returns:

```js
{
  query,
  context,
  raw,
  parsed
}
```

βœ“ fully test-covered
βœ“ easy to replace provider/model

---

## **4.4 Verifier (src/verifier/verifier_core.mjs)**

Pure function:

```js
async function runVerifier(sample, provider)
```

Applies:

* structural JSON check
* alignment/tone check
* error correction fallback

Returns:

```js
{
  ok: boolean,
  raw,
  parsed,
  sample
}
```

βœ“ test-covered

---

## **4.5 Reward Model (src/reward/reward_core.mjs)**

Pure scoring function:

```js
async function runReward(sample, provider)
```

* loads reward prompt
* calls provider
* ensures `score` is numeric
* computes `ok` based on positivity

βœ“ test-covered

(This will eventually be replaced with your Skywork or Nemotron reward server.)

---

## **4.6 Question Generation (src/question/question_core.mjs)**

Your newest subsystem.

```js
async function runQuestionGeneration(chunk, provider, maxQuestions)
```

Flow:

1. Take a raw content chunk (from distill-rag)
2. Prompt an LLM to extract 1–N questions
3. Parse/repair JSON
4. Return array of questions

Used when:

```
PIPELINE_SEED_MODE=question-first
```

So the pipeline becomes:

```
chunk β†’ questions β†’ retrieval β†’ generator β†’ ...
```

βœ“ test-covered
βœ“ modular
βœ“ will become core for bootstrap distillation

---

## **4.7 Pipeline Orchestrator (src/pipeline/pipeline.mjs)**

This is the master controller.

Key functions:

### `runPipelineStep({ question, verbose })`

Performs:

1. retrieval
2. generator
3. verifier
4. reward

and returns:

```
{
  status: 'accepted' | 'generator_failed' | ...,
  question,
  context,
  gen,
  ver,
  rew
}
```

Extensive verbose logging is built in:

```
   [retrieval] ...
   [generator] ...
   [verifier] ...
   [reward] ...
```

### `runPipelineBatch({ seedsPath, limit, verbose })`

Iterates over seeds:

* static seed mode (default)
* or question-first mode (pending)

Writes accepted samples via:

### `appendGoldRecord(outPath, record)`

---

# **5. Seed Modes**

There are two entry strategies:

---

## **5.1 Static Question Mode**

```
PIPELINE_SEED_MODE=static
```

Loads:

```
test_samples/seed_questions.jsonl
```

Simple and deterministic.

---

## **5.2 Question-First Mode** *(recommended)*

```
PIPELINE_SEED_MODE=question-first
```

Pipeline:

```
for each chunk:
    questions = runQuestionGeneration(chunk)
    for each question:
        runPipelineStep(question)
```

This is the correct mode for massive bootstrap distillation because not every chunk answers the same static seed questions.

This mode uses:

* `QUESTION_PROVIDER`
* `QUESTION_MODEL`

---

# **6. Modularization Status**

Already modular:

* generator_core.mjs
* verifier_core.mjs
* reward_core.mjs
* provider.mjs
* question_core.mjs
* retrieval.mjs

Partially modular:

* pipeline.mjs (big but structured)
* pipeline_cli.mjs (needs handling for dynamic seed mode)

Planned:

```
pipeline/
  retrieval_stage.mjs
  generator_stage.mjs
  verifier_stage.mjs
  reward_stage.mjs
  gold_writer.mjs
```

This matches the ROADMAP.

---

# **7. What Can Be Tested**

All pure modules have unit tests:

| Module              | Tested?  | Notes          |
| ------------------- | -------- | -------------- |
| generator_core      | βœ“        | mock provider  |
| verifier_core       | βœ“        | mock provider  |
| reward_core         | βœ“        | mock provider  |
| question_core       | βœ“        | mock provider  |
| provider dispatcher | βœ“        | dispatch logic |
| retrieval           | βœ“βœ“       | mock + real ES |
| pipeline (mock)     | βœ“        | integration    |
| pipeline (real)     | optional | can add later  |

Your test suite is healthy:

```
9 files, 27 tests β†’ all pass
```

---

# **8. Logging & Verbose Mode**

All stages print diagnostics when `verbose` is passed to:

```
npm run pipeline -- --verbose
```

Includes:

* first chunk preview
* raw LLM output
* parsed JSON
* acceptance status
* error messages

---

# **9. Future Extensions**

As per ROADMAP:

* split pipeline into smaller modules
* improved QG (HyDE, retries, JSON repair)
* dedupe (minhash)
* gold dataset quality metrics
* full distillation cycle (generator β†’ verifier β†’ reward β†’ training β†’ new generator)

---

# **10. Successor Notes**

This project is:

* entirely Node.js ESM
* fully testable end-to-end
* GPU-agnostic
* provider-agnostic
* prompt-driven
* safe to modify when modularized

Golden rule:

> Never mix CLI code with pipeline logic.
> Put everything pure into `*_core.mjs`, test it, then wrap it in CLI tools.

---

If you'd like, I can also:

βœ“ generate the **next version** of pipeline modularization
βœ“ implement `PIPELINE_SEED_MODE=question-first` fully
βœ“ add a **chunk loader** so QG works immediately
βœ“ produce a **Mermaid architecture diagram**
βœ“ produce a **successor prompt** to embed in the repo

Just tell me.