Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format token count with thousands separator #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ env_logger = "0.11.3"
arboard = "3.4.0"
pyo3 = { version = "0.23", features = ["extension-module", "abi3-py312", "generate-import-lib"] }
globset = "0.4.15"
num-format = "0.4"

[profile.release]
lto = "thin"
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use indicatif::{ProgressBar, ProgressStyle};
use log::{debug, error};
use serde_json::json;
use std::path::PathBuf;
use num_format::{Locale, ToFormattedString};

// Constants
const DEFAULT_TEMPLATE_NAME: &str = "default";
Expand All @@ -23,8 +24,8 @@ const CUSTOM_TEMPLATE_NAME: &str = "custom";
// CLI Arguments
#[derive(Parser)]
#[clap(
name = env!("CARGO_PKG_NAME"),
version = env!("CARGO_PKG_VERSION"),
name = env!("CARGO_PKG_NAME"),
version = env!("CARGO_PKG_VERSION"),
author = env!("CARGO_PKG_AUTHORS")
)]
#[command(arg_required_else_help = true)]
Expand Down Expand Up @@ -261,12 +262,13 @@ fn main() -> Result<()> {
println!("{}", serde_json::to_string_pretty(&json_output)?);
return Ok(());
} else if args.tokens {
let formatted_token_count = token_count.to_formatted_string(&Locale::en);
println!(
"{}{}{} Token count: {}, Model info: {}",
"[".bold().white(),
"i".bold().blue(),
"]".bold().white(),
token_count.to_string().bold().yellow(),
formatted_token_count.bold().yellow(),
model_info
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use colored::*;
use tiktoken_rs::{cl100k_base, o200k_base, p50k_base, p50k_edit, r50k_base, CoreBPE};
use num_format::{Locale, ToFormattedString};

/// Returns the appropriate tokenizer based on the provided encoding.
///
Expand Down Expand Up @@ -69,13 +70,14 @@ pub fn count_tokens(rendered: &str, encoding: &Option<String>) {
};

let token_count = bpe.unwrap().encode_with_special_tokens(rendered).len();
let formatted_token_count = token_count.to_formatted_string(&Locale::en);

println!(
"{}{}{} Token count: {}, Model info: {}",
"[".bold().white(),
"i".bold().blue(),
"]".bold().white(),
token_count.to_string().bold().yellow(),
formatted_token_count.bold().yellow(),
model_info
);
}