mistral_common.tokens.tokenizers.utils
chunks(lst, chunk_size)
Chunk a list into smaller lists of a given size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lst
|
list[str]
|
The list to chunk. |
required |
chunk_size
|
int
|
The size of each chunk. |
required |
Returns:
| Type | Description |
|---|---|
Iterator[list[str]]
|
An iterator over the chunks. |
Examples:
Source code in src/mistral_common/tokens/tokenizers/utils.py
download_tokenizer_from_hf_hub(repo_id, cache_dir=None, token=None, revision=None, force_download=False, local_files_only=False)
Download the tokenizer file of a Mistral model from the Hugging Face Hub.
See here for a list of our OSS models.
Note
You need to install the huggingface_hub package to use this method.
Please run pip install mistral-common[hf-hub] to install it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_id
|
str
|
The Hugging Face repo ID, e.g. "mistralai/Mistral-Small-2411". |
required |
cache_dir
|
str | Path | None
|
Directory where the tokenizer is cached. If |
None
|
token
|
bool | str | None
|
Hugging Face access token for private repos. If |
None
|
revision
|
str | None
|
Git branch, tag, or commit hash to download. If |
None
|
force_download
|
bool
|
If |
False
|
local_files_only
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The downloaded tokenizer local path for the given model ID. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
ValueError
|
If |
FileNotFoundError
|
If local files were required but none were found in the cache. |
MistralCommonException
|
If an unexpected error occurs while listing the repo files on the Hugging Face Hub. |
Source code in src/mistral_common/tokens/tokenizers/utils.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
get_one_valid_tokenizer_file(files)
Get one valid tokenizer file from a list of files.
Valid files are tekken.json or sentencepiece files with a known version
suffix. If multiple valid files exist, tekken.json wins; otherwise the
last one in sorted order is chosen with a warning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
list[str]
|
The list of file paths to choose from. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The path to the selected tokenizer file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no valid tokenizer file is found. |
Source code in src/mistral_common/tokens/tokenizers/utils.py
list_local_hf_repo_files(repo_id, revision, cache_dir=None)
List the files of a locally cached Hugging Face repo.
Reads directly from the Hugging Face cache directory without network access. Branch/tag revisions are resolved to commit hashes via the refs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_id
|
str
|
The Hugging Face repo ID. |
required |
revision
|
str | None
|
Git branch, tag, or commit hash. If |
required |
cache_dir
|
str | Path | None
|
Directory where the repo is cached. If |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
The file names in the repo snapshot, or an empty list if the repo or |
list[str]
|
revision is not cached locally. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |