pi-mega-compact 0.7.6 → 0.7.8
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/README.md +120 -31
- package/dist/extensions/dashboard-server.js +137 -4
- package/dist/extensions/dashboard-server.test.js +59 -0
- package/dist/extensions/mega-compact.test.js +221 -22
- package/dist/extensions/mega-config.js +10 -0
- package/dist/extensions/mega-events.js +64 -17
- package/dist/extensions/mega-pipeline.js +25 -1
- package/dist/extensions/mega-runtime.js +50 -5
- package/dist/src/store/sqlite.cachehit.test.js +55 -0
- package/dist/src/store/sqlite.js +23 -0
- package/extensions/dashboard-server.test.ts +69 -0
- package/extensions/dashboard-server.ts +141 -1
- package/extensions/mega-compact.test.ts +296 -35
- package/extensions/mega-config.ts +22 -0
- package/extensions/mega-dashboard.ts +17 -0
- package/extensions/mega-events.ts +66 -18
- package/extensions/mega-pipeline.ts +18 -1
- package/extensions/mega-runtime.ts +61 -5
- package/package.json +1 -1
- package/src/store/sqlite.cachehit.test.ts +68 -0
- package/src/store/sqlite.ts +28 -0
- package/dist/extensions/openclaw-mega-compact.js +0 -291
- package/dist/src/minilm.js +0 -92
- package/dist/src/wordpiece.js +0 -129
package/dist/src/wordpiece.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* wordpiece.ts — a self-contained WordPiece tokenizer for BERT/MiniLM.
|
|
3
|
-
*
|
|
4
|
-
* Loads the canonical `vocab.txt` (bert-base-uncased, ~30K tokens) from disk and
|
|
5
|
-
* implements the standard uncased BERT preprocessing + greedy longest-match
|
|
6
|
-
* WordPiece segmentation. No native dependency, no network — the vocab file is a
|
|
7
|
-
* local artifact fetched once by scripts/setup-minilm.mjs (PREVENT-PI-004).
|
|
8
|
-
*
|
|
9
|
-
* This mirrors HuggingFace `BertTokenizer` closely enough for sentence-embedding
|
|
10
|
-
* use: lowercase, strip accents, split on whitespace + punctuation, then
|
|
11
|
-
* WordPiece each token with the `##` continuation convention. Special tokens
|
|
12
|
-
* [CLS]/[SEP] are added by the caller's encode().
|
|
13
|
-
*/
|
|
14
|
-
import { readFileSync, existsSync } from "node:fs";
|
|
15
|
-
const UNK = "[UNK]";
|
|
16
|
-
const CLS = "[CLS]";
|
|
17
|
-
const SEP = "[SEP]";
|
|
18
|
-
const PAD = "[PAD]";
|
|
19
|
-
const MAX_INPUT_CHARS_PER_WORD = 200;
|
|
20
|
-
export class WordPieceTokenizer {
|
|
21
|
-
vocab;
|
|
22
|
-
clsId;
|
|
23
|
-
sepId;
|
|
24
|
-
padId;
|
|
25
|
-
unkId;
|
|
26
|
-
constructor(vocab) {
|
|
27
|
-
this.vocab = vocab;
|
|
28
|
-
this.clsId = vocab.get(CLS) ?? 101;
|
|
29
|
-
this.sepId = vocab.get(SEP) ?? 102;
|
|
30
|
-
this.padId = vocab.get(PAD) ?? 0;
|
|
31
|
-
this.unkId = vocab.get(UNK) ?? 100;
|
|
32
|
-
}
|
|
33
|
-
/** Build a tokenizer from a vocab.txt file (one token per line, index = line). */
|
|
34
|
-
static fromVocabFile(path) {
|
|
35
|
-
if (!existsSync(path)) {
|
|
36
|
-
throw new Error(`WordPiece vocab not found at ${path}. Run: node scripts/setup-minilm.mjs`);
|
|
37
|
-
}
|
|
38
|
-
const lines = readFileSync(path, "utf-8").split("\n");
|
|
39
|
-
const vocab = new Map();
|
|
40
|
-
for (let i = 0; i < lines.length; i++) {
|
|
41
|
-
const tok = lines[i].replace(/\r$/, "");
|
|
42
|
-
if (tok.length > 0 || i < lines.length - 1)
|
|
43
|
-
vocab.set(tok, i);
|
|
44
|
-
}
|
|
45
|
-
return new WordPieceTokenizer(vocab);
|
|
46
|
-
}
|
|
47
|
-
/** Uncased BERT basic tokenization: lowercase, strip accents, split on ws+punct. */
|
|
48
|
-
basicTokenize(text) {
|
|
49
|
-
// NFD + strip combining marks (accent removal), then lowercase.
|
|
50
|
-
const cleaned = text
|
|
51
|
-
.normalize("NFD")
|
|
52
|
-
.replace(/[̀-ͯ]/g, "")
|
|
53
|
-
.toLowerCase();
|
|
54
|
-
const tokens = [];
|
|
55
|
-
let buf = "";
|
|
56
|
-
const flush = () => {
|
|
57
|
-
if (buf.length > 0) {
|
|
58
|
-
tokens.push(buf);
|
|
59
|
-
buf = "";
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
for (const ch of cleaned) {
|
|
63
|
-
if (/\s/.test(ch)) {
|
|
64
|
-
flush();
|
|
65
|
-
}
|
|
66
|
-
else if (/[!-/:-@[-`{-~¡-¿]/.test(ch)) {
|
|
67
|
-
// Punctuation becomes its own token.
|
|
68
|
-
flush();
|
|
69
|
-
tokens.push(ch);
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
buf += ch;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
flush();
|
|
76
|
-
return tokens;
|
|
77
|
-
}
|
|
78
|
-
/** Greedy longest-match WordPiece for a single word. */
|
|
79
|
-
wordpiece(word) {
|
|
80
|
-
if (word.length > MAX_INPUT_CHARS_PER_WORD)
|
|
81
|
-
return [UNK];
|
|
82
|
-
const pieces = [];
|
|
83
|
-
let start = 0;
|
|
84
|
-
while (start < word.length) {
|
|
85
|
-
let end = word.length;
|
|
86
|
-
let cur = null;
|
|
87
|
-
while (start < end) {
|
|
88
|
-
let sub = word.slice(start, end);
|
|
89
|
-
if (start > 0)
|
|
90
|
-
sub = "##" + sub;
|
|
91
|
-
if (this.vocab.has(sub)) {
|
|
92
|
-
cur = sub;
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
end--;
|
|
96
|
-
}
|
|
97
|
-
if (cur === null)
|
|
98
|
-
return [UNK]; // any unmatchable piece → whole word is UNK
|
|
99
|
-
pieces.push(cur);
|
|
100
|
-
start = end;
|
|
101
|
-
}
|
|
102
|
-
return pieces;
|
|
103
|
-
}
|
|
104
|
-
/** Tokenize text into WordPiece token strings (no special tokens). */
|
|
105
|
-
tokenize(text) {
|
|
106
|
-
const out = [];
|
|
107
|
-
for (const word of this.basicTokenize(text)) {
|
|
108
|
-
for (const piece of this.wordpiece(word))
|
|
109
|
-
out.push(piece);
|
|
110
|
-
}
|
|
111
|
-
return out;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Encode text into model inputs with [CLS]…[SEP], truncated to `maxLen`.
|
|
115
|
-
* attention_mask is all 1s (no padding for single-sequence inference).
|
|
116
|
-
*/
|
|
117
|
-
encode(text, maxLen = 256) {
|
|
118
|
-
const pieces = this.tokenize(text).slice(0, Math.max(0, maxLen - 2));
|
|
119
|
-
const inputIds = [this.clsId];
|
|
120
|
-
for (const p of pieces)
|
|
121
|
-
inputIds.push(this.vocab.get(p) ?? this.unkId);
|
|
122
|
-
inputIds.push(this.sepId);
|
|
123
|
-
return {
|
|
124
|
-
inputIds,
|
|
125
|
-
attentionMask: inputIds.map(() => 1),
|
|
126
|
-
tokenTypeIds: inputIds.map(() => 0),
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
}
|