@goliapkg/tiktoken-wasm 5.1.0 → 5.2.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/README.md +10 -6
- package/package.json +2 -1
- package/snippets/tiktoken-wasm-3cf2b5020898fcbd/inline0.js +105 -0
- package/tiktoken_wasm.d.ts +14 -14
- package/tiktoken_wasm.js +103 -72
- package/tiktoken_wasm_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -13,22 +13,26 @@ WebAssembly bindings for the [tiktoken](https://crates.io/crates/tiktoken) BPE t
|
|
|
13
13
|
npm install @goliapkg/tiktoken-wasm
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
+
The module is built with WebAssembly SIMD (`simd128`) and needs Chrome 91,
|
|
17
|
+
Firefox 89 or Safari 16.4 and later; older browsers fail to compile it.
|
|
18
|
+
|
|
16
19
|
## Build from source
|
|
17
20
|
|
|
18
21
|
```bash
|
|
19
22
|
# requires wasm-pack: cargo install wasm-pack
|
|
20
23
|
cd tiktoken-wasm
|
|
21
|
-
wasm-pack build --target web --release
|
|
24
|
+
wasm-pack build --target web --release --scope goliapkg
|
|
22
25
|
```
|
|
23
26
|
|
|
24
27
|
Output is in `pkg/` — a complete npm-ready package containing:
|
|
25
28
|
- `tiktoken_wasm.js` — ES module with WASM loader
|
|
26
|
-
- `tiktoken_wasm_bg.wasm` — compiled WASM binary (8.
|
|
29
|
+
- `tiktoken_wasm_bg.wasm` — compiled WASM binary (8.65 MB, 8.01 MB gzipped, all 20 vocabularies)
|
|
30
|
+
- `tiktoken_wasm.d.ts` — TypeScript type definitions
|
|
31
|
+
- `snippets/` — a small JavaScript helper that moves text and token ids in and out of WASM memory
|
|
27
32
|
|
|
28
33
|
Building it yourself, `--no-default-features --features vocab-o200k_base` (or
|
|
29
34
|
any other `vocab-*` / vendor group forwarded from `tiktoken`) drops that to
|
|
30
|
-
1.
|
|
31
|
-
- `tiktoken_wasm.d.ts` — TypeScript type definitions
|
|
35
|
+
1.64 MB / 1.00 MB gzipped.
|
|
32
36
|
|
|
33
37
|
## Usage
|
|
34
38
|
|
|
@@ -68,7 +72,7 @@ const countST: number = enc.countWithSpecialTokens('hi<|endoftext|>bye')
|
|
|
68
72
|
console.log(enc.vocabSize) // 100256
|
|
69
73
|
console.log(enc.numSpecialTokens) // 5
|
|
70
74
|
|
|
71
|
-
// by model name —
|
|
75
|
+
// by model name — OpenAI, Meta, DeepSeek, Qwen, Mistral, Moonshot, Zhipu, MiniMax
|
|
72
76
|
const enc2 = encodingForModel('gpt-4o')
|
|
73
77
|
const encName = modelToEncoding('llama-4-scout') // "llama4"
|
|
74
78
|
|
|
@@ -126,7 +130,7 @@ module.exports = {
|
|
|
126
130
|
|
|
127
131
|
### `listEncodings(): string[]`
|
|
128
132
|
|
|
129
|
-
List all available encoding names (
|
|
133
|
+
List all available encoding names (20 encodings).
|
|
130
134
|
|
|
131
135
|
### `getEncoding(name: string): Encoding`
|
|
132
136
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@goliapkg/tiktoken-wasm",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"description": "WASM bindings for the tiktoken BPE tokenizer",
|
|
5
|
-
"version": "5.
|
|
5
|
+
"version": "5.2.0",
|
|
6
6
|
"license": "MIT OR Apache-2.0",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"tiktoken_wasm_bg.wasm",
|
|
13
13
|
"tiktoken_wasm.js",
|
|
14
14
|
"tiktoken_wasm.d.ts",
|
|
15
|
+
"snippets",
|
|
15
16
|
"LICENSE-MIT",
|
|
16
17
|
"LICENSE-APACHE",
|
|
17
18
|
"LICENSE-3RD-PARTY",
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
|
|
2
|
+
const te = new TextEncoder();
|
|
3
|
+
let mem = null, u8 = null, u16 = null, u32 = null;
|
|
4
|
+
function views() {
|
|
5
|
+
u8 = new Uint8Array(mem.buffer);
|
|
6
|
+
u16 = new Uint16Array(mem.buffer);
|
|
7
|
+
u32 = new Uint32Array(mem.buffer);
|
|
8
|
+
}
|
|
9
|
+
// the module in the comment at the top of this file, base64
|
|
10
|
+
const COPY = 'AGFzbQEAAAABFwRedwFgAW8Bf2ADb2MAfwF/YAJvfwF/AkgDAW0GbWVtb3J5AgAADndhc206anMtc3RyaW5nBmxlbmd0aAABDndhc206anMtc3RyaW5nEWludG9DaGFyQ29kZUFycmF5AAIDAgEDBgoBZAABQQD7BwALBwgBBGNvcHkAAgqSAQGPAQICfwFkACAAEAAhAiMAIQQgBPsPIAJBBGpJBEAgAkGACGr7BwAhBCAEJAALIAAgBEEAEAEaAkADQCADIAJPDQEgASADQQF0aiAEIAP7DQCtIAQgA0EBavsNAK1CEIaEIAQgA0ECavsNAK1CIIYgBCADQQNq+w0ArUIwhoSENwMAIANBBGohAwwACwALIAIL';
|
|
11
|
+
// copyUnits(s, p16) writes the code units of s at p16 plus up to 3 more; null
|
|
12
|
+
// where the engine lacks the builtins or GC arrays
|
|
13
|
+
let copyUnits = null;
|
|
14
|
+
export function tk_bind(m) {
|
|
15
|
+
mem = m;
|
|
16
|
+
views();
|
|
17
|
+
try {
|
|
18
|
+
const bytes = Uint8Array.from(atob(COPY), (c) => c.charCodeAt(0));
|
|
19
|
+
const mod = new WebAssembly.Module(bytes, { builtins: ['js-string'] });
|
|
20
|
+
// an engine that applied the builtins leaves only the memory import
|
|
21
|
+
if (WebAssembly.Module.imports(mod).length === 1) {
|
|
22
|
+
copyUnits = new WebAssembly.Instance(mod, { m: { memory: m } }).exports.copy;
|
|
23
|
+
}
|
|
24
|
+
} catch {
|
|
25
|
+
copyUnits = null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// whether TextEncoder.encode plus a copy beats encodeInto, timed once on
|
|
29
|
+
// 1 KB of ASCII: JavaScriptCore's encodeInto runs about 20 times slower
|
|
30
|
+
// than its encode, V8's about 1.6 times faster
|
|
31
|
+
let copyEncoded = null;
|
|
32
|
+
function pick() {
|
|
33
|
+
if (typeof performance === 'undefined') return false;
|
|
34
|
+
const s = 'The quick brown fox jumps over the lazy dog. '.repeat(24);
|
|
35
|
+
const buf = new Uint8Array(4096);
|
|
36
|
+
const time = (f) => {
|
|
37
|
+
const t0 = performance.now();
|
|
38
|
+
let k = 0, t = 0;
|
|
39
|
+
do {
|
|
40
|
+
for (let i = 0; i < 16; i++) f();
|
|
41
|
+
k += 16;
|
|
42
|
+
t = performance.now() - t0;
|
|
43
|
+
} while (t < 1);
|
|
44
|
+
return t / k;
|
|
45
|
+
};
|
|
46
|
+
const into = time(() => te.encodeInto(s, buf));
|
|
47
|
+
const copy = time(() => buf.set(te.encode(s)));
|
|
48
|
+
return copy < into * 0.8;
|
|
49
|
+
}
|
|
50
|
+
// mostly chars of two or more utf-8 bytes, judged from 16 samples
|
|
51
|
+
function wide(s, n) {
|
|
52
|
+
const step = n >>> 4;
|
|
53
|
+
let w = 0;
|
|
54
|
+
for (let i = 0; i < 16; i++) if (s.charCodeAt(i * step) > 0x7f) w++;
|
|
55
|
+
return w >= 10;
|
|
56
|
+
}
|
|
57
|
+
// 2 * length of the utf-8 written at p8, or 2 * units + 1 of the utf-16
|
|
58
|
+
// written at p16; -1 - units when the buffers are too small
|
|
59
|
+
export function tk_read(s, p8, c8, p16, c16) {
|
|
60
|
+
if (typeof s !== 'string') throw new TypeError('text must be a string');
|
|
61
|
+
const n = s.length;
|
|
62
|
+
// 16: the slack the utf-16 conversion may write past its output; 4: the
|
|
63
|
+
// units copy may write past the text
|
|
64
|
+
if (n * 3 + 16 > c8 || n + 4 > c16) return -1 - n;
|
|
65
|
+
// memory.grow detaches the old buffer
|
|
66
|
+
if (u8.byteLength === 0) views();
|
|
67
|
+
if (n < 32) {
|
|
68
|
+
let i = 0;
|
|
69
|
+
for (; i < n; i++) {
|
|
70
|
+
const c = s.charCodeAt(i);
|
|
71
|
+
if (c > 0x7f) break;
|
|
72
|
+
u8[p8 + i] = c;
|
|
73
|
+
}
|
|
74
|
+
if (i === n) return 2 * n;
|
|
75
|
+
return 2 * (i + te.encodeInto(s.slice(i), u8.subarray(p8 + i, p8 + c8)).written);
|
|
76
|
+
}
|
|
77
|
+
if (n >= 64 && wide(s, n)) {
|
|
78
|
+
if (copyUnits !== null) {
|
|
79
|
+
copyUnits(s, p16);
|
|
80
|
+
return 2 * n + 1;
|
|
81
|
+
}
|
|
82
|
+
const v = u16, at = p16 >>> 1;
|
|
83
|
+
let i = 0;
|
|
84
|
+
for (; i + 4 <= n; i += 4) {
|
|
85
|
+
v[at + i] = s.charCodeAt(i);
|
|
86
|
+
v[at + i + 1] = s.charCodeAt(i + 1);
|
|
87
|
+
v[at + i + 2] = s.charCodeAt(i + 2);
|
|
88
|
+
v[at + i + 3] = s.charCodeAt(i + 3);
|
|
89
|
+
}
|
|
90
|
+
for (; i < n; i++) v[at + i] = s.charCodeAt(i);
|
|
91
|
+
return 2 * n + 1;
|
|
92
|
+
}
|
|
93
|
+
if (copyEncoded === null) copyEncoded = pick();
|
|
94
|
+
if (copyEncoded) {
|
|
95
|
+
const b = te.encode(s);
|
|
96
|
+
u8.set(b, p8);
|
|
97
|
+
return 2 * b.length;
|
|
98
|
+
}
|
|
99
|
+
return 2 * te.encodeInto(s, u8.subarray(p8, p8 + c8)).written;
|
|
100
|
+
}
|
|
101
|
+
export function tk_ids(ptr, len) {
|
|
102
|
+
if (u32.byteLength === 0) views();
|
|
103
|
+
const at = ptr >>> 2;
|
|
104
|
+
return u32.slice(at, at + len);
|
|
105
|
+
}
|
package/tiktoken_wasm.d.ts
CHANGED
|
@@ -84,7 +84,7 @@ export function allModels(): ModelInfo[];
|
|
|
84
84
|
/**
|
|
85
85
|
* Get an encoding for a model name (e.g. `"gpt-4o"`, `"o3-mini"`, `"llama-4"`, `"deepseek-r1"`).
|
|
86
86
|
*
|
|
87
|
-
* Supports models from OpenAI, Meta, DeepSeek, Qwen, and
|
|
87
|
+
* Supports models from OpenAI, Meta, DeepSeek, Qwen, Mistral, Moonshot, Zhipu and MiniMax.
|
|
88
88
|
* Automatically resolves the model name to the correct encoding.
|
|
89
89
|
* Throws `Error` for unknown model names.
|
|
90
90
|
*/
|
|
@@ -93,7 +93,7 @@ export function encodingForModel(model: string): Encoding;
|
|
|
93
93
|
/**
|
|
94
94
|
* Estimate cost in USD for a given model, input token count, and output token count.
|
|
95
95
|
*
|
|
96
|
-
* Supports OpenAI, Anthropic
|
|
96
|
+
* Supports 153 models across 11 providers: OpenAI, Anthropic, Google, Meta, DeepSeek, Alibaba, Mistral, Moonshot, Zhipu, MiniMax and Voyage models.
|
|
97
97
|
* Throws `Error` for unknown model ids.
|
|
98
98
|
*/
|
|
99
99
|
export function estimateCost(model_id: string, input_tokens: number, output_tokens: number): number;
|
|
@@ -103,7 +103,7 @@ export function estimateCost(model_id: string, input_tokens: number, output_toke
|
|
|
103
103
|
*
|
|
104
104
|
* Supported encodings:
|
|
105
105
|
* - `"cl100k_base"` — GPT-4, GPT-3.5-turbo
|
|
106
|
-
* - `"o200k_base"` — GPT-4o, GPT-4.1, GPT-5.x, GPT-6.x, o1, o3
|
|
106
|
+
* - `"o200k_base"` — GPT-4o, GPT-4.1, GPT-5.x, GPT-6.x (unconfirmed), o1, o3, o4-mini
|
|
107
107
|
* - `"o200k_harmony"` — gpt-oss (harmony chat format)
|
|
108
108
|
* - `"p50k_base"` — text-davinci-002/003
|
|
109
109
|
* - `"p50k_edit"` — text-davinci-edit
|
|
@@ -113,7 +113,7 @@ export function estimateCost(model_id: string, input_tokens: number, output_toke
|
|
|
113
113
|
* - `"llama4"` — Meta Llama 4, Muse
|
|
114
114
|
* - `"deepseek_v3"` — DeepSeek V3/R1
|
|
115
115
|
* - `"qwen2"` — Qwen 2/2.5/3
|
|
116
|
-
* - `"qwen3_5"` — Qwen 3.5–3.8
|
|
116
|
+
* - `"qwen3_5"` — Qwen 3.5–3.8, Qwen 4 (unconfirmed)
|
|
117
117
|
* - `"mistral_v3"` — Mistral/Codestral/Pixtral
|
|
118
118
|
* - `"mistral_v15"` — Mistral Large 4
|
|
119
119
|
* - `"deepseek_v4"`, `"kimi_k2"`, `"kimi_k3"`, `"glm4"`, `"glm5"`, `"minimax_m2"`
|
|
@@ -160,6 +160,8 @@ export function modelToEncoding(model: string): string | undefined;
|
|
|
160
160
|
*/
|
|
161
161
|
export function modelsByProvider(provider: string): ModelInfo[];
|
|
162
162
|
|
|
163
|
+
export function start(): void;
|
|
164
|
+
|
|
163
165
|
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
164
166
|
|
|
165
167
|
export interface InitOutput {
|
|
@@ -168,11 +170,11 @@ export interface InitOutput {
|
|
|
168
170
|
readonly __wbg_modelinfo_free: (a: number, b: number) => void;
|
|
169
171
|
readonly allModels: (a: number) => void;
|
|
170
172
|
readonly encodingForModel: (a: number, b: number, c: number) => void;
|
|
171
|
-
readonly encoding_count: (a: number, b: number
|
|
172
|
-
readonly encoding_countWithSpecialTokens: (a: number, b: number
|
|
173
|
+
readonly encoding_count: (a: number, b: number) => number;
|
|
174
|
+
readonly encoding_countWithSpecialTokens: (a: number, b: number) => number;
|
|
173
175
|
readonly encoding_decode: (a: number, b: number, c: number, d: number) => void;
|
|
174
|
-
readonly encoding_encode: (a: number, b: number
|
|
175
|
-
readonly encoding_encodeWithSpecialTokens: (a: number, b: number
|
|
176
|
+
readonly encoding_encode: (a: number, b: number) => number;
|
|
177
|
+
readonly encoding_encodeWithSpecialTokens: (a: number, b: number) => number;
|
|
176
178
|
readonly encoding_name: (a: number, b: number) => void;
|
|
177
179
|
readonly encoding_numSpecialTokens: (a: number) => number;
|
|
178
180
|
readonly encoding_vocabSize: (a: number) => number;
|
|
@@ -189,13 +191,11 @@ export interface InitOutput {
|
|
|
189
191
|
readonly modelinfo_outputPer1m: (a: number) => number;
|
|
190
192
|
readonly modelinfo_provider: (a: number, b: number) => void;
|
|
191
193
|
readonly modelsByProvider: (a: number, b: number, c: number) => void;
|
|
192
|
-
readonly
|
|
194
|
+
readonly start: () => void;
|
|
193
195
|
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
|
|
194
|
-
readonly __wbindgen_export: (a: number, b: number) => void;
|
|
195
|
-
readonly __wbindgen_export2: (a: number, b: number
|
|
196
|
-
readonly __wbindgen_export3: (a: number, b: number) => number;
|
|
197
|
-
readonly __wbindgen_export4: (a: number, b: number, c: number, d: number) => number;
|
|
198
|
-
readonly __wbindgen_export5: (a: number) => void;
|
|
196
|
+
readonly __wbindgen_export: (a: number, b: number, c: number) => void;
|
|
197
|
+
readonly __wbindgen_export2: (a: number, b: number) => number;
|
|
198
|
+
readonly __wbindgen_export3: (a: number, b: number, c: number, d: number) => number;
|
|
199
199
|
readonly __wbindgen_start: () => void;
|
|
200
200
|
}
|
|
201
201
|
|
package/tiktoken_wasm.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/* @ts-self-types="./tiktoken_wasm.d.ts" */
|
|
2
|
+
import { tk_bind, tk_ids, tk_read } from './snippets/tiktoken-wasm-3cf2b5020898fcbd/inline0.js';
|
|
3
|
+
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* WASM wrapper around a tiktoken encoding instance.
|
|
@@ -32,10 +34,13 @@ export class Encoding {
|
|
|
32
34
|
* @returns {number}
|
|
33
35
|
*/
|
|
34
36
|
countWithSpecialTokens(text) {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
const borrow0 = addBorrowedObject(text);
|
|
38
|
+
try {
|
|
39
|
+
const ret = wasm.encoding_countWithSpecialTokens(this.__wbg_ptr, borrow0);
|
|
40
|
+
return ret >>> 0;
|
|
41
|
+
} finally {
|
|
42
|
+
heap[stack_pointer++] = undefined;
|
|
43
|
+
}
|
|
39
44
|
}
|
|
40
45
|
/**
|
|
41
46
|
* Count tokens without building the full token id array.
|
|
@@ -45,10 +50,13 @@ export class Encoding {
|
|
|
45
50
|
* @returns {number}
|
|
46
51
|
*/
|
|
47
52
|
count(text) {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
53
|
+
const borrow0 = addBorrowedObject(text);
|
|
54
|
+
try {
|
|
55
|
+
const ret = wasm.encoding_count(this.__wbg_ptr, borrow0);
|
|
56
|
+
return ret >>> 0;
|
|
57
|
+
} finally {
|
|
58
|
+
heap[stack_pointer++] = undefined;
|
|
59
|
+
}
|
|
52
60
|
}
|
|
53
61
|
/**
|
|
54
62
|
* Decode token ids back to a UTF-8 string.
|
|
@@ -62,7 +70,7 @@ export class Encoding {
|
|
|
62
70
|
let deferred2_1;
|
|
63
71
|
try {
|
|
64
72
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16) >>> 0;
|
|
65
|
-
const ptr0 = passArray32ToWasm0(tokens, wasm.
|
|
73
|
+
const ptr0 = passArray32ToWasm0(tokens, wasm.__wbindgen_export2);
|
|
66
74
|
const len0 = WASM_VECTOR_LEN;
|
|
67
75
|
wasm.encoding_decode(retptr, this.__wbg_ptr, ptr0, len0);
|
|
68
76
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
@@ -72,7 +80,7 @@ export class Encoding {
|
|
|
72
80
|
return getStringFromWasm0(r0, r1);
|
|
73
81
|
} finally {
|
|
74
82
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
75
|
-
wasm.
|
|
83
|
+
wasm.__wbindgen_export(deferred2_0, deferred2_1, 1);
|
|
76
84
|
}
|
|
77
85
|
}
|
|
78
86
|
/**
|
|
@@ -84,18 +92,12 @@ export class Encoding {
|
|
|
84
92
|
* @returns {Uint32Array}
|
|
85
93
|
*/
|
|
86
94
|
encodeWithSpecialTokens(text) {
|
|
95
|
+
const borrow0 = addBorrowedObject(text);
|
|
87
96
|
try {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
const len0 = WASM_VECTOR_LEN;
|
|
91
|
-
wasm.encoding_encodeWithSpecialTokens(retptr, this.__wbg_ptr, ptr0, len0);
|
|
92
|
-
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
93
|
-
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
94
|
-
var v2 = getArrayU32FromWasm0(r0, r1).slice();
|
|
95
|
-
wasm.__wbindgen_export2(r0, r1 * 4, 4);
|
|
96
|
-
return v2;
|
|
97
|
+
const ret = wasm.encoding_encodeWithSpecialTokens(this.__wbg_ptr, borrow0);
|
|
98
|
+
return takeObject(ret);
|
|
97
99
|
} finally {
|
|
98
|
-
|
|
100
|
+
heap[stack_pointer++] = undefined;
|
|
99
101
|
}
|
|
100
102
|
}
|
|
101
103
|
/**
|
|
@@ -107,18 +109,12 @@ export class Encoding {
|
|
|
107
109
|
* @returns {Uint32Array}
|
|
108
110
|
*/
|
|
109
111
|
encode(text) {
|
|
112
|
+
const borrow0 = addBorrowedObject(text);
|
|
110
113
|
try {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
const len0 = WASM_VECTOR_LEN;
|
|
114
|
-
wasm.encoding_encode(retptr, this.__wbg_ptr, ptr0, len0);
|
|
115
|
-
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
116
|
-
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
117
|
-
var v2 = getArrayU32FromWasm0(r0, r1).slice();
|
|
118
|
-
wasm.__wbindgen_export2(r0, r1 * 4, 4);
|
|
119
|
-
return v2;
|
|
114
|
+
const ret = wasm.encoding_encode(this.__wbg_ptr, borrow0);
|
|
115
|
+
return takeObject(ret);
|
|
120
116
|
} finally {
|
|
121
|
-
|
|
117
|
+
heap[stack_pointer++] = undefined;
|
|
122
118
|
}
|
|
123
119
|
}
|
|
124
120
|
/**
|
|
@@ -138,7 +134,7 @@ export class Encoding {
|
|
|
138
134
|
return getStringFromWasm0(r0, r1);
|
|
139
135
|
} finally {
|
|
140
136
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
141
|
-
wasm.
|
|
137
|
+
wasm.__wbindgen_export(deferred1_0, deferred1_1, 1);
|
|
142
138
|
}
|
|
143
139
|
}
|
|
144
140
|
/**
|
|
@@ -217,7 +213,7 @@ export class ModelInfo {
|
|
|
217
213
|
return getStringFromWasm0(r0, r1);
|
|
218
214
|
} finally {
|
|
219
215
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
220
|
-
wasm.
|
|
216
|
+
wasm.__wbindgen_export(deferred1_0, deferred1_1, 1);
|
|
221
217
|
}
|
|
222
218
|
}
|
|
223
219
|
/**
|
|
@@ -257,7 +253,7 @@ export class ModelInfo {
|
|
|
257
253
|
return getStringFromWasm0(r0, r1);
|
|
258
254
|
} finally {
|
|
259
255
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
260
|
-
wasm.
|
|
256
|
+
wasm.__wbindgen_export(deferred1_0, deferred1_1, 1);
|
|
261
257
|
}
|
|
262
258
|
}
|
|
263
259
|
}
|
|
@@ -276,7 +272,7 @@ export function allModels() {
|
|
|
276
272
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
277
273
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
278
274
|
var v1 = getArrayJsValueFromWasm0(r0, r1);
|
|
279
|
-
wasm.
|
|
275
|
+
wasm.__wbindgen_export(r0, r1 * 4, 4);
|
|
280
276
|
return v1;
|
|
281
277
|
} finally {
|
|
282
278
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
@@ -286,7 +282,7 @@ export function allModels() {
|
|
|
286
282
|
/**
|
|
287
283
|
* Get an encoding for a model name (e.g. `"gpt-4o"`, `"o3-mini"`, `"llama-4"`, `"deepseek-r1"`).
|
|
288
284
|
*
|
|
289
|
-
* Supports models from OpenAI, Meta, DeepSeek, Qwen, and
|
|
285
|
+
* Supports models from OpenAI, Meta, DeepSeek, Qwen, Mistral, Moonshot, Zhipu and MiniMax.
|
|
290
286
|
* Automatically resolves the model name to the correct encoding.
|
|
291
287
|
* Throws `Error` for unknown model names.
|
|
292
288
|
* @param {string} model
|
|
@@ -295,14 +291,14 @@ export function allModels() {
|
|
|
295
291
|
export function encodingForModel(model) {
|
|
296
292
|
try {
|
|
297
293
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16) >>> 0;
|
|
298
|
-
const ptr0 = passStringToWasm0(model, wasm.
|
|
294
|
+
const ptr0 = passStringToWasm0(model, wasm.__wbindgen_export2, wasm.__wbindgen_export3);
|
|
299
295
|
const len0 = WASM_VECTOR_LEN;
|
|
300
296
|
wasm.encodingForModel(retptr, ptr0, len0);
|
|
301
297
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
302
298
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
303
299
|
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
304
300
|
if (r2) {
|
|
305
|
-
throw
|
|
301
|
+
throw takeObject(r1);
|
|
306
302
|
}
|
|
307
303
|
return Encoding.__wrap(r0);
|
|
308
304
|
} finally {
|
|
@@ -313,7 +309,7 @@ export function encodingForModel(model) {
|
|
|
313
309
|
/**
|
|
314
310
|
* Estimate cost in USD for a given model, input token count, and output token count.
|
|
315
311
|
*
|
|
316
|
-
* Supports OpenAI, Anthropic
|
|
312
|
+
* Supports 153 models across 11 providers: OpenAI, Anthropic, Google, Meta, DeepSeek, Alibaba, Mistral, Moonshot, Zhipu, MiniMax and Voyage models.
|
|
317
313
|
* Throws `Error` for unknown model ids.
|
|
318
314
|
* @param {string} model_id
|
|
319
315
|
* @param {number} input_tokens
|
|
@@ -325,14 +321,14 @@ export function estimateCost(model_id, input_tokens, output_tokens) {
|
|
|
325
321
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16) >>> 0;
|
|
326
322
|
const val2 = +input_tokens;
|
|
327
323
|
const val3 = +output_tokens;
|
|
328
|
-
const ptr0 = passStringToWasm0(model_id, wasm.
|
|
324
|
+
const ptr0 = passStringToWasm0(model_id, wasm.__wbindgen_export2, wasm.__wbindgen_export3);
|
|
329
325
|
const len0 = WASM_VECTOR_LEN;
|
|
330
326
|
wasm.estimateCost(retptr, ptr0, len0, val2, val3);
|
|
331
327
|
var r0 = getDataViewMemory0().getFloat64(retptr + 8 * 0, true);
|
|
332
328
|
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
333
329
|
var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
|
|
334
330
|
if (r3) {
|
|
335
|
-
throw
|
|
331
|
+
throw takeObject(r2);
|
|
336
332
|
}
|
|
337
333
|
return r0;
|
|
338
334
|
} finally {
|
|
@@ -345,7 +341,7 @@ export function estimateCost(model_id, input_tokens, output_tokens) {
|
|
|
345
341
|
*
|
|
346
342
|
* Supported encodings:
|
|
347
343
|
* - `"cl100k_base"` — GPT-4, GPT-3.5-turbo
|
|
348
|
-
* - `"o200k_base"` — GPT-4o, GPT-4.1, GPT-5.x, GPT-6.x, o1, o3
|
|
344
|
+
* - `"o200k_base"` — GPT-4o, GPT-4.1, GPT-5.x, GPT-6.x (unconfirmed), o1, o3, o4-mini
|
|
349
345
|
* - `"o200k_harmony"` — gpt-oss (harmony chat format)
|
|
350
346
|
* - `"p50k_base"` — text-davinci-002/003
|
|
351
347
|
* - `"p50k_edit"` — text-davinci-edit
|
|
@@ -355,7 +351,7 @@ export function estimateCost(model_id, input_tokens, output_tokens) {
|
|
|
355
351
|
* - `"llama4"` — Meta Llama 4, Muse
|
|
356
352
|
* - `"deepseek_v3"` — DeepSeek V3/R1
|
|
357
353
|
* - `"qwen2"` — Qwen 2/2.5/3
|
|
358
|
-
* - `"qwen3_5"` — Qwen 3.5–3.8
|
|
354
|
+
* - `"qwen3_5"` — Qwen 3.5–3.8, Qwen 4 (unconfirmed)
|
|
359
355
|
* - `"mistral_v3"` — Mistral/Codestral/Pixtral
|
|
360
356
|
* - `"mistral_v15"` — Mistral Large 4
|
|
361
357
|
* - `"deepseek_v4"`, `"kimi_k2"`, `"kimi_k3"`, `"glm4"`, `"glm5"`, `"minimax_m2"`
|
|
@@ -367,14 +363,14 @@ export function estimateCost(model_id, input_tokens, output_tokens) {
|
|
|
367
363
|
export function getEncoding(name) {
|
|
368
364
|
try {
|
|
369
365
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16) >>> 0;
|
|
370
|
-
const ptr0 = passStringToWasm0(name, wasm.
|
|
366
|
+
const ptr0 = passStringToWasm0(name, wasm.__wbindgen_export2, wasm.__wbindgen_export3);
|
|
371
367
|
const len0 = WASM_VECTOR_LEN;
|
|
372
368
|
wasm.getEncoding(retptr, ptr0, len0);
|
|
373
369
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
374
370
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
375
371
|
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
376
372
|
if (r2) {
|
|
377
|
-
throw
|
|
373
|
+
throw takeObject(r1);
|
|
378
374
|
}
|
|
379
375
|
return Encoding.__wrap(r0);
|
|
380
376
|
} finally {
|
|
@@ -400,14 +396,14 @@ export function getEncoding(name) {
|
|
|
400
396
|
export function getModelInfo(model_id) {
|
|
401
397
|
try {
|
|
402
398
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16) >>> 0;
|
|
403
|
-
const ptr0 = passStringToWasm0(model_id, wasm.
|
|
399
|
+
const ptr0 = passStringToWasm0(model_id, wasm.__wbindgen_export2, wasm.__wbindgen_export3);
|
|
404
400
|
const len0 = WASM_VECTOR_LEN;
|
|
405
401
|
wasm.getModelInfo(retptr, ptr0, len0);
|
|
406
402
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
407
403
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
408
404
|
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
409
405
|
if (r2) {
|
|
410
|
-
throw
|
|
406
|
+
throw takeObject(r1);
|
|
411
407
|
}
|
|
412
408
|
return ModelInfo.__wrap(r0);
|
|
413
409
|
} finally {
|
|
@@ -428,7 +424,7 @@ export function listEncodings() {
|
|
|
428
424
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
429
425
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
430
426
|
var v1 = getArrayJsValueFromWasm0(r0, r1);
|
|
431
|
-
wasm.
|
|
427
|
+
wasm.__wbindgen_export(r0, r1 * 4, 4);
|
|
432
428
|
return v1;
|
|
433
429
|
} finally {
|
|
434
430
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
@@ -445,7 +441,7 @@ export function listEncodings() {
|
|
|
445
441
|
export function modelToEncoding(model) {
|
|
446
442
|
try {
|
|
447
443
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16) >>> 0;
|
|
448
|
-
const ptr0 = passStringToWasm0(model, wasm.
|
|
444
|
+
const ptr0 = passStringToWasm0(model, wasm.__wbindgen_export2, wasm.__wbindgen_export3);
|
|
449
445
|
const len0 = WASM_VECTOR_LEN;
|
|
450
446
|
wasm.modelToEncoding(retptr, ptr0, len0);
|
|
451
447
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
@@ -453,7 +449,7 @@ export function modelToEncoding(model) {
|
|
|
453
449
|
let v2;
|
|
454
450
|
if (r0 !== 0) {
|
|
455
451
|
v2 = getStringFromWasm0(r0, r1);
|
|
456
|
-
wasm.
|
|
452
|
+
wasm.__wbindgen_export(r0, r1 * 1, 1);
|
|
457
453
|
}
|
|
458
454
|
return v2;
|
|
459
455
|
} finally {
|
|
@@ -473,44 +469,55 @@ export function modelToEncoding(model) {
|
|
|
473
469
|
export function modelsByProvider(provider) {
|
|
474
470
|
try {
|
|
475
471
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16) >>> 0;
|
|
476
|
-
const ptr0 = passStringToWasm0(provider, wasm.
|
|
472
|
+
const ptr0 = passStringToWasm0(provider, wasm.__wbindgen_export2, wasm.__wbindgen_export3);
|
|
477
473
|
const len0 = WASM_VECTOR_LEN;
|
|
478
474
|
wasm.modelsByProvider(retptr, ptr0, len0);
|
|
479
475
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
480
476
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
481
477
|
var v2 = getArrayJsValueFromWasm0(r0, r1);
|
|
482
|
-
wasm.
|
|
478
|
+
wasm.__wbindgen_export(r0, r1 * 4, 4);
|
|
483
479
|
return v2;
|
|
484
480
|
} finally {
|
|
485
481
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
486
482
|
}
|
|
487
483
|
}
|
|
484
|
+
|
|
485
|
+
export function start() {
|
|
486
|
+
wasm.start();
|
|
487
|
+
}
|
|
488
488
|
function __wbg_get_imports() {
|
|
489
489
|
const import0 = {
|
|
490
490
|
__proto__: null,
|
|
491
491
|
__wbg_Error_208446807d0d1e30: function(arg0, arg1) {
|
|
492
492
|
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
493
|
-
return ret;
|
|
493
|
+
return addHeapObject(ret);
|
|
494
|
+
},
|
|
495
|
+
__wbg___wbindgen_memory_17a1178bc3328ea3: function() {
|
|
496
|
+
const ret = wasm.memory;
|
|
497
|
+
return addHeapObject(ret);
|
|
494
498
|
},
|
|
495
499
|
__wbg___wbindgen_throw_4c88d8bf264fd984: function(arg0, arg1) {
|
|
496
500
|
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
497
501
|
},
|
|
498
502
|
__wbg_modelinfo_new: function(arg0) {
|
|
499
503
|
const ret = ModelInfo.__wrap(arg0);
|
|
504
|
+
return addHeapObject(ret);
|
|
505
|
+
},
|
|
506
|
+
__wbg_tk_bind_7c88ca2d82b178ed: function(arg0) {
|
|
507
|
+
tk_bind(takeObject(arg0));
|
|
508
|
+
},
|
|
509
|
+
__wbg_tk_ids_8bb443b1dbf54a7e: function(arg0, arg1) {
|
|
510
|
+
const ret = tk_ids(arg0 >>> 0, arg1 >>> 0);
|
|
511
|
+
return addHeapObject(ret);
|
|
512
|
+
},
|
|
513
|
+
__wbg_tk_read_676ec0123360850e: function(arg0, arg1, arg2, arg3, arg4) {
|
|
514
|
+
const ret = tk_read(getObject(arg0), arg1 >>> 0, arg2 >>> 0, arg3 >>> 0, arg4 >>> 0);
|
|
500
515
|
return ret;
|
|
501
516
|
},
|
|
502
517
|
__wbindgen_generic_0000000000000001: function(arg0, arg1) {
|
|
503
518
|
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
504
519
|
const ret = getStringFromWasm0(arg0, arg1);
|
|
505
|
-
return ret;
|
|
506
|
-
},
|
|
507
|
-
__wbindgen_init_externref_table: function() {
|
|
508
|
-
const table = wasm.__wbindgen_externrefs;
|
|
509
|
-
table.set(0, undefined);
|
|
510
|
-
table.set(1024, undefined);
|
|
511
|
-
table.set(1025, null);
|
|
512
|
-
table.set(1026, true);
|
|
513
|
-
table.set(1027, false);
|
|
520
|
+
return addHeapObject(ret);
|
|
514
521
|
},
|
|
515
522
|
};
|
|
516
523
|
return {
|
|
@@ -526,22 +533,37 @@ const ModelInfoFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
|
526
533
|
? { register: () => {}, unregister: () => {} }
|
|
527
534
|
: new FinalizationRegistry(ptr => wasm.__wbg_modelinfo_free(ptr, 1));
|
|
528
535
|
|
|
536
|
+
function addHeapObject(obj) {
|
|
537
|
+
if (heap_next === heap.length) heap.push(heap.length + 1);
|
|
538
|
+
const idx = heap_next;
|
|
539
|
+
heap_next = heap[idx];
|
|
540
|
+
|
|
541
|
+
heap[idx] = obj;
|
|
542
|
+
return idx;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function addBorrowedObject(obj) {
|
|
546
|
+
if (stack_pointer == 1) throw new Error('out of js stack');
|
|
547
|
+
heap[--stack_pointer] = obj;
|
|
548
|
+
return stack_pointer;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function dropObject(idx) {
|
|
552
|
+
if (idx < 1028) return;
|
|
553
|
+
heap[idx] = heap_next;
|
|
554
|
+
heap_next = idx;
|
|
555
|
+
}
|
|
556
|
+
|
|
529
557
|
function getArrayJsValueFromWasm0(ptr, len) {
|
|
530
558
|
ptr = ptr >>> 0;
|
|
531
559
|
const mem = getDataViewMemory0();
|
|
532
560
|
const result = [];
|
|
533
561
|
for (let i = ptr; i < ptr + 4 * len; i += 4) {
|
|
534
|
-
result.push(
|
|
562
|
+
result.push(takeObject(mem.getUint32(i, true)));
|
|
535
563
|
}
|
|
536
|
-
wasm.__wbindgen_export(ptr, len);
|
|
537
564
|
return result;
|
|
538
565
|
}
|
|
539
566
|
|
|
540
|
-
function getArrayU32FromWasm0(ptr, len) {
|
|
541
|
-
ptr = ptr >>> 0;
|
|
542
|
-
return getUint32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len);
|
|
543
|
-
}
|
|
544
|
-
|
|
545
567
|
let cachedDataViewMemory0 = null;
|
|
546
568
|
function getDataViewMemory0() {
|
|
547
569
|
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
@@ -571,6 +593,13 @@ function getUint8ArrayMemory0() {
|
|
|
571
593
|
return cachedUint8ArrayMemory0;
|
|
572
594
|
}
|
|
573
595
|
|
|
596
|
+
function getObject(idx) { return heap[idx]; }
|
|
597
|
+
|
|
598
|
+
let heap = new Array(1024).fill(undefined);
|
|
599
|
+
heap.push(undefined, null, true, false);
|
|
600
|
+
|
|
601
|
+
let heap_next = heap.length;
|
|
602
|
+
|
|
574
603
|
function passArray32ToWasm0(arg, malloc) {
|
|
575
604
|
const ptr = malloc(arg.length * 4, 4) >>> 0;
|
|
576
605
|
getUint32ArrayMemory0().set(arg, ptr / 4);
|
|
@@ -615,10 +644,12 @@ function passStringToWasm0(arg, malloc, realloc) {
|
|
|
615
644
|
return ptr;
|
|
616
645
|
}
|
|
617
646
|
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
647
|
+
let stack_pointer = 1024;
|
|
648
|
+
|
|
649
|
+
function takeObject(idx) {
|
|
650
|
+
const ret = getObject(idx);
|
|
651
|
+
dropObject(idx);
|
|
652
|
+
return ret;
|
|
622
653
|
}
|
|
623
654
|
|
|
624
655
|
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
|
package/tiktoken_wasm_bg.wasm
CHANGED
|
Binary file
|