{"id":49311442,"url":"https://github.com/zerfoo/ztoken","last_synced_at":"2026-04-26T13:03:05.611Z","repository":{"id":344714557,"uuid":"1182817992","full_name":"zerfoo/ztoken","owner":"zerfoo","description":"BPE tokenizer library for Go with HuggingFace tokenizer.json and GGUF tokenizer extraction support. Zero external dependencies.","archived":false,"fork":false,"pushed_at":"2026-03-26T18:32:08.000Z","size":100,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-27T06:46:29.790Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zerfoo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-16T01:39:03.000Z","updated_at":"2026-03-26T18:32:08.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/zerfoo/ztoken","commit_stats":null,"previous_names":["zerfoo/ztoken"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/zerfoo/ztoken","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zerfoo%2Fztoken","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zerfoo%2Fztoken/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zerfoo%2Fztoken/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zerfoo%2Fztoken/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zerfoo","download_url":"https://codeload.github.com/zerfoo/ztoken/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zerfoo%2Fztoken/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32297907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T09:34:17.070Z","status":"ssl_error","status_checked_at":"2026-04-26T09:34:00.993Z","response_time":129,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-04-26T13:02:54.507Z","updated_at":"2026-04-26T13:03:05.602Z","avatar_url":"https://github.com/zerfoo.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ztoken\n\n[![CI](https://github.com/zerfoo/ztoken/actions/workflows/ci.yml/badge.svg)](https://github.com/zerfoo/ztoken/actions/workflows/ci.yml)\n[![Go Reference](https://pkg.go.dev/badge/github.com/zerfoo/ztoken.svg)](https://pkg.go.dev/github.com/zerfoo/ztoken)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n\nBPE tokenizer library for Go with HuggingFace compatibility.\n\nPart of the [Zerfoo](https://github.com/zerfoo) ML ecosystem.\n\n## Features\n\n- **Byte-Pair Encoding (BPE)** tokenizer with full merge-based encoding/decoding\n- **HuggingFace `tokenizer.json`** loading — compatible with GPT-2, Llama, Gemma, Mistral, and other models\n- **GGUF tokenizer extraction** — extract tokenizer data directly from GGUF model files via `ztoken/gguf`\n- **SentencePiece compatibility** — handles U+2581 space markers used by Llama-family models\n- **Special token handling** — BOS, EOS, PAD, UNK with exact-match encoding for control tokens\n- **Byte-level BPE** — GPT-2 style byte-to-Unicode encoding for full UTF-8 coverage\n- **Text normalization** — configurable normalizer pipeline (NFC, NFD, NFKC, lowercase, etc.)\n- **Zero external dependencies** — stdlib only, plus `golang.org/x/text` for Unicode normalization\n\n## Installation\n\n```bash\ngo get github.com/zerfoo/ztoken\n```\n\n## Quick Start\n\n### Load from HuggingFace tokenizer.json\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    \"github.com/zerfoo/ztoken\"\n)\n\nfunc main() {\n    // Load a HuggingFace tokenizer.json file\n    tok, err := ztoken.LoadFromJSON(\"tokenizer.json\")\n    if err != nil {\n        panic(err)\n    }\n\n    // Encode text to token IDs\n    ids, _ := tok.Encode(\"Hello, world!\")\n    fmt.Println(ids)\n\n    // Decode token IDs back to text\n    text, _ := tok.Decode(ids)\n    fmt.Println(text) // Hello, world!\n\n    // Inspect vocabulary\n    fmt.Println(tok.VocabSize())\n\n    // Access special tokens\n    special := tok.SpecialTokens()\n    fmt.Printf(\"BOS=%d EOS=%d PAD=%d UNK=%d\\n\",\n        special.BOS, special.EOS, special.PAD, special.UNK)\n}\n```\n\n### Extract Tokenizer from GGUF Model Files\n\nThe `ztoken/gguf` sub-package extracts tokenizer data directly from GGUF model files, so you don't need a separate `tokenizer.json`:\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    \"github.com/zerfoo/ztoken/gguf\"\n)\n\nfunc main() {\n    // metadata is any type implementing gguf.Metadata interface:\n    //   GetString(key string) (string, bool)\n    //   GetStringArray(key string) ([]string, bool)\n    //   GetUint32(key string) (uint32, bool)\n    //   GetInt32Array(key string) ([]int32, bool)\n    tok, err := gguf.ExtractTokenizer(metadata)\n    if err != nil {\n        panic(err)\n    }\n\n    ids, _ := tok.Encode(\"Hello from GGUF!\")\n    fmt.Println(ids)\n}\n```\n\n### Build a Tokenizer Programmatically\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    \"github.com/zerfoo/ztoken\"\n)\n\nfunc main() {\n    vocab := map[string]int{\n        \"hello\": 0, \"world\": 1, \" \": 2,\n        \"\u003cunk\u003e\": 3, \"\u003cs\u003e\": 4, \"\u003c/s\u003e\": 5, \"\u003cpad\u003e\": 6,\n    }\n    merges := []ztoken.MergePair{\n        {Left: \"hel\", Right: \"lo\"},\n        {Left: \"wor\", Right: \"ld\"},\n    }\n    special := ztoken.SpecialTokens{BOS: 4, EOS: 5, PAD: 6, UNK: 3}\n\n    tok := ztoken.NewBPETokenizer(vocab, merges, special, false)\n    ids, _ := tok.Encode(\"hello\")\n    fmt.Println(ids) // [0]\n}\n```\n\n## SentencePiece Compatibility\n\nModels using SentencePiece tokenization (Llama, Gemma) encode spaces as the U+2581 character. ztoken handles this automatically when loading from GGUF files with `tokenizer.ggml.model = \"llama\"`, or you can enable it manually:\n\n```go\ntok := ztoken.NewBPETokenizer(vocab, merges, special, false)\ntok.SetSentencePiece(true)\n```\n\n## Use Cases\n\n- **ML inference preprocessing** — tokenize prompts before feeding them to transformer models via [zerfoo](https://github.com/zerfoo/zerfoo)\n- **Text processing pipelines** — encode/decode text with production-grade BPE\n- **Model tooling** — extract and inspect tokenizers from GGUF and HuggingFace model files\n- **Embedding in Go services** — zero-CGo tokenization that compiles with `go build`\n\n## Package Structure\n\n| Package | Description |\n|---------|-------------|\n| `ztoken` | Core tokenizer interface, BPE implementation, HuggingFace JSON loader |\n| `ztoken/gguf` | GGUF metadata-based tokenizer extraction |\n\n## Dependencies\n\nztoken has zero external dependencies beyond the Go standard library and `golang.org/x/text` for Unicode normalization.\n\nztoken is used by:\n\n- [zerfoo](https://github.com/zerfoo/zerfoo) — ML inference, training, and serving framework\n\n## License\n\nApache 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzerfoo%2Fztoken","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzerfoo%2Fztoken","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzerfoo%2Fztoken/lists"}