Update README.md
Browse files
README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
# Baseten Tokenizer
|
| 2 |
|
| 3 |
-
High-performance, Rust-backed BPE tokenization for inference.
|
| 4 |
-
package is named `basetenkenizer`.
|
| 5 |
|
| 6 |
-
##
|
| 7 |
|
| 8 |

|
| 9 |
|
|
@@ -17,59 +17,6 @@ Loading by Hugging Face model ID downloads `tokenizer.json` on first use and
|
|
| 17 |
then uses the local Hugging Face cache. Private or gated models use the usual
|
| 18 |
`HF_TOKEN` environment variable.
|
| 19 |
|
| 20 |
-
## Encode text
|
| 21 |
-
|
| 22 |
-
```python
|
| 23 |
-
from basetenkenizer import Tokenizer
|
| 24 |
-
|
| 25 |
-
tokenizer = Tokenizer.from_model("deepseek-ai/DeepSeek-V3.2")
|
| 26 |
-
encoding = tokenizer.encode(
|
| 27 |
-
"A very long prompt that is now much faster.",
|
| 28 |
-
add_special_tokens=False,
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
print(encoding.ids)
|
| 32 |
-
print(tokenizer.decode(encoding.ids))
|
| 33 |
-
```
|
| 34 |
-
|
| 35 |
-
`Tokenizer.from_file("tokenizer.json")` loads a local tokenizer. Encoding
|
| 36 |
-
objects expose `ids`, `attention_mask`, `type_ids`, and
|
| 37 |
-
`special_tokens_mask`; selected fields can be moved into NumPy arrays with
|
| 38 |
-
`encoding.into_numpy(...)`.
|
| 39 |
-
|
| 40 |
-
## Kimi K2.7 Code
|
| 41 |
-
|
| 42 |
-
Load the published tokenizer directly from its model repository:
|
| 43 |
-
|
| 44 |
-
```python
|
| 45 |
-
from basetenkenizer import Tokenizer
|
| 46 |
-
|
| 47 |
-
tokenizer = Tokenizer.from_model("moonshotai/Kimi-K2.7-Code")
|
| 48 |
-
ids = tokenizer.encode("def hello():\n return 'world'").ids
|
| 49 |
-
```
|
| 50 |
-
|
| 51 |
-
For a chat prompt, preserve the boundary between template control tokens and
|
| 52 |
-
untrusted message content with `encode_segments`:
|
| 53 |
-
|
| 54 |
-
```python
|
| 55 |
-
user_message = "Write a Python HTTP server."
|
| 56 |
-
|
| 57 |
-
segments = [
|
| 58 |
-
("<|im_user|>user<|im_middle|>", True),
|
| 59 |
-
(user_message, False),
|
| 60 |
-
("<|im_end|>", True),
|
| 61 |
-
("<|im_assistant|>assistant<|im_middle|><think>", True),
|
| 62 |
-
]
|
| 63 |
-
|
| 64 |
-
encoding = tokenizer.encode_segments(segments)
|
| 65 |
-
input_ids = encoding.ids
|
| 66 |
-
```
|
| 67 |
-
|
| 68 |
-
This is the minimal Kimi K2.7 Code user/assistant shape. Applications using
|
| 69 |
-
system messages, tools, images, or existing assistant messages should render
|
| 70 |
-
the complete official model template and retain the same control-text versus
|
| 71 |
-
message-text boundaries.
|
| 72 |
-
|
| 73 |
## Kimi K3
|
| 74 |
|
| 75 |
Kimi K3 uses XTML and a Python chat renderer rather than a Jinja chat
|
|
@@ -78,7 +25,7 @@ template. Raw text can still be encoded normally:
|
|
| 78 |
```python
|
| 79 |
from basetenkenizer import Tokenizer
|
| 80 |
|
| 81 |
-
tokenizer = Tokenizer.from_model("
|
| 82 |
ids = tokenizer.encode("Hello from Kimi K3").ids
|
| 83 |
```
|
| 84 |
|
|
@@ -141,6 +88,59 @@ and whole-segment BPE encoding is intentional.
|
|
| 141 |
Post-processing, truncation, and padding are applied after all segment IDs
|
| 142 |
have been joined.
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
## Use with Transformers
|
| 145 |
|
| 146 |
Call `patch_transformers` before loading a tokenizer:
|
|
|
|
| 1 |
+
# Baseten Tokenizer for Kimi K3
|
| 2 |
|
| 3 |
+
High-performance, Rust-backed BPE tokenization for inference. This repo contains the `tokenizer.json`.
|
| 4 |
+
The Python package is named `basetenkenizer`.
|
| 5 |
|
| 6 |
+
## Fast tokenization for Kimi K3
|
| 7 |
|
| 8 |

