@exulu/backend 5.6.0 → 5.7.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/dist/{chunk-SJNP75II.js → chunk-TAXHJJ5R.js} +65 -7
- package/dist/{convert-exulu-tools-to-ai-sdk-tools-7TYZVHEQ.js → convert-exulu-tools-to-ai-sdk-tools-BPZSN22N.js} +1 -1
- package/dist/index.cjs +307 -38
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +212 -13
- package/ee/tokenizer.test.ts +178 -0
- package/ee/tokenizer.ts +80 -6
- package/package.json +1 -1
package/ee/tokenizer.ts
CHANGED
|
@@ -5,25 +5,93 @@ import models from "tiktoken/model_to_encoding.json" with { type: "json" };
|
|
|
5
5
|
|
|
6
6
|
export type TokenizerModelName = keyof typeof models;
|
|
7
7
|
|
|
8
|
+
type TiktokenBPE = Awaited<ReturnType<typeof load>>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The BPE ranks, cached for the lifetime of the process and keyed by ENCODING
|
|
12
|
+
* rather than by model — most model names share one encoding (every current
|
|
13
|
+
* gpt-3.5/gpt-4 name maps to cl100k_base), so keying by model would re-download
|
|
14
|
+
* the same bytes.
|
|
15
|
+
*
|
|
16
|
+
* Why this cache has to exist: tiktoken's `load()` holds no cache of its own
|
|
17
|
+
* (node_modules/tiktoken/load.cjs — a bare `fetch(url).then(r => r.text())`),
|
|
18
|
+
* and the ranks for cl100k_base are ~1.6MB fetched from
|
|
19
|
+
* openaipublic.blob.core.windows.net. `createAndUpsertEmbeddings` builds a
|
|
20
|
+
* chunker per item, so without this, embedding N items meant N downloads from
|
|
21
|
+
* a third-party host — the dominant cost of a bulk re-embed, and an undeclared
|
|
22
|
+
* hard dependency on that host being reachable.
|
|
23
|
+
*
|
|
24
|
+
* The PROMISE is cached, not just the result, so concurrent callers share one
|
|
25
|
+
* in-flight download instead of racing to start their own.
|
|
26
|
+
*/
|
|
27
|
+
const ranksByEncoding = new Map<string, Promise<TiktokenBPE>>();
|
|
28
|
+
|
|
29
|
+
const loadRanks = (encoding: string): Promise<TiktokenBPE> => {
|
|
30
|
+
const cached = ranksByEncoding.get(encoding);
|
|
31
|
+
if (cached) return cached;
|
|
32
|
+
|
|
33
|
+
const time = performance.now();
|
|
34
|
+
console.log("[EXULU] Loading tokenizer ranks.", encoding);
|
|
35
|
+
const pending = load(registry[encoding as keyof typeof registry])
|
|
36
|
+
.then((ranks): TiktokenBPE => {
|
|
37
|
+
console.log("[EXULU] Loaded tokenizer ranks.", encoding, performance.now() - time);
|
|
38
|
+
return ranks;
|
|
39
|
+
})
|
|
40
|
+
.catch((error: unknown) => {
|
|
41
|
+
// Never leave a rejected promise in the cache: a single transient
|
|
42
|
+
// timeout would otherwise fail every embed for the life of the process.
|
|
43
|
+
ranksByEncoding.delete(encoding);
|
|
44
|
+
throw error;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
ranksByEncoding.set(encoding, pending);
|
|
48
|
+
return pending;
|
|
49
|
+
};
|
|
50
|
+
|
|
8
51
|
export class ExuluTokenizer {
|
|
9
52
|
constructor() {}
|
|
10
53
|
|
|
11
54
|
public encoder: Tiktoken | null = null;
|
|
12
55
|
|
|
56
|
+
/** Remembered so `ensure()` can rebuild an encoder that `free()` released. */
|
|
57
|
+
private modelName: TokenizerModelName | null = null;
|
|
58
|
+
|
|
13
59
|
async create(modelName: TokenizerModelName): Promise<Tiktoken> {
|
|
60
|
+
this.modelName = modelName;
|
|
14
61
|
if (this.encoder) {
|
|
15
62
|
return this.encoder;
|
|
16
63
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
64
|
+
// A Tiktoken per instance, built from the shared ranks. Deliberately NOT a
|
|
65
|
+
// shared encoder: both chunkers call `tokenizer.free()` at the end of every
|
|
66
|
+
// chunk() (sentence.ts, recursive.ts), which would free a shared one out
|
|
67
|
+
// from under every other caller. Construction from cached ranks is the
|
|
68
|
+
// cheap half — the download was the expensive part.
|
|
69
|
+
const ranks = await loadRanks(models[modelName]);
|
|
70
|
+
const encoder = new Tiktoken(ranks.bpe_ranks, ranks.special_tokens, ranks.pat_str);
|
|
71
|
+
console.log("[EXULU] Set encoder.", modelName);
|
|
23
72
|
this.encoder = encoder;
|
|
24
73
|
return encoder;
|
|
25
74
|
}
|
|
26
75
|
|
|
76
|
+
/**
|
|
77
|
+
* The encoder, rebuilt if a previous `chunk()` freed it.
|
|
78
|
+
*
|
|
79
|
+
* Both chunkers free their tokenizer at the end of every `chunk()`, so an
|
|
80
|
+
* instance that is used twice — directly, or via `chunkBatch`, which calls
|
|
81
|
+
* `chunk()` once per text — needs its encoder back. Rebuilding is cheap:
|
|
82
|
+
* the ranks are already cached process-wide, so this costs a `Tiktoken`
|
|
83
|
+
* construction and no network.
|
|
84
|
+
*/
|
|
85
|
+
async ensure(): Promise<Tiktoken> {
|
|
86
|
+
if (this.encoder) {
|
|
87
|
+
return this.encoder;
|
|
88
|
+
}
|
|
89
|
+
if (!this.modelName) {
|
|
90
|
+
throw new Error("Tokenizer not initialized");
|
|
91
|
+
}
|
|
92
|
+
return this.create(this.modelName);
|
|
93
|
+
}
|
|
94
|
+
|
|
27
95
|
async decode(tokens: Uint32Array): Promise<string> {
|
|
28
96
|
if (!this.encoder) {
|
|
29
97
|
throw new Error("Tokenizer not initialized");
|
|
@@ -75,6 +143,12 @@ export class ExuluTokenizer {
|
|
|
75
143
|
console.log("[EXULU] Freeing tokenizer.");
|
|
76
144
|
if (this.encoder) {
|
|
77
145
|
this.encoder.free();
|
|
146
|
+
// Drop the handle along with the allocation. A freed Tiktoken is a
|
|
147
|
+
// dangling pointer into WASM memory, and both chunkers free after EVERY
|
|
148
|
+
// chunk(), so leaving it set made the next call on the same instance
|
|
149
|
+
// crash with "null pointer passed to rust". Null means `ensure()`
|
|
150
|
+
// rebuilds instead.
|
|
151
|
+
this.encoder = null;
|
|
78
152
|
}
|
|
79
153
|
}
|
|
80
154
|
}
|