diff --git a/Cargo.lock b/Cargo.lock index cbfdcfd..d9c1f73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,7 +36,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "rustc-hash 2.1.2", + "rustc-hash", "shlex", "syn", ] @@ -218,18 +218,18 @@ dependencies = [ [[package]] name = "rb-sys" -version = "0.9.127" +version = "0.9.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7d7c9560fe42dcffa576941394075f18a17dce89fcf718a2fa90b7dc2134d12" +checksum = "45ca28513560e56cfb79a62b1fce363c73af170a182024ce880c77ee9429920a" dependencies = [ "rb-sys-build", ] [[package]] name = "rb-sys-build" -version = "0.9.127" +version = "0.9.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1688e8f32967ba48c89e4dfa283b57f901075f542fc7ee9c3d7c5f9091ca1d9" +checksum = "ce04b2c55eff3a21aaa623fcc655d94373238e72cac6b3e1a3641ff31649f99a" dependencies = [ "bindgen", "lazy_static", @@ -275,12 +275,6 @@ version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" -[[package]] -name = "rustc-hash" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - [[package]] name = "rustc-hash" version = "2.1.2" @@ -347,9 +341,9 @@ dependencies = [ [[package]] name = "tiktoken-rs" -version = "0.11.0" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fac4a168cfc1d8ed65bf17a6ee0843ad9a68f863c63c0fb2fa7eab67838782ee" +checksum = "027853bbf8c7763b77c5c595f1c271c7d536ced7d6f83452911b944621e57fc2" dependencies = [ "anyhow", "base64", @@ -357,7 +351,7 @@ dependencies = [ "fancy-regex", "lazy_static", "regex", - "rustc-hash 1.1.0", + "rustc-hash", ] [[package]] diff --git a/ext/tiktoken_ruby/Cargo.toml b/ext/tiktoken_ruby/Cargo.toml index 004fea7..27b1f84 100644 --- a/ext/tiktoken_ruby/Cargo.toml +++ b/ext/tiktoken_ruby/Cargo.toml @@ -12,4 +12,4 @@ crate-type = ["cdylib"] [dependencies] magnus = { version = "0.8.2" } rb-sys = { version = "0.9.127", features = ["stable-api-compiled-fallback"] } -tiktoken-rs = { version = "0.11.0" } +tiktoken-rs = { version = "0.12.0" } diff --git a/ext/tiktoken_ruby/src/core_bpe_wrapper.rs b/ext/tiktoken_ruby/src/core_bpe_wrapper.rs index c054ff6..5feded7 100644 --- a/ext/tiktoken_ruby/src/core_bpe_wrapper.rs +++ b/ext/tiktoken_ruby/src/core_bpe_wrapper.rs @@ -20,7 +20,7 @@ struct EncodeData { core_bpe: *const tiktoken_rs::CoreBPE, text: String, allowed_special: HashSet, - result: Vec, + result: Result, String>, } struct EncodeSpecialData { @@ -46,7 +46,11 @@ unsafe extern "C" fn encode_without_gvl(data: *mut c_void) -> *mut c_void { let data = &mut *(data as *mut EncodeData); let core_bpe = &*data.core_bpe; let allowed_special: HashSet<&str> = data.allowed_special.iter().map(|s| s.as_str()).collect(); - data.result = core_bpe.encode(&data.text, &allowed_special).0; + // Keep only the token vector and surface any tokenization failure to Ruby. + data.result = core_bpe + .encode(&data.text, &allowed_special) + .map(|(tokens, _last_piece_token_len)| tokens) + .map_err(|e| e.to_string()); std::ptr::null_mut() } @@ -99,7 +103,7 @@ impl CoreBPEWrapper { core_bpe: &self.core_bpe as *const _, text, allowed_special: HashSet::from_iter(allowed_special), - result: Vec::new(), + result: Ok(Vec::new()), }; unsafe { @@ -111,7 +115,9 @@ impl CoreBPEWrapper { ); } - Ok(data.result) + data.result.map_err(|e| { + magnus::Error::new(magnus::Ruby::get().unwrap().exception_runtime_error(), e) + }) } pub fn encode_with_special_tokens(&self, text: String) -> Vec {