|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | #include <tokenizers/tokenizers.h> |
6 | | -#include <jinja2cpp/template.h> |
7 | | -#include <jinja2cpp/value.h> |
| 6 | +#include <sstream> |
| 7 | +#include <iomanip> |
8 | 8 |
|
9 | 9 | namespace tokenizers { |
10 | 10 |
|
| 11 | +// Helper to escape JSON strings - handles special characters properly |
| 12 | +static std::string json_escape(const std::string& input) { |
| 13 | + std::string output; |
| 14 | + output.reserve(input.size() * 1.1); // Reserve extra space for escapes |
| 15 | + for (unsigned char c : input) { |
| 16 | + switch (c) { |
| 17 | + case '"': output += "\\\""; break; |
| 18 | + case '\\': output += "\\\\"; break; |
| 19 | + case '\b': output += "\\b"; break; |
| 20 | + case '\f': output += "\\f"; break; |
| 21 | + case '\n': output += "\\n"; break; |
| 22 | + case '\r': output += "\\r"; break; |
| 23 | + case '\t': output += "\\t"; break; |
| 24 | + default: |
| 25 | + if (c < 0x20) { |
| 26 | + // Control characters: escape as \uXXXX |
| 27 | + char buf[7]; |
| 28 | + snprintf(buf, sizeof(buf), "\\u%04x", c); |
| 29 | + output += buf; |
| 30 | + } else { |
| 31 | + output += c; |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + return output; |
| 36 | +} |
| 37 | + |
11 | 38 | std::string Tokenizer::apply_chat_template( |
| 39 | + const std::string& template_str, |
12 | 40 | const std::vector<ChatMessage>& messages, |
13 | 41 | bool add_generation_prompt |
14 | 42 | ) const { |
15 | | - // Get the template string |
16 | | - std::string tmpl_str = chat_template(); |
17 | | - if (tmpl_str.empty()) { |
18 | | - throw ChatTemplateError("No chat template available for this tokenizer"); |
| 43 | + // Build messages JSON array manually |
| 44 | + std::stringstream ss; |
| 45 | + ss << "["; |
| 46 | + for (size_t i = 0; i < messages.size(); ++i) { |
| 47 | + if (i > 0) ss << ","; |
| 48 | + ss << "{\"role\":\"" << json_escape(messages[i].role) |
| 49 | + << "\",\"content\":\"" << json_escape(messages[i].content) << "\"}"; |
19 | 50 | } |
| 51 | + ss << "]"; |
| 52 | + std::string messages_json_str = ss.str(); |
20 | 53 |
|
21 | | - // Create Jinja2 template |
22 | | - jinja2::Template tpl; |
23 | | - auto load_result = tpl.Load(tmpl_str, "chat_template"); |
24 | | - if (!load_result) { |
25 | | - throw ChatTemplateError("Failed to parse chat template: " + |
26 | | - load_result.error().ToString()); |
27 | | - } |
| 54 | + // Get special tokens (pass as C strings, can be null) |
| 55 | + std::string bos_str = bos_token(); |
| 56 | + std::string eos_str = eos_token(); |
| 57 | + const char* bos_ptr = bos_str.empty() ? nullptr : bos_str.c_str(); |
| 58 | + const char* eos_ptr = eos_str.empty() ? nullptr : eos_str.c_str(); |
28 | 59 |
|
29 | | - // Convert messages to Jinja2 values |
30 | | - jinja2::ValuesList jinja_messages; |
31 | | - for (const auto& msg : messages) { |
32 | | - jinja2::ValuesMap msg_map; |
33 | | - msg_map["role"] = msg.role; |
34 | | - msg_map["content"] = msg.content; |
35 | | - jinja_messages.push_back(std::move(msg_map)); |
36 | | - } |
| 60 | + // Call C FFI function with custom template |
| 61 | + char* error_msg = nullptr; |
| 62 | + char* result = tokenizers_apply_chat_template( |
| 63 | + handle_, |
| 64 | + template_str.c_str(), |
| 65 | + messages_json_str.c_str(), |
| 66 | + add_generation_prompt, |
| 67 | + bos_ptr, |
| 68 | + eos_ptr, |
| 69 | + &error_msg |
| 70 | + ); |
37 | 71 |
|
38 | | - // Build parameters map |
39 | | - jinja2::ValuesMap params; |
40 | | - params["messages"] = std::move(jinja_messages); |
41 | | - params["add_generation_prompt"] = add_generation_prompt; |
| 72 | + if (result == nullptr) { |
| 73 | + std::string error = error_msg ? error_msg : "Failed to apply chat template"; |
| 74 | + if (error_msg) { |
| 75 | + tokenizers_string_free(error_msg); |
| 76 | + } |
| 77 | + throw ChatTemplateError(error); |
| 78 | + } |
42 | 79 |
|
43 | | - // Add special tokens as variables (commonly used in templates) |
44 | | - params["bos_token"] = bos_token(); |
45 | | - params["eos_token"] = eos_token(); |
46 | | - params["pad_token"] = pad_token(); |
47 | | - params["unk_token"] = unk_token(); |
| 80 | + std::string rendered(result); |
| 81 | + tokenizers_string_free(result); |
48 | 82 |
|
49 | | - // Render the template |
50 | | - auto render_result = tpl.RenderAsString(params); |
51 | | - if (!render_result) { |
52 | | - throw ChatTemplateError("Failed to render chat template: " + |
53 | | - render_result.error().ToString()); |
| 83 | + return rendered; |
| 84 | +} |
| 85 | + |
| 86 | +std::string Tokenizer::apply_chat_template( |
| 87 | + const std::vector<ChatMessage>& messages, |
| 88 | + bool add_generation_prompt |
| 89 | +) const { |
| 90 | + // Get the template string from config and delegate to the overload |
| 91 | + std::string tmpl_str = chat_template(); |
| 92 | + if (tmpl_str.empty()) { |
| 93 | + throw ChatTemplateError("No chat template available for this tokenizer"); |
54 | 94 | } |
55 | | - |
56 | | - return render_result.value(); |
| 95 | + return apply_chat_template(tmpl_str, messages, add_generation_prompt); |
57 | 96 | } |
58 | 97 |
|
59 | 98 | } // namespace tokenizers |
0 commit comments