gpt-tokenizer 2.3.0 → 2.4.1

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 (88) hide show
  1. package/README.md +13 -1
  2. package/cjs/BytePairEncodingCore.d.ts +30 -17
  3. package/cjs/BytePairEncodingCore.js +202 -134
  4. package/cjs/BytePairEncodingCore.js.map +1 -1
  5. package/cjs/GptEncoding.d.ts +6 -4
  6. package/cjs/GptEncoding.js +48 -14
  7. package/cjs/GptEncoding.js.map +1 -1
  8. package/cjs/encoding/cl100k_base.d.ts +1 -1
  9. package/cjs/encoding/o200k_base.d.ts +1 -1
  10. package/cjs/encoding/p50k_base.d.ts +1 -1
  11. package/cjs/encoding/p50k_edit.d.ts +1 -1
  12. package/cjs/encoding/r50k_base.d.ts +1 -1
  13. package/cjs/model/gpt-3.5-turbo-0125.d.ts +1 -1
  14. package/cjs/model/gpt-3.5-turbo-0301.d.ts +1 -1
  15. package/cjs/model/gpt-3.5-turbo-0613.d.ts +1 -1
  16. package/cjs/model/gpt-3.5-turbo-1106.d.ts +1 -1
  17. package/cjs/model/gpt-3.5-turbo-16k-0613.d.ts +1 -1
  18. package/cjs/model/gpt-3.5-turbo-16k.d.ts +1 -1
  19. package/cjs/model/gpt-3.5-turbo-instruct-0914.d.ts +1 -1
  20. package/cjs/model/gpt-3.5-turbo-instruct.d.ts +1 -1
  21. package/cjs/model/gpt-3.5-turbo.d.ts +1 -1
  22. package/cjs/model/gpt-4-0125-preview.d.ts +1 -1
  23. package/cjs/model/gpt-4-0314.d.ts +1 -1
  24. package/cjs/model/gpt-4-0613.d.ts +1 -1
  25. package/cjs/model/gpt-4-1106-preview.d.ts +1 -1
  26. package/cjs/model/gpt-4-32k-0314.d.ts +1 -1
  27. package/cjs/model/gpt-4-32k-0613.d.ts +1 -1
  28. package/cjs/model/gpt-4-32k.d.ts +1 -1
  29. package/cjs/model/gpt-4-turbo-2024-04-09.d.ts +1 -1
  30. package/cjs/model/gpt-4-turbo-preview.d.ts +1 -1
  31. package/cjs/model/gpt-4-turbo.d.ts +1 -1
  32. package/cjs/model/gpt-4-vision-preview.d.ts +1 -1
  33. package/cjs/model/gpt-4.d.ts +1 -1
  34. package/cjs/model/gpt-4o-2024-05-13.d.ts +1 -1
  35. package/cjs/model/gpt-4o-2024-08-06.d.ts +1 -1
  36. package/cjs/model/gpt-4o-mini-2024-07-18.d.ts +1 -1
  37. package/cjs/model/gpt-4o-mini.d.ts +1 -1
  38. package/cjs/model/gpt-4o.d.ts +1 -1
  39. package/dist/cl100k_base.js +1 -1
  40. package/dist/cl100k_base.js.map +1 -1
  41. package/dist/o200k_base.js +1 -1
  42. package/dist/o200k_base.js.map +1 -1
  43. package/dist/p50k_base.js +1 -1
  44. package/dist/p50k_base.js.map +1 -1
  45. package/dist/p50k_edit.js +1 -1
  46. package/dist/p50k_edit.js.map +1 -1
  47. package/dist/r50k_base.js +1 -1
  48. package/dist/r50k_base.js.map +1 -1
  49. package/esm/BytePairEncodingCore.d.ts +30 -17
  50. package/esm/BytePairEncodingCore.js +201 -133
  51. package/esm/BytePairEncodingCore.js.map +1 -1
  52. package/esm/GptEncoding.d.ts +6 -4
  53. package/esm/GptEncoding.js +49 -15
  54. package/esm/GptEncoding.js.map +1 -1
  55. package/esm/encoding/cl100k_base.d.ts +1 -1
  56. package/esm/encoding/o200k_base.d.ts +1 -1
  57. package/esm/encoding/p50k_base.d.ts +1 -1
  58. package/esm/encoding/p50k_edit.d.ts +1 -1
  59. package/esm/encoding/r50k_base.d.ts +1 -1
  60. package/esm/model/gpt-3.5-turbo-0125.d.ts +1 -1
  61. package/esm/model/gpt-3.5-turbo-0301.d.ts +1 -1
  62. package/esm/model/gpt-3.5-turbo-0613.d.ts +1 -1
  63. package/esm/model/gpt-3.5-turbo-1106.d.ts +1 -1
  64. package/esm/model/gpt-3.5-turbo-16k-0613.d.ts +1 -1
  65. package/esm/model/gpt-3.5-turbo-16k.d.ts +1 -1
  66. package/esm/model/gpt-3.5-turbo-instruct-0914.d.ts +1 -1
  67. package/esm/model/gpt-3.5-turbo-instruct.d.ts +1 -1
  68. package/esm/model/gpt-3.5-turbo.d.ts +1 -1
  69. package/esm/model/gpt-4-0125-preview.d.ts +1 -1
  70. package/esm/model/gpt-4-0314.d.ts +1 -1
  71. package/esm/model/gpt-4-0613.d.ts +1 -1
  72. package/esm/model/gpt-4-1106-preview.d.ts +1 -1
  73. package/esm/model/gpt-4-32k-0314.d.ts +1 -1
  74. package/esm/model/gpt-4-32k-0613.d.ts +1 -1
  75. package/esm/model/gpt-4-32k.d.ts +1 -1
  76. package/esm/model/gpt-4-turbo-2024-04-09.d.ts +1 -1
  77. package/esm/model/gpt-4-turbo-preview.d.ts +1 -1
  78. package/esm/model/gpt-4-turbo.d.ts +1 -1
  79. package/esm/model/gpt-4-vision-preview.d.ts +1 -1
  80. package/esm/model/gpt-4.d.ts +1 -1
  81. package/esm/model/gpt-4o-2024-05-13.d.ts +1 -1
  82. package/esm/model/gpt-4o-2024-08-06.d.ts +1 -1
  83. package/esm/model/gpt-4o-mini-2024-07-18.d.ts +1 -1
  84. package/esm/model/gpt-4o-mini.d.ts +1 -1
  85. package/esm/model/gpt-4o.d.ts +1 -1
  86. package/package.json +1 -1
  87. package/src/BytePairEncodingCore.ts +260 -173
  88. package/src/GptEncoding.ts +82 -20
