This example demonstrates how to use the HuggingFace tokenizer loading functionality in pure-tokenizers.
- Loading public models - Download and use models like BERT, GPT-2, DistilBERT
- Custom cache directory - Control where models are cached locally
- Authentication - Access private or gated models with HF tokens
- Offline mode - Use cached models without network access
- Cache management - Query and clear model caches
go run main.goTo test authenticated model loading, set your HuggingFace token:
export HF_TOKEN=your_huggingface_token_here
go run main.goGet your token from: https://huggingface.co/settings/tokens
You can load any tokenizer from HuggingFace that uses the tokenizer.json format. Popular examples include:
bert-base-uncased- BERT base modelgpt2- GPT-2 modeldistilbert-base-uncased- DistilBERTsentence-transformers/all-MiniLM-L6-v2- Sentence transformergoogle/flan-t5-base- FLAN-T5 model
Models are cached locally for offline use:
- macOS:
~/Library/Caches/tokenizers/lib/hf/models/ - Linux:
~/.cache/tokenizers/lib/hf/models/or$XDG_CACHE_HOME/tokenizers/lib/hf/models/ - Windows:
%APPDATA%/tokenizers/lib/hf/models/
HF_TOKEN- Your HuggingFace authentication tokenHF_HOME- Override the default cache directory
The example includes comprehensive error handling for common scenarios:
- Network timeouts
- Authentication failures
- Rate limiting
- Model not found
- Cache permission issues
tok, err := tokenizers.FromHuggingFace("bert-base-uncased",
tokenizers.WithHFRevision("refs/pr/1"),
)tok, err := tokenizers.FromHuggingFace("large-model",
tokenizers.WithHFTimeout(60 * time.Second),
)tok, err := tokenizers.FromHuggingFace("private-model",
tokenizers.WithHFToken(token),
tokenizers.WithHFCacheDir("/custom/cache"),
tokenizers.WithHFTimeout(30 * time.Second),
tokenizers.WithHFRevision("v2.0"),
)