Description
tiktoken-go is a Go port of OpenAI's tiktoken, a fast byte-pair encoding tokenizer for OpenAI models. Go programs can use it to encode text and count tokens, looking up an encoding by its name or by the model it belongs to. The README has examples for both lookups, plus an example that counts tokens in chat API messages in the style used for gpt-3.5-turbo and gpt-4. That example notes the calculation may change over time, so the official documentation should be checked for exact figures. Token dictionaries are downloaded the first time an encoding is initialized, and the TIKTOKEN_CACHE_DIR variable sets where they are cached. For environments without network access, or where a download each run is unwanted, an alternative BPE loader can be plugged in through SetBpeLoader. An offline loader is available as a separate project. It is a library, and the source is public on GitHub.…
Highlights
- Port of the OpenAI tiktoken BPE tokenizer
- Encoding lookup by encoding name or by model
- Dictionary cache set with TIKTOKEN_CACHE_DIR
- Pluggable BPE loaders, including an offline option
- Examples for counting tokens in chat API messages
Ratings & Reviews
No ratings yet
Be the first to rate Tiktoken Go.
People also visit
Tiktoken Go FAQ
What is tiktoken-go?
A Go library that tokenizes text with the same BPE encodings as OpenAI's tiktoken.
How are the token dictionaries loaded?
They download when an encoding is first initialized. Set TIKTOKEN_CACHE_DIR to keep them on disk, or use a custom or offline BPE loader.
How do I install it?
Add it to a Go project using the install step in the README, then call GetEncoding or EncodingForModel.
Are the chat token counts exact?
The README says the chat counting method may change at any time and that official OpenAI documentation should be used when exact figures are needed.
Is it open source?
Yes. The code is public on GitHub.
What are the best Tiktoken Go alternatives?
Popular alternatives to Tiktoken Go include Tiktokenizer, Go Openai and Chatgpt. See all Tiktoken Go alternatives
