MCPcopy Create free account
hub / github.com/M4THYOU/TokenDagger / LoadTokenizer

Function LoadTokenizer

src/mistral_main.cpp:99–146  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

97
98
99void LoadTokenizer(const std::string& filename, MistralTokenizer& tokenizer) {
100 std::ifstream file(filename);
101 nlohmann::json json_data;
102 file >> json_data;
103
104 // Parse config
105 tokenizer.config = json_data["config"].get<TokenizerConfig>();
106
107 // Parse vocab with custom handling for optional token_str
108 tokenizer.vocab.clear();
109
110 int vocab_size = tokenizer.config.default_vocab_size - tokenizer.config.default_num_special_tokens;
111
112 for (size_t i = 0; i < vocab_size; i++) {
113 const auto& vocab_item = json_data["vocab"][i];
114 VocabItem item;
115 item.rank = vocab_item["rank"].get<int>();
116
117 std::string token_bytes_str = vocab_item["token_bytes"].get<std::string>();
118
119 // Decode base64 string to binary data
120 std::string decoded_bytes;
121 try {
122 decoded_bytes = base64_decode(token_bytes_str);
123 } catch (const std::exception& e) {
124 fprintf(stderr, "ERROR: Failed to decode base64 token_bytes: %s\n", e.what());
125 continue; // Skip this vocab item
126 }
127
128 item.token_bytes = std::vector<unsigned char>(decoded_bytes.begin(), decoded_bytes.end());
129
130 // Handle optional token_str field
131 if (vocab_item.contains("token_str") && !vocab_item["token_str"].is_null()) {
132 item.token_str = vocab_item["token_str"].get<std::string>();
133 } else {
134 item.token_str = ""; // Default to empty string if not present or null
135 }
136
137 tokenizer.vocab.push_back(item);
138 }
139
140 // Create the BPE tokenizer and initialize it
141 tokenizer.bpe = std::make_unique<tiktoken::CoreBPE>(tokenizer.vocab);
142 if (!tokenizer.bpe->init_regex(tokenizer.config.pattern)) {
143 fprintf(stderr, "ERROR: Failed to initialize regex for tokenizer\n");
144 tokenizer.bpe.reset(); // Clear the pointer
145 }
146}
147
148
149void Tokenize(const MistralTokenizer& tokenizer, const std::string& prompt) {

Callers 1

mainFunction · 0.70

Calls 7

whatMethod · 0.80
init_regexMethod · 0.80
base64_decodeFunction · 0.70
clearMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected