Skip to content
Merged
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
12 changes: 6 additions & 6 deletions crates/goose/src/token_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::sync::OnceCell;

use crate::conversation::message::Message;

static TOKENIZER: OnceCell<Arc<CoreBPE>> = OnceCell::const_new();
static TOKENIZER: OnceCell<Result<Arc<CoreBPE>, String>> = OnceCell::const_new();

const MAX_TOKEN_CACHE_SIZE: usize = 10_000;

Expand Down Expand Up @@ -182,15 +182,15 @@ impl TokenCounter {
}

async fn get_tokenizer() -> Result<Arc<CoreBPE>, String> {
let tokenizer = TOKENIZER
TOKENIZER
.get_or_init(|| async {
match tiktoken_rs::o200k_base() {
Ok(bpe) => Arc::new(bpe),
Err(e) => panic!("Failed to initialize o200k_base tokenizer: {}", e),
Ok(bpe) => Ok(Arc::new(bpe)),
Err(e) => Err(format!("Failed to initialize o200k_base tokenizer: {}", e)),
}
})
.await;
Ok(tokenizer.clone())
.await
.clone()
}

pub async fn create_token_counter() -> Result<TokenCounter, String> {
Expand Down