1
0

test-chat.cpp 65 KB

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