mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-02 01:28:38 +00:00
* feat(rust_bridge): count budget-check input tokens in Rust on all LLM routes Rust counts input tokens from the raw JSON body with the GIL released inside the existing budget reservation, covering every LLM route the auth dependency guards. It only fires for models on the Anthropic tokenizer when a budget is set, and Python counts whenever Rust is off, missing, or declines a body shape. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * perf(rust): count byte-level BPE tokens without the GPT-2 split regex (#40594) The oniguruma run of the ByteLevel pre-tokenizer regex is about 90% of encode_fast on a 100k token body (100 ms of the ~110 ms Rust admission count in the gateway pod). A hand-written scanner that yields the same pieces, then feeds the model directly, counts the same text in 10 ms. It only engages for tokenizers with the Anthropic shape (optional NFKC, ByteLevel without prefix space, no post-processor) and falls back to the full encoder when the text contains an added token. Parity with encode_fast is tested on random texts, the pieces are compared with the real pre-tokenizer, and the \p{L}/\p{N}/\s tables are checked against oniguruma for every code point. NFKC runs through unicode-normalization-alignments, the crate and Unicode tables NormalizedString::nfkc already uses, so the fast path normalizes exactly what the full encoder would. Using the newer unicode-normalization crate changed the count for 171 code points that gained compatibility decompositions after Unicode 9 (U+32FF, U+A7F1..). The fast normalizer is compared with the tokenizer's for every scalar value and on random texts. The scanner is built without mutable state: byte_char and mapped_len replace the const table builders and the reusable mapped buffer, and iter::successors replaces the stateful piece iterator. byte_chars_match_the_byte_level_alphabet checks the byte mapping against ByteLevel for every scalar value. Co-authored-by: yassin <yassin@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(rust_bridge): bound concurrent token-count encodes and share the Anthropic tokenizer predicate Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: yassin <yassin@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
155 lines
4.7 KiB
Rust
155 lines
4.7 KiB
Rust
//! `json.dumps(value)` with Python's default arguments: `", "` and `": "`
|
|
//! separators, `ensure_ascii=True`, and keys in insertion order.
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use serde::Serialize;
|
|
use serde_json::ser::{Formatter, Serializer};
|
|
|
|
use super::Error;
|
|
use super::types::TextValue;
|
|
|
|
pub(super) fn dumps(value: &TextValue) -> Result<String, Error> {
|
|
let mut output = Vec::with_capacity(serialized_len(value)?);
|
|
value
|
|
.serialize(&mut Serializer::with_formatter(
|
|
&mut output,
|
|
PythonFormatter,
|
|
))
|
|
.map_err(Error::JsonSerialization)?;
|
|
debug_assert_eq!(output.len(), output.capacity());
|
|
String::from_utf8(output).map_err(Error::JsonUtf8)
|
|
}
|
|
|
|
fn serialized_len(value: &TextValue) -> Result<usize, Error> {
|
|
match value {
|
|
TextValue::Null => Ok(4),
|
|
TextValue::Bool(true) => Ok(4),
|
|
TextValue::Bool(false) => Ok(5),
|
|
TextValue::Number(number) => match (number.as_i64(), number.as_u64()) {
|
|
(Some(number), _) => Ok(unsigned_len(number.unsigned_abs()) + usize::from(number < 0)),
|
|
(_, Some(number)) => Ok(unsigned_len(number)),
|
|
_ => Err(Error::FloatText),
|
|
},
|
|
TextValue::Text(text) => Ok(quoted_len(text)),
|
|
TextValue::List(items) => items
|
|
.iter()
|
|
.try_fold(2 + items.len().saturating_sub(1) * 2, |len, item| {
|
|
Ok(len + serialized_len(item)?)
|
|
}),
|
|
TextValue::Object(entries) => entries.iter().try_fold(
|
|
2 + entries.len().saturating_sub(1) * 2,
|
|
|len, (key, value)| Ok(len + quoted_len(key) + 2 + serialized_len(value)?),
|
|
),
|
|
}
|
|
}
|
|
|
|
fn unsigned_len(number: u64) -> usize {
|
|
if number == 0 {
|
|
1
|
|
} else {
|
|
number.ilog10() as usize + 1
|
|
}
|
|
}
|
|
|
|
fn quoted_len(value: &str) -> usize {
|
|
value.chars().fold(2, |len, character| {
|
|
len + match character {
|
|
'"' | '\\' | '\u{0008}' | '\u{000c}' | '\n' | '\r' | '\t' => 2,
|
|
'\u{0000}'..='\u{001f}' => 6,
|
|
' '..='~' => 1,
|
|
_ => character.len_utf16() * 6,
|
|
}
|
|
})
|
|
}
|
|
|
|
struct PythonFormatter;
|
|
|
|
impl Formatter for PythonFormatter {
|
|
fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
|
|
where
|
|
W: ?Sized + Write,
|
|
{
|
|
if !first {
|
|
writer.write_all(b", ")?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
|
|
where
|
|
W: ?Sized + Write,
|
|
{
|
|
if !first {
|
|
writer.write_all(b", ")?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn begin_object_value<W>(&mut self, writer: &mut W) -> io::Result<()>
|
|
where
|
|
W: ?Sized + Write,
|
|
{
|
|
writer.write_all(b": ")
|
|
}
|
|
|
|
fn write_string_fragment<W>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()>
|
|
where
|
|
W: ?Sized + Write,
|
|
{
|
|
for character in fragment.chars() {
|
|
if (' '..='~').contains(&character) {
|
|
write!(writer, "{character}")?;
|
|
continue;
|
|
}
|
|
let mut units = [0u16; 2];
|
|
for unit in character.encode_utf16(&mut units) {
|
|
write!(writer, "\\u{unit:04x}")?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use rstest::rstest;
|
|
|
|
use super::dumps;
|
|
|
|
#[rstest]
|
|
#[case::null("null", "null")]
|
|
#[case::boolean("true", "true")]
|
|
#[case::signed_integer("-3", "-3")]
|
|
#[case::unsigned_integer("18446744073709551615", "18446744073709551615")]
|
|
#[case::empty_array("[]", "[]")]
|
|
#[case::empty_object("{}", "{}")]
|
|
#[case::nested(
|
|
r#"{"first":1,"second":{"ok":true,"none":null},"third":[false,2]}"#,
|
|
r#"{"first": 1, "second": {"ok": true, "none": null}, "third": [false, 2]}"#
|
|
)]
|
|
#[case::string_escaping(
|
|
r#""caf\u00e9 \u2014 \ud83d\ude00 \"q\" \\ \n\t\u0001\u007f ~ /""#,
|
|
r#""caf\u00e9 \u2014 \ud83d\ude00 \"q\" \\ \n\t\u0001\u007f ~ /""#
|
|
)]
|
|
#[case::short_control_escapes(r#""\b\f\r""#, r#""\b\f\r""#)]
|
|
fn matches_python_json_dumps(#[case] input: &str, #[case] expected: &str) {
|
|
let value = serde_json::from_str(input).expect("fixture parses");
|
|
|
|
assert_eq!(dumps(&value).expect("fixture dumps"), expected);
|
|
}
|
|
|
|
#[rstest]
|
|
#[case::top_level("1.5")]
|
|
#[case::array("[1,2.5]")]
|
|
#[case::object(r#"{"nested":{"value":-0.25}}"#)]
|
|
fn rejects_floats(#[case] input: &str) {
|
|
let value = serde_json::from_str(input).expect("fixture parses");
|
|
let error = dumps(&value).expect_err("floats are declined");
|
|
|
|
assert_eq!(
|
|
error.to_string(),
|
|
"unsupported by the rust token counter: float text values are counted by the python path"
|
|
);
|
|
}
|
|
}
|