@mailwoman/corpus 11.4.0 → 12.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data/address-source-register.json +9 -1
- package/data/builds/v0.8.22-nz-overlay/OVERLAY_PLAN.json +19 -0
- package/data/builds/v0.8.22-wofrank-overlay/OVERLAY_PLAN.json +51 -0
- package/data/builds/v0.8.24-everyplace/inputs.json +35 -0
- package/data/license-decisions.json +9 -1
- package/data/training-configs/v8.14.0-base-c6-wofreps-cursor-v0822-s42.yaml +926 -0
- package/data/training-configs/v8.15.0-base-c6-wofreps-cursor-emit-v0822-s42.yaml +973 -0
- package/data/training-configs/v8.16.0-base-c6-wofreps-positions-v0822-s42.yaml +988 -0
- package/data/training-configs/v8.17.0-stage1-gazetteer-v0822-s42.yaml +557 -0
- package/data/training-configs/v8.17.0-stage2-corpus-v0822-s42.yaml +1008 -0
- package/data/training-configs/v8.18.0-gazetteer-then-mixture-v0822-s42.yaml +1225 -0
- package/data/training-configs/v8.19.0-nz-v0822-s42.yaml +870 -0
- package/data/training-configs/v8.20.0-wofrank-v0822-s42.yaml +863 -0
- package/data/training-configs/v8.21.0-outcomes-v0822-s42.yaml +873 -0
- package/data/training-configs/v8.22.0-stage1-everyplace-v0824-s42.yaml +374 -0
- package/data/training-configs/v8.22.0-stage2-corpus-v0824-s42.yaml +808 -0
- package/data/training-configs/v8.22.1-stage1-everyplace-v0824-s42.yaml +376 -0
- package/data/training-configs/v8.22.1-stage2-corpus-v0824-s42.yaml +793 -0
- package/data/training-configs/v9.0.0-bytestem-v090-s42.yaml +665 -0
- package/data/training-manifests/v0.8.22-nz-overlay.effective.json +2556 -0
- package/data/training-manifests/v0.8.22-nz-overlay.json +9000 -0
- package/data/training-manifests/v0.8.22-wofrank-overlay.json +9000 -0
- package/data/training-manifests/v0.8.24-everyplace-overlay.json +9000 -0
- package/data/training-manifests/v0.9.0-compact.effective.json +2556 -0
- package/data/training-manifests/v0.9.0-compact.json +9000 -0
- package/lib/adapters/wof/admin/json/adapter.ts +38 -3
- package/lib/adapters/wof/json-rows.ts +84 -5
- package/lib/adapters/wof/sample.ts +42 -6
- package/lib/build/code-hash.ts +1 -1
- package/lib/cjk/foreign-places.ts +1 -1
- package/lib/kr/adapters/localdata/permit-rows.ts +1 -1
- package/lib/parquet/schema.ts +7 -0
- package/lib/tools/anthropic/batches.ts +221 -0
- package/lib/tools/anthropic/labeling.ts +263 -0
- package/lib/tools/compact-v9.ts +872 -0
- package/lib/tools/fetch/wikidata/vital-places/entities.ts +20 -2
- package/lib/tools/fetch/wikidata/vital-places/qrank.ts +5 -4
- package/lib/tools/fragment-assay.run.ts +2 -1
- package/lib/tools/name-roles/name-roles.run.ts +296 -0
- package/lib/tools/name-roles/prompt.ts +253 -0
- package/lib/tools/overlay/deletions.ts +1 -1
- package/lib/tools/overlay/draw-weight.ts +275 -0
- package/lib/tools/overlay/manifest.ts +36 -5
- package/lib/tools/place-twins/place-twins.run.ts +528 -0
- package/lib/tools/place-twins/prompt.ts +276 -0
- package/lib/tools/prominent-shapes/prominent-shapes.run.ts +420 -0
- package/lib/tools/prominent-shapes/prompt.ts +189 -0
- package/lib/tools/prominent-shapes/read-seeds.ts +286 -0
- package/lib/tools/prominent-shapes/seeds.ts +281 -0
- package/lib/tools/prominent-shapes/validate.ts +563 -0
- package/lib/training/config/admission.ts +9 -2
- package/lib/training/config/schema.ts +84 -0
- package/lib/types.ts +23 -0
- package/lib/us/adapters/usgov/nad/adapter.ts +8 -3
- package/lib/utils/split.ts +1 -1
- package/out/adapters/wof/admin/json/adapter.d.ts +7 -0
- package/out/adapters/wof/admin/json/adapter.d.ts.map +1 -1
- package/out/adapters/wof/admin/json/adapter.js +28 -3
- package/out/adapters/wof/admin/json/adapter.js.map +1 -1
- package/out/adapters/wof/json-rows.d.ts +8 -0
- package/out/adapters/wof/json-rows.d.ts.map +1 -1
- package/out/adapters/wof/json-rows.js +56 -4
- package/out/adapters/wof/json-rows.js.map +1 -1
- package/out/adapters/wof/sample.d.ts +12 -0
- package/out/adapters/wof/sample.d.ts.map +1 -1
- package/out/adapters/wof/sample.js +37 -6
- package/out/adapters/wof/sample.js.map +1 -1
- package/out/build/code-hash.js +1 -1
- package/out/build/code-hash.js.map +1 -1
- package/out/cjk/foreign-places.js +1 -1
- package/out/cjk/foreign-places.js.map +1 -1
- package/out/kr/adapters/localdata/permit-rows.js +1 -1
- package/out/kr/adapters/localdata/permit-rows.js.map +1 -1
- package/out/parquet/schema.d.ts +6 -0
- package/out/parquet/schema.d.ts.map +1 -1
- package/out/parquet/schema.js +6 -0
- package/out/parquet/schema.js.map +1 -1
- package/out/tools/anthropic/batches.d.ts +117 -0
- package/out/tools/anthropic/batches.d.ts.map +1 -0
- package/out/tools/anthropic/batches.js +141 -0
- package/out/tools/anthropic/batches.js.map +1 -0
- package/out/tools/anthropic/labeling.d.ts +97 -0
- package/out/tools/anthropic/labeling.d.ts.map +1 -0
- package/out/tools/anthropic/labeling.js +146 -0
- package/out/tools/anthropic/labeling.js.map +1 -0
- package/out/tools/compact-v9.d.ts +117 -0
- package/out/tools/compact-v9.d.ts.map +1 -0
- package/out/tools/compact-v9.js +494 -0
- package/out/tools/compact-v9.js.map +1 -0
- package/out/tools/fetch/wikidata/vital-places/entities.d.ts +3 -0
- package/out/tools/fetch/wikidata/vital-places/entities.d.ts.map +1 -1
- package/out/tools/fetch/wikidata/vital-places/entities.js +19 -1
- package/out/tools/fetch/wikidata/vital-places/entities.js.map +1 -1
- package/out/tools/fetch/wikidata/vital-places/qrank.d.ts.map +1 -1
- package/out/tools/fetch/wikidata/vital-places/qrank.js +4 -4
- package/out/tools/fetch/wikidata/vital-places/qrank.js.map +1 -1
- package/out/tools/fragment-assay.run.js +1 -1
- package/out/tools/fragment-assay.run.js.map +1 -1
- package/out/tools/name-roles/name-roles.run.d.ts +2 -0
- package/out/tools/name-roles/name-roles.run.d.ts.map +1 -0
- package/out/tools/name-roles/name-roles.run.js +230 -0
- package/out/tools/name-roles/name-roles.run.js.map +1 -0
- package/out/tools/name-roles/prompt.d.ts +166 -0
- package/out/tools/name-roles/prompt.d.ts.map +1 -0
- package/out/tools/name-roles/prompt.js +176 -0
- package/out/tools/name-roles/prompt.js.map +1 -0
- package/out/tools/overlay/deletions.js +1 -1
- package/out/tools/overlay/deletions.js.map +1 -1
- package/out/tools/overlay/draw-weight.d.ts +86 -0
- package/out/tools/overlay/draw-weight.d.ts.map +1 -0
- package/out/tools/overlay/draw-weight.js +131 -0
- package/out/tools/overlay/draw-weight.js.map +1 -0
- package/out/tools/overlay/manifest.d.ts.map +1 -1
- package/out/tools/overlay/manifest.js +29 -5
- package/out/tools/overlay/manifest.js.map +1 -1
- package/out/tools/place-twins/place-twins.run.d.ts +2 -0
- package/out/tools/place-twins/place-twins.run.d.ts.map +1 -0
- package/out/tools/place-twins/place-twins.run.js +385 -0
- package/out/tools/place-twins/place-twins.run.js.map +1 -0
- package/out/tools/place-twins/prompt.d.ts +169 -0
- package/out/tools/place-twins/prompt.d.ts.map +1 -0
- package/out/tools/place-twins/prompt.js +183 -0
- package/out/tools/place-twins/prompt.js.map +1 -0
- package/out/tools/prominent-shapes/prominent-shapes.run.d.ts +2 -0
- package/out/tools/prominent-shapes/prominent-shapes.run.d.ts.map +1 -0
- package/out/tools/prominent-shapes/prominent-shapes.run.js +302 -0
- package/out/tools/prominent-shapes/prominent-shapes.run.js.map +1 -0
- package/out/tools/prominent-shapes/prompt.d.ts +74 -0
- package/out/tools/prominent-shapes/prompt.d.ts.map +1 -0
- package/out/tools/prominent-shapes/prompt.js +156 -0
- package/out/tools/prominent-shapes/prompt.js.map +1 -0
- package/out/tools/prominent-shapes/read-seeds.d.ts +23 -0
- package/out/tools/prominent-shapes/read-seeds.d.ts.map +1 -0
- package/out/tools/prominent-shapes/read-seeds.js +219 -0
- package/out/tools/prominent-shapes/read-seeds.js.map +1 -0
- package/out/tools/prominent-shapes/seeds.d.ts +132 -0
- package/out/tools/prominent-shapes/seeds.d.ts.map +1 -0
- package/out/tools/prominent-shapes/seeds.js +178 -0
- package/out/tools/prominent-shapes/seeds.js.map +1 -0
- package/out/tools/prominent-shapes/validate.d.ts +152 -0
- package/out/tools/prominent-shapes/validate.d.ts.map +1 -0
- package/out/tools/prominent-shapes/validate.js +389 -0
- package/out/tools/prominent-shapes/validate.js.map +1 -0
- package/out/training/config/admission.d.ts +2 -1
- package/out/training/config/admission.d.ts.map +1 -1
- package/out/training/config/admission.js +9 -2
- package/out/training/config/admission.js.map +1 -1
- package/out/training/config/schema.d.ts +321 -0
- package/out/training/config/schema.d.ts.map +1 -1
- package/out/training/config/schema.js +76 -0
- package/out/training/config/schema.js.map +1 -1
- package/out/types.d.ts +21 -0
- package/out/types.d.ts.map +1 -1
- package/out/types.js +13 -0
- package/out/types.js.map +1 -1
- package/out/us/adapters/usgov/nad/adapter.d.ts +4 -0
- package/out/us/adapters/usgov/nad/adapter.d.ts.map +1 -1
- package/out/us/adapters/usgov/nad/adapter.js +8 -3
- package/out/us/adapters/usgov/nad/adapter.js.map +1 -1
- package/out/utils/split.js +1 -1
- package/out/utils/split.js.map +1 -1
- package/package.json +24 -24
|
@@ -72,9 +72,16 @@ import {
|
|
|
72
72
|
import { RowSampler } from "#adapters/wof/sample"
|
|
73
73
|
import { openStoredRecordEntries } from "#adapters/wof/stored-records"
|
|
74
74
|
import { SourceRegister } from "#registers"
|
|
75
|
-
import {
|
|
75
|
+
import {
|
|
76
|
+
AddressRole,
|
|
77
|
+
type AdapterOptions,
|
|
78
|
+
type CanonicalRow,
|
|
79
|
+
type CorpusAdapter,
|
|
80
|
+
EveryPlaceNames,
|
|
81
|
+
SurfaceOrigin,
|
|
82
|
+
} from "#types"
|
|
76
83
|
import { nameValues, preferredName, type AncestorRecords, type WOFRecord } from "#utils"
|
|
77
|
-
import { ancestorRecordsOf } from "#utils/wof-json"
|
|
84
|
+
import { ancestorRecordsOf, parseWOFSlotKey } from "#utils/wof-json"
|
|
78
85
|
|
|
79
86
|
/**
|
|
80
87
|
* Map a WOF placetype to a Mailwoman `ComponentTag`, or `undefined` to skip.
|
|
@@ -633,6 +640,33 @@ export function nameSlotsFor(rec: WOFRecord): NameSlot[] {
|
|
|
633
640
|
return slots.map((slot) => (slot.language && !kept.has(slot.language) ? { key: slot.key, value: slot.value } : slot))
|
|
634
641
|
}
|
|
635
642
|
|
|
643
|
+
/**
|
|
644
|
+
* Whether the adapter renders `record` as a place: its placetype maps to a
|
|
645
|
+
* component tag, and WOF assigns it to a country.
|
|
646
|
+
*
|
|
647
|
+
* The place-coverage audit counts the places a corpus can train against this same rule.
|
|
648
|
+
*/
|
|
649
|
+
export function rendersWOFAdminPlace(record: WOFRecord): boolean {
|
|
650
|
+
return Boolean(placetypeToTag(record.placetype)) && record.country !== WOF_NO_COUNTRY
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* The name slots of which every place keeps one row under `names`: the default name,
|
|
655
|
+
* and under {@linkcode EveryPlaceNames.Official} the first preferred name in
|
|
656
|
+
* each official language of the place's country.
|
|
657
|
+
*/
|
|
658
|
+
function everyPlaceSlot(names: EveryPlaceNames): (country: string, slotKey: string) => boolean {
|
|
659
|
+
return (country, slotKey) => {
|
|
660
|
+
if (slotKey === "default") return true
|
|
661
|
+
|
|
662
|
+
if (names !== EveryPlaceNames.Official || /-[0-9]+$/.test(slotKey)) return false
|
|
663
|
+
|
|
664
|
+
const { kind, language } = parseWOFSlotKey(slotKey)
|
|
665
|
+
|
|
666
|
+
return kind === "preferred" && OFFICIAL_LANGUAGES[country]?.official.includes(language) === true
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
636
670
|
/**
|
|
637
671
|
* The `wof:country` WOF writes on a record it assigns to no country,
|
|
638
672
|
* such as a disputed area or a record not yet placed.
|
|
@@ -672,7 +706,7 @@ export function createWOFAdminAdapter(): CorpusAdapter {
|
|
|
672
706
|
// A record WOF assigns to no country names none for its rows, so it is skipped too.
|
|
673
707
|
// A country-filtered run keeps only that country's records, ancestors included.
|
|
674
708
|
using stored = await openStoredRecordEntries(opts, {
|
|
675
|
-
keep:
|
|
709
|
+
keep: rendersWOFAdminPlace,
|
|
676
710
|
resolve: (record, lookup) => ancestorRecordsOf(record, lookup, placetypeToTag),
|
|
677
711
|
})
|
|
678
712
|
|
|
@@ -694,6 +728,7 @@ export function createWOFAdminAdapter(): CorpusAdapter {
|
|
|
694
728
|
variantsFor,
|
|
695
729
|
sampler,
|
|
696
730
|
rowRule: dualRoleRowRule(WOF_ADMIN_ADAPTER_ID, pairs),
|
|
731
|
+
...(opts.sample?.everyPlace ? { placeSlot: everyPlaceSlot(opts.sample.everyPlace) } : {}),
|
|
697
732
|
})
|
|
698
733
|
|
|
699
734
|
await sampler?.writeOutputs()
|
|
@@ -275,6 +275,14 @@ interface EmitWOFJSONRowsOptions<A extends object> {
|
|
|
275
275
|
* Rows the adapter refuses or requires whatever the sample keeps.
|
|
276
276
|
*/
|
|
277
277
|
rowRule?: WOFRowRule
|
|
278
|
+
/**
|
|
279
|
+
* Whether a place keeps one row of a name slot whatever the sample keeps,
|
|
280
|
+
* read from the row's country and slot key.
|
|
281
|
+
*
|
|
282
|
+
* Of a kept slot's rows, the one with the most components is emitted while the
|
|
283
|
+
* records are rendered, so the sampler holds no payload for it.
|
|
284
|
+
*/
|
|
285
|
+
placeSlot?: (country: string, slotKey: string) => boolean
|
|
278
286
|
}
|
|
279
287
|
|
|
280
288
|
/**
|
|
@@ -301,13 +309,31 @@ export interface WOFRowRule {
|
|
|
301
309
|
* while the row keeps the record's own country.
|
|
302
310
|
*/
|
|
303
311
|
export function* emitWOFJSONRows<A extends object>(options: EmitWOFJSONRowsOptions<A>): Generator<CanonicalRow> {
|
|
304
|
-
const { adapterOptions, sampler, rowRule } = options
|
|
312
|
+
const { adapterOptions, sampler, rowRule, placeSlot } = options
|
|
305
313
|
const limit = adapterOptions.limit
|
|
306
314
|
let emitted = 0
|
|
307
315
|
|
|
308
316
|
if (sampler) {
|
|
309
|
-
for (const
|
|
310
|
-
|
|
317
|
+
for (const place of rowsByRecord(renderWOFRows(options))) {
|
|
318
|
+
const placeRows = placeSlot ? fullestRowPerSlot(place, placeSlot, rowRule) : new Set<CanonicalRow>()
|
|
319
|
+
|
|
320
|
+
for (const { row, slotKey } of place) {
|
|
321
|
+
const sampled = sampledRow(row, slotKey)
|
|
322
|
+
|
|
323
|
+
if (placeRows.has(row)) {
|
|
324
|
+
if (!sampler.observeEmitted(sampled, row)) continue
|
|
325
|
+
|
|
326
|
+
if (typeof limit === "number" && emitted >= limit) return
|
|
327
|
+
|
|
328
|
+
yield row
|
|
329
|
+
|
|
330
|
+
emitted++
|
|
331
|
+
|
|
332
|
+
continue
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
sampler.observe(sampled, row, rowRule?.requires(row) ?? false)
|
|
336
|
+
}
|
|
311
337
|
}
|
|
312
338
|
}
|
|
313
339
|
|
|
@@ -330,16 +356,68 @@ export function* emitWOFJSONRows<A extends object>(options: EmitWOFJSONRowsOptio
|
|
|
330
356
|
}
|
|
331
357
|
}
|
|
332
358
|
|
|
359
|
+
/**
|
|
360
|
+
* The rendered rows grouped by record, one group per record in the order rendered.
|
|
361
|
+
*
|
|
362
|
+
* The renderer writes all of a record's rows before the next record's,
|
|
363
|
+
* so a group closes when the record id changes.
|
|
364
|
+
*/
|
|
365
|
+
function* rowsByRecord(
|
|
366
|
+
rows: Iterable<{ row: CanonicalRow; slotKey: string; recordID: number }>
|
|
367
|
+
): Generator<Array<{ row: CanonicalRow; slotKey: string }>> {
|
|
368
|
+
let group: Array<{ row: CanonicalRow; slotKey: string }> = []
|
|
369
|
+
let current: number | null = null
|
|
370
|
+
|
|
371
|
+
for (const { row, slotKey, recordID } of rows) {
|
|
372
|
+
if (recordID !== current && group.length) {
|
|
373
|
+
yield group
|
|
374
|
+
group = []
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
current = recordID
|
|
378
|
+
group.push({ row, slotKey })
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (group.length) {
|
|
382
|
+
yield group
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Of each name slot `placeSlot` keeps, the row with the most components, leaving out a row `rowRule` drops.
|
|
388
|
+
* A tie keeps the row rendered first.
|
|
389
|
+
*/
|
|
390
|
+
function fullestRowPerSlot(
|
|
391
|
+
place: ReadonlyArray<{ row: CanonicalRow; slotKey: string }>,
|
|
392
|
+
placeSlot: (country: string, slotKey: string) => boolean,
|
|
393
|
+
rowRule: WOFRowRule | undefined
|
|
394
|
+
): Set<CanonicalRow> {
|
|
395
|
+
const fullest = new Map<string, CanonicalRow>()
|
|
396
|
+
|
|
397
|
+
for (const { row, slotKey } of place) {
|
|
398
|
+
if (!placeSlot(row.country, slotKey) || rowRule?.drops(row)) continue
|
|
399
|
+
|
|
400
|
+
const held = fullest.get(slotKey)
|
|
401
|
+
|
|
402
|
+
if (!held || Object.keys(row.components).length > Object.keys(held.components).length) {
|
|
403
|
+
fullest.set(slotKey, row)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return new Set(fullest.values())
|
|
408
|
+
}
|
|
409
|
+
|
|
333
410
|
function sampledRow(row: CanonicalRow, slotKey: string): SampledRow {
|
|
334
411
|
return { sourceID: row.source_id, country: row.country, slotKey, raw: row.raw, components: row.components }
|
|
335
412
|
}
|
|
336
413
|
|
|
337
414
|
/**
|
|
338
|
-
* Every row the records render, in WOF-id order, each with the name-slot key
|
|
415
|
+
* Every row the records render, in WOF-id order, each with the name-slot key
|
|
416
|
+
* and the record id it came from.
|
|
339
417
|
*/
|
|
340
418
|
function* renderWOFRows<A extends object>(
|
|
341
419
|
options: EmitWOFJSONRowsOptions<A>
|
|
342
|
-
): Generator<{ row: CanonicalRow; slotKey: string }> {
|
|
420
|
+
): Generator<{ row: CanonicalRow; slotKey: string; recordID: number }> {
|
|
343
421
|
const {
|
|
344
422
|
records,
|
|
345
423
|
adapterOptions,
|
|
@@ -367,6 +445,7 @@ function* renderWOFRows<A extends object>(
|
|
|
367
445
|
|
|
368
446
|
yield {
|
|
369
447
|
slotKey: slot.key,
|
|
448
|
+
recordID: record.id,
|
|
370
449
|
row: {
|
|
371
450
|
raw,
|
|
372
451
|
components,
|
|
@@ -209,6 +209,11 @@ export class RowSampler<T = SampledRow> {
|
|
|
209
209
|
readonly #comparison = new Map<string, LowestValues>()
|
|
210
210
|
readonly #comparisonCandidates = new Map<string, Candidate<T> & { order: number }>()
|
|
211
211
|
#keptRaws: Set<number> | null = null
|
|
212
|
+
/**
|
|
213
|
+
* Case-folded text hashes of the rows the caller emitted itself
|
|
214
|
+
* through {@linkcode RowSampler.observeEmitted}.
|
|
215
|
+
*/
|
|
216
|
+
readonly #emittedRaws = new Set<number>()
|
|
212
217
|
/**
|
|
213
218
|
* Renderable rows per full cell, `country|tag order|kind|language`.
|
|
214
219
|
*/
|
|
@@ -251,9 +256,28 @@ export class RowSampler<T = SampledRow> {
|
|
|
251
256
|
* moves no other row's keep decision.
|
|
252
257
|
*/
|
|
253
258
|
observe(row: SampledRow, payload: T, required = false): void {
|
|
259
|
+
this.#observe(row, payload, required ? "required" : "sampled")
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Counts a row its caller emits itself, whatever its hash, and returns whether the caller may emit it.
|
|
264
|
+
*
|
|
265
|
+
* The row enters its cells' heaps as a required row does, so emitting it
|
|
266
|
+
* moves no other row's keep decision.
|
|
267
|
+
* The sampler holds no payload for it, so emitting a row of every place adds
|
|
268
|
+
* no row to the sampler's memory.
|
|
269
|
+
*
|
|
270
|
+
* A row the aligner would quarantine, or a row in a held-out cell, returns false:
|
|
271
|
+
* the first is dropped and the second is held out as {@linkcode RowSampler.observe} holds it.
|
|
272
|
+
*/
|
|
273
|
+
observeEmitted(row: SampledRow, payload: T): boolean {
|
|
274
|
+
return this.#observe(row, payload, "emitted")
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
#observe(row: SampledRow, payload: T, mode: "sampled" | "required" | "emitted"): boolean {
|
|
254
278
|
const keys = this.#keys(row)
|
|
255
279
|
|
|
256
|
-
if (!keys) return
|
|
280
|
+
if (!keys) return false
|
|
257
281
|
|
|
258
282
|
const { hash, structure, order, language, cell } = keys
|
|
259
283
|
const raw = hashFNV1a(row.raw.normalize("NFC").toLowerCase())
|
|
@@ -285,7 +309,7 @@ export class RowSampler<T = SampledRow> {
|
|
|
285
309
|
this.#heldOutCandidates.set(row.sourceID, { ...base, group: heldOutGroup })
|
|
286
310
|
}
|
|
287
311
|
|
|
288
|
-
return
|
|
312
|
+
return false
|
|
289
313
|
}
|
|
290
314
|
|
|
291
315
|
const held = [
|
|
@@ -294,8 +318,15 @@ export class RowSampler<T = SampledRow> {
|
|
|
294
318
|
lowest(this.#country, row.country, this.#sample.rowsPerCountry),
|
|
295
319
|
].some(Boolean)
|
|
296
320
|
|
|
297
|
-
|
|
298
|
-
|
|
321
|
+
// An emitted row is kept by construction, so it can't be a comparison row and needs no candidate.
|
|
322
|
+
if (mode === "emitted") {
|
|
323
|
+
this.#emittedRaws.add(raw)
|
|
324
|
+
|
|
325
|
+
return true
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (held || mode === "required") {
|
|
329
|
+
this.#candidates.set(row.sourceID, mode === "required" ? { ...base, required: true } : base)
|
|
299
330
|
}
|
|
300
331
|
|
|
301
332
|
const compareGroup = [structure, order].find((key) => this.#compare.has(key))
|
|
@@ -313,6 +344,8 @@ export class RowSampler<T = SampledRow> {
|
|
|
313
344
|
this.#comparisonCandidates.set(row.sourceID, { ...base, group: compareGroup, order: second })
|
|
314
345
|
}
|
|
315
346
|
}
|
|
347
|
+
|
|
348
|
+
return false
|
|
316
349
|
}
|
|
317
350
|
|
|
318
351
|
#isKept(candidate: Candidate<T>): boolean {
|
|
@@ -325,10 +358,13 @@ export class RowSampler<T = SampledRow> {
|
|
|
325
358
|
}
|
|
326
359
|
|
|
327
360
|
/**
|
|
328
|
-
* Case-folded text hashes of every kept row, computed once the pass is over.
|
|
361
|
+
* Case-folded text hashes of every kept or emitted row, computed once the pass is over.
|
|
329
362
|
*/
|
|
330
363
|
#keptRawHashes(): Set<number> {
|
|
331
|
-
this.#keptRaws ??= new Set([
|
|
364
|
+
this.#keptRaws ??= new Set([
|
|
365
|
+
...this.#emittedRaws,
|
|
366
|
+
...[...this.#candidates.values()].filter((c) => this.#isKept(c)).map((c) => c.raw),
|
|
367
|
+
])
|
|
332
368
|
|
|
333
369
|
return this.#keptRaws
|
|
334
370
|
}
|
package/lib/build/code-hash.ts
CHANGED
|
@@ -136,7 +136,7 @@ export async function cruiseModuleGraph(roots: readonly string[], repositoryRoot
|
|
|
136
136
|
{}
|
|
137
137
|
)
|
|
138
138
|
|
|
139
|
-
if (typeof result.output === "string") throw new
|
|
139
|
+
if (typeof result.output === "string") throw new TypeError("cruiseModuleGraph: dependency-cruiser returned text")
|
|
140
140
|
|
|
141
141
|
const edges = new Map<string, string[]>()
|
|
142
142
|
const packages = new Map<string, string[]>()
|
|
@@ -233,7 +233,7 @@ export async function heldOutIDs(splitAttribute: string): Promise<Map<string, st
|
|
|
233
233
|
for (const split of ["val", "test"] as const) {
|
|
234
234
|
const ids = attribute[split]
|
|
235
235
|
|
|
236
|
-
if (!Array.isArray(ids)) throw new
|
|
236
|
+
if (!Array.isArray(ids)) throw new TypeError(`split attribute ${splitAttribute} has no ${split} list`)
|
|
237
237
|
|
|
238
238
|
for (const id of ids) {
|
|
239
239
|
held.set(id, split)
|
|
@@ -96,7 +96,7 @@ function toNumber(value: string, field: string): number | null {
|
|
|
96
96
|
|
|
97
97
|
const parsed = Number(value)
|
|
98
98
|
|
|
99
|
-
if (!Number.isFinite(parsed)) throw new
|
|
99
|
+
if (!Number.isFinite(parsed)) throw new TypeError(`a permit row's ${field} is ${stringifyJSON(value)}, not a number`)
|
|
100
100
|
|
|
101
101
|
return parsed
|
|
102
102
|
}
|
package/lib/parquet/schema.ts
CHANGED
|
@@ -29,6 +29,13 @@ export const ROW_GROUP_SIZE = 50_000
|
|
|
29
29
|
*/
|
|
30
30
|
export const ROWS_PER_FILE = 1_000_000
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* The optional per-row draw weight: a positive float that scales a row's chance of
|
|
34
|
+
* being drawn within its source, with mean 1 over the source's train rows.
|
|
35
|
+
* A file without the column reads 1.0 for every row.
|
|
36
|
+
*/
|
|
37
|
+
export const DRAW_WEIGHT_COLUMN = "draw_weight"
|
|
38
|
+
|
|
32
39
|
/**
|
|
33
40
|
* Snappy is the codec selected for corpus parquet files.
|
|
34
41
|
*/
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @file A client for Anthropic's Message Batches endpoint. The corpus labeling tools submit their requests through it.
|
|
6
|
+
*
|
|
7
|
+
* A batch is asynchronous: `createMessageBatch` returns an id, `readMessageBatch` reports its state,
|
|
8
|
+
* and `readMessageBatchResults` returns one line per request once the state is `ended`.
|
|
9
|
+
* Result lines arrive in any order, so a caller joins them to its requests by `custom_id`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { APIClient, pluckResponseData } from "@mailwoman/core/api"
|
|
13
|
+
import { tryParsingJSON } from "@mailwoman/core/values/json"
|
|
14
|
+
import { TextSpliterator } from "spliterator"
|
|
15
|
+
|
|
16
|
+
import { $private } from "#env"
|
|
17
|
+
|
|
18
|
+
const BATCHES_URL = "https://api.anthropic.com/v1/messages/batches"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The most requests one batch accepts.
|
|
22
|
+
*/
|
|
23
|
+
export const MAX_BATCH_REQUESTS = 100_000
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* One Messages request inside a batch, addressed by a caller-chosen id of at most 64 characters.
|
|
27
|
+
*/
|
|
28
|
+
export interface MessageBatchRequest {
|
|
29
|
+
custom_id: string
|
|
30
|
+
params: Record<string, unknown>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The state of a submitted batch.
|
|
35
|
+
*/
|
|
36
|
+
export interface MessageBatch {
|
|
37
|
+
id: string
|
|
38
|
+
processing_status: "in_progress" | "canceling" | "ended"
|
|
39
|
+
request_counts: { processing: number; succeeded: number; errored: number; canceled: number; expired: number }
|
|
40
|
+
results_url: string | null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Token counts the API bills a response for.
|
|
45
|
+
*/
|
|
46
|
+
export interface MessageUsage {
|
|
47
|
+
input_tokens: number
|
|
48
|
+
output_tokens: number
|
|
49
|
+
cache_creation_input_tokens?: number | null
|
|
50
|
+
cache_read_input_tokens?: number | null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* One request's outcome in a finished batch.
|
|
55
|
+
*
|
|
56
|
+
* `message` is present only when `type` is `succeeded`.
|
|
57
|
+
*/
|
|
58
|
+
export interface MessageBatchResult {
|
|
59
|
+
custom_id: string
|
|
60
|
+
result: {
|
|
61
|
+
type: "succeeded" | "errored" | "canceled" | "expired"
|
|
62
|
+
message?: {
|
|
63
|
+
content: Array<{ type: string; text?: string }>
|
|
64
|
+
stop_reason: string | null
|
|
65
|
+
usage: MessageUsage
|
|
66
|
+
}
|
|
67
|
+
error?: unknown
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Dollars per million tokens for one model on the batch endpoint.
|
|
73
|
+
*/
|
|
74
|
+
export interface TokenPrice {
|
|
75
|
+
input: number
|
|
76
|
+
output: number
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Creates the client every batch call shares.
|
|
81
|
+
*
|
|
82
|
+
* @throws When `ANTHROPIC_API_KEY` is unset.
|
|
83
|
+
*/
|
|
84
|
+
export function createAnthropicClient(): APIClient {
|
|
85
|
+
const apiKey = $private.ANTHROPIC_API_KEY
|
|
86
|
+
|
|
87
|
+
if (!apiKey) throw new Error("ANTHROPIC_API_KEY is required to call Anthropic")
|
|
88
|
+
|
|
89
|
+
return new APIClient({
|
|
90
|
+
displayName: "anthropic",
|
|
91
|
+
retry: true,
|
|
92
|
+
axios: {
|
|
93
|
+
headers: { "x-api-key": apiKey, "anthropic-version": "2023-06-01", "Content-Type": "application/json" },
|
|
94
|
+
},
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Sends one Messages request outside a batch, at the standard price,
|
|
100
|
+
* and returns it in a batch result's shape.
|
|
101
|
+
*
|
|
102
|
+
* A labeling tool sends one of these before a batch to confirm the API accepts its
|
|
103
|
+
* parameters and to measure the tokens one request uses.
|
|
104
|
+
*/
|
|
105
|
+
export async function createMessage(client: APIClient, request: MessageBatchRequest): Promise<MessageBatchResult> {
|
|
106
|
+
const message = (await client
|
|
107
|
+
.fetch<NonNullable<MessageBatchResult["result"]["message"]>>({
|
|
108
|
+
url: "https://api.anthropic.com/v1/messages",
|
|
109
|
+
method: "post",
|
|
110
|
+
timeout: 600_000,
|
|
111
|
+
data: request.params,
|
|
112
|
+
cache: false,
|
|
113
|
+
})
|
|
114
|
+
.then(pluckResponseData)) as NonNullable<MessageBatchResult["result"]["message"]>
|
|
115
|
+
|
|
116
|
+
return { custom_id: request.custom_id, result: { type: "succeeded", message } }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Submits `requests` as one batch and returns its initial state.
|
|
121
|
+
*
|
|
122
|
+
* @throws When `requests` is empty or exceeds `MAX_BATCH_REQUESTS`.
|
|
123
|
+
*/
|
|
124
|
+
export async function createMessageBatch(client: APIClient, requests: MessageBatchRequest[]): Promise<MessageBatch> {
|
|
125
|
+
if (!requests.length || requests.length > MAX_BATCH_REQUESTS) {
|
|
126
|
+
throw new Error(`A batch takes 1 to ${MAX_BATCH_REQUESTS} requests, and this one has ${requests.length}`)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return client
|
|
130
|
+
.fetch<MessageBatch>({ url: BATCHES_URL, method: "post", timeout: 600_000, data: { requests }, cache: false })
|
|
131
|
+
.then(pluckResponseData) as Promise<MessageBatch>
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Reads the current state of the batch `id`.
|
|
136
|
+
*/
|
|
137
|
+
export async function readMessageBatch(client: APIClient, id: string): Promise<MessageBatch> {
|
|
138
|
+
return client
|
|
139
|
+
.fetch<MessageBatch>({ url: `${BATCHES_URL}/${id}`, timeout: 60_000, cache: false })
|
|
140
|
+
.then(pluckResponseData) as Promise<MessageBatch>
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Reads every result line of the finished batch `id`.
|
|
145
|
+
*
|
|
146
|
+
* @throws When the batch is still in progress, or when a line of the result file fails to parse.
|
|
147
|
+
*/
|
|
148
|
+
export async function readMessageBatchResults(client: APIClient, id: string): Promise<MessageBatchResult[]> {
|
|
149
|
+
const batch = await readMessageBatch(client, id)
|
|
150
|
+
|
|
151
|
+
if (batch.processing_status !== "ended") {
|
|
152
|
+
throw new Error(`Batch ${id} is ${batch.processing_status}; its results exist only once it has ended`)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const { data } = await client.fetch<string>({
|
|
156
|
+
url: `${BATCHES_URL}/${id}/results`,
|
|
157
|
+
timeout: 600_000,
|
|
158
|
+
responseType: "text",
|
|
159
|
+
transformResponse: (body: unknown) => body,
|
|
160
|
+
cache: false,
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
return parseMessageBatchResults(String(data), `Batch ${id}`)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Parses the JSONL body of a batch's results, as the API returns it or as a run saved it.
|
|
168
|
+
*
|
|
169
|
+
* @param source Where the body came from, as an error states it.
|
|
170
|
+
* @throws When a line fails to parse or lacks a request id or outcome, or when the body holds no result.
|
|
171
|
+
*/
|
|
172
|
+
export function parseMessageBatchResults(body: string, source: string): MessageBatchResult[] {
|
|
173
|
+
const results: MessageBatchResult[] = []
|
|
174
|
+
|
|
175
|
+
for (const line of TextSpliterator.from(body)) {
|
|
176
|
+
if (line.trim() === "") continue
|
|
177
|
+
|
|
178
|
+
const parsed = tryParsingJSON(line) as MessageBatchResult | null
|
|
179
|
+
|
|
180
|
+
if (parsed?.custom_id == null || parsed.result?.type == null) {
|
|
181
|
+
throw new Error(`${source} holds a result line that is not a batch result: ${line.slice(0, 200)}`)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
results.push(parsed)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!results.length) throw new Error(`${source} holds no batch result`)
|
|
188
|
+
|
|
189
|
+
return results
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Returns the text of a succeeded result, or `null` for any other outcome
|
|
194
|
+
* or a response that stopped at `max_tokens`.
|
|
195
|
+
*/
|
|
196
|
+
export function resultText(result: MessageBatchResult): string | null {
|
|
197
|
+
const message = result.result.message
|
|
198
|
+
|
|
199
|
+
if (result.result.type !== "succeeded" || !message || message.stop_reason !== "end_turn") return null
|
|
200
|
+
|
|
201
|
+
return message.content.find((block) => block.type === "text")?.text ?? null
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Sums the dollars the succeeded results cost at `price`.
|
|
206
|
+
*/
|
|
207
|
+
export function batchCostUSD(results: readonly MessageBatchResult[], price: TokenPrice): number {
|
|
208
|
+
let input = 0
|
|
209
|
+
let output = 0
|
|
210
|
+
|
|
211
|
+
for (const { result } of results) {
|
|
212
|
+
const usage = result.message?.usage
|
|
213
|
+
|
|
214
|
+
if (!usage) continue
|
|
215
|
+
|
|
216
|
+
input += usage.input_tokens + (usage.cache_creation_input_tokens ?? 0) + (usage.cache_read_input_tokens ?? 0)
|
|
217
|
+
output += usage.output_tokens
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return (input * price.input + output * price.output) / 1_000_000
|
|
221
|
+
}
|