@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.
Files changed (162) hide show
  1. package/data/address-source-register.json +9 -1
  2. package/data/builds/v0.8.22-nz-overlay/OVERLAY_PLAN.json +19 -0
  3. package/data/builds/v0.8.22-wofrank-overlay/OVERLAY_PLAN.json +51 -0
  4. package/data/builds/v0.8.24-everyplace/inputs.json +35 -0
  5. package/data/license-decisions.json +9 -1
  6. package/data/training-configs/v8.14.0-base-c6-wofreps-cursor-v0822-s42.yaml +926 -0
  7. package/data/training-configs/v8.15.0-base-c6-wofreps-cursor-emit-v0822-s42.yaml +973 -0
  8. package/data/training-configs/v8.16.0-base-c6-wofreps-positions-v0822-s42.yaml +988 -0
  9. package/data/training-configs/v8.17.0-stage1-gazetteer-v0822-s42.yaml +557 -0
  10. package/data/training-configs/v8.17.0-stage2-corpus-v0822-s42.yaml +1008 -0
  11. package/data/training-configs/v8.18.0-gazetteer-then-mixture-v0822-s42.yaml +1225 -0
  12. package/data/training-configs/v8.19.0-nz-v0822-s42.yaml +870 -0
  13. package/data/training-configs/v8.20.0-wofrank-v0822-s42.yaml +863 -0
  14. package/data/training-configs/v8.21.0-outcomes-v0822-s42.yaml +873 -0
  15. package/data/training-configs/v8.22.0-stage1-everyplace-v0824-s42.yaml +374 -0
  16. package/data/training-configs/v8.22.0-stage2-corpus-v0824-s42.yaml +808 -0
  17. package/data/training-configs/v8.22.1-stage1-everyplace-v0824-s42.yaml +376 -0
  18. package/data/training-configs/v8.22.1-stage2-corpus-v0824-s42.yaml +793 -0
  19. package/data/training-configs/v9.0.0-bytestem-v090-s42.yaml +665 -0
  20. package/data/training-manifests/v0.8.22-nz-overlay.effective.json +2556 -0
  21. package/data/training-manifests/v0.8.22-nz-overlay.json +9000 -0
  22. package/data/training-manifests/v0.8.22-wofrank-overlay.json +9000 -0
  23. package/data/training-manifests/v0.8.24-everyplace-overlay.json +9000 -0
  24. package/data/training-manifests/v0.9.0-compact.effective.json +2556 -0
  25. package/data/training-manifests/v0.9.0-compact.json +9000 -0
  26. package/lib/adapters/wof/admin/json/adapter.ts +38 -3
  27. package/lib/adapters/wof/json-rows.ts +84 -5
  28. package/lib/adapters/wof/sample.ts +42 -6
  29. package/lib/build/code-hash.ts +1 -1
  30. package/lib/cjk/foreign-places.ts +1 -1
  31. package/lib/kr/adapters/localdata/permit-rows.ts +1 -1
  32. package/lib/parquet/schema.ts +7 -0
  33. package/lib/tools/anthropic/batches.ts +221 -0
  34. package/lib/tools/anthropic/labeling.ts +263 -0
  35. package/lib/tools/compact-v9.ts +872 -0
  36. package/lib/tools/fetch/wikidata/vital-places/entities.ts +20 -2
  37. package/lib/tools/fetch/wikidata/vital-places/qrank.ts +5 -4
  38. package/lib/tools/fragment-assay.run.ts +2 -1
  39. package/lib/tools/name-roles/name-roles.run.ts +296 -0
  40. package/lib/tools/name-roles/prompt.ts +253 -0
  41. package/lib/tools/overlay/deletions.ts +1 -1
  42. package/lib/tools/overlay/draw-weight.ts +275 -0
  43. package/lib/tools/overlay/manifest.ts +36 -5
  44. package/lib/tools/place-twins/place-twins.run.ts +528 -0
  45. package/lib/tools/place-twins/prompt.ts +276 -0
  46. package/lib/tools/prominent-shapes/prominent-shapes.run.ts +420 -0
  47. package/lib/tools/prominent-shapes/prompt.ts +189 -0
  48. package/lib/tools/prominent-shapes/read-seeds.ts +286 -0
  49. package/lib/tools/prominent-shapes/seeds.ts +281 -0
  50. package/lib/tools/prominent-shapes/validate.ts +563 -0
  51. package/lib/training/config/admission.ts +9 -2
  52. package/lib/training/config/schema.ts +84 -0
  53. package/lib/types.ts +23 -0
  54. package/lib/us/adapters/usgov/nad/adapter.ts +8 -3
  55. package/lib/utils/split.ts +1 -1
  56. package/out/adapters/wof/admin/json/adapter.d.ts +7 -0
  57. package/out/adapters/wof/admin/json/adapter.d.ts.map +1 -1
  58. package/out/adapters/wof/admin/json/adapter.js +28 -3
  59. package/out/adapters/wof/admin/json/adapter.js.map +1 -1
  60. package/out/adapters/wof/json-rows.d.ts +8 -0
  61. package/out/adapters/wof/json-rows.d.ts.map +1 -1
  62. package/out/adapters/wof/json-rows.js +56 -4
  63. package/out/adapters/wof/json-rows.js.map +1 -1
  64. package/out/adapters/wof/sample.d.ts +12 -0
  65. package/out/adapters/wof/sample.d.ts.map +1 -1
  66. package/out/adapters/wof/sample.js +37 -6
  67. package/out/adapters/wof/sample.js.map +1 -1
  68. package/out/build/code-hash.js +1 -1
  69. package/out/build/code-hash.js.map +1 -1
  70. package/out/cjk/foreign-places.js +1 -1
  71. package/out/cjk/foreign-places.js.map +1 -1
  72. package/out/kr/adapters/localdata/permit-rows.js +1 -1
  73. package/out/kr/adapters/localdata/permit-rows.js.map +1 -1
  74. package/out/parquet/schema.d.ts +6 -0
  75. package/out/parquet/schema.d.ts.map +1 -1
  76. package/out/parquet/schema.js +6 -0
  77. package/out/parquet/schema.js.map +1 -1
  78. package/out/tools/anthropic/batches.d.ts +117 -0
  79. package/out/tools/anthropic/batches.d.ts.map +1 -0
  80. package/out/tools/anthropic/batches.js +141 -0
  81. package/out/tools/anthropic/batches.js.map +1 -0
  82. package/out/tools/anthropic/labeling.d.ts +97 -0
  83. package/out/tools/anthropic/labeling.d.ts.map +1 -0
  84. package/out/tools/anthropic/labeling.js +146 -0
  85. package/out/tools/anthropic/labeling.js.map +1 -0
  86. package/out/tools/compact-v9.d.ts +117 -0
  87. package/out/tools/compact-v9.d.ts.map +1 -0
  88. package/out/tools/compact-v9.js +494 -0
  89. package/out/tools/compact-v9.js.map +1 -0
  90. package/out/tools/fetch/wikidata/vital-places/entities.d.ts +3 -0
  91. package/out/tools/fetch/wikidata/vital-places/entities.d.ts.map +1 -1
  92. package/out/tools/fetch/wikidata/vital-places/entities.js +19 -1
  93. package/out/tools/fetch/wikidata/vital-places/entities.js.map +1 -1
  94. package/out/tools/fetch/wikidata/vital-places/qrank.d.ts.map +1 -1
  95. package/out/tools/fetch/wikidata/vital-places/qrank.js +4 -4
  96. package/out/tools/fetch/wikidata/vital-places/qrank.js.map +1 -1
  97. package/out/tools/fragment-assay.run.js +1 -1
  98. package/out/tools/fragment-assay.run.js.map +1 -1
  99. package/out/tools/name-roles/name-roles.run.d.ts +2 -0
  100. package/out/tools/name-roles/name-roles.run.d.ts.map +1 -0
  101. package/out/tools/name-roles/name-roles.run.js +230 -0
  102. package/out/tools/name-roles/name-roles.run.js.map +1 -0
  103. package/out/tools/name-roles/prompt.d.ts +166 -0
  104. package/out/tools/name-roles/prompt.d.ts.map +1 -0
  105. package/out/tools/name-roles/prompt.js +176 -0
  106. package/out/tools/name-roles/prompt.js.map +1 -0
  107. package/out/tools/overlay/deletions.js +1 -1
  108. package/out/tools/overlay/deletions.js.map +1 -1
  109. package/out/tools/overlay/draw-weight.d.ts +86 -0
  110. package/out/tools/overlay/draw-weight.d.ts.map +1 -0
  111. package/out/tools/overlay/draw-weight.js +131 -0
  112. package/out/tools/overlay/draw-weight.js.map +1 -0
  113. package/out/tools/overlay/manifest.d.ts.map +1 -1
  114. package/out/tools/overlay/manifest.js +29 -5
  115. package/out/tools/overlay/manifest.js.map +1 -1
  116. package/out/tools/place-twins/place-twins.run.d.ts +2 -0
  117. package/out/tools/place-twins/place-twins.run.d.ts.map +1 -0
  118. package/out/tools/place-twins/place-twins.run.js +385 -0
  119. package/out/tools/place-twins/place-twins.run.js.map +1 -0
  120. package/out/tools/place-twins/prompt.d.ts +169 -0
  121. package/out/tools/place-twins/prompt.d.ts.map +1 -0
  122. package/out/tools/place-twins/prompt.js +183 -0
  123. package/out/tools/place-twins/prompt.js.map +1 -0
  124. package/out/tools/prominent-shapes/prominent-shapes.run.d.ts +2 -0
  125. package/out/tools/prominent-shapes/prominent-shapes.run.d.ts.map +1 -0
  126. package/out/tools/prominent-shapes/prominent-shapes.run.js +302 -0
  127. package/out/tools/prominent-shapes/prominent-shapes.run.js.map +1 -0
  128. package/out/tools/prominent-shapes/prompt.d.ts +74 -0
  129. package/out/tools/prominent-shapes/prompt.d.ts.map +1 -0
  130. package/out/tools/prominent-shapes/prompt.js +156 -0
  131. package/out/tools/prominent-shapes/prompt.js.map +1 -0
  132. package/out/tools/prominent-shapes/read-seeds.d.ts +23 -0
  133. package/out/tools/prominent-shapes/read-seeds.d.ts.map +1 -0
  134. package/out/tools/prominent-shapes/read-seeds.js +219 -0
  135. package/out/tools/prominent-shapes/read-seeds.js.map +1 -0
  136. package/out/tools/prominent-shapes/seeds.d.ts +132 -0
  137. package/out/tools/prominent-shapes/seeds.d.ts.map +1 -0
  138. package/out/tools/prominent-shapes/seeds.js +178 -0
  139. package/out/tools/prominent-shapes/seeds.js.map +1 -0
  140. package/out/tools/prominent-shapes/validate.d.ts +152 -0
  141. package/out/tools/prominent-shapes/validate.d.ts.map +1 -0
  142. package/out/tools/prominent-shapes/validate.js +389 -0
  143. package/out/tools/prominent-shapes/validate.js.map +1 -0
  144. package/out/training/config/admission.d.ts +2 -1
  145. package/out/training/config/admission.d.ts.map +1 -1
  146. package/out/training/config/admission.js +9 -2
  147. package/out/training/config/admission.js.map +1 -1
  148. package/out/training/config/schema.d.ts +321 -0
  149. package/out/training/config/schema.d.ts.map +1 -1
  150. package/out/training/config/schema.js +76 -0
  151. package/out/training/config/schema.js.map +1 -1
  152. package/out/types.d.ts +21 -0
  153. package/out/types.d.ts.map +1 -1
  154. package/out/types.js +13 -0
  155. package/out/types.js.map +1 -1
  156. package/out/us/adapters/usgov/nad/adapter.d.ts +4 -0
  157. package/out/us/adapters/usgov/nad/adapter.d.ts.map +1 -1
  158. package/out/us/adapters/usgov/nad/adapter.js +8 -3
  159. package/out/us/adapters/usgov/nad/adapter.js.map +1 -1
  160. package/out/utils/split.js +1 -1
  161. package/out/utils/split.js.map +1 -1
  162. package/package.json +24 -24
