diff --git a/core/text/tokenizer.h b/core/text/tokenizer.h index 96a2c822..fe0740fc 100644 --- a/core/text/tokenizer.h +++ b/core/text/tokenizer.h @@ -320,6 +320,12 @@ int parse_tokenizer_json(const char* path, VocabBuild& out); /* Same, from an in-memory buffer, for a tokenizer.json that arrived some other way. */ int parse_tokenizer_json_buf(std::string_view json, const char* origin, VocabBuild& out); +/* Read a standalone chat template file into out.chat_template, trailing newlines stripped. + * A release that ships no template (DeepSeek's does not) needs one from elsewhere; the caller + * passes the file. An unreadable or empty file is an error, never a silent skip. Returns + * RAD_OK, or RAD_E_IO when the file cannot be read. */ +int read_chat_template_path(const char* path, VocabBuild& out); + /* Build a VocabBuild from a GGUF's tokenizer.ggml.* metadata. * * GGUF has no declared chain, only `tokenizer.ggml.pre`, a name llama.cpp derived by hashing the diff --git a/core/text/tokenizer_json.cpp b/core/text/tokenizer_json.cpp index 4ce100ea..79e389e4 100644 --- a/core/text/tokenizer_json.cpp +++ b/core/text/tokenizer_json.cpp @@ -638,6 +638,27 @@ void read_config(const std::string& dir, VocabBuild& out) { } /* namespace */ +int read_chat_template_path(const char* path, VocabBuild& out) { + if (!path) return RAD_E_INVAL; + std::ifstream f(path); + if (!f) { + RAD_ERR("tokenizer: cannot read chat template %s", path); + return RAD_E_IO; + } + std::ostringstream ss; + ss << f.rdbuf(); + std::string t = ss.str(); + while (!t.empty() && (t.back() == '\n' || t.back() == '\r')) t.pop_back(); + if (t.empty()) { + RAD_ERR("tokenizer: chat template %s is empty", path); + return RAD_E_IO; + } + RAD_INFO("tokenizer: chat template from %s (%zu bytes), overriding the checkpoint's", + path, t.size()); + out.chat_template = std::move(t); + return RAD_OK; +} + /* ================================================================== parse_tokenizer_json */ namespace { diff --git a/tests/convert_test.cpp b/tests/convert_test.cpp index d529e0a3..8e2de6a8 100644 --- a/tests/convert_test.cpp +++ b/tests/convert_test.cpp @@ -12,6 +12,7 @@ #include "format/encoding.h" #include "format/radfile.h" #include "gen_config.h" +#include "text/tokenizer.h" #include @@ -967,4 +968,27 @@ TEST(an_fp8_kv_cache_declares) { CHECK(exists(d / "k.rad")); } +/* --chat-template bakes a template into the container's vocab section, for a release that ships + * none (DeepSeek's). Without the flag the container carries what the checkpoint carried. A missing + * file is refused, not silently skipped. */ +TEST(chat_template_flag_bakes_the_template) { + World& w = world(); + REQUIRE(w.ok); + tiny::TempDir d("convert_ct"); + const std::string tmpl = d / "chat_template.jinja"; + REQUIRE(tiny::write_text(tmpl, "{%- raw %}{{ messages }}{%- endraw %}\n\n")); + std::string log; + REQUIRE_EQ(convert(w.ckpt, { "-o", d / "with.rad", "--recipe", w.recipe, + "--chat-template", tmpl }, &log), 0); + RadFile f; + REQUIRE_EQ(f.open((d / "with.rad").c_str()), RAD_OK); + VocabBuild vb; + REQUIRE_EQ(vocab_deserialize(f.base(), (uint64_t)f.size(), f.vocab().h, vb), RAD_OK); + CHECK_EQ(vb.chat_template, std::string("{%- raw %}{{ messages }}{%- endraw %}")); + + std::string bad; + CHECK_EQ(convert(w.ckpt, { "-o", d / "bad.rad", "--recipe", w.recipe, + "--chat-template", d / "absent.jinja" }, &bad), 1); +} + RAD_TEST_MAIN() diff --git a/tests/text_test.cpp b/tests/text_test.cpp index ebbda93d..618f5897 100644 --- a/tests/text_test.cpp +++ b/tests/text_test.cpp @@ -21,6 +21,8 @@ */ #include "rad_test.h" +#include + #include "text/gguf.h" #include "text/tokenizer.h" #include "text/tokenizer_nfc.h" @@ -2240,4 +2242,29 @@ TEST(the_normal_forms_agree_with_an_independent_implementation) { CHECK(rows > 2500); } +/* A release that ships no chat template (DeepSeek's) is why --chat-template exists: the file is + * read, trailing newlines stripped exactly as the checkpoint's own file is, and an unreadable or + * empty file is refused rather than skipped. */ +TEST(read_chat_template_path_strips_newlines_and_refuses_empty) { + VocabBuild vb; + const std::string p = radtest::tmp_path("chat_template.jinja"); + { + std::ofstream f(p); + f << "{%- raw %}{{ messages }}{%- endraw %}\n\n"; + } + CHECK_OK(read_chat_template_path(p.c_str(), vb)); + CHECK_EQ(vb.chat_template, std::string("{%- raw %}{{ messages }}{%- endraw %}")); + + vb.chat_template.clear(); + { + std::ofstream f(p); + f << "\n\r\n"; + } + CHECK_EQ(read_chat_template_path(p.c_str(), vb), (int)RAD_E_IO); + CHECK(vb.chat_template.empty()); + + CHECK_EQ(read_chat_template_path(radtest::tmp_path("absent.jinja").c_str(), vb), (int)RAD_E_IO); + std::remove(p.c_str()); +} + RAD_TEST_MAIN() diff --git a/tools/rad_convert.cpp b/tools/rad_convert.cpp index fd4571b8..e0b7c6ff 100644 --- a/tools/rad_convert.cpp +++ b/tools/rad_convert.cpp @@ -132,6 +132,10 @@ void usage() { " --imatrix F llama.cpp imatrix: the expert-popularity profile, the column\n" " importances handed to each quantiser, and the weighting of the\n" " error report\n" + " --chat-template F a jinja template baked as the container's chat template,\n" + " overriding the checkpoint's own (or its absence). A release that\n" + " ships none (DeepSeek's) needs one, or /v1/chat/completions answers\n" + " 501. Trailing newlines are stripped\n" " --set K=V a metadata key, seen by declare AND written into the container,\n" " so a convert-time choice that must also hold at serve time is\n" " stated once. Repeatable\n" @@ -201,7 +205,7 @@ int for_row_pieces(int64_t n, bool parallel, std::string* why, F fn) { } /* namespace */ int main(int argc, char** argv) { - std::string in, out, arch_override, home = rad_home(), tokjson, imat_path, draft_dir; + std::string in, out, arch_override, home = rad_home(), tokjson, imat_path, draft_dir, chat_tmpl; std::string tokjson2, config_dir; std::vector add_files; std::vector hierarchy = rad_hierarchy_from_env(); @@ -245,6 +249,7 @@ int main(int argc, char** argv) { else if (a == "--add") add_files.push_back(next("--add")); else if (a == "--config") config_dir = next("--config"); else if (a == "--imatrix") imat_path = next("--imatrix"); + else if (a == "--chat-template") chat_tmpl = next("--chat-template"); else if (a == "--set") { const std::string kv = next("--set"); const size_t eq = kv.find('='); @@ -967,6 +972,12 @@ int main(int argc, char** argv) { w.abort(); return 1; } + /* The operator's template replaces whatever the checkpoint carried (or lacked), so a release + * that ships none still bakes a container that serves /v1/chat/completions. */ + if (!chat_tmpl.empty() && read_chat_template_path(chat_tmpl.c_str(), vb) < 0) { + w.abort(); + return 1; + } /* Serialised at section offset 0; finish() rebases once the string blob is sized. */ std::vector vocab_blob; if (vocab_serialize(vb, sink, 0, vocab_blob) < 0) { w.abort(); return 1; }