mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-11 15:16:28 +08:00
072557eba1
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Added GPT-5 family and made GPT-5/-mini the new defaults for Copilot scenarios and prompts. - Bug Fixes - Improved streaming chunk formats and reasoning/text semantics, consistent attachment mediaType handling, and more reliable reranking via log-prob handling. - Refactor - Unified maxOutputTokens usage; removed per-call step caps and migrated several tools to a unified inputSchema shape. - Chores - Upgraded AI SDK dependencies and bumped an internal dependency version. - Tests - Updated mocks and tests to reference GPT-5 variants and new stream formats. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
46 lines
1.1 KiB
Rust
46 lines
1.1 KiB
Rust
use std::collections::HashSet;
|
|
|
|
use tiktoken_rs::{get_bpe_from_tokenizer, tokenizer::Tokenizer as TiktokenTokenizer};
|
|
|
|
#[napi]
|
|
pub struct Tokenizer {
|
|
inner: tiktoken_rs::CoreBPE,
|
|
}
|
|
|
|
#[napi]
|
|
pub fn from_model_name(model_name: String) -> Option<Tokenizer> {
|
|
if model_name.starts_with("gpt-5") {
|
|
let bpe = get_bpe_from_tokenizer(TiktokenTokenizer::O200kBase).ok()?;
|
|
return Some(Tokenizer { inner: bpe });
|
|
}
|
|
let bpe = tiktoken_rs::get_bpe_from_model(&model_name).ok()?;
|
|
Some(Tokenizer { inner: bpe })
|
|
}
|
|
|
|
#[napi]
|
|
impl Tokenizer {
|
|
#[napi]
|
|
pub fn count(&self, content: String, allowed_special: Option<Vec<String>>) -> u32 {
|
|
let allowed_special = if let Some(allowed_special) = &allowed_special {
|
|
HashSet::from_iter(allowed_special.iter().map(|s| s.as_str()))
|
|
} else {
|
|
Default::default()
|
|
};
|
|
|
|
self.inner.encode(&content, &allowed_special).0.len() as u32
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_tokenizer() {
|
|
let tokenizer = from_model_name("gpt-5".to_string()).unwrap();
|
|
let content = "Hello, world!";
|
|
let count = tokenizer.count(content.to_string(), None);
|
|
assert!(count > 0);
|
|
}
|
|
}
|