rz_tiktoken_ruby 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/Cargo.lock +492 -0
- data/Cargo.toml +7 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +93 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/Rakefile +23 -0
- data/doctest_helper.rb +1 -0
- data/ext/tiktoken_ruby/Cargo.toml +14 -0
- data/ext/tiktoken_ruby/extconf.rb +6 -0
- data/ext/tiktoken_ruby/src/core_bpe_wrapper.rs +44 -0
- data/ext/tiktoken_ruby/src/lib.rs +52 -0
- data/lib/tiktoken_ruby/encoding.rb +52 -0
- data/lib/tiktoken_ruby/version.rb +5 -0
- data/lib/tiktoken_ruby.rb +114 -0
- data/sig/tiktoken_ruby.rbs +4 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6ce25f57d6649247f65ff73e1e5ecc192e0ef05005cc0d1fe797a9ac7ae27c35
|
4
|
+
data.tar.gz: 5f46c48a02b5819d579ec6624b1b7fc8b812c89fe9c5d18621e3a8f3e86af870
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dfa470a837de1f8c1a59e31a3ef250049b545e37c792e0b837ac4314d8e126380484150a52cfd0a065bab62683eb401b3f90fd458d7dc6ab00fc0ef43e23d871
|
7
|
+
data.tar.gz: 2d2d16cb8b80f30db22fb391fc3edb4fe58690420b518606aefebddb906bd4285abb0d32a1e953d3e07bb8074e5e164dc35c05ad9836204f4924fee80fe73f2d
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/Cargo.lock
ADDED
@@ -0,0 +1,492 @@
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
2
|
+
# It is not intended for manual editing.
|
3
|
+
version = 3
|
4
|
+
|
5
|
+
[[package]]
|
6
|
+
name = "aho-corasick"
|
7
|
+
version = "1.0.4"
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
9
|
+
checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a"
|
10
|
+
dependencies = [
|
11
|
+
"memchr",
|
12
|
+
]
|
13
|
+
|
14
|
+
[[package]]
|
15
|
+
name = "anyhow"
|
16
|
+
version = "1.0.75"
|
17
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
18
|
+
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
|
19
|
+
|
20
|
+
[[package]]
|
21
|
+
name = "autocfg"
|
22
|
+
version = "1.1.0"
|
23
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
24
|
+
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
25
|
+
|
26
|
+
[[package]]
|
27
|
+
name = "base64"
|
28
|
+
version = "0.21.2"
|
29
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
30
|
+
checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d"
|
31
|
+
|
32
|
+
[[package]]
|
33
|
+
name = "bindgen"
|
34
|
+
version = "0.66.1"
|
35
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
36
|
+
checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7"
|
37
|
+
dependencies = [
|
38
|
+
"bitflags 2.4.0",
|
39
|
+
"cexpr",
|
40
|
+
"clang-sys",
|
41
|
+
"lazy_static",
|
42
|
+
"lazycell",
|
43
|
+
"peeking_take_while",
|
44
|
+
"proc-macro2",
|
45
|
+
"quote",
|
46
|
+
"regex",
|
47
|
+
"rustc-hash",
|
48
|
+
"shlex",
|
49
|
+
"syn 2.0.29",
|
50
|
+
]
|
51
|
+
|
52
|
+
[[package]]
|
53
|
+
name = "bit-set"
|
54
|
+
version = "0.5.3"
|
55
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
56
|
+
checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1"
|
57
|
+
dependencies = [
|
58
|
+
"bit-vec",
|
59
|
+
]
|
60
|
+
|
61
|
+
[[package]]
|
62
|
+
name = "bit-vec"
|
63
|
+
version = "0.6.3"
|
64
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
65
|
+
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
|
66
|
+
|
67
|
+
[[package]]
|
68
|
+
name = "bitflags"
|
69
|
+
version = "1.3.2"
|
70
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
71
|
+
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
72
|
+
|
73
|
+
[[package]]
|
74
|
+
name = "bitflags"
|
75
|
+
version = "2.4.0"
|
76
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
77
|
+
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
|
78
|
+
|
79
|
+
[[package]]
|
80
|
+
name = "bstr"
|
81
|
+
version = "1.6.0"
|
82
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
83
|
+
checksum = "6798148dccfbff0fae41c7574d2fa8f1ef3492fba0face179de5d8d447d67b05"
|
84
|
+
dependencies = [
|
85
|
+
"memchr",
|
86
|
+
"regex-automata",
|
87
|
+
"serde",
|
88
|
+
]
|
89
|
+
|
90
|
+
[[package]]
|
91
|
+
name = "cexpr"
|
92
|
+
version = "0.6.0"
|
93
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
94
|
+
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
|
95
|
+
dependencies = [
|
96
|
+
"nom",
|
97
|
+
]
|
98
|
+
|
99
|
+
[[package]]
|
100
|
+
name = "cfg-if"
|
101
|
+
version = "1.0.0"
|
102
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
103
|
+
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
104
|
+
|
105
|
+
[[package]]
|
106
|
+
name = "clang-sys"
|
107
|
+
version = "1.6.1"
|
108
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
109
|
+
checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f"
|
110
|
+
dependencies = [
|
111
|
+
"glob",
|
112
|
+
"libc",
|
113
|
+
"libloading",
|
114
|
+
]
|
115
|
+
|
116
|
+
[[package]]
|
117
|
+
name = "fancy-regex"
|
118
|
+
version = "0.11.0"
|
119
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
120
|
+
checksum = "b95f7c0680e4142284cf8b22c14a476e87d61b004a3a0861872b32ef7ead40a2"
|
121
|
+
dependencies = [
|
122
|
+
"bit-set",
|
123
|
+
"regex",
|
124
|
+
]
|
125
|
+
|
126
|
+
[[package]]
|
127
|
+
name = "glob"
|
128
|
+
version = "0.3.1"
|
129
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
130
|
+
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
|
131
|
+
|
132
|
+
[[package]]
|
133
|
+
name = "lazy_static"
|
134
|
+
version = "1.4.0"
|
135
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
136
|
+
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
137
|
+
|
138
|
+
[[package]]
|
139
|
+
name = "lazycell"
|
140
|
+
version = "1.3.0"
|
141
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
142
|
+
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
143
|
+
|
144
|
+
[[package]]
|
145
|
+
name = "libc"
|
146
|
+
version = "0.2.147"
|
147
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
148
|
+
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
149
|
+
|
150
|
+
[[package]]
|
151
|
+
name = "libloading"
|
152
|
+
version = "0.7.4"
|
153
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
154
|
+
checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f"
|
155
|
+
dependencies = [
|
156
|
+
"cfg-if",
|
157
|
+
"winapi",
|
158
|
+
]
|
159
|
+
|
160
|
+
[[package]]
|
161
|
+
name = "lock_api"
|
162
|
+
version = "0.4.10"
|
163
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
164
|
+
checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16"
|
165
|
+
dependencies = [
|
166
|
+
"autocfg",
|
167
|
+
"scopeguard",
|
168
|
+
]
|
169
|
+
|
170
|
+
[[package]]
|
171
|
+
name = "magnus"
|
172
|
+
version = "0.4.4"
|
173
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
174
|
+
checksum = "fc87660cd7daa49fddbfd524c836de54d5c927d520cd163f43700c5087c57d6c"
|
175
|
+
dependencies = [
|
176
|
+
"magnus-macros",
|
177
|
+
"rb-sys",
|
178
|
+
"rb-sys-env",
|
179
|
+
]
|
180
|
+
|
181
|
+
[[package]]
|
182
|
+
name = "magnus-macros"
|
183
|
+
version = "0.3.0"
|
184
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
185
|
+
checksum = "206cb23bfeea05180c97522ef6a3e52a4eb17b0ed2f30ee3ca9c4f994d2378ae"
|
186
|
+
dependencies = [
|
187
|
+
"proc-macro2",
|
188
|
+
"quote",
|
189
|
+
"syn 1.0.109",
|
190
|
+
]
|
191
|
+
|
192
|
+
[[package]]
|
193
|
+
name = "memchr"
|
194
|
+
version = "2.5.0"
|
195
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
196
|
+
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
197
|
+
|
198
|
+
[[package]]
|
199
|
+
name = "minimal-lexical"
|
200
|
+
version = "0.2.1"
|
201
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
202
|
+
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
203
|
+
|
204
|
+
[[package]]
|
205
|
+
name = "nom"
|
206
|
+
version = "7.1.3"
|
207
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
208
|
+
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
209
|
+
dependencies = [
|
210
|
+
"memchr",
|
211
|
+
"minimal-lexical",
|
212
|
+
]
|
213
|
+
|
214
|
+
[[package]]
|
215
|
+
name = "parking_lot"
|
216
|
+
version = "0.12.1"
|
217
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
218
|
+
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
|
219
|
+
dependencies = [
|
220
|
+
"lock_api",
|
221
|
+
"parking_lot_core",
|
222
|
+
]
|
223
|
+
|
224
|
+
[[package]]
|
225
|
+
name = "parking_lot_core"
|
226
|
+
version = "0.9.8"
|
227
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
228
|
+
checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447"
|
229
|
+
dependencies = [
|
230
|
+
"cfg-if",
|
231
|
+
"libc",
|
232
|
+
"redox_syscall",
|
233
|
+
"smallvec",
|
234
|
+
"windows-targets",
|
235
|
+
]
|
236
|
+
|
237
|
+
[[package]]
|
238
|
+
name = "peeking_take_while"
|
239
|
+
version = "0.1.2"
|
240
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
241
|
+
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
|
242
|
+
|
243
|
+
[[package]]
|
244
|
+
name = "proc-macro2"
|
245
|
+
version = "1.0.66"
|
246
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
247
|
+
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
|
248
|
+
dependencies = [
|
249
|
+
"unicode-ident",
|
250
|
+
]
|
251
|
+
|
252
|
+
[[package]]
|
253
|
+
name = "quote"
|
254
|
+
version = "1.0.33"
|
255
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
256
|
+
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
257
|
+
dependencies = [
|
258
|
+
"proc-macro2",
|
259
|
+
]
|
260
|
+
|
261
|
+
[[package]]
|
262
|
+
name = "rb-sys"
|
263
|
+
version = "0.9.81"
|
264
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
265
|
+
checksum = "a57240b308b155b09dce81e32829966a99f52d1088b45957e4283e526c5317a1"
|
266
|
+
dependencies = [
|
267
|
+
"rb-sys-build",
|
268
|
+
]
|
269
|
+
|
270
|
+
[[package]]
|
271
|
+
name = "rb-sys-build"
|
272
|
+
version = "0.9.81"
|
273
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
274
|
+
checksum = "f24ce877a4c5d07f06f6aa6fec3ac95e4b357b9f73b0f5445d8cbb7266d410e8"
|
275
|
+
dependencies = [
|
276
|
+
"bindgen",
|
277
|
+
"lazy_static",
|
278
|
+
"proc-macro2",
|
279
|
+
"quote",
|
280
|
+
"regex",
|
281
|
+
"shell-words",
|
282
|
+
"syn 2.0.29",
|
283
|
+
]
|
284
|
+
|
285
|
+
[[package]]
|
286
|
+
name = "rb-sys-env"
|
287
|
+
version = "0.1.2"
|
288
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
289
|
+
checksum = "a35802679f07360454b418a5d1735c89716bde01d35b1560fc953c1415a0b3bb"
|
290
|
+
|
291
|
+
[[package]]
|
292
|
+
name = "redox_syscall"
|
293
|
+
version = "0.3.5"
|
294
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
295
|
+
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
|
296
|
+
dependencies = [
|
297
|
+
"bitflags 1.3.2",
|
298
|
+
]
|
299
|
+
|
300
|
+
[[package]]
|
301
|
+
name = "regex"
|
302
|
+
version = "1.9.3"
|
303
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
304
|
+
checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a"
|
305
|
+
dependencies = [
|
306
|
+
"aho-corasick",
|
307
|
+
"memchr",
|
308
|
+
"regex-automata",
|
309
|
+
"regex-syntax",
|
310
|
+
]
|
311
|
+
|
312
|
+
[[package]]
|
313
|
+
name = "regex-automata"
|
314
|
+
version = "0.3.6"
|
315
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
316
|
+
checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69"
|
317
|
+
dependencies = [
|
318
|
+
"aho-corasick",
|
319
|
+
"memchr",
|
320
|
+
"regex-syntax",
|
321
|
+
]
|
322
|
+
|
323
|
+
[[package]]
|
324
|
+
name = "regex-syntax"
|
325
|
+
version = "0.7.4"
|
326
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
327
|
+
checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2"
|
328
|
+
|
329
|
+
[[package]]
|
330
|
+
name = "rustc-hash"
|
331
|
+
version = "1.1.0"
|
332
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
333
|
+
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
334
|
+
|
335
|
+
[[package]]
|
336
|
+
name = "scopeguard"
|
337
|
+
version = "1.2.0"
|
338
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
339
|
+
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
340
|
+
|
341
|
+
[[package]]
|
342
|
+
name = "serde"
|
343
|
+
version = "1.0.185"
|
344
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
345
|
+
checksum = "be9b6f69f1dfd54c3b568ffa45c310d6973a5e5148fd40cf515acaf38cf5bc31"
|
346
|
+
|
347
|
+
[[package]]
|
348
|
+
name = "shell-words"
|
349
|
+
version = "1.1.0"
|
350
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
351
|
+
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
|
352
|
+
|
353
|
+
[[package]]
|
354
|
+
name = "shlex"
|
355
|
+
version = "1.1.0"
|
356
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
357
|
+
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
|
358
|
+
|
359
|
+
[[package]]
|
360
|
+
name = "smallvec"
|
361
|
+
version = "1.11.0"
|
362
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
363
|
+
checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9"
|
364
|
+
|
365
|
+
[[package]]
|
366
|
+
name = "syn"
|
367
|
+
version = "1.0.109"
|
368
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
369
|
+
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
370
|
+
dependencies = [
|
371
|
+
"proc-macro2",
|
372
|
+
"quote",
|
373
|
+
"unicode-ident",
|
374
|
+
]
|
375
|
+
|
376
|
+
[[package]]
|
377
|
+
name = "syn"
|
378
|
+
version = "2.0.29"
|
379
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
380
|
+
checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a"
|
381
|
+
dependencies = [
|
382
|
+
"proc-macro2",
|
383
|
+
"quote",
|
384
|
+
"unicode-ident",
|
385
|
+
]
|
386
|
+
|
387
|
+
[[package]]
|
388
|
+
name = "tiktoken-rs"
|
389
|
+
version = "0.5.1"
|
390
|
+
source = "git+https://github.com/zurawiki/tiktoken-rs.git#963bc718734c49ae5f2e73adc3e118dddcde6c28"
|
391
|
+
dependencies = [
|
392
|
+
"anyhow",
|
393
|
+
"base64",
|
394
|
+
"bstr",
|
395
|
+
"fancy-regex",
|
396
|
+
"lazy_static",
|
397
|
+
"parking_lot",
|
398
|
+
"rustc-hash",
|
399
|
+
]
|
400
|
+
|
401
|
+
[[package]]
|
402
|
+
name = "tiktoken_ruby"
|
403
|
+
version = "0.1.0"
|
404
|
+
dependencies = [
|
405
|
+
"magnus",
|
406
|
+
"tiktoken-rs",
|
407
|
+
]
|
408
|
+
|
409
|
+
[[package]]
|
410
|
+
name = "unicode-ident"
|
411
|
+
version = "1.0.11"
|
412
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
413
|
+
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
|
414
|
+
|
415
|
+
[[package]]
|
416
|
+
name = "winapi"
|
417
|
+
version = "0.3.9"
|
418
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
419
|
+
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
420
|
+
dependencies = [
|
421
|
+
"winapi-i686-pc-windows-gnu",
|
422
|
+
"winapi-x86_64-pc-windows-gnu",
|
423
|
+
]
|
424
|
+
|
425
|
+
[[package]]
|
426
|
+
name = "winapi-i686-pc-windows-gnu"
|
427
|
+
version = "0.4.0"
|
428
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
429
|
+
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
430
|
+
|
431
|
+
[[package]]
|
432
|
+
name = "winapi-x86_64-pc-windows-gnu"
|
433
|
+
version = "0.4.0"
|
434
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
435
|
+
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
436
|
+
|
437
|
+
[[package]]
|
438
|
+
name = "windows-targets"
|
439
|
+
version = "0.48.5"
|
440
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
441
|
+
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
442
|
+
dependencies = [
|
443
|
+
"windows_aarch64_gnullvm",
|
444
|
+
"windows_aarch64_msvc",
|
445
|
+
"windows_i686_gnu",
|
446
|
+
"windows_i686_msvc",
|
447
|
+
"windows_x86_64_gnu",
|
448
|
+
"windows_x86_64_gnullvm",
|
449
|
+
"windows_x86_64_msvc",
|
450
|
+
]
|
451
|
+
|
452
|
+
[[package]]
|
453
|
+
name = "windows_aarch64_gnullvm"
|
454
|
+
version = "0.48.5"
|
455
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
456
|
+
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
457
|
+
|
458
|
+
[[package]]
|
459
|
+
name = "windows_aarch64_msvc"
|
460
|
+
version = "0.48.5"
|
461
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
462
|
+
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
463
|
+
|
464
|
+
[[package]]
|
465
|
+
name = "windows_i686_gnu"
|
466
|
+
version = "0.48.5"
|
467
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
468
|
+
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
469
|
+
|
470
|
+
[[package]]
|
471
|
+
name = "windows_i686_msvc"
|
472
|
+
version = "0.48.5"
|
473
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
474
|
+
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
475
|
+
|
476
|
+
[[package]]
|
477
|
+
name = "windows_x86_64_gnu"
|
478
|
+
version = "0.48.5"
|
479
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
480
|
+
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
481
|
+
|
482
|
+
[[package]]
|
483
|
+
name = "windows_x86_64_gnullvm"
|
484
|
+
version = "0.48.5"
|
485
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
486
|
+
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
487
|
+
|
488
|
+
[[package]]
|
489
|
+
name = "windows_x86_64_msvc"
|
490
|
+
version = "0.48.5"
|
491
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
492
|
+
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
data/Cargo.toml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in tiktoken_ruby.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rake-compiler"
|
11
|
+
|
12
|
+
gem "rspec", "~> 3.0"
|
13
|
+
|
14
|
+
gem "standard", "~> 1.3"
|
15
|
+
|
16
|
+
gem "yard-doctest", "~> 0.1.17"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rz_tiktoken_ruby (0.0.6)
|
5
|
+
rb_sys (~> 0.9.81)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
base64 (0.1.1)
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
json (2.6.3)
|
14
|
+
language_server-protocol (3.17.0.3)
|
15
|
+
lint_roller (1.1.0)
|
16
|
+
minitest (5.19.0)
|
17
|
+
parallel (1.23.0)
|
18
|
+
parser (3.2.2.3)
|
19
|
+
ast (~> 2.4.1)
|
20
|
+
racc
|
21
|
+
racc (1.7.1)
|
22
|
+
rainbow (3.1.1)
|
23
|
+
rake (13.0.6)
|
24
|
+
rake-compiler (1.2.5)
|
25
|
+
rake
|
26
|
+
rb_sys (0.9.81)
|
27
|
+
regexp_parser (2.8.1)
|
28
|
+
rexml (3.2.6)
|
29
|
+
rspec (3.12.0)
|
30
|
+
rspec-core (~> 3.12.0)
|
31
|
+
rspec-expectations (~> 3.12.0)
|
32
|
+
rspec-mocks (~> 3.12.0)
|
33
|
+
rspec-core (3.12.2)
|
34
|
+
rspec-support (~> 3.12.0)
|
35
|
+
rspec-expectations (3.12.3)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.12.0)
|
38
|
+
rspec-mocks (3.12.6)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.12.0)
|
41
|
+
rspec-support (3.12.1)
|
42
|
+
rubocop (1.56.1)
|
43
|
+
base64 (~> 0.1.1)
|
44
|
+
json (~> 2.3)
|
45
|
+
language_server-protocol (>= 3.17.0)
|
46
|
+
parallel (~> 1.10)
|
47
|
+
parser (>= 3.2.2.3)
|
48
|
+
rainbow (>= 2.2.2, < 4.0)
|
49
|
+
regexp_parser (>= 1.8, < 3.0)
|
50
|
+
rexml (>= 3.2.5, < 4.0)
|
51
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
52
|
+
ruby-progressbar (~> 1.7)
|
53
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
54
|
+
rubocop-ast (1.29.0)
|
55
|
+
parser (>= 3.2.1.0)
|
56
|
+
rubocop-performance (1.19.0)
|
57
|
+
rubocop (>= 1.7.0, < 2.0)
|
58
|
+
rubocop-ast (>= 0.4.0)
|
59
|
+
ruby-progressbar (1.13.0)
|
60
|
+
standard (1.31.0)
|
61
|
+
language_server-protocol (~> 3.17.0.2)
|
62
|
+
lint_roller (~> 1.0)
|
63
|
+
rubocop (~> 1.56.0)
|
64
|
+
standard-custom (~> 1.0.0)
|
65
|
+
standard-performance (~> 1.2)
|
66
|
+
standard-custom (1.0.2)
|
67
|
+
lint_roller (~> 1.0)
|
68
|
+
rubocop (~> 1.50)
|
69
|
+
standard-performance (1.2.0)
|
70
|
+
lint_roller (~> 1.1)
|
71
|
+
rubocop-performance (~> 1.19.0)
|
72
|
+
unicode-display_width (2.4.2)
|
73
|
+
yard (0.9.34)
|
74
|
+
yard-doctest (0.1.17)
|
75
|
+
minitest
|
76
|
+
yard
|
77
|
+
|
78
|
+
PLATFORMS
|
79
|
+
arm64-darwin-22
|
80
|
+
ruby
|
81
|
+
x86_64-darwin-22
|
82
|
+
x86_64-linux
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
rake (~> 13.0)
|
86
|
+
rake-compiler
|
87
|
+
rspec (~> 3.0)
|
88
|
+
rz_tiktoken_ruby!
|
89
|
+
standard (~> 1.3)
|
90
|
+
yard-doctest (~> 0.1.17)
|
91
|
+
|
92
|
+
BUNDLED WITH
|
93
|
+
2.4.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 IAPark
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/tiktoken_ruby.svg)](https://badge.fury.io/rb/tiktoken_ruby)
|
2
|
+
# tiktoken_ruby
|
3
|
+
|
4
|
+
[Tiktoken](https://github.com/openai/tiktoken) is BPE tokenizer from OpenAI used with their GPT models.
|
5
|
+
This is a wrapper around it aimed primarily at enabling accurate counts of GPT model tokens used.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add tiktoken_ruby
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
$ gem install tiktoken_ruby
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
Usage should be very similar to the python library. Here's a simple example
|
19
|
+
|
20
|
+
Encode and decode text
|
21
|
+
```ruby
|
22
|
+
require 'tiktoken_ruby'
|
23
|
+
|
24
|
+
enc = Tiktoken.get_encoding("cl100k_base")
|
25
|
+
enc.decode(enc.encode("hello world")) #=> "hello world"
|
26
|
+
```
|
27
|
+
|
28
|
+
Encoders can also be retrieved by model name
|
29
|
+
```ruby
|
30
|
+
require 'tiktoken_ruby'
|
31
|
+
|
32
|
+
enc = Tiktoken.encoding_for_model("gpt-4")
|
33
|
+
enc.encode("hello world").length #=> 2
|
34
|
+
```
|
35
|
+
|
36
|
+
## Development
|
37
|
+
|
38
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
39
|
+
|
40
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/iapark/tiktoken_ruby.
|
45
|
+
|
46
|
+
To get started with development:
|
47
|
+
|
48
|
+
```sh
|
49
|
+
git clone https://github.com/IAPark/tiktoken_ruby.git
|
50
|
+
cd tiktoken_ruby
|
51
|
+
bundle install
|
52
|
+
bundle exec rake compile
|
53
|
+
bundle exec rake spec
|
54
|
+
```
|
55
|
+
|
56
|
+
|
57
|
+
## License
|
58
|
+
|
59
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
require "standard/rake"
|
6
|
+
require "rake/extensiontask"
|
7
|
+
require "rb_sys/extensiontask"
|
8
|
+
|
9
|
+
GEMSPEC = Gem::Specification.load("tiktoken_ruby.gemspec")
|
10
|
+
|
11
|
+
RbSys::ExtensionTask.new("tiktoken_ruby", GEMSPEC) do |ext|
|
12
|
+
ext.lib_dir = "lib/tiktoken_ruby"
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec::Core::RakeTask.new(:spec)
|
16
|
+
|
17
|
+
task :native, [:platform] do |_t, platform:|
|
18
|
+
sh "bundle", "exec", "rb-sys-dock", "--platform", platform, "--build"
|
19
|
+
end
|
20
|
+
|
21
|
+
task build: :compile
|
22
|
+
|
23
|
+
task default: %i[compile spec standard]
|
data/doctest_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "lib/tiktoken_ruby"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
[package]
|
2
|
+
name = "tiktoken_ruby"
|
3
|
+
version = "0.1.0"
|
4
|
+
edition = "2021"
|
5
|
+
authors = ["IAPark <isaac.a.park@gmail.com>"]
|
6
|
+
license = "MIT"
|
7
|
+
publish = false
|
8
|
+
|
9
|
+
[lib]
|
10
|
+
crate-type = ["cdylib"]
|
11
|
+
|
12
|
+
[dependencies]
|
13
|
+
magnus = { version = "0.4" }
|
14
|
+
tiktoken-rs = { git = "https://github.com/zurawiki/tiktoken-rs.git" }
|
@@ -0,0 +1,44 @@
|
|
1
|
+
use std::collections::HashSet;
|
2
|
+
|
3
|
+
use crate::uncicode_error;
|
4
|
+
|
5
|
+
|
6
|
+
#[magnus::wrap(class = "Tiktoken::Ext::CoreBPE")]
|
7
|
+
pub struct CoreBPEWrapper {
|
8
|
+
core_bpe: tiktoken_rs::CoreBPE,
|
9
|
+
}
|
10
|
+
|
11
|
+
impl CoreBPEWrapper {
|
12
|
+
pub fn new(core_bpe: tiktoken_rs::CoreBPE) -> Self {
|
13
|
+
Self { core_bpe }
|
14
|
+
}
|
15
|
+
|
16
|
+
pub fn encode_ordinary(&self, text: String) -> Vec<usize> {
|
17
|
+
self.core_bpe.encode_ordinary(text.as_str())
|
18
|
+
}
|
19
|
+
|
20
|
+
pub fn encode(&self, text: String, allowed_special: magnus::RArray) -> Result<Vec<usize>, magnus::Error> {
|
21
|
+
let allowed_special: Vec<String> = allowed_special.to_vec()?;
|
22
|
+
let allowed_special: Vec<&str> = allowed_special.iter().map(|s| s.as_str()).collect();
|
23
|
+
let allowed_special: HashSet<&str> = HashSet::from_iter(allowed_special.iter().cloned());
|
24
|
+
|
25
|
+
Ok(self.core_bpe.encode(text.as_str(), allowed_special))
|
26
|
+
}
|
27
|
+
|
28
|
+
pub fn encode_with_special_tokens(&self, text: String) -> Vec<usize> {
|
29
|
+
self.core_bpe.encode_with_special_tokens(text.as_str())
|
30
|
+
}
|
31
|
+
|
32
|
+
pub fn decode(&self, ids: Vec<usize>) -> Result<String, magnus::Error> {
|
33
|
+
self.core_bpe.decode(ids)
|
34
|
+
.map_err(|e| {
|
35
|
+
let error = match uncicode_error() {
|
36
|
+
Ok(error) => error,
|
37
|
+
Err(e) => return e
|
38
|
+
};
|
39
|
+
|
40
|
+
magnus::Error::new(error, e.to_string())
|
41
|
+
})
|
42
|
+
|
43
|
+
}
|
44
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
mod core_bpe_wrapper;
|
2
|
+
|
3
|
+
use core_bpe_wrapper::CoreBPEWrapper;
|
4
|
+
use magnus::{define_module, function, prelude::*, Error, method, class, RModule, ExceptionClass};
|
5
|
+
|
6
|
+
fn r50k_base() -> CoreBPEWrapper {
|
7
|
+
let core_bpe = tiktoken_rs::r50k_base().unwrap();
|
8
|
+
CoreBPEWrapper::new(core_bpe)
|
9
|
+
}
|
10
|
+
fn p50k_base() -> CoreBPEWrapper {
|
11
|
+
let core_bpe = tiktoken_rs::p50k_base().unwrap();
|
12
|
+
CoreBPEWrapper::new(core_bpe)
|
13
|
+
}
|
14
|
+
fn p50k_edit() -> CoreBPEWrapper {
|
15
|
+
let core_bpe = tiktoken_rs::p50k_edit().unwrap();
|
16
|
+
CoreBPEWrapper::new(core_bpe)
|
17
|
+
}
|
18
|
+
fn cl100k_base() -> CoreBPEWrapper {
|
19
|
+
let core_bpe = tiktoken_rs::cl100k_base().unwrap();
|
20
|
+
CoreBPEWrapper::new(core_bpe)
|
21
|
+
}
|
22
|
+
|
23
|
+
fn module() -> Result<RModule, magnus::Error> {
|
24
|
+
define_module("Tiktoken")
|
25
|
+
}
|
26
|
+
|
27
|
+
fn uncicode_error() -> Result<ExceptionClass, magnus::Error> {
|
28
|
+
module()?.define_error("UnicodeError", magnus::exception::standard_error())
|
29
|
+
}
|
30
|
+
|
31
|
+
#[magnus::init]
|
32
|
+
fn init() -> Result<(), Error> {
|
33
|
+
let module = module()?;
|
34
|
+
|
35
|
+
let factory_module = module.define_module("BpeFactory")?;
|
36
|
+
factory_module.define_singleton_method("r50k_base", function!(r50k_base, 0))?;
|
37
|
+
factory_module.define_singleton_method("p50k_base", function!(p50k_base, 0))?;
|
38
|
+
factory_module.define_singleton_method("p50k_edit", function!(p50k_edit, 0))?;
|
39
|
+
factory_module.define_singleton_method("cl100k_base", function!(cl100k_base, 0))?;
|
40
|
+
|
41
|
+
|
42
|
+
let ext_module = module.define_module("Ext")?;
|
43
|
+
let bpe_class = ext_module.define_class("CoreBPE", class::object())?;
|
44
|
+
|
45
|
+
bpe_class.define_method("encode_ordinary", method!(CoreBPEWrapper::encode_ordinary, 1))?;
|
46
|
+
bpe_class.define_method("encode", method!(CoreBPEWrapper::encode, 2))?;
|
47
|
+
bpe_class.define_method("encode_with_special_tokens", method!(CoreBPEWrapper::encode_with_special_tokens, 1))?;
|
48
|
+
|
49
|
+
|
50
|
+
bpe_class.define_method("decode", method!(CoreBPEWrapper::decode, 1))?;
|
51
|
+
Ok(())
|
52
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Tiktoken::Encoding
|
4
|
+
attr_reader :name
|
5
|
+
|
6
|
+
# This returns a new Tiktoken::Encoding instance for the requested encoding
|
7
|
+
# @param encoding [Symbol] The name of the encoding to load
|
8
|
+
# @return [Tiktoken::Encoding] The encoding instance
|
9
|
+
def self.for_name(encoding)
|
10
|
+
Tiktoken::Encoding.new(Tiktoken::BpeFactory.send(encoding.to_sym), encoding.to_sym)
|
11
|
+
end
|
12
|
+
|
13
|
+
# This returns a Tiktoken::Encoding instance for the requested encoding
|
14
|
+
# It will reuse an existing encoding if it's already been loaded
|
15
|
+
# @param encoding [Symbol] The name of the encoding to load
|
16
|
+
# @return [Tiktoken::Encoding] The encoding instance
|
17
|
+
def self.for_name_cached(encoding)
|
18
|
+
@encodings ||= {}
|
19
|
+
@encodings[encoding.to_sym] ||= Tiktoken::Encoding.for_name(encoding)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Encodes the text as a list of integer tokens. This encoding will encode special non text tokens
|
23
|
+
# basically it's unescaped
|
24
|
+
# @param text [String] The text to encode
|
25
|
+
# @return [Array<Integer>] The encoded tokens
|
26
|
+
def encode_ordinary(text)
|
27
|
+
@ext_base_bpe.encode_ordinary(text)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Encodes the text as a list of integer tokens. This encoding will treat special non text tokens
|
31
|
+
# as text unless they're in the allowed_special array. It's basically like the text was escaped
|
32
|
+
# @param text [String] The text to encode
|
33
|
+
# @param allowed_special [Array<String>] An array of special tokens to allow
|
34
|
+
# @return [Array<Integer>] The encoded tokens
|
35
|
+
def encode(text, allowed_special: [])
|
36
|
+
@ext_base_bpe.encode(text, allowed_special)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Decodes the tokens back into text
|
40
|
+
# @param tokens [Array<Integer>] The tokens to decode
|
41
|
+
# @return [String] The decoded text
|
42
|
+
def decode(tokens)
|
43
|
+
@ext_base_bpe.decode(tokens)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def initialize(ext_base_bpe, name)
|
49
|
+
@ext_base_bpe = ext_base_bpe
|
50
|
+
@name = name
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "tiktoken_ruby/version"
|
4
|
+
require_relative "tiktoken_ruby/encoding"
|
5
|
+
|
6
|
+
begin
|
7
|
+
RUBY_VERSION =~ /(\d+\.\d+)/
|
8
|
+
require_relative "tiktoken_ruby/#{$1}/tiktoken_ruby"
|
9
|
+
rescue LoadError
|
10
|
+
require_relative "tiktoken_ruby/tiktoken_ruby"
|
11
|
+
end
|
12
|
+
|
13
|
+
module Tiktoken
|
14
|
+
class << self
|
15
|
+
# Returns an encoding by name. If the encoding is not already loaded it will be loaded, but otherwise
|
16
|
+
# it will reuse the instance of that type that was previous loaded
|
17
|
+
# @param name [Symbol|String] The name of the encoding to load
|
18
|
+
# @return [Tiktoken::Encoding] The encoding instance
|
19
|
+
# @example Encode and decode text
|
20
|
+
# enc = Tiktoken.get_encoding("cl100k_base")
|
21
|
+
# enc.decode(enc.encode("hello world")) #=> "hello world"
|
22
|
+
def get_encoding(name)
|
23
|
+
name = name.to_sym
|
24
|
+
return nil unless SUPPORTED_ENCODINGS.include?(name)
|
25
|
+
|
26
|
+
Tiktoken::Encoding.for_name_cached(name)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Gets the encoding for an OpenAI model
|
30
|
+
# @param model_name [Symbol|String] The name of the model to get the encoding for
|
31
|
+
# @return [Tiktoken::Encoding] The encoding instance
|
32
|
+
# @example Count tokens for text
|
33
|
+
# enc = Tiktoken.encoding_for_model("gpt-4")
|
34
|
+
# enc.encode("hello world").length #=> 2
|
35
|
+
def encoding_for_model(model_name)
|
36
|
+
PREFIX_MODELS.each do |prefix|
|
37
|
+
if model_name.to_s.start_with?("#{prefix}-")
|
38
|
+
model_name = prefix
|
39
|
+
break
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
encoding_name = MODEL_TO_ENCODING_NAME[model_name.to_sym]
|
44
|
+
return nil unless encoding_name
|
45
|
+
|
46
|
+
get_encoding(encoding_name)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Lists all the encodings that are supported
|
50
|
+
# @return [Array<Symbol>] The list of supported encodings
|
51
|
+
def list_encoding_names
|
52
|
+
SUPPORTED_ENCODINGS
|
53
|
+
end
|
54
|
+
|
55
|
+
# Lists all the models that are supported
|
56
|
+
# @return [Array<Symbol>] The list of supported models
|
57
|
+
def list_model_names
|
58
|
+
MODEL_TO_ENCODING_NAME.keys
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
SUPPORTED_ENCODINGS = [
|
64
|
+
:r50k_base,
|
65
|
+
:p50k_base,
|
66
|
+
:p50k_edit,
|
67
|
+
:cl100k_base
|
68
|
+
]
|
69
|
+
|
70
|
+
# taken from the python library here https://github.com/openai/tiktoken/blob/main/tiktoken/model.py#L13-L53
|
71
|
+
# that is also MIT licensed but by OpenAI
|
72
|
+
MODEL_TO_ENCODING_NAME = {
|
73
|
+
"gpt-4": "cl100k_base",
|
74
|
+
"gpt-3.5-turbo": "cl100k_base",
|
75
|
+
# text
|
76
|
+
"text-davinci-003": "p50k_base",
|
77
|
+
"text-davinci-002": "p50k_base",
|
78
|
+
"text-davinci-001": "r50k_base",
|
79
|
+
"text-curie-001": "r50k_base",
|
80
|
+
"text-babbage-001": "r50k_base",
|
81
|
+
"text-ada-001": "r50k_base",
|
82
|
+
davinci: "r50k_base",
|
83
|
+
curie: "r50k_base",
|
84
|
+
babbage: "r50k_base",
|
85
|
+
ada: "r50k_base",
|
86
|
+
# code
|
87
|
+
"code-davinci-002": "p50k_base",
|
88
|
+
"code-davinci-001": "p50k_base",
|
89
|
+
"code-cushman-002": "p50k_base",
|
90
|
+
"code-cushman-001": "p50k_base",
|
91
|
+
"davinci-codex": "p50k_base",
|
92
|
+
"cushman-codex": "p50k_base",
|
93
|
+
# edit
|
94
|
+
"text-davinci-edit-001": "p50k_edit",
|
95
|
+
"code-davinci-edit-001": "p50k_edit",
|
96
|
+
# embeddings
|
97
|
+
"text-embedding-ada-002": "cl100k_base",
|
98
|
+
# old embeddings
|
99
|
+
"text-similarity-davinci-001": "r50k_base",
|
100
|
+
"text-similarity-curie-001": "r50k_base",
|
101
|
+
"text-similarity-babbage-001": "r50k_base",
|
102
|
+
"text-similarity-ada-001": "r50k_base",
|
103
|
+
"text-search-davinci-doc-001": "r50k_base",
|
104
|
+
"text-search-curie-doc-001": "r50k_base",
|
105
|
+
"text-search-babbage-doc-001": "r50k_base",
|
106
|
+
"text-search-ada-doc-001": "r50k_base",
|
107
|
+
"code-search-babbage-code-001": "r50k_base",
|
108
|
+
"code-search-ada-code-001": "r50k_base"
|
109
|
+
}
|
110
|
+
|
111
|
+
# these are models that have a versioned models that are otherwise identical
|
112
|
+
PREFIX_MODELS = ["gpt-4", "gpt-3.5-turbo"]
|
113
|
+
end
|
114
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rz_tiktoken_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- IAPark
|
8
|
+
- judy
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2023-08-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rb_sys
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 0.9.81
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 0.9.81
|
28
|
+
description: |
|
29
|
+
An unofficial Ruby wrapper for Tiktoken, a BPE tokenizer written by and used by OpenAI. It
|
30
|
+
can be used to count the number of tokens in text before sending it to OpenAI APIs. This
|
31
|
+
is a fork of tiktoken_ruby by IAPark, which has been cross-compiled for multiple platforms.
|
32
|
+
This way compilation with Rust extensions doesn't need to happen wherever you are deploying it.
|
33
|
+
email:
|
34
|
+
- isaac.a.park@gmail.com
|
35
|
+
- clinton@judy.io
|
36
|
+
executables: []
|
37
|
+
extensions:
|
38
|
+
- ext/tiktoken_ruby/extconf.rb
|
39
|
+
extra_rdoc_files: []
|
40
|
+
files:
|
41
|
+
- ".rspec"
|
42
|
+
- ".standard.yml"
|
43
|
+
- Cargo.lock
|
44
|
+
- Cargo.toml
|
45
|
+
- Gemfile
|
46
|
+
- Gemfile.lock
|
47
|
+
- LICENSE.txt
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- doctest_helper.rb
|
51
|
+
- ext/tiktoken_ruby/Cargo.toml
|
52
|
+
- ext/tiktoken_ruby/extconf.rb
|
53
|
+
- ext/tiktoken_ruby/src/core_bpe_wrapper.rs
|
54
|
+
- ext/tiktoken_ruby/src/lib.rs
|
55
|
+
- lib/tiktoken_ruby.rb
|
56
|
+
- lib/tiktoken_ruby/encoding.rb
|
57
|
+
- lib/tiktoken_ruby/version.rb
|
58
|
+
- sig/tiktoken_ruby.rbs
|
59
|
+
homepage: https://github.com/retailzipline/tiktoken_ruby
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata:
|
63
|
+
homepage_uri: https://github.com/retailzipline/tiktoken_ruby
|
64
|
+
source_code_uri: https://github.com/retailzipline/tiktoken_ruby
|
65
|
+
documentation_uri: https://rubydoc.info/github/IAPark/tiktoken_ruby/main
|
66
|
+
allowed_push_host: https://rubygems.org
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.7.0
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 3.1.0
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.4.19
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Ruby wrapper for Tiktoken
|
86
|
+
test_files: []
|