pub struct TokenSequence {
pub vocabulary_id: VocabularyId,
pub vocabulary_version: Version,
pub vocabulary_digest: String,
pub modality: Modality,
pub adapter: Option<String>,
pub ids: Vec<u32>,
pub escapes: Vec<String>,
}Expand description
Token ids scoped to the exact vocabulary that produced them.
Ids from different vocabularies are never interchangeable, so a sequence always travels with the vocabulary id, version and content digest.
Fields§
§vocabulary_id: VocabularyId§vocabulary_version: Version§vocabulary_digest: String§modality: Modality§adapter: Option<String>§ids: Vec<u32>§escapes: Vec<String>Symbols the vocabulary does not contain, carried literally. An id of
vocabulary length + i stands for escapes[i]. Empty unless the
sequence was encoded with UnknownPolicy::Escape.
Implementations§
Source§impl TokenSequence
impl TokenSequence
Sourcepub fn to_bytes(&self) -> Result<Vec<u8>>
pub fn to_bytes(&self) -> Result<Vec<u8>>
Serialize to the .kute container: magic, little-endian header length,
JSON header, then LEB128 varint token ids.
Sourcepub fn to_bytes_compressed(&self) -> Result<Vec<u8>>
pub fn to_bytes_compressed(&self) -> Result<Vec<u8>>
Serialize to the compressed .kute container (format 2): the same
header, then the ids as fixed-width little-endian integers inside one
zlib stream. Smaller than format 1 on anything repetitive; from_bytes
reads both.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Parse a .kute container of either format. Rejects truncated,
oversized or trailing data.
A compressed container can hold about four thousand times its own
size in ids. For a container from a party you do not trust, use
TokenSequence::from_bytes_bounded.
Sourcepub fn from_bytes_bounded(bytes: &[u8], max_tokens: u64) -> Result<Self>
pub fn from_bytes_bounded(bytes: &[u8], max_tokens: u64) -> Result<Self>
As TokenSequence::from_bytes, but a container that declares more
than max_tokens tokens is refused before any id is read or
decompressed, so the caller decides how much memory a container may
claim.