@@ -0,0 +1,263 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * @file The submit, status and collect steps every batch labeling tool shares.
6
+ *
7
+ * A labeling tool splits its items into chunks, one request each, and the model returns one label per item.
8
+ * `submitLabelingBatch` writes `chunks.jsonl` and `batch.json` to the tool's output directory,
9
+ * and `collectLabelingBatch` joins the finished batch back to those chunks by `custom_id`.
10
+ * A request that failed, stopped at `max_tokens`, or returned labels the tool's parser refuses is listed as failed
11
+ * with its reason, and its items receive no label.
12
+ */
13
+
14
+ import { readLocalJSONFile } from "@mailwoman/core/fs/readers"
15
+ import { writeLocalJSONFile, writeLocalJSONLFile } from "@mailwoman/core/fs/writers"
16
+ import type { PathBuilder } from "path-ts"
17
+ import { JSONSpliterator } from "spliterator"
18
+
19
+ import {
20
+ batchCostUSD,
21
+ createAnthropicClient,
22
+ createMessage,
23
+ createMessageBatch,
24
+ readMessageBatch,
25
+ readMessageBatchResults,
26
+ resultText,
27
+ type MessageBatch,
28
+ type MessageBatchRequest,
29
+ type MessageBatchResult,
30
+ type MessageUsage,
31
+ type TokenPrice,
32
+ } from "#tools/anthropic/batches"
33
+
34
+ /**
35
+ * Dollars per million tokens on the batch endpoint, at half the standard price.
36
+ */
37
+ const BATCH_PRICES: Record<string, TokenPrice> = {
38
+ "claude-opus-5-5": { input: 2, output: 10 },
39
+ "claude-sonnet-5-5": { input: 1, output: 5 },
40
+ "claude-haiku-5-5": { input: 0.05, output: 0.25 },
41
+ }
42
+
43
+ /**
44
+ * Returns the batch price of `model`.
45
+ *
46
+ * @throws When no price is recorded for it, so a cost is never reported from a guessed rate.
47
+ */
48
+ export function batchPrice(model: string): TokenPrice {
49
+ const price = BATCH_PRICES[model]
50
+
51
+ if (!price) throw new Error(`No batch price is recorded for ${model}; add it to BATCH_PRICES`)
52
+
53
+ return price
54
+ }
55
+
56
+ /**
57
+ * The items one request labels.
58
+ */
59
+ export interface LabelingChunk<Item> {
60
+ custom_id: string
61
+ items: Item[]
62
+ }
63
+
64
+ /**
65
+ * What `submitLabelingBatch` records, so a later `status` or `collect` reads
66
+ * the settings the batch ran under.
67
+ */
68
+ export interface LabelingBatchRecord {
69
+ id: string
70
+ model: string
71
+ effort: string
72
+ requests: number
73
+ items: number
74
+ }
75
+
76
+ /**
77
+ * The model and effort a tool's requests run under.
78
+ */
79
+ export interface LabelingSettings {
80
+ model: string
81
+ effort: string
82
+ }
83
+
84
+ type ToParams<Item> = (items: readonly Item[], settings: LabelingSettings) => MessageBatchRequest["params"]
85
+
86
+ /**
87
+ * Parses one response into a label per item of its request, in item order,
88
+ * and throws on a response it refuses.
89
+ */
90
+ type ParseLabels<Item, Label> = (text: string, items: readonly Item[]) => Label[]
91
+
92
+ /**
93
+ * Splits `items` into requests of `size`, with ids `<prefix>-<n>`.
94
+ */
95
+ export function toLabelingChunks<Item>(items: readonly Item[], size: number, prefix: string): LabelingChunk<Item>[] {
96
+ if (!Number.isInteger(size) || size < 1)
97
+ throw new Error(`A chunk holds a whole number of items, and --chunk was ${size}`)
98
+
99
+ const chunks: LabelingChunk<Item>[] = []
100
+
101
+ for (let start = 0; start < items.length; start += size) {
102
+ chunks.push({ custom_id: `${prefix}-${chunks.length}`, items: items.slice(start, start + size) })
103
+ }
104
+
105
+ return chunks
106
+ }
107
+
108
+ /**
109
+ * Sends `count` evenly spaced chunks one at a time at the standard price.
110
+ *
111
+ * @returns Each chunk with its labels and token use, and the batch cost per request those requests imply.
112
+ * @throws When a response stops at `max_tokens` or the parser refuses it,
113
+ * since a batch of the same requests would fail the same way.
114
+ */
115
+ export async function pilotLabelingRequests<Item, Label>(
116
+ chunks: ReadonlyArray<LabelingChunk<Item>>,
117
+ count: number,
118
+ settings: LabelingSettings,
119
+ toParams: ToParams<Item>,
120
+ parse: ParseLabels<Item, Label>
121
+ ): Promise<{
122
+ requests: Array<{ chunk: LabelingChunk<Item>; labels: Label[]; usage: MessageUsage }>
123
+ batchCostPerRequestUSD: number
124
+ }> {
125
+ const client = createAnthropicClient()
126
+ const stride = Math.max(1, Math.floor(chunks.length / count))
127
+ const requests: Array<{ chunk: LabelingChunk<Item>; labels: Label[]; usage: MessageUsage }> = []
128
+ const results: MessageBatchResult[] = []
129
+
130
+ for (let n = 0; n < count && n * stride < chunks.length; n++) {
131
+ const chunk = chunks[n * stride]!
132
+ const result = await createMessage(client, { custom_id: chunk.custom_id, params: toParams(chunk.items, settings) })
133
+ const text = resultText(result)
134
+
135
+ if (text == null)
136
+ throw new Error(`${chunk.custom_id} stopped at ${result.result.message?.stop_reason ?? "no message"}`)
137
+
138
+ requests.push({ chunk, labels: parse(text, chunk.items), usage: result.result.message!.usage })
139
+ results.push(result)
140
+ }
141
+
142
+ return { requests, batchCostPerRequestUSD: batchCostUSD(results, batchPrice(settings.model)) / results.length }
143
+ }
144
+
145
+ /**
146
+ * Writes `chunks.jsonl`, creates one batch of the chunks, and records it in `batch.json`.
147
+ */
148
+ export async function submitLabelingBatch<Item>(
149
+ out: PathBuilder,
150
+ chunks: ReadonlyArray<LabelingChunk<Item>>,
151
+ settings: LabelingSettings,
152
+ toParams: ToParams<Item>
153
+ ): Promise<LabelingBatchRecord> {
154
+ batchPrice(settings.model)
155
+ await writeLocalJSONLFile(chunks, out("chunks.jsonl"))
156
+
157
+ const batch = await createMessageBatch(
158
+ createAnthropicClient(),
159
+ chunks.map((chunk) => ({ custom_id: chunk.custom_id, params: toParams(chunk.items, settings) }))
160
+ )
161
+
162
+ const record: LabelingBatchRecord = {
163
+ id: batch.id,
164
+ ...settings,
165
+ requests: chunks.length,
166
+ items: chunks.reduce((sum, chunk) => sum + chunk.items.length, 0),
167
+ }
168
+
169
+ await writeLocalJSONFile(record, out("batch.json"))
170
+
171
+ return record
172
+ }
173
+
174
+ /**
175
+ * Reads the state of the batch `batch.json` records.
176
+ */
177
+ export async function readLabelingBatch(out: PathBuilder): Promise<MessageBatch> {
178
+ const { id } = await readLocalJSONFile<LabelingBatchRecord>(out("batch.json"))
179
+
180
+ return readMessageBatch(createAnthropicClient(), id)
181
+ }
182
+
183
+ /**
184
+ * A request whose items received no label, and why.
185
+ */
186
+ export interface FailedChunk {
187
+ custom_id: string
188
+ reason: string
189
+ items: number
190
+ }
191
+
192
+ /**
193
+ * Joins the finished batch to its chunks and writes `failed-chunks.jsonl`.
194
+ *
195
+ * @returns Every labeled item beside its label, the failed requests, the result
196
+ * count per outcome type, and the dollars the batch cost.
197
+ * @throws When the batch is still in progress.
198
+ */
199
+ export async function collectLabelingBatch<Item, Label>(
200
+ out: PathBuilder,
201
+ parse: ParseLabels<Item, Label>
202
+ ): Promise<{
203
+ record: LabelingBatchRecord
204
+ labeled: Array<{ item: Item; label: Label }>
205
+ failed: FailedChunk[]
206
+ resultsByType: Record<string, number>
207
+ costUSD: number
208
+ }> {
209
+ const record = await readLocalJSONFile<LabelingBatchRecord>(out("batch.json"))
210
+ const chunks = await JSONSpliterator.fromAsync<LabelingChunk<Item>>(out("chunks.jsonl")).toArray()
211
+ const results = await readMessageBatchResults(createAnthropicClient(), record.id)
212
+ const byCustomID = new Map(results.map((result) => [result.custom_id, result]))
213
+ const labeled: Array<{ item: Item; label: Label }> = []
214
+ const failed: FailedChunk[] = []
215
+
216
+ for (const chunk of chunks) {
217
+ const result = byCustomID.get(chunk.custom_id)
218
+ const text = result ? resultText(result) : null
219
+
220
+ if (!result || text == null) {
221
+ const reason = result
222
+ ? `${result.result.type}:${result.result.message?.stop_reason ?? "no message"}`
223
+ : "no result line"
224
+
225
+ failed.push({ custom_id: chunk.custom_id, reason, items: chunk.items.length })
226
+
227
+ continue
228
+ }
229
+
230
+ try {
231
+ const labels = parse(text, chunk.items)
232
+
233
+ for (const [i, item] of chunk.items.entries()) {
234
+ labeled.push({ item, label: labels[i]! })
235
+ }
236
+ } catch (error) {
237
+ failed.push({ custom_id: chunk.custom_id, reason: (error as Error).message, items: chunk.items.length })
238
+ }
239
+ }
240
+
241
+ await writeLocalJSONLFile(failed, out("failed-chunks.jsonl"))
242
+
243
+ return {
244
+ record,
245
+ labeled,
246
+ failed,
247
+ resultsByType: countEach(results.map((result) => result.result.type)),
248
+ costUSD: Number(batchCostUSD(results, batchPrice(record.model)).toFixed(2)),
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Counts each distinct value.
254
+ */
255
+ export function countEach(values: Iterable<string>): Record<string, number> {
256
+ const counts: Record<string, number> = {}
257
+
258
+ for (const value of values) {
259
+ counts[value] = (counts[value] ?? 0) + 1
260
+ }
261
+
262
+ return counts
263
+ }