|
| 9 |
|
|
|
|
| 17 |
then uses the local Hugging Face cache. Private or gated models use the usual
|
| 18 |
`HF_TOKEN` environment variable.
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
## Kimi K3
|
| 21 |
|
| 22 |
Kimi K3 uses XTML and a Python chat renderer rather than a Jinja chat
|
|
|
|
| 25 |
```python
|
| 26 |
from basetenkenizer import Tokenizer
|
| 27 |
|
| 28 |
+
tokenizer = Tokenizer.from_model("baseten/kimi-k3-tokenizer")
|
| 29 |
ids = tokenizer.encode("Hello from Kimi K3").ids
|
| 30 |
```
|
| 31 |
|
|
|
|
| 88 |
Post-processing, truncation, and padding are applied after all segment IDs
|
| 89 |
have been joined.
|
| 90 |
|
| 91 |
+
## Encode text with basetenkenizer
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
from basetenkenizer import Tokenizer
|
| 95 |
+
|
| 96 |
+
tokenizer = Tokenizer.from_model("deepseek-ai/DeepSeek-V3.2")
|
| 97 |
+
encoding = tokenizer.encode(
|
| 98 |
+
"A very long prompt that is now much faster.",
|
| 99 |
+
add_special_tokens=False,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
print(encoding.ids)
|
| 103 |
+
print(tokenizer.decode(encoding.ids))
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
`Tokenizer.from_file("tokenizer.json")` loads a local tokenizer. Encoding
|
| 107 |
+
objects expose `ids`, `attention_mask`, `type_ids`, and
|
| 108 |
+
`special_tokens_mask`; selected fields can be moved into NumPy arrays with
|
| 109 |
+
`encoding.into_numpy(...)`.
|
| 110 |
+
|
| 111 |
+
## Kimi K2.7 Code with basetenkenizer
|
| 112 |
+
|
| 113 |
+
Load the published tokenizer directly from its model repository:
|
| 114 |
+
|
| 115 |
+
```python
|
| 116 |
+
from basetenkenizer import Tokenizer
|
| 117 |
+
|
| 118 |
+
tokenizer = Tokenizer.from_model("moonshotai/Kimi-K2.7-Code")
|
| 119 |
+
ids = tokenizer.encode("def hello():\n return 'world'").ids
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
For a chat prompt, preserve the boundary between template control tokens and
|
| 123 |
+
untrusted message content with `encode_segments`:
|
| 124 |
+
|
| 125 |
+
```python
|
| 126 |
+
user_message = "Write a Python HTTP server."
|
| 127 |
+
|
| 128 |
+
segments = [
|
| 129 |
+
("<|im_user|>user<|im_middle|>", True),
|
| 130 |
+
(user_message, False),
|
| 131 |
+
("<|im_end|>", True),
|
| 132 |
+
("<|im_assistant|>assistant<|im_middle|><think>", True),
|
| 133 |
+
]
|
| 134 |
+
|
| 135 |
+
encoding = tokenizer.encode_segments(segments)
|
| 136 |
+
input_ids = encoding.ids
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
This is the minimal Kimi K2.7 Code user/assistant shape. Applications using
|
| 140 |
+
system messages, tools, images, or existing assistant messages should render
|
| 141 |
+
the complete official model template and retain the same control-text versus
|
| 142 |
+
message-text boundaries.
|
| 143 |
+
|
| 144 |
## Use with Transformers
|
| 145 |
|
| 146 |
Call `patch_transformers` before loading a tokenizer:
|