| 162 | } |
| 163 | |
| 164 | void autoparser::analyze_template(const common_chat_template & tmpl) { |
| 165 | jinja_caps = tmpl.original_caps(); |
| 166 | reasoning = analyze_reasoning(tmpl, jinja_caps.supports_tool_calls); |
| 167 | content = analyze_content(tmpl, reasoning); |
| 168 | tools = analyze_tools(jinja_caps.supports_tool_calls ? analyze_tools(tmpl, jinja_caps, reasoning) : analyze_tools()); |
| 169 | collect_preserved_tokens(); |
| 170 | |
| 171 | for (auto & workaround : workarounds) { |
| 172 | workaround(tmpl, *this); |
| 173 | } |
| 174 | |
| 175 | LOG_DBG("\n--- Reasoning & Content Structure ---\n"); |
| 176 | LOG_DBG("reasoning_mode: %s\n", mode_to_str(reasoning.mode).c_str()); |
| 177 | LOG_DBG("reasoning_start: '%s'\n", reasoning.start.c_str()); |
| 178 | LOG_DBG("reasoning_end: '%s'\n", reasoning.end.c_str()); |
| 179 | LOG_DBG("content_mode: %s\n", mode_to_str(content.mode).c_str()); |
| 180 | LOG_DBG("content_start: '%s'\n", content.start.c_str()); |
| 181 | LOG_DBG("content_end: '%s'\n", content.end.c_str()); |
| 182 | |
| 183 | LOG_DBG("\n--- Tool Call Structure ---\n"); |
| 184 | LOG_DBG("tool_mode: %s\n", mode_to_str(tools.format.mode).c_str()); |
| 185 | LOG_DBG("supports_tools: %s\n", jinja_caps.supports_tools ? "true" : "false"); |
| 186 | LOG_DBG("supports_parallel_calls: %s\n", jinja_caps.supports_parallel_tool_calls ? "true" : "false"); |
| 187 | LOG_DBG("tool_section_start: '%s'\n", tools.format.section_start.c_str()); |
| 188 | LOG_DBG("tool_section_end: '%s'\n", tools.format.section_end.c_str()); |
| 189 | LOG_DBG("per_call_start: '%s'\n", tools.format.per_call_start.c_str()); |
| 190 | LOG_DBG("per_call_end: '%s'\n", tools.format.per_call_end.c_str()); |
| 191 | LOG_DBG("func_name_prefix: '%s'\n", tools.function.name_prefix.c_str()); |
| 192 | LOG_DBG("func_name_suffix: '%s'\n", tools.function.name_suffix.c_str()); |
| 193 | LOG_DBG("func_close: '%s'\n", tools.function.close.c_str()); |
| 194 | LOG_DBG("call_id_prefix: '%s'\n", tools.call_id.prefix.c_str()); |
| 195 | LOG_DBG("call_id_suffix: '%s'\n", tools.call_id.suffix.c_str()); |
| 196 | LOG_DBG("call_id_pos: '%s'\n", mode_to_str(tools.call_id.pos).c_str()); |
| 197 | LOG_DBG("args_start: '%s'\n", tools.arguments.start.c_str()); |
| 198 | LOG_DBG("args_end: '%s'\n", tools.arguments.end.c_str()); |
| 199 | LOG_DBG("arg_name_prefix: '%s'\n", tools.arguments.name_prefix.c_str()); |
| 200 | LOG_DBG("arg_name_suffix: '%s'\n", tools.arguments.name_suffix.c_str()); |
| 201 | LOG_DBG("arg_value_prefix: '%s'\n", tools.arguments.value_prefix.c_str()); |
| 202 | LOG_DBG("arg_value_suffix: '%s'\n", tools.arguments.value_suffix.c_str()); |
| 203 | LOG_DBG("name_field: '%s'\n", tools.format.name_field.c_str()); |
| 204 | LOG_DBG("args_field: '%s'\n", tools.format.args_field.c_str()); |
| 205 | LOG_DBG("id_field: '%s'\n", tools.format.id_field.c_str()); |
| 206 | LOG_DBG("gen_id_field: '%s'\n", tools.format.gen_id_field.c_str()); |
| 207 | LOG_DBG("parameter_order: '%s'\n", std::accumulate(tools.format.parameter_order.begin(), tools.format.parameter_order.end(), |
| 208 | std::string(""), [] (const std::string & a, const std::string & b) { return a.empty() ? b : a + ", " + b; } |
| 209 | ).c_str()); |
| 210 | |
| 211 | LOG_DBG(ANSI_PURPLE "=== Differential analysis complete ===\n" ANSI_RESET); |
| 212 | analysis_complete = true; |
| 213 | } |
| 214 | |
| 215 | void autoparser::collect_preserved_tokens() { |
| 216 | auto add_token = [this](const std::string & org_token) { |