test-chat.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. // Tests chat handling, including grammar generation and parsing for tool calling, for various templates.
  2. //
  3. // Also acts as a CLI to generate a Markdown summary of the formats of Jinja templates,
  4. // e.g. given Minja (http://github.com/google/minja) checked out in parent dir:
  5. //
  6. // cmake -B build && cmake --build build --parallel && ./build/bin/test-chat ../minja/build/tests/*.jinja 2>/dev/null
  7. //
  8. #include "chat.h"
  9. #include "../src/unicode.h"
  10. #include "../src/llama-grammar.h"
  11. #include <nlohmann/json.hpp>
  12. #include <fstream>
  13. #include <iostream>
  14. #include <string>
  15. using json = nlohmann::ordered_json;
  16. static std::ostream & operator<<(std::ostream & os, const common_chat_msg_diff & diff) {
  17. os << "{ content_delta: " << diff.content_delta << "; ";
  18. os << "reasoning_content_delta: " << diff.reasoning_content_delta << "; ";
  19. if (diff.tool_call_index != std::string::npos) {
  20. os << "tool_call_index: " << diff.tool_call_index << "; ";
  21. os << "tool_call_delta.name: " << diff.tool_call_delta.name << "; ";
  22. os << "tool_call_delta.id: " << diff.tool_call_delta.id << "; ";
  23. os << "tool_call_delta.arguments: " << diff.tool_call_delta.arguments << "; ";
  24. }
  25. os << "}";
  26. return os;
  27. }
  28. // operator<< for vector<common_chat_msg_diff>:
  29. static std::ostream & operator<<(std::ostream & os, const std::vector<common_chat_msg_diff> & diffs) {
  30. os << "[\n";
  31. for (const auto & diff : diffs) {
  32. os << " " << diff << ",\n";
  33. }
  34. os << "]";
  35. return os;
  36. }
  37. static std::ostream & operator<<(std::ostream & os, const common_chat_msg & msg) {
  38. os << "{ role: " << msg.role << "; ";
  39. os << "content: " << msg.content << "; ";
  40. os << "content_parts: [\n";
  41. for (const auto & part : msg.content_parts) {
  42. os << " { type: " << part.type << "; text: " << part.text << " },\n";
  43. }
  44. os << "]; ";
  45. os << "reasoning_content: " << msg.reasoning_content << "; ";
  46. os << "tool_calls: [\n";
  47. for (const auto & tool_call : msg.tool_calls) {
  48. os << " { name: " << tool_call.name << "; arguments: " << tool_call.arguments << "; id: " << tool_call.id << " },\n";
  49. }
  50. os << "]";
  51. os << "}";
  52. return os;
  53. }
  54. template <class T> static bool equals(const T & expected, const T & actual) {
  55. return expected == actual;
  56. }
  57. static common_chat_msg normalize(const common_chat_msg & msg) {
  58. common_chat_msg normalized = msg;
  59. for (auto & tool_call : normalized.tool_calls) {
  60. try {
  61. tool_call.arguments = json::parse(tool_call.arguments).dump();
  62. } catch (const std::exception &) {
  63. // Do nothing
  64. }
  65. }
  66. return normalized;
  67. }
  68. template <>
  69. bool equals(const common_chat_msg & expected, const common_chat_msg & actual) {
  70. return normalize(expected) == normalize(actual);
  71. }
  72. template <class T> static void assert_equals(const T & expected, const T & actual) {
  73. if (!equals(expected, actual)) {
  74. std::cerr << "Expected: " << expected << std::endl;
  75. std::cerr << "Actual: " << actual << std::endl;
  76. std::cerr << std::flush;
  77. throw std::runtime_error("Test failed");
  78. }
  79. }
  80. static std::string read_file(const std::string & path) {
  81. std::cerr << "# Reading: " << path << '\n' << std::flush;
  82. std::ifstream fs(path, std::ios_base::binary);
  83. if (!fs.is_open()) {
  84. fs = std::ifstream("../" + path, std::ios_base::binary);
  85. if (!fs.is_open()) {
  86. throw std::runtime_error("Failed to open file: " + path);
  87. }
  88. }
  89. fs.seekg(0, std::ios_base::end);
  90. auto size = fs.tellg();
  91. fs.seekg(0);
  92. std::string out;
  93. out.resize(static_cast<size_t>(size));
  94. fs.read(out.data(), static_cast<std::streamsize>(size));
  95. return out;
  96. }
  97. static common_chat_templates_ptr read_templates(const std::string & path) {
  98. return common_chat_templates_ptr(common_chat_templates_init(/* model= */ nullptr, read_file(path)));
  99. }
  100. static std::unique_ptr<llama_grammar> build_grammar(const std::string & grammar_str) {
  101. return std::unique_ptr<llama_grammar>(
  102. llama_grammar_init_impl(nullptr, grammar_str.c_str(), "root", false, nullptr, 0, nullptr, 0));
  103. }
  104. // TODO: extract to common helper (copied from test-grammar-integration.cpp)
  105. static bool match_string(const std::string & input, llama_grammar * grammar) {
  106. const auto cpts = unicode_cpts_from_utf8(input);
  107. auto & stacks_cur = llama_grammar_get_stacks(grammar);
  108. for (const auto & cpt : cpts) {
  109. llama_grammar_accept(grammar, cpt);
  110. if (stacks_cur.empty()) {
  111. // no stacks means that the grammar failed to match at this point
  112. return false;
  113. }
  114. }
  115. if (std::any_of(stacks_cur.begin(), stacks_cur.end(), [](const auto & stack) { return stack.empty(); })) {
  116. // An empty stack means that the grammar has been completed
  117. return true;
  118. }
  119. return false;
  120. }
  121. static std::string renormalize_json(const std::string & json_str) {
  122. try {
  123. auto json_obj = json::parse(json_str);
  124. return json_obj.dump();
  125. } catch (const std::exception & e) {
  126. std::cerr << "Failed to parse JSON: " << e.what() << '\n';
  127. return json_str;
  128. }
  129. }
  130. static void assert_msg_equals(const common_chat_msg & expected, const common_chat_msg & actual) {
  131. assert_equals(expected.role, actual.role);
  132. assert_equals(expected.content, actual.content);
  133. assert_equals(expected.content_parts.size(), actual.content_parts.size());
  134. for (size_t i = 0; i < expected.content_parts.size(); i++) {
  135. const auto & expected_part = expected.content_parts[i];
  136. const auto & actual_part = actual.content_parts[i];
  137. assert_equals(expected_part.type, actual_part.type);
  138. assert_equals(expected_part.text, actual_part.text);
  139. }
  140. assert_equals(expected.reasoning_content, actual.reasoning_content);
  141. assert_equals(expected.tool_calls.size(), actual.tool_calls.size());
  142. for (size_t i = 0; i < expected.tool_calls.size(); i++) {
  143. const auto & expected_tool_call = expected.tool_calls[i];
  144. const auto & actual_tool_call = actual.tool_calls[i];
  145. assert_equals(expected_tool_call.name, actual_tool_call.name);
  146. assert_equals(renormalize_json(expected_tool_call.arguments), renormalize_json(actual_tool_call.arguments));
  147. assert_equals(expected_tool_call.id, actual_tool_call.id);
  148. }
  149. }
  150. common_chat_tool special_function_tool {
  151. /* .name = */ "special_function",
  152. /* .description = */ "I'm special",
  153. /* .parameters = */ R"({
  154. "type": "object",
  155. "properties": {
  156. "arg1": {
  157. "type": "integer",
  158. "description": "The arg."
  159. }
  160. },
  161. "required": ["arg1"]
  162. })",
  163. };
  164. common_chat_tool python_tool {
  165. /* .name = */ "python",
  166. /* .description = */ "an ipython interpreter",
  167. /* .parameters = */ R"({
  168. "type": "object",
  169. "properties": {
  170. "code": {
  171. "type": "string",
  172. "description": "Python code to execute."
  173. }
  174. },
  175. "required": ["code"]
  176. })",
  177. };
  178. common_chat_tool code_interpreter_tool {
  179. /* .name = */ "code_interpreter",
  180. /* .description = */ "an ipython interpreter",
  181. /* .parameters = */ R"({
  182. "type": "object",
  183. "properties": {
  184. "code": {
  185. "type": "string",
  186. "description": "Python code to execute."
  187. }
  188. },
  189. "required": ["code"]
  190. })",
  191. };
  192. std::vector<common_chat_tool> tools { special_function_tool, python_tool };
  193. std::vector<common_chat_tool> llama_3_1_tools { special_function_tool, code_interpreter_tool };
  194. struct delta_data {
  195. std::string delta;
  196. common_chat_params params;
  197. };
  198. static delta_data init_delta(const struct common_chat_templates * tmpls, const std::vector<std::string> & end_tokens,
  199. const common_chat_msg & user_message,
  200. const common_chat_msg & delta_message,
  201. const std::vector<common_chat_tool> & tools,
  202. const common_chat_tool_choice & tool_choice) {
  203. common_chat_templates_inputs inputs;
  204. inputs.parallel_tool_calls = true;
  205. inputs.messages.push_back(user_message);
  206. inputs.tools = tools;
  207. inputs.tool_choice = tool_choice;
  208. auto params_prefix = common_chat_templates_apply(tmpls, inputs);
  209. inputs.messages.push_back(delta_message);
  210. inputs.add_generation_prompt = false;
  211. auto params_full = common_chat_templates_apply(tmpls, inputs);
  212. std::string prefix = params_prefix.prompt;
  213. std::string full = params_full.prompt;
  214. if (full == prefix) {
  215. throw std::runtime_error("Full message is the same as the prefix");
  216. }
  217. size_t common_prefix_length = 0;
  218. for (size_t i = 0; i < prefix.size() && i < full.size(); ++i) {
  219. if (prefix[i] != full[i]) {
  220. break;
  221. }
  222. if (prefix[i] == '<') {
  223. // DeepSeek R1's template (as of 20250209) adds a trailing <think> if add_generation_prompt,
  224. // but it removes thinking tags for past messages.
  225. // The prefix and full strings diverge at <think> vs. <|tool▁calls▁begin|>, we avoid consuming the leading <.
  226. continue;
  227. }
  228. common_prefix_length = i + 1;
  229. }
  230. auto delta = full.substr(common_prefix_length);
  231. // Strip end tokens
  232. for (const auto & end_token : end_tokens) {
  233. // rfind to find the last occurrence
  234. auto pos = delta.rfind(end_token);
  235. if (pos != std::string::npos) {
  236. delta = delta.substr(0, pos);
  237. break;
  238. }
  239. }
  240. return { delta, params_full };
  241. }
  242. /*
  243. Applies the template to 1 user message w/ add_generation_prompt=true, then w/ the test message w/ add_generation_prompt=false,
  244. gets the diff, removes any end tokens and parses the result w/ the grammar, checking that
  245. the parsed message is the same as the test_message
  246. */
  247. static void test_templates(const struct common_chat_templates * tmpls, const std::vector<std::string> & end_tokens,
  248. const common_chat_msg & test_message,
  249. const std::vector<common_chat_tool> & tools = {},
  250. const std::string & expected_delta = "",
  251. bool expect_grammar_triggered = true,
  252. bool test_grammar_if_triggered = true,
  253. common_reasoning_format reasoning_format = COMMON_REASONING_FORMAT_NONE) {
  254. common_chat_msg user_message;
  255. user_message.role = "user";
  256. user_message.content = "Hello, world!";
  257. for (const auto & tool_choice : std::vector<common_chat_tool_choice> {COMMON_CHAT_TOOL_CHOICE_AUTO, COMMON_CHAT_TOOL_CHOICE_REQUIRED}) {
  258. auto data = init_delta(tmpls, end_tokens, user_message, test_message, tools, tool_choice);
  259. if (!expected_delta.empty()) {
  260. assert_equals(expected_delta, data.delta);
  261. }
  262. if (expect_grammar_triggered) {
  263. common_chat_syntax syntax;
  264. syntax.format = data.params.format;
  265. syntax.reasoning_format = reasoning_format;
  266. const auto msg = common_chat_parse(data.delta, /* is_partial= */ false, syntax);
  267. assert_msg_equals(test_message, msg);
  268. }
  269. if (!test_message.tool_calls.empty()) {
  270. GGML_ASSERT(!data.params.grammar.empty());
  271. }
  272. if (!data.params.grammar.empty()) {
  273. auto grammar = build_grammar(data.params.grammar);
  274. if (!grammar) {
  275. throw std::runtime_error("Failed to build grammar");
  276. }
  277. auto earliest_trigger_pos = std::string::npos;
  278. auto constrained = data.delta;
  279. for (const auto & trigger : data.params.grammar_triggers) {
  280. size_t pos = std::string::npos;
  281. std::smatch match;
  282. switch (trigger.type) {
  283. case COMMON_GRAMMAR_TRIGGER_TYPE_WORD:
  284. {
  285. const auto & word = trigger.value;
  286. pos = constrained.find(word);
  287. break;
  288. }
  289. case COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN:
  290. {
  291. const auto & pattern = trigger.value;
  292. if (std::regex_search(constrained, match, std::regex(pattern))) {
  293. pos = match.position(1);
  294. }
  295. break;
  296. }
  297. case COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_FULL:
  298. {
  299. const auto & pattern = trigger.value;
  300. if (std::regex_match(constrained, match, std::regex(pattern))) {
  301. auto mpos = std::string::npos;
  302. for (size_t i = 1; i < match.size(); ++i) {
  303. if (match[i].length() > 0) {
  304. mpos = match.position(i);
  305. break;
  306. }
  307. }
  308. if (mpos == std::string::npos) {
  309. mpos = match.position(0);
  310. }
  311. pos = mpos;
  312. }
  313. break;
  314. }
  315. default:
  316. throw std::runtime_error("Unknown trigger type");
  317. }
  318. if (pos == std::string::npos) {
  319. continue;
  320. }
  321. if (earliest_trigger_pos == std::string::npos || pos < earliest_trigger_pos) {
  322. earliest_trigger_pos = pos;
  323. }
  324. }
  325. auto grammar_triggered = false;
  326. if (earliest_trigger_pos != std::string::npos) {
  327. constrained = constrained.substr(earliest_trigger_pos);
  328. grammar_triggered = true;
  329. }
  330. if (data.params.grammar_lazy) {
  331. assert_equals(expect_grammar_triggered, grammar_triggered);
  332. }
  333. if (grammar_triggered && test_grammar_if_triggered && !match_string(constrained, grammar.get())) {
  334. throw std::runtime_error("Failed to match delta against grammar:\n\n" + data.delta +
  335. "\n\nConstrained: " + constrained +
  336. "\n\nGrammar: " + data.params.grammar);
  337. }
  338. }
  339. }
  340. }
  341. const common_chat_msg message_user {
  342. "user",
  343. "Hey there!",
  344. /* .content_parts = */ {},
  345. /* .tool_calls = */ {},
  346. /* .reasoning_content = */ "",
  347. /* .tool_name = */ "",
  348. /* .tool_call_id = */ "",
  349. };
  350. const common_chat_msg message_user_parts {
  351. "user",
  352. /* .content = */ "",
  353. /* .content_parts = */ {
  354. { "text", "Hey" },
  355. { "text", "there" },
  356. },
  357. /* .tool_calls = */ {},
  358. /* .reasoning_content = */ "",
  359. /* .tool_name = */ "",
  360. /* .tool_call_id = */ "",
  361. };
  362. static common_chat_msg simple_assist_msg(const std::string & content, const std::string & reasoning_content = "", const std::string & tool_name = "", const std::string & arguments = "", const std::string & id = "") {
  363. common_chat_msg msg;
  364. msg.role = "assistant";
  365. msg.content = content;
  366. msg.reasoning_content = reasoning_content;
  367. if (!tool_name.empty()) {
  368. msg.tool_calls.push_back({ tool_name, arguments, id });
  369. }
  370. return msg;
  371. }
  372. const common_chat_msg message_assist = simple_assist_msg("Hello, world!\nWhat's up?");
  373. const common_chat_msg message_assist_empty = simple_assist_msg("");
  374. const common_chat_msg message_assist_thoughts_unparsed_deepseek = simple_assist_msg("<think>I'm\nthinking</think>Hello, world!\nWhat's up?");
  375. const common_chat_msg message_assist_thoughts_unparsed_md = simple_assist_msg("<think>I'm\nthinking</think>Hello, world!\nWhat's up?\n```json\n{}```");
  376. const common_chat_msg message_assist_thoughts_unparsed_md_partial = simple_assist_msg("<think>I'm\nthinking</think>Hello, world!\nWhat's up?\n```json\n{}");
  377. const common_chat_msg message_assist_thoughts_unparsed_r7b = simple_assist_msg("<|START_THINKING|>I'm\nthinking<|END_THINKING|>Hello, world!\nWhat's up?");
  378. const common_chat_msg message_assist_thoughts = simple_assist_msg("Hello, world!\nWhat's up?", "I'm\nthinking");
  379. const common_chat_msg message_assist_thoughts_unopened_unparsed = simple_assist_msg("I'm\nthinking</think>Hello, world!\nWhat's up?");
  380. const common_chat_msg message_assist_thoughts_no_content = simple_assist_msg("", "I'm\nthinking");
  381. const common_chat_msg message_assist_call = simple_assist_msg("", "", "special_function", "{\"arg1\": 1}");
  382. const common_chat_msg message_assist_call_content = simple_assist_msg("Hello, world!\nWhat's up?", "", "special_function", "{\"arg1\":1}");
  383. const common_chat_msg message_assist_call_empty_args = simple_assist_msg("", "", "special_function");
  384. const common_chat_msg message_assist_call_cutoff_args = simple_assist_msg("", "", "special_function", "{\"arg");
  385. const common_chat_msg message_assist_call_thoughts = simple_assist_msg("", "I'm\nthinking", "special_function", "{\"arg1\":1}");
  386. const common_chat_msg message_assist_call_thoughts_unparsed = simple_assist_msg("<think>I'm\nthinking</think>\n\n", "", "special_function", "{\"arg1\": 1}");
  387. const common_chat_msg message_assist_call_id = simple_assist_msg("", "", "special_function", "{\"arg1\":1}", /* .id = */ "123456789");
  388. const common_chat_msg message_assist_call_idx = simple_assist_msg("", "", "special_function", "{\"arg1\":1}", /* .id = */ "0");
  389. const common_chat_msg message_assist_thoughts_call_idx = simple_assist_msg("", "I'm\nthinking", "special_function", "{\"arg1\": 1}", /* id = */ "0");
  390. const common_chat_msg message_assist_call_python = simple_assist_msg("", "", "python", "{\"code\":\"print('hey')\"}");
  391. const common_chat_msg message_assist_call_python_lines = simple_assist_msg("", "", "python", "{\"code\":\"# This is a program:\\nprint('hey')\"}");
  392. const common_chat_msg message_assist_call_python_lines_unclosed = simple_assist_msg("", "", "python", "{\"code\":\"# This is a program:\\nprint('hey')");
  393. const common_chat_msg message_assist_call_code_interpreter = simple_assist_msg("", "", "code_interpreter", "{\"code\":\"print('hey')\"}");
  394. static void test_msgs_oaicompat_json_conversion() {
  395. printf("[%s]\n", __func__);
  396. std::vector<common_chat_msg> msgs{
  397. message_user,
  398. message_user_parts,
  399. message_assist_call,
  400. message_assist_call_thoughts,
  401. message_assist_call_thoughts_unparsed,
  402. message_assist_call_id,
  403. message_assist_call_idx,
  404. message_assist_call_python,
  405. message_assist_call_code_interpreter,
  406. };
  407. for (const auto & msg : msgs) {
  408. auto oai_json = common_chat_msgs_to_json_oaicompat<json>({msg});
  409. auto msgs2 = common_chat_msgs_parse_oaicompat(oai_json);
  410. assert_equals((size_t) 1, msgs2.size());
  411. auto msg2 = msgs2[0];
  412. assert_msg_equals(msg, msg2);
  413. }
  414. assert_equals(
  415. std::string(
  416. "[\n"
  417. " {\n"
  418. " \"role\": \"user\",\n"
  419. " \"content\": [\n"
  420. " {\n"
  421. " \"type\": \"text\",\n"
  422. " \"text\": \"Hey\"\n"
  423. " },\n"
  424. " {\n"
  425. " \"type\": \"text\",\n"
  426. " \"text\": \"there\"\n"
  427. " }\n"
  428. " ]\n"
  429. " }\n"
  430. "]"
  431. ),
  432. common_chat_msgs_to_json_oaicompat<json>({message_user_parts}).dump(2));
  433. assert_equals(
  434. std::string(
  435. "[\n"
  436. " {\n"
  437. " \"role\": \"assistant\",\n"
  438. " \"content\": null,\n"
  439. " \"tool_calls\": [\n"
  440. " {\n"
  441. " \"type\": \"function\",\n"
  442. " \"function\": {\n"
  443. " \"name\": \"python\",\n"
  444. " \"arguments\": \"{\\\"code\\\":\\\"print('hey')\\\"}\"\n"
  445. " }\n"
  446. " }\n"
  447. " ]\n"
  448. " }\n"
  449. "]"
  450. ),
  451. common_chat_msgs_to_json_oaicompat<json>({message_assist_call_python}).dump(2));
  452. auto res = common_chat_msgs_parse_oaicompat(json::parse("[{\"role\": \"assistant\", \"tool_calls\": []}]"));
  453. assert_equals<size_t>(1, res.size());
  454. assert_equals<std::string>(res[0].role, "assistant");
  455. assert_equals(true, res[0].content.empty());
  456. assert_equals(true, res[0].tool_calls.empty());
  457. try {
  458. common_chat_msgs_parse_oaicompat(json::parse("[{\"role\": \"assistant\"}]"));
  459. throw std::runtime_error("Expected exception");
  460. } catch (const std::exception & e) {
  461. if (std::string(e.what()).find("'content'") == std::string::npos) {
  462. throw std::runtime_error("Expected exception about missing 'content'");
  463. }
  464. }
  465. }
  466. static void test_tools_oaicompat_json_conversion() {
  467. printf("[%s]\n", __func__);
  468. std::vector<common_chat_tool> tools{
  469. special_function_tool,
  470. python_tool,
  471. code_interpreter_tool,
  472. };
  473. for (const auto & tool : tools) {
  474. auto oai_json = common_chat_tools_to_json_oaicompat<json>({tool});
  475. auto tools2 = common_chat_tools_parse_oaicompat(oai_json);
  476. assert_equals((size_t) 1, tools2.size());
  477. auto tool2 = tools2[0];
  478. assert_equals(tool.name, tool2.name);
  479. assert_equals(tool.description, tool2.description);
  480. assert_equals(json::parse(tool.parameters).dump(2), json::parse(tool2.parameters).dump(2));
  481. }
  482. assert_equals(
  483. std::string(
  484. "[\n"
  485. " {\n"
  486. " \"type\": \"function\",\n"
  487. " \"function\": {\n"
  488. " \"name\": \"special_function\",\n"
  489. " \"description\": \"I'm special\",\n"
  490. " \"parameters\": {\n"
  491. " \"type\": \"object\",\n"
  492. " \"properties\": {\n"
  493. " \"arg1\": {\n"
  494. " \"type\": \"integer\",\n"
  495. " \"description\": \"The arg.\"\n"
  496. " }\n"
  497. " },\n"
  498. " \"required\": [\n"
  499. " \"arg1\"\n"
  500. " ]\n"
  501. " }\n"
  502. " }\n"
  503. " }\n"
  504. "]"
  505. ),
  506. common_chat_tools_to_json_oaicompat<json>({special_function_tool}).dump(2));
  507. }
  508. static void test_template_output_parsers() {
  509. printf("[%s]\n", __func__);
  510. common_chat_templates_inputs inputs_no_tools;
  511. inputs_no_tools.messages = {message_user};
  512. common_chat_templates_inputs inputs_tools;
  513. inputs_tools.messages = {message_user};
  514. inputs_tools.tools = {special_function_tool};
  515. common_chat_templates_inputs inputs_tools_builtin;
  516. inputs_tools_builtin.messages = {message_user};
  517. inputs_tools_builtin.tools = {python_tool};
  518. {
  519. // Not supported yet
  520. auto tmpls = read_templates("models/templates/CohereForAI-c4ai-command-r-plus-tool_use.jinja");
  521. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  522. assert_equals(COMMON_CHAT_FORMAT_GENERIC, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  523. }
  524. {
  525. auto tmpls = read_templates("models/templates/CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja");
  526. std::vector<std::string> end_tokens{ "<|END_OF_TURN_TOKEN|>" };
  527. for (const auto & inputs : { inputs_no_tools, inputs_tools }) {
  528. auto params = common_chat_templates_apply(tmpls.get(), inputs);
  529. assert_equals(COMMON_CHAT_FORMAT_COMMAND_R7B, params.format);
  530. assert_equals(false, params.thinking_forced_open);
  531. }
  532. assert_msg_equals(message_assist,
  533. common_chat_parse(
  534. "Hello, world!\nWhat's up?",
  535. /* is_partial= */ false,
  536. {COMMON_CHAT_FORMAT_COMMAND_R7B}));
  537. assert_msg_equals(message_assist,
  538. common_chat_parse(
  539. "<|START_RESPONSE|>Hello, world!\nWhat's up?<|END_RESPONSE|>",
  540. /* is_partial= */ false,
  541. {COMMON_CHAT_FORMAT_COMMAND_R7B}));
  542. assert_msg_equals(message_assist_thoughts,
  543. common_chat_parse(
  544. "<|START_THINKING|>I'm\nthinking<|END_THINKING|>"
  545. "<|START_RESPONSE|>Hello, world!\nWhat's up?<|END_RESPONSE|>",
  546. /* is_partial= */ false,
  547. {
  548. /* .format = */ COMMON_CHAT_FORMAT_COMMAND_R7B,
  549. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  550. }));
  551. assert_msg_equals(message_assist_thoughts_unparsed_deepseek,
  552. common_chat_parse(
  553. "<|START_THINKING|>I'm\nthinking<|END_THINKING|>"
  554. "<|START_RESPONSE|>Hello, world!\nWhat's up?<|END_RESPONSE|>",
  555. /* is_partial= */ false,
  556. {
  557. /* .format = */ COMMON_CHAT_FORMAT_COMMAND_R7B,
  558. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  559. /* .reasoning_in_content = */ true,
  560. /* .thinking_forced_open = */ false,
  561. }));
  562. assert_msg_equals(message_assist_thoughts_unparsed_r7b,
  563. common_chat_parse(
  564. "<|START_THINKING|>I'm\nthinking<|END_THINKING|>"
  565. "<|START_RESPONSE|>Hello, world!\nWhat's up?<|END_RESPONSE|>",
  566. /* is_partial= */ false,
  567. {COMMON_CHAT_FORMAT_COMMAND_R7B}));
  568. assert_msg_equals(message_assist_thoughts,
  569. common_chat_parse(
  570. "<|START_THINKING|>I'm\nthinking<|END_THINKING|>"
  571. "<|START_RESPONSE|>Hello, world!\nWhat's up?<|END_RESPONSE|>",
  572. /* is_partial= */ false,
  573. {
  574. /* .format = */ COMMON_CHAT_FORMAT_COMMAND_R7B,
  575. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  576. }));
  577. assert_msg_equals(message_assist_thoughts_call_idx,
  578. common_chat_parse(
  579. "<|START_THINKING|>I'm\nthinking<|END_THINKING|>"
  580. "<|START_ACTION|>[\n"
  581. " {\"tool_call_id\": \"0\", \"tool_name\": \"special_function\", \"parameters\": {\"arg1\": 1}}\n"
  582. "]<|END_ACTION|>",
  583. /* is_partial= */ false,
  584. {
  585. /* .format = */ COMMON_CHAT_FORMAT_COMMAND_R7B,
  586. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  587. }));
  588. assert_msg_equals(message_assist_thoughts_no_content,
  589. common_chat_parse(
  590. "<|START_THINKING|>I'm\nthinking<|END_THINKING|>"
  591. "<|START_ACTION|>[\n"
  592. " {\"tool_call_id\": \"0\", \"tool_name\": \"special",
  593. /* is_partial= */ true,
  594. {
  595. /* .format = */ COMMON_CHAT_FORMAT_COMMAND_R7B,
  596. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  597. }));
  598. test_templates(tmpls.get(), end_tokens, message_assist_call_idx, tools,
  599. "<|START_THINKING|><|END_THINKING|>"
  600. "<|START_ACTION|>[\n"
  601. " {\"tool_call_id\": \"0\", \"tool_name\": \"special_function\", \"parameters\": {\"arg1\": 1}}\n"
  602. "]<|END_ACTION|>",
  603. /* expect_grammar_triggered= */ true,
  604. /* test_grammar_if_triggered= */ true,
  605. COMMON_REASONING_FORMAT_DEEPSEEK);
  606. test_templates(tmpls.get(), end_tokens, message_assist, tools,
  607. "<|START_RESPONSE|>Hello, world!\n"
  608. "What's up?<|END_RESPONSE|>",
  609. /* expect_grammar_triggered= */ false);
  610. }
  611. {
  612. auto tmpls = read_templates("models/templates/google-gemma-2-2b-it.jinja");
  613. std::vector<std::string> end_tokens{ "<end_of_turn>" };
  614. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  615. assert_equals(COMMON_CHAT_FORMAT_GENERIC, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  616. assert_equals(COMMON_CHAT_FORMAT_GENERIC,
  617. common_chat_templates_apply(
  618. read_templates("models/templates/microsoft-Phi-3.5-mini-instruct.jinja").get(),
  619. inputs_tools)
  620. .format);
  621. // Generic tool calls doesn't generate / parse content-only messages symmetrically.
  622. assert_equals(
  623. simple_assist_msg("{ \"tool_call\" : { \"name\" : \"t"),
  624. common_chat_parse(
  625. "{ \"tool_call\" : { \"name\" : \"t",
  626. /* is_partial= */ true,
  627. {
  628. /* .format = */ COMMON_CHAT_FORMAT_GENERIC,
  629. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  630. /* .reasoning_in_content = */ false,
  631. /* .thinking_forced_open = */ true,
  632. /* .parse_tool_calls = */ false,
  633. }));
  634. assert_equals(
  635. message_assist_empty,
  636. common_chat_parse(
  637. "{ \"tool_call\" : { \"name\" : \"t",
  638. /* is_partial= */ true,
  639. {COMMON_CHAT_FORMAT_GENERIC}));
  640. assert_equals(
  641. simple_assist_msg("", "", "puppeteer_screenshot", "{\"name\":\"servethehome_homepage\","),
  642. common_chat_parse(
  643. R"({"tool_call": {"name": "puppeteer_screenshot", "arguments": {"name": "servethehome_homepage",)",
  644. /* is_partial= */ true,
  645. {COMMON_CHAT_FORMAT_GENERIC}));
  646. assert_equals(
  647. message_assist_call_empty_args,
  648. common_chat_parse(
  649. "{ \"tool_call\" : { \"name\" : \"special_function\"",
  650. /* is_partial= */ true,
  651. {COMMON_CHAT_FORMAT_GENERIC}));
  652. assert_equals(
  653. message_assist_call_cutoff_args,
  654. common_chat_parse(
  655. "{ \"tool_call\" : { \"name\" : \"special_function\", \"arguments\" : { \"arg",
  656. /* is_partial= */ true,
  657. {COMMON_CHAT_FORMAT_GENERIC}));
  658. assert_msg_equals(message_assist,
  659. common_chat_parse(
  660. "{\n"
  661. " \"response\": \"Hello, world!\\nWhat's up?\"\n"
  662. "}",
  663. /* is_partial= */ false,
  664. {COMMON_CHAT_FORMAT_GENERIC}));
  665. test_templates(tmpls.get(), end_tokens, message_assist_call_id, tools,
  666. "{\n"
  667. " \"tool_calls\": [\n"
  668. " {\n"
  669. " \"name\": \"special_function\",\n"
  670. " \"arguments\": {\n"
  671. " \"arg1\": 1\n"
  672. " },\n"
  673. " \"id\": \"123456789\"\n"
  674. " }\n"
  675. " ]\n"
  676. "}");
  677. }
  678. {
  679. auto tmpls = read_templates("models/templates/mistralai-Mistral-Nemo-Instruct-2407.jinja");
  680. std::vector<std::string> end_tokens{ "</s>" };
  681. assert_equals(COMMON_CHAT_FORMAT_MISTRAL_NEMO, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  682. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  683. test_templates(
  684. tmpls.get(), end_tokens, message_assist_call_id, tools,
  685. "[TOOL_CALLS][{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}, \"id\": \"123456789\"}]");
  686. }
  687. {
  688. auto tmpls = read_templates("models/templates/Qwen-QwQ-32B.jinja");
  689. std::vector<std::string> end_tokens{ "<|im_end|>" };
  690. assert_equals(COMMON_CHAT_FORMAT_HERMES_2_PRO, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  691. assert_equals(COMMON_CHAT_FORMAT_HERMES_2_PRO, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  692. }
  693. {
  694. auto tmpls = read_templates("models/templates/NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja");
  695. std::vector<std::string> end_tokens{ "<|im_end|>" };
  696. assert_equals(COMMON_CHAT_FORMAT_HERMES_2_PRO, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  697. assert_equals(COMMON_CHAT_FORMAT_HERMES_2_PRO, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  698. assert_equals(
  699. COMMON_CHAT_FORMAT_HERMES_2_PRO,
  700. common_chat_templates_apply(
  701. read_templates("models/templates/NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja").get(),
  702. inputs_tools)
  703. .format);
  704. assert_equals(
  705. COMMON_CHAT_FORMAT_HERMES_2_PRO,
  706. common_chat_templates_apply(
  707. read_templates("models/templates/Qwen-Qwen2.5-7B-Instruct.jinja").get(),
  708. inputs_tools)
  709. .format);
  710. // Test parsing
  711. assert_msg_equals(
  712. simple_assist_msg("", "", "python", ""),
  713. common_chat_parse(
  714. "```json\n"
  715. "<function_call> { \"name\" : \"python\"",
  716. /* is_partial= */ true,
  717. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  718. assert_msg_equals(
  719. simple_assist_msg("Let's call something\n"),
  720. common_chat_parse(
  721. "Let's call something\n"
  722. "<tool_call>{\"name\"",
  723. /* is_partial= */ true,
  724. {
  725. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  726. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  727. }));
  728. assert_msg_equals(
  729. simple_assist_msg("Let's call something\n"),
  730. common_chat_parse(
  731. "Let's call something\n"
  732. "<tool_call>{\"name",
  733. /* is_partial= */ true,
  734. {
  735. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  736. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  737. }));
  738. assert_msg_equals(message_assist_call_thoughts,
  739. common_chat_parse(
  740. // QwQ-32B's template adds a trailing <think> if add_generation_prompt
  741. "I'm\nthinking</think>\n"
  742. "<tool_call>{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}</tool_call>",
  743. /* is_partial= */ false,
  744. {
  745. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  746. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  747. /* .reasoning_in_content = */ false,
  748. /* .thinking_forced_open = */ true,
  749. }));
  750. assert_msg_equals(
  751. message_assist_call,
  752. common_chat_parse(
  753. "<tool_call>\n"
  754. "{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  755. "</tool_call>",
  756. /* is_partial= */ false,
  757. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  758. assert_msg_equals(message_assist_call_content,
  759. common_chat_parse(
  760. "Hello, world!\nWhat's up?<tool_call>\n"
  761. "{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  762. "</tool_call>",
  763. /* is_partial= */ false,
  764. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  765. assert_msg_equals(
  766. message_assist_call,
  767. common_chat_parse(
  768. "<function=special_function>{\"arg1\": 1}</function>",
  769. /* is_partial= */ false,
  770. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  771. assert_msg_equals(
  772. message_assist_call,
  773. common_chat_parse(
  774. "<function name=\"special_function\">\n"
  775. "{\"arg1\": 1}\n"
  776. "</function>",
  777. /* is_partial= */ false,
  778. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  779. assert_msg_equals(
  780. message_assist_call,
  781. common_chat_parse(
  782. "<tool>\n"
  783. " {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  784. "</tool>",
  785. /* is_partial= */ false,
  786. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  787. assert_msg_equals(
  788. message_assist_call,
  789. common_chat_parse(
  790. "<tools>\n"
  791. " {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  792. "</tools>",
  793. /* is_partial= */ false,
  794. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  795. assert_msg_equals(
  796. message_assist_call,
  797. common_chat_parse(
  798. "<response>\n"
  799. " {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  800. "</response>",
  801. /* is_partial= */ false,
  802. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  803. assert_msg_equals(
  804. message_assist_call,
  805. common_chat_parse(
  806. "```xml\n"
  807. "<response>\n"
  808. " {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  809. "</response>\n"
  810. "```",
  811. /* is_partial= */ false,
  812. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  813. assert_msg_equals(
  814. message_assist_call,
  815. common_chat_parse(
  816. "```xml\n"
  817. " {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  818. "```",
  819. /* is_partial= */ false,
  820. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  821. assert_msg_equals(
  822. message_assist_call,
  823. common_chat_parse(
  824. "```\n"
  825. " {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  826. "```",
  827. /* is_partial= */ false,
  828. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  829. assert_msg_equals(
  830. message_assist_call,
  831. common_chat_parse(
  832. "```\n"
  833. "{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  834. "```",
  835. /* is_partial= */ false,
  836. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  837. assert_msg_equals(
  838. message_assist_call,
  839. common_chat_parse(
  840. "```json\n"
  841. " {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  842. "```",
  843. /* is_partial= */ false,
  844. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  845. assert_msg_equals(
  846. message_assist_call,
  847. common_chat_parse(
  848. "```json\n"
  849. "\n"
  850. " <function_call> {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}} \n"
  851. " </function_call> \n"
  852. "``` ",
  853. /* is_partial= */ false,
  854. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  855. assert_msg_equals(
  856. message_assist_call,
  857. common_chat_parse(
  858. "<json>\n"
  859. " {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  860. "</json>",
  861. /* is_partial= */ false,
  862. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  863. assert_msg_equals(
  864. message_assist_call,
  865. common_chat_parse(
  866. "<xml>\n"
  867. " {\n"
  868. " \"name\": \"special_function\", \"arguments\": {\"arg1\": 1}\n"
  869. " }\n"
  870. "</xml>",
  871. /* is_partial= */ false,
  872. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  873. assert_msg_equals(
  874. message_assist_call,
  875. common_chat_parse(
  876. "<JSON>\n"
  877. " {\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  878. "</JSON>",
  879. /* is_partial= */ false,
  880. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  881. assert_msg_equals(
  882. message_assist_call,
  883. common_chat_parse(
  884. "{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}",
  885. /* is_partial= */ false,
  886. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  887. assert_msg_equals(
  888. message_assist_call,
  889. common_chat_parse(
  890. "{\n \"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}",
  891. /* is_partial= */ false,
  892. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  893. assert_msg_equals(
  894. simple_assist_msg(
  895. "This is not a tool call:",
  896. "",
  897. "special_function",
  898. "{\"arg1\": 1}"),
  899. common_chat_parse(
  900. "This is not a tool call:\n"
  901. "{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}",
  902. /* is_partial= */ false,
  903. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  904. assert_msg_equals(message_assist,
  905. common_chat_parse(
  906. "Hello, world!\nWhat's up?",
  907. /* is_partial= */ false,
  908. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  909. assert_msg_equals(message_assist_thoughts_unparsed_deepseek,
  910. common_chat_parse(
  911. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  912. /* is_partial= */ false,
  913. {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
  914. // assert_msg_equals(message_assist_thoughts_unparsed_deepseek,
  915. // common_chat_parse(
  916. // "I'm\nthinking</think>Hello, world!\nWhat's up?",
  917. // COMMON_CHAT_FORMAT_HERMES_2_PRO));
  918. assert_msg_equals(message_assist_thoughts,
  919. common_chat_parse(
  920. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  921. /* is_partial= */ false,
  922. {
  923. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  924. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  925. }));
  926. assert_msg_equals(message_assist_thoughts,
  927. common_chat_parse(
  928. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  929. /* is_partial= */ true,
  930. {
  931. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  932. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  933. }));
  934. assert_msg_equals(message_assist_thoughts_unparsed_md,
  935. common_chat_parse(
  936. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?\n```json\n{}```",
  937. /* is_partial= */ false,
  938. {
  939. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  940. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  941. /* .reasoning_in_content = */ true,
  942. /* .thinking_forced_open = */ false,
  943. /* .parse_tool_calls = */ false,
  944. }));
  945. assert_msg_equals(message_assist_thoughts_unparsed_md_partial,
  946. common_chat_parse(
  947. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?\n```json\n{}```",
  948. /* is_partial= */ true,
  949. {
  950. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  951. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  952. /* .reasoning_in_content = */ true,
  953. /* .thinking_forced_open = */ false,
  954. }));
  955. assert_msg_equals(message_assist_thoughts_unopened_unparsed,
  956. common_chat_parse(
  957. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  958. /* is_partial= */ false,
  959. {
  960. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  961. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  962. }));
  963. assert_msg_equals(message_assist_thoughts,
  964. common_chat_parse(
  965. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  966. /* is_partial= */ false,
  967. {
  968. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  969. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  970. /* .reasoning_in_content = */ false,
  971. /* .thinking_forced_open = */ true,
  972. }));
  973. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  974. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  975. "<tool_call>\n"
  976. "{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}\n"
  977. "</tool_call>");
  978. test_templates(tmpls.get(), end_tokens, message_assist_call_python_lines, tools,
  979. "<tool_call>\n"
  980. "{\"name\": \"python\", \"arguments\": {\"code\":\"# This is a program:\\nprint('hey')\"}}\n"
  981. "</tool_call>");
  982. assert_msg_equals(
  983. simple_assist_msg("", /* reasoning_content= */ "<tool_call>nah uhg</tool_call>"),
  984. common_chat_parse(
  985. "<think><tool_call>nah uhg</tool_call>",
  986. /* is_partial= */ false,
  987. {
  988. /* .format = */ COMMON_CHAT_FORMAT_HERMES_2_PRO,
  989. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  990. }));
  991. }
  992. {
  993. auto tmpls = read_templates("models/templates/meta-llama-Llama-3.1-8B-Instruct.jinja");
  994. std::vector<std::string> end_tokens{ "<|eom_id|>", "<|eot_id|>" };
  995. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  996. assert_equals(COMMON_CHAT_FORMAT_LLAMA_3_X, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  997. assert_equals(COMMON_CHAT_FORMAT_LLAMA_3_X_WITH_BUILTIN_TOOLS,
  998. common_chat_templates_apply(tmpls.get(), inputs_tools_builtin).format);
  999. assert_equals(COMMON_CHAT_FORMAT_LLAMA_3_X_WITH_BUILTIN_TOOLS,
  1000. common_chat_templates_apply(
  1001. read_templates("models/templates/meta-llama-Llama-3.3-70B-Instruct.jinja").get(),
  1002. inputs_tools_builtin)
  1003. .format);
  1004. assert_equals(
  1005. message_assist_call,
  1006. common_chat_parse(
  1007. "{\"name\": \"special_function\", \"parameters\": {\"arg1\": 1}}",
  1008. /* is_partial= */ false,
  1009. {COMMON_CHAT_FORMAT_LLAMA_3_X}));
  1010. // test_templates(tmpls.get(), end_tokens, message_assist, tools, R"(?)", /* expect_grammar_triggered= */ false);
  1011. test_templates(tmpls.get(), end_tokens, message_assist_call_code_interpreter, llama_3_1_tools,
  1012. "<|python_tag|>code_interpreter.call(code=\"print('hey')\")");
  1013. test_templates(tmpls.get(), end_tokens, message_assist_call_python, tools,
  1014. "<|python_tag|>python.call(code=\"print('hey')\")");
  1015. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1016. "{\"name\": \"special_function\", \"parameters\": {\"arg1\": 1}}");
  1017. }
  1018. {
  1019. auto tmpls = read_templates("models/templates/meta-llama-Llama-3.2-3B-Instruct.jinja");
  1020. std::vector<std::string> end_tokens{ "<|eom_id|>", "<|eot_id|>" };
  1021. assert_equals(COMMON_CHAT_FORMAT_LLAMA_3_X, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1022. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1023. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1024. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1025. "{\"name\": \"special_function\", \"parameters\": {\"arg1\": 1}}");
  1026. }
  1027. {
  1028. auto tmpls = read_templates("models/templates/meetkai-functionary-medium-v3.1.jinja");
  1029. std::vector<std::string> end_tokens{ "<|eom_id|>", "<|eot_id|>" };
  1030. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY,
  1031. common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1032. assert_equals(COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1,
  1033. common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1034. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY,
  1035. common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1036. for (auto is_partial : { false, true }) {
  1037. assert_equals(
  1038. message_assist_call,
  1039. common_chat_parse(
  1040. "<function=special_function>{\"arg1\": 1}</function>",
  1041. is_partial,
  1042. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1}));
  1043. }
  1044. assert_equals(
  1045. message_assist_call,
  1046. common_chat_parse(
  1047. "<function=special_function>{\"arg1\": 1}<",
  1048. /* is_partial= */ true,
  1049. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1}));
  1050. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1051. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1052. "<function=special_function>{\"arg1\": 1}</function>");
  1053. }
  1054. {
  1055. auto tmpls = read_templates("models/templates/meetkai-functionary-medium-v3.2.jinja");
  1056. std::vector<std::string> end_tokens{ "<|eom_id|>", "<|eot_id|>" };
  1057. assert_equals(COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1058. assert_equals(COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1059. assert_msg_equals(
  1060. simple_assist_msg(
  1061. "Hello, world!\nnono\nWhat's up?",
  1062. "",
  1063. "special_function",
  1064. "{\"arg1\": 1}"),
  1065. common_chat_parse(
  1066. "all\n"
  1067. "Hello, world!\n"
  1068. "nono\n"
  1069. "What's up?>>>special_function\n"
  1070. "{\"arg1\": 1}\n",
  1071. /* is_partial= */ false,
  1072. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1073. assert_msg_equals(message_assist_call_python_lines,
  1074. common_chat_parse(
  1075. "python\n"
  1076. "# This is a program:\n"
  1077. "print('hey')",
  1078. /* is_partial= */ false,
  1079. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1080. assert_msg_equals(message_assist_call_python_lines_unclosed,
  1081. common_chat_parse(
  1082. "python\n"
  1083. "# This is a program:\n"
  1084. "print('hey')",
  1085. /* is_partial= */ true,
  1086. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1087. assert_msg_equals(message_assist_call,
  1088. common_chat_parse(
  1089. "special_function\n"
  1090. "{\"arg1\": 1} \n ",
  1091. /* is_partial= */ false,
  1092. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1093. assert_msg_equals(message_assist,
  1094. common_chat_parse(
  1095. "all\n"
  1096. "Hello, world!\nWhat's up?",
  1097. /* is_partial= */ false,
  1098. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1099. test_templates(tmpls.get(), end_tokens, message_assist, {},
  1100. "all\n"
  1101. "Hello, world!\n"
  1102. "What's up?",
  1103. /* expect_grammar_triggered= */ false);
  1104. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1105. "special_function\n"
  1106. "{\"arg1\": 1}");
  1107. }
  1108. {
  1109. auto tmpls = read_templates("models/templates/fireworks-ai-llama-3-firefunction-v2.jinja");
  1110. std::vector<std::string> end_tokens{ "<|eot_id|>" };
  1111. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1112. assert_equals(COMMON_CHAT_FORMAT_FIREFUNCTION_V2, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1113. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1114. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1115. " functools[{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}]");
  1116. }
  1117. {
  1118. // Original DeepSeek R1 template. Leaves <|tool▁calls▁begin|> and others unclosed. Our logic fixes the prompt.
  1119. auto tmpls = read_templates("models/templates/deepseek-ai-DeepSeek-R1-Distill-Llama-8B.jinja");
  1120. std::vector<std::string> end_tokens{ "<|end▁of▁sentence|>" };
  1121. for (const auto & inputs : { inputs_no_tools, inputs_tools }) {
  1122. auto params = common_chat_templates_apply(tmpls.get(), inputs);
  1123. assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, params.format);
  1124. assert_equals(true, params.thinking_forced_open);
  1125. }
  1126. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1127. test_templates(tmpls.get(), end_tokens, message_assist_thoughts, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1128. assert_msg_equals(
  1129. simple_assist_msg("Hello, world!\nWhat's up?", "<think>I'm\nthinking"),
  1130. common_chat_parse(
  1131. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  1132. /* is_partial= */ false,
  1133. {
  1134. COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1135. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1136. /* .reasoning_in_content = */ false,
  1137. /* .thinking_forced_open = */ true,
  1138. }));
  1139. assert_msg_equals(
  1140. simple_assist_msg("", "I need to remember the correct syntax. It starts with <|tool▁calls▁begin|> and ends with"),
  1141. common_chat_parse(
  1142. "I need to remember the correct syntax. It starts with <|tool▁calls▁begin|> and ends with",
  1143. /* is_partial= */ true,
  1144. {
  1145. COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1146. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1147. /* .reasoning_in_content = */ false,
  1148. /* .thinking_forced_open = */ true,
  1149. }));
  1150. assert_msg_equals(message_assist_thoughts,
  1151. common_chat_parse(
  1152. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  1153. /* is_partial= */ false,
  1154. {
  1155. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1156. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1157. }));
  1158. assert_msg_equals(message_assist_thoughts_unopened_unparsed,
  1159. common_chat_parse(
  1160. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  1161. /* is_partial= */ false,
  1162. {
  1163. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1164. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1165. }));
  1166. assert_msg_equals(message_assist_thoughts,
  1167. common_chat_parse(
  1168. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  1169. /* is_partial= */ false,
  1170. {
  1171. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1172. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1173. /* .reasoning_in_content = */ false,
  1174. /* .thinking_forced_open = */ true,
  1175. }));
  1176. assert_msg_equals(message_assist_thoughts,
  1177. // Latest template update (ast of 20250209) adds a trailing <think>\n if add_generation_prompt is true.
  1178. common_chat_parse(
  1179. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  1180. /* is_partial= */ false,
  1181. {
  1182. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1183. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1184. /* .reasoning_in_content = */ false,
  1185. /* .thinking_forced_open = */ true,
  1186. }));
  1187. // test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1188. // "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"
  1189. // "```json\n"
  1190. // "{\"arg1\": 1}\n"
  1191. // // Look what's not here: <|tool▁calls▁end|> (also missing the <|end▁of▁sentence|>, but that is removed lazily by the test's delta logic)
  1192. // "```<|tool▁call▁end|>",
  1193. // /* expect_grammar_triggered= */ true,
  1194. // /* test_grammar_if_triggered= */ false);
  1195. }
  1196. {
  1197. // Replacement DeepSeek R1 template. Makes the Distill Qwen 7B/32B models happy to call tools and all.
  1198. auto tmpls = read_templates("models/templates/llama-cpp-deepseek-r1.jinja");
  1199. std::vector<std::string> end_tokens{ "<|end▁of▁sentence|>" };
  1200. assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1201. assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1202. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1203. test_templates(tmpls.get(), end_tokens, message_assist_thoughts, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1204. assert_msg_equals(message_assist_thoughts_unparsed_deepseek,
  1205. common_chat_parse(
  1206. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  1207. /* is_partial= */ false,
  1208. {COMMON_CHAT_FORMAT_DEEPSEEK_R1}));
  1209. assert_msg_equals(message_assist_thoughts,
  1210. common_chat_parse(
  1211. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  1212. /* is_partial= */ false,
  1213. {
  1214. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1215. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1216. }));
  1217. assert_msg_equals(message_assist_thoughts,
  1218. common_chat_parse(
  1219. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  1220. /* is_partial= */ false,
  1221. {
  1222. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1223. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1224. /* .reasoning_in_content = */ false,
  1225. /* .thinking_forced_open = */ true,
  1226. }));
  1227. assert_msg_equals(message_assist_call_thoughts_unparsed,
  1228. common_chat_parse(
  1229. "<think>I'm\nthinking</think>\n\n"
  1230. "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"
  1231. "```json\n"
  1232. "{\"arg1\": 1}\n"
  1233. "```<|tool▁call▁end|><|tool▁calls▁end|>",
  1234. /* is_partial= */ false,
  1235. {COMMON_CHAT_FORMAT_DEEPSEEK_R1}));
  1236. assert_msg_equals(message_assist_call,
  1237. common_chat_parse(
  1238. "<|tool▁calls|>function<|tool▁sep|>special_function\n"
  1239. "```json\n"
  1240. "{\"arg1\": 1}\n"
  1241. "```<|tool▁call▁end|><|tool▁calls▁end|>",
  1242. /* is_partial= */ false,
  1243. {COMMON_CHAT_FORMAT_DEEPSEEK_R1}));
  1244. assert_msg_equals(message_assist_call_thoughts,
  1245. common_chat_parse(
  1246. "<think>I'm\nthinking</think>\n\n"
  1247. "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"
  1248. "```json\n"
  1249. "{\"arg1\": 1}\n"
  1250. "```<|tool▁call▁end|><|tool▁calls▁end|>",
  1251. /* is_partial= */ false,
  1252. {
  1253. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1254. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1255. }));
  1256. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1257. "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"
  1258. "```json\n"
  1259. "{\"arg1\": 1}\n"
  1260. "```<|tool▁call▁end|><|tool▁calls▁end|>");
  1261. }
  1262. }
  1263. static void test_msg_diffs_compute() {
  1264. printf("[%s]\n", __func__);
  1265. {
  1266. common_chat_msg msg1;
  1267. common_chat_msg msg2;
  1268. msg2.content = "Hello, world!";
  1269. common_chat_msg_diff diff;
  1270. diff.content_delta = "Hello, world!";
  1271. assert_equals(
  1272. {diff},
  1273. common_chat_msg_diff::compute_diffs(msg1, msg2));
  1274. }
  1275. {
  1276. common_chat_msg msg1;
  1277. msg1.content = "Hello,";
  1278. common_chat_msg msg2;
  1279. msg2.content = "Hello, world!";
  1280. common_chat_msg_diff diff;
  1281. diff.content_delta = " world!";
  1282. assert_equals(
  1283. {diff},
  1284. common_chat_msg_diff::compute_diffs(msg1, msg2));
  1285. }
  1286. {
  1287. common_chat_msg msg0;
  1288. common_chat_msg msg1;
  1289. msg1.tool_calls = { { "special_function", "{\"ar", /* .id = */ "123" } };
  1290. common_chat_msg msg2;
  1291. msg2.tool_calls = { { "special_function", "{\"arg1\": 1}", /* .id = */ "123" } };
  1292. common_chat_msg_diff diff01;
  1293. diff01.tool_call_index = 0;
  1294. diff01.tool_call_delta.name = "special_function";
  1295. diff01.tool_call_delta.id = "123";
  1296. diff01.tool_call_delta.arguments = "{\"ar";
  1297. assert_equals(
  1298. {diff01},
  1299. common_chat_msg_diff::compute_diffs(msg0, msg1));
  1300. common_chat_msg_diff diff12;
  1301. diff12.tool_call_index = 0;
  1302. // Note: neither id nor name change here.
  1303. diff12.tool_call_delta.arguments = "g1\": 1}";
  1304. assert_equals(
  1305. {diff12},
  1306. common_chat_msg_diff::compute_diffs(msg1, msg2));
  1307. }
  1308. {
  1309. common_chat_msg msg0;
  1310. common_chat_msg msg2;
  1311. msg2.tool_calls = {
  1312. { "f1", "{\"arg1\": 1}", /* .id = */ "123" },
  1313. { "f2", "{\"arg2\": 2}", /* .id = */ "222" },
  1314. };
  1315. common_chat_msg_diff diff1;
  1316. diff1.tool_call_index = 0;
  1317. diff1.tool_call_delta.name = "f1";
  1318. diff1.tool_call_delta.id = "123";
  1319. diff1.tool_call_delta.arguments = "{\"arg1\": 1}";
  1320. common_chat_msg_diff diff2;
  1321. diff2.tool_call_index = 1;
  1322. diff2.tool_call_delta.name = "f2";
  1323. diff2.tool_call_delta.id = "222";
  1324. diff2.tool_call_delta.arguments = "{\"arg2\": 2}";
  1325. assert_equals(
  1326. {diff1, diff2},
  1327. common_chat_msg_diff::compute_diffs(msg0, msg2));
  1328. }
  1329. }
  1330. int main(int argc, char ** argv) {
  1331. // try {
  1332. #ifndef _WIN32
  1333. if (argc > 1) {
  1334. common_chat_templates_inputs inputs;
  1335. common_chat_msg msg;
  1336. msg.role = "user";
  1337. msg.content = "Hey";
  1338. inputs.messages = {msg};
  1339. inputs.tools = { special_function_tool };
  1340. std::cout << "| Template | Format |\n";
  1341. std::cout << "|----------|--------|\n";
  1342. for (int i = 1; i < argc; i++) {
  1343. try {
  1344. std::string path = argv[i];
  1345. if (path.rfind(".jinja") != path.size() - 6) {
  1346. std::cerr << "Skipping non-jinja file: " << path << '\n';
  1347. continue;
  1348. }
  1349. auto tmpls = read_templates(path);
  1350. auto parts = string_split(path, "/");
  1351. auto name = parts[parts.size() - 1];
  1352. auto format = common_chat_format_name(common_chat_templates_apply(tmpls.get(), inputs).format);
  1353. std::cout << "| " << name << " | " << format << " |\n";
  1354. } catch (const std::exception & e) {
  1355. std::cerr << "Failed to process " << argv[i] << ": " << e.what() << '\n';
  1356. }
  1357. }
  1358. } else
  1359. #endif
  1360. {
  1361. test_msg_diffs_compute();
  1362. test_msgs_oaicompat_json_conversion();
  1363. test_tools_oaicompat_json_conversion();
  1364. test_template_output_parsers();
  1365. std::cout << "\n[chat] All tests passed!" << '\n';
  1366. }
  1367. return 0;
  1368. // } catch (const std::exception & e) {
  1369. // std::cerr << "Error: " << e.what() << '\n';
  1370. // return 1;
  1371. // }
  1372. }