test-chat.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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 <fstream>
  9. #include <iostream>
  10. #include <json.hpp>
  11. #include <string>
  12. #include "chat.h"
  13. #include "../src/unicode.h"
  14. #include "../src/llama-grammar.h"
  15. using json = nlohmann::ordered_json;
  16. static std::ostream & operator<<(std::ostream & os, const common_chat_msg_diff & diff) {
  17. // os << "reasoning_content_delta: " << diff.reasoning_content_delta << '\n';
  18. os << "{ content_delta: " << diff.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. }
  983. {
  984. auto tmpls = read_templates("models/templates/meta-llama-Llama-3.1-8B-Instruct.jinja");
  985. std::vector<std::string> end_tokens{ "<|eom_id|>", "<|eot_id|>" };
  986. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  987. assert_equals(COMMON_CHAT_FORMAT_LLAMA_3_X, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  988. assert_equals(COMMON_CHAT_FORMAT_LLAMA_3_X_WITH_BUILTIN_TOOLS,
  989. common_chat_templates_apply(tmpls.get(), inputs_tools_builtin).format);
  990. assert_equals(COMMON_CHAT_FORMAT_LLAMA_3_X_WITH_BUILTIN_TOOLS,
  991. common_chat_templates_apply(
  992. read_templates("models/templates/meta-llama-Llama-3.3-70B-Instruct.jinja").get(),
  993. inputs_tools_builtin)
  994. .format);
  995. assert_equals(
  996. message_assist_call,
  997. common_chat_parse(
  998. "{\"name\": \"special_function\", \"parameters\": {\"arg1\": 1}}",
  999. /* is_partial= */ false,
  1000. {COMMON_CHAT_FORMAT_LLAMA_3_X}));
  1001. // test_templates(tmpls.get(), end_tokens, message_assist, tools, R"(?)", /* expect_grammar_triggered= */ false);
  1002. test_templates(tmpls.get(), end_tokens, message_assist_call_code_interpreter, llama_3_1_tools,
  1003. "<|python_tag|>code_interpreter.call(code=\"print('hey')\")");
  1004. test_templates(tmpls.get(), end_tokens, message_assist_call_python, tools,
  1005. "<|python_tag|>python.call(code=\"print('hey')\")");
  1006. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1007. "{\"name\": \"special_function\", \"parameters\": {\"arg1\": 1}}");
  1008. }
  1009. {
  1010. auto tmpls = read_templates("models/templates/meta-llama-Llama-3.2-3B-Instruct.jinja");
  1011. std::vector<std::string> end_tokens{ "<|eom_id|>", "<|eot_id|>" };
  1012. assert_equals(COMMON_CHAT_FORMAT_LLAMA_3_X, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1013. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1014. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  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/meetkai-functionary-medium-v3.1.jinja");
  1020. std::vector<std::string> end_tokens{ "<|eom_id|>", "<|eot_id|>" };
  1021. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY,
  1022. common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1023. assert_equals(COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1,
  1024. common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1025. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY,
  1026. common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1027. for (auto is_partial : { false, true }) {
  1028. assert_equals(
  1029. message_assist_call,
  1030. common_chat_parse(
  1031. "<function=special_function>{\"arg1\": 1}</function>",
  1032. is_partial,
  1033. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1}));
  1034. }
  1035. assert_equals(
  1036. message_assist_call,
  1037. common_chat_parse(
  1038. "<function=special_function>{\"arg1\": 1}<",
  1039. /* is_partial= */ true,
  1040. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1}));
  1041. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1042. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1043. "<function=special_function>{\"arg1\": 1}</function>");
  1044. }
  1045. {
  1046. auto tmpls = read_templates("models/templates/meetkai-functionary-medium-v3.2.jinja");
  1047. std::vector<std::string> end_tokens{ "<|eom_id|>", "<|eot_id|>" };
  1048. assert_equals(COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1049. assert_equals(COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1050. assert_msg_equals(
  1051. simple_assist_msg(
  1052. "Hello, world!\nnono\nWhat's up?",
  1053. "",
  1054. "special_function",
  1055. "{\"arg1\": 1}"),
  1056. common_chat_parse(
  1057. "all\n"
  1058. "Hello, world!\n"
  1059. "nono\n"
  1060. "What's up?>>>special_function\n"
  1061. "{\"arg1\": 1}\n",
  1062. /* is_partial= */ false,
  1063. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1064. assert_msg_equals(message_assist_call_python_lines,
  1065. common_chat_parse(
  1066. "python\n"
  1067. "# This is a program:\n"
  1068. "print('hey')",
  1069. /* is_partial= */ false,
  1070. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1071. assert_msg_equals(message_assist_call_python_lines_unclosed,
  1072. common_chat_parse(
  1073. "python\n"
  1074. "# This is a program:\n"
  1075. "print('hey')",
  1076. /* is_partial= */ true,
  1077. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1078. assert_msg_equals(message_assist_call,
  1079. common_chat_parse(
  1080. "special_function\n"
  1081. "{\"arg1\": 1} \n ",
  1082. /* is_partial= */ false,
  1083. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1084. assert_msg_equals(message_assist,
  1085. common_chat_parse(
  1086. "all\n"
  1087. "Hello, world!\nWhat's up?",
  1088. /* is_partial= */ false,
  1089. {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2}));
  1090. test_templates(tmpls.get(), end_tokens, message_assist, {},
  1091. "all\n"
  1092. "Hello, world!\n"
  1093. "What's up?",
  1094. /* expect_grammar_triggered= */ false);
  1095. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1096. "special_function\n"
  1097. "{\"arg1\": 1}");
  1098. }
  1099. {
  1100. auto tmpls = read_templates("models/templates/fireworks-ai-llama-3-firefunction-v2.jinja");
  1101. std::vector<std::string> end_tokens{ "<|eot_id|>" };
  1102. assert_equals(COMMON_CHAT_FORMAT_CONTENT_ONLY, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1103. assert_equals(COMMON_CHAT_FORMAT_FIREFUNCTION_V2, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1104. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1105. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1106. " functools[{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}]");
  1107. }
  1108. {
  1109. // Original DeepSeek R1 template. Leaves <|tool▁calls▁begin|> and others unclosed. Our logic fixes the prompt.
  1110. auto tmpls = read_templates("models/templates/deepseek-ai-DeepSeek-R1-Distill-Llama-8B.jinja");
  1111. std::vector<std::string> end_tokens{ "<|end▁of▁sentence|>" };
  1112. for (const auto & inputs : { inputs_no_tools, inputs_tools }) {
  1113. auto params = common_chat_templates_apply(tmpls.get(), inputs);
  1114. assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, params.format);
  1115. assert_equals(true, params.thinking_forced_open);
  1116. }
  1117. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1118. test_templates(tmpls.get(), end_tokens, message_assist_thoughts, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1119. assert_msg_equals(
  1120. simple_assist_msg("Hello, world!\nWhat's up?", "<think>I'm\nthinking"),
  1121. common_chat_parse(
  1122. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  1123. /* is_partial= */ false,
  1124. {
  1125. COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1126. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1127. /* .reasoning_in_content = */ false,
  1128. /* .thinking_forced_open = */ true,
  1129. }));
  1130. assert_msg_equals(
  1131. simple_assist_msg("", "I need to remember the correct syntax. It starts with <|tool▁calls▁begin|> and ends with"),
  1132. common_chat_parse(
  1133. "I need to remember the correct syntax. It starts with <|tool▁calls▁begin|> and ends with",
  1134. /* is_partial= */ true,
  1135. {
  1136. COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1137. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1138. /* .reasoning_in_content = */ false,
  1139. /* .thinking_forced_open = */ true,
  1140. }));
  1141. assert_msg_equals(message_assist_thoughts,
  1142. common_chat_parse(
  1143. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  1144. /* is_partial= */ false,
  1145. {
  1146. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1147. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1148. }));
  1149. assert_msg_equals(message_assist_thoughts_unopened_unparsed,
  1150. common_chat_parse(
  1151. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  1152. /* is_partial= */ false,
  1153. {
  1154. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1155. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1156. }));
  1157. assert_msg_equals(message_assist_thoughts,
  1158. common_chat_parse(
  1159. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  1160. /* is_partial= */ false,
  1161. {
  1162. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1163. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1164. /* .reasoning_in_content = */ false,
  1165. /* .thinking_forced_open = */ true,
  1166. }));
  1167. assert_msg_equals(message_assist_thoughts,
  1168. // Latest template update (ast of 20250209) adds a trailing <think>\n if add_generation_prompt is true.
  1169. common_chat_parse(
  1170. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  1171. /* is_partial= */ false,
  1172. {
  1173. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1174. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1175. /* .reasoning_in_content = */ false,
  1176. /* .thinking_forced_open = */ true,
  1177. }));
  1178. // test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1179. // "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"
  1180. // "```json\n"
  1181. // "{\"arg1\": 1}\n"
  1182. // // 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)
  1183. // "```<|tool▁call▁end|>",
  1184. // /* expect_grammar_triggered= */ true,
  1185. // /* test_grammar_if_triggered= */ false);
  1186. }
  1187. {
  1188. // Replacement DeepSeek R1 template. Makes the Distill Qwen 7B/32B models happy to call tools and all.
  1189. auto tmpls = read_templates("models/templates/llama-cpp-deepseek-r1.jinja");
  1190. std::vector<std::string> end_tokens{ "<|end▁of▁sentence|>" };
  1191. assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, common_chat_templates_apply(tmpls.get(), inputs_no_tools).format);
  1192. assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, common_chat_templates_apply(tmpls.get(), inputs_tools).format);
  1193. test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1194. test_templates(tmpls.get(), end_tokens, message_assist_thoughts, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
  1195. assert_msg_equals(message_assist_thoughts_unparsed_deepseek,
  1196. common_chat_parse(
  1197. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  1198. /* is_partial= */ false,
  1199. {COMMON_CHAT_FORMAT_DEEPSEEK_R1}));
  1200. assert_msg_equals(message_assist_thoughts,
  1201. common_chat_parse(
  1202. "<think>I'm\nthinking</think>Hello, world!\nWhat's up?",
  1203. /* is_partial= */ false,
  1204. {
  1205. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1206. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1207. }));
  1208. assert_msg_equals(message_assist_thoughts,
  1209. common_chat_parse(
  1210. "I'm\nthinking</think>Hello, world!\nWhat's up?",
  1211. /* is_partial= */ false,
  1212. {
  1213. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1214. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1215. /* .reasoning_in_content = */ false,
  1216. /* .thinking_forced_open = */ true,
  1217. }));
  1218. assert_msg_equals(message_assist_call_thoughts_unparsed,
  1219. common_chat_parse(
  1220. "<think>I'm\nthinking</think>\n\n"
  1221. "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"
  1222. "```json\n"
  1223. "{\"arg1\": 1}\n"
  1224. "```<|tool▁call▁end|><|tool▁calls▁end|>",
  1225. /* is_partial= */ false,
  1226. {COMMON_CHAT_FORMAT_DEEPSEEK_R1}));
  1227. assert_msg_equals(message_assist_call,
  1228. common_chat_parse(
  1229. "<|tool▁calls|>function<|tool▁sep|>special_function\n"
  1230. "```json\n"
  1231. "{\"arg1\": 1}\n"
  1232. "```<|tool▁call▁end|><|tool▁calls▁end|>",
  1233. /* is_partial= */ false,
  1234. {COMMON_CHAT_FORMAT_DEEPSEEK_R1}));
  1235. assert_msg_equals(message_assist_call_thoughts,
  1236. common_chat_parse(
  1237. "<think>I'm\nthinking</think>\n\n"
  1238. "<|tool▁calls▁begin|><|tool▁call▁begin|>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. {
  1244. /* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  1245. /* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
  1246. }));
  1247. test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
  1248. "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"
  1249. "```json\n"
  1250. "{\"arg1\": 1}\n"
  1251. "```<|tool▁call▁end|><|tool▁calls▁end|>");
  1252. }
  1253. }
  1254. static void test_msg_diffs_compute() {
  1255. printf("[%s]\n", __func__);
  1256. {
  1257. common_chat_msg msg1;
  1258. common_chat_msg msg2;
  1259. msg2.content = "Hello, world!";
  1260. common_chat_msg_diff diff;
  1261. diff.content_delta = "Hello, world!";
  1262. assert_equals(
  1263. {diff},
  1264. common_chat_msg_diff::compute_diffs(msg1, msg2));
  1265. }
  1266. {
  1267. common_chat_msg msg1;
  1268. msg1.content = "Hello,";
  1269. common_chat_msg msg2;
  1270. msg2.content = "Hello, world!";
  1271. common_chat_msg_diff diff;
  1272. diff.content_delta = " world!";
  1273. assert_equals(
  1274. {diff},
  1275. common_chat_msg_diff::compute_diffs(msg1, msg2));
  1276. }
  1277. {
  1278. common_chat_msg msg0;
  1279. common_chat_msg msg1;
  1280. msg1.tool_calls = { { "special_function", "{\"ar", /* .id = */ "123" } };
  1281. common_chat_msg msg2;
  1282. msg2.tool_calls = { { "special_function", "{\"arg1\": 1}", /* .id = */ "123" } };
  1283. common_chat_msg_diff diff01;
  1284. diff01.tool_call_index = 0;
  1285. diff01.tool_call_delta.name = "special_function";
  1286. diff01.tool_call_delta.id = "123";
  1287. diff01.tool_call_delta.arguments = "{\"ar";
  1288. assert_equals(
  1289. {diff01},
  1290. common_chat_msg_diff::compute_diffs(msg0, msg1));
  1291. common_chat_msg_diff diff12;
  1292. diff12.tool_call_index = 0;
  1293. // Note: neither id nor name change here.
  1294. diff12.tool_call_delta.arguments = "g1\": 1}";
  1295. assert_equals(
  1296. {diff12},
  1297. common_chat_msg_diff::compute_diffs(msg1, msg2));
  1298. }
  1299. {
  1300. common_chat_msg msg0;
  1301. common_chat_msg msg2;
  1302. msg2.tool_calls = {
  1303. { "f1", "{\"arg1\": 1}", /* .id = */ "123" },
  1304. { "f2", "{\"arg2\": 2}", /* .id = */ "222" },
  1305. };
  1306. common_chat_msg_diff diff1;
  1307. diff1.tool_call_index = 0;
  1308. diff1.tool_call_delta.name = "f1";
  1309. diff1.tool_call_delta.id = "123";
  1310. diff1.tool_call_delta.arguments = "{\"arg1\": 1}";
  1311. common_chat_msg_diff diff2;
  1312. diff2.tool_call_index = 1;
  1313. diff2.tool_call_delta.name = "f2";
  1314. diff2.tool_call_delta.id = "222";
  1315. diff2.tool_call_delta.arguments = "{\"arg2\": 2}";
  1316. assert_equals(
  1317. {diff1, diff2},
  1318. common_chat_msg_diff::compute_diffs(msg0, msg2));
  1319. }
  1320. }
  1321. int main(int argc, char ** argv) {
  1322. // try {
  1323. #ifndef _WIN32
  1324. if (argc > 1) {
  1325. common_chat_templates_inputs inputs;
  1326. common_chat_msg msg;
  1327. msg.role = "user";
  1328. msg.content = "Hey";
  1329. inputs.messages = {msg};
  1330. inputs.tools = { special_function_tool };
  1331. std::cout << "| Template | Format |\n";
  1332. std::cout << "|----------|--------|\n";
  1333. for (int i = 1; i < argc; i++) {
  1334. try {
  1335. std::string path = argv[i];
  1336. if (path.rfind(".jinja") != path.size() - 6) {
  1337. std::cerr << "Skipping non-jinja file: " << path << '\n';
  1338. continue;
  1339. }
  1340. auto tmpls = read_templates(path);
  1341. auto parts = string_split(path, "/");
  1342. auto name = parts[parts.size() - 1];
  1343. auto format = common_chat_format_name(common_chat_templates_apply(tmpls.get(), inputs).format);
  1344. std::cout << "| " << name << " | " << format << " |\n";
  1345. } catch (const std::exception & e) {
  1346. std::cerr << "Failed to process " << argv[i] << ": " << e.what() << '\n';
  1347. }
  1348. }
  1349. } else
  1350. #endif
  1351. {
  1352. test_msg_diffs_compute();
  1353. test_msgs_oaicompat_json_conversion();
  1354. test_tools_oaicompat_json_conversion();
  1355. test_template_output_parsers();
  1356. std::cout << "\n[chat] All tests passed!" << '\n';
  1357. }
  1358. return 0;
  1359. // } catch (const std::exception & e) {
  1360. // std::cerr << "Error: " << e.what() << '\n';
  1361. // return 1;
  1362. // }
  1363. }