@@ -1,5 +1,6 @@
1
+ /* eslint-disable @typescript-eslint/member-ordering */
1
2
  /* eslint-disable no-param-reassign */
2
- import { BytePairEncodingCore } from './BytePairEncodingCore.js'
3
+ import { BytePairEncodingCore, decoder } from './BytePairEncodingCore.js'
3
4
  import {
4
5
  type ChatModelName,
5
6
  type ChatParameters,
@@ -45,6 +46,11 @@ export interface EncodeChatOptions {
45
46
  primeWithAssistantResponse?: string
46
47
  }
47
48
 
49
+ interface SpecialTokenConfig {
50
+ allowedSpecial: Set<string> | undefined
51
+ regexPattern: RegExp | undefined
52
+ }
53
+
48
54
  export class GptEncoding {
49
55
  static EndOfPrompt = EndOfPrompt
50
56
  static EndOfText = EndOfText
@@ -53,11 +59,11 @@ export class GptEncoding {
53
59
  static FimSuffix = FimSuffix
54
60
 
55
61
  modelName?: ModelName
56
- private decoder = new TextDecoder('utf8')
57
62
  private bytePairEncodingCoreProcessor: BytePairEncodingCore
58
63
  private specialTokenMapping: Map<string, number>
59
64
  private specialTokensSet: Set<string>
60
65
  private allSpecialTokenRegex: RegExp
66
+ private defaultSpecialTokenConfig: SpecialTokenConfig
61
67
 
62
68
  private constructor({
63
69
  mergeableBytePairRanks,
@@ -75,6 +81,7 @@ export class GptEncoding {
75
81
  specialTokenMapping,
76
82
  ...rest,
77
83
  })
84
+ this.defaultSpecialTokenConfig = this.processSpecialTokens()
78
85
 
79
86
  const maxTokenValue = Math.max(
80
87
  mergeableBytePairRanks.length - 1,
@@ -82,7 +89,7 @@ export class GptEncoding {
82
89
  )
83
90
  if (expectedVocabularySize !== undefined) {
84
91
  if (
85
- this.bytePairEncodingCoreProcessor.bytePairEncoderSize +
92
+ this.bytePairEncodingCoreProcessor.mergeableBytePairRankCount +
86
93
  specialTokenMapping.size !==
87
94
  expectedVocabularySize
88
95
  ) {
@@ -105,6 +112,7 @@ export class GptEncoding {
105
112
  this.encodeGenerator = this.encodeGenerator.bind(this)
106
113
  this.decodeGenerator = this.decodeGenerator.bind(this)
107
114
  this.decodeAsyncGenerator = this.decodeAsyncGenerator.bind(this)
115
+ this.decodeAsync = this.decodeAsync.bind(this)
108
116
  this.isWithinTokenLimit = this.isWithinTokenLimit.bind(this)
109
117
  this.encodeChat = this.encodeChat.bind(this)
110
118
  this.encodeChatGenerator = this.encodeChatGenerator.bind(this)
@@ -151,10 +159,10 @@ export class GptEncoding {
151
159
  return new GptEncoding({ ...modelParams, modelName })
152
160
  }
153
161
 
154
- encodeGenerator(
155
- lineToEncode: string,
156
- { allowedSpecial, disallowedSpecial }: EncodeOptions = {},
157
- ): Generator<number[], number, undefined> {
162
+ private processSpecialTokens({
163
+ allowedSpecial,
164
+ disallowedSpecial,
165
+ }: EncodeOptions = {}): SpecialTokenConfig {
158
166
  let regexPattern: RegExp | undefined
159
167
 
160
168
  if (allowedSpecial?.has(ALL_SPECIAL_TOKENS)) {
@@ -173,21 +181,51 @@ export class GptEncoding {
173
181
  }
174
182
  }
175
183
 
176
- if (regexPattern) {
177
- const match = lineToEncode.match(regexPattern)
184
+ return { allowedSpecial, regexPattern }
185
+ }
186
+
187
+ encodeGenerator(
188
+ lineToEncode: string,
189
+ encodeOptions?: EncodeOptions,
190
+ ): Generator<number[], number, undefined> {
191
+ const specialTokenConfig = encodeOptions
192
+ ? this.processSpecialTokens(encodeOptions)
193
+ : this.defaultSpecialTokenConfig
194
+
195
+ if (specialTokenConfig.regexPattern) {
196
+ const match = lineToEncode.match(specialTokenConfig.regexPattern)
178
197
  if (match !== null) {
179
198
  throw new Error(`Disallowed special token found: ${match[0]}`)
180
199
  }
181
200
  }
182
201
 
183
- return this.bytePairEncodingCoreProcessor.encodeNative(
202
+ return this.bytePairEncodingCoreProcessor.encodeNativeGenerator(
184
203
  lineToEncode,
185
- allowedSpecial,
204
+ specialTokenConfig.allowedSpecial,
186
205
  )
187
206
  }
188
207
 
189
- encode(lineToEncode: string, encodeOptions: EncodeOptions = {}): number[] {
190
- return [...this.encodeGenerator(lineToEncode, encodeOptions)].flat()
208
+ encode(lineToEncode: string, encodeOptions?: EncodeOptions): number[] {
209
+ // const encodedTokens: number[] = []
210
+ // for (const tokens of this.encodeGenerator(lineToEncode, encodeOptions)) {
211
+ // encodedTokens.push(...tokens)
212
+ // }
213
+ // return encodedTokens
214
+ const specialTokenConfig = encodeOptions
215
+ ? this.processSpecialTokens(encodeOptions)
216
+ : this.defaultSpecialTokenConfig
217
+
218
+ if (specialTokenConfig.regexPattern) {
219
+ const match = lineToEncode.match(specialTokenConfig.regexPattern)
220
+ if (match !== null) {
221
+ throw new Error(`Disallowed special token found: ${match[0]}`)
222
+ }
223
+ }
224
+
225
+ return this.bytePairEncodingCoreProcessor.encodeNative(
226
+ lineToEncode,
227
+ specialTokenConfig.allowedSpecial,
228
+ )
191
229
  }
192
230
 
193
231
  /**
@@ -282,11 +320,17 @@ export class GptEncoding {
282
320
  return count
283
321
  }
284
322
 
323
+ decode(inputTokensToDecode: Iterable<number>): string {
324
+ return this.bytePairEncodingCoreProcessor.decodeNative(inputTokensToDecode)
325
+ }
326
+
285
327
  *decodeGenerator(
286
328
  inputTokensToDecode: Iterable<number>,
287
- ): Generator<string, void> {
329
+ ): Generator<string, void, void> {
288
330
  const decodedByteGenerator =
289
- this.bytePairEncodingCoreProcessor.decodeNative(inputTokensToDecode)
331
+ this.bytePairEncodingCoreProcessor.decodeNativeGenerator(
332
+ inputTokensToDecode,
333
+ )
290
334
 
291
335
  let buffer = ''
292
336
 
@@ -294,7 +338,7 @@ export class GptEncoding {
294
338
  buffer +=
295
339
  typeof decodedPart === 'string'
296
340
  ? decodedPart
297
- : this.decoder.decode(decodedPart, { stream: true })
341
+ : decoder.decode(decodedPart, { stream: true })
298
342
 
299
343
  if (buffer.length === 0 || endsWithIncompleteUtfPairSurrogate(buffer)) {
300
344
  // Keep the high surrogate in the buffer and continue with the next token
@@ -317,7 +361,9 @@ export class GptEncoding {
317
361
  inputTokensToDecode: AsyncIterable<number>,
318
362
  ): AsyncGenerator<string, void> {
319
363
  const decodedByteGenerator =
320
- this.bytePairEncodingCoreProcessor.decodeNativeAsync(inputTokensToDecode)
364
+ this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(
365
+ inputTokensToDecode,
366
+ )
321
367
 
322
368
  let buffer = ''
323
369
 
@@ -325,7 +371,7 @@ export class GptEncoding {
325
371
  buffer +=
326
372
  typeof decodedPart === 'string'
327
373
  ? decodedPart
328
- : this.decoder.decode(decodedPart, { stream: true })
374
+ : decoder.decode(decodedPart, { stream: true })
329
375
 
330
376
  if (buffer.length === 0 || endsWithIncompleteUtfPairSurrogate(buffer)) {
331
377
  // Keep the high surrogate in the buffer and continue with the next token
@@ -344,7 +390,23 @@ export class GptEncoding {
344
390
  }
345
391
  }
346
392
 
347
- decode(inputTokensToDecode: Iterable<number>): string {
348
- return [...this.decodeGenerator(inputTokensToDecode)].join('')
393
+ async decodeAsync(
394
+ inputTokensToDecode: AsyncIterable<number>,
395
+ ): Promise<string> {
396
+ const decodedByteGenerator =
397
+ this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(
398
+ inputTokensToDecode,
399
+ )
400
+
401
+ let buffer = ''
402
+
403
+ for await (const decodedPart of decodedByteGenerator) {
404
+ buffer +=
405
+ typeof decodedPart === 'string'
406
+ ? decodedPart
407
+ : decoder.decode(decodedPart, { stream: true })
408
+ }
409
+
410
+ return buffer
349
411
  }
350
412
  }