mirror of
https://github.com/ikawrakow/ik_llama.cpp.git
synced 2026-03-10 14:00:08 +00:00
--------- Co-authored-by: Piotr Wilkin <piotr.wilkin@syndatis.com> common : add nemotron 3 parsing (#18077) common : add parser for ministral/mistral large 3/devstral 2 (#17713) common : default content to an empty string (#18485) chat: make tool description and parameters optional per OpenAI spec (#18478) Per the OpenAI API specification, both 'description' and 'parameters' fields in tool function definitions are optional. Previously, the parser would throw an exception if these fields were missing. Attempts to fix #17667 common : implement new jinja template engine (#18462) --------- Co-authored-by: Alde Rojas <hello@alde.dev> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> Co-authored-by: Georgi Gerganov <ggerganov@gmail.com> jinja: correct member access rule (#18905) jinja : fix lexing of float literals with sign (#18901) jinja : add missing tojson filter for bool (#18900) jinja : attribute support for join, map and sort (#18883) jinja : fix object item order (and properly implement dictsort) (#18904) tests : add test-jinja -py option for cross-checking (#18906) Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> ci : run test-jinja -py on high perf [no ci] (#18916) jinja : fix undefined keys and attributes and int/float as bool (#18924) jinja: support none|string (#18995) Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> jinja : implement mixed type object keys (#18955) --------- Co-authored-by: Xuan Son Nguyen <son@huggingface.co> jinja : undefined should be treated as sequence/iterable (return string/array) by filters/tests (#19147) `tojson` is not a supported `undefined` filter keep it DRY and fix some types jinja : do not pass empty tools and add some none filters (#19176) jinja : add unordered_map include to value.h [no ci] (#19205) jinja : add missing 'in' test to template engine (#19004) (#19239) The jinja template parser was missing the 'in' test from global_builtins(), causing templates using reject("in", ...), select("in", ...), or 'x is in(y)' to fail with "selectattr: unknown test 'in'". This broke tool-calling for Qwen3-Coder and any other model whose chat template uses the 'in' test. Added test_is_in supporting array, string, and object containment checks, mirroring the existing 'in' operator logic in runtime.cpp. Includes test cases for all three containment types plus reject/select filter usage. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Sid Mohan <sidmohan0@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Xuan Son Nguyen <son@huggingface.co> Add Jinja support for "indent" string filter (#19529) Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> add vendor refactor chat server : support preserving reasoning_content in assistant message (#18994) chat : fix translategemma crash on common_chat_format_example (#19019) chat: fix language input for translategemma (#19052) Co-authored-by: Aldehir Rojas <hello@alde.dev> --------- Co-authored-by: Aldehir Rojas <hello@alde.dev> chat: fix case where template accepts type content only (#19419) mtmd : chat : Fix extra \n between text and media marker (#19595) Thanks to @tugot17 for detecting and reporting the issue. For vision models (e.g. LFM2.5-VL-1.6B and Qwen/Qwen3-VL-4B-Instruct) `llama-mtmd-cli` produces identical output to HF implementation. However `llama-server` doesn't. I traced it down to extra newline inserted after `<__media__>`. This happens in `to_json_oaicompat`, that treats media markers as text and joins all parts with `\n` separator. PR introduces new type `media_marker` and uses it for media markers. Extra logic is added to prevent insertion of newlines before and after media markers. With this change number of input tokens is identical to HF implementation and as a result the output is also identical. I explored other ways to address the issue * remove completely `\n` between text parts in `to_json_oaicompat` * merge text messages in server-common.cpp before sending them to `to_json_oaicompat` Please propose alternative ways of fixing this issue. Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com> --------- Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com> common : merge qwen3-coder and nemotron nano 3 parsers (#19765) common : fix improper trimming in XML parser on complete message (#19805) Co-authored-by: Jules LEIDELINGER <11395311+julio75012@users.noreply.github.com> jinja: correct stats for tojson and string filters (#19785) jinja : correct default size for string slices (#19913) common : handle unicode during partial json parsing (#16526) common : fix json schema with '\' in literals (#17307) add back qwen_coder_xml and mirothinker Co-authored-by: Aldehir Rojas <hello@alde.dev>
322 lines
16 KiB
C++
322 lines
16 KiB
C++
#include <json-partial.h>
|
|
#include "ggml.h"
|
|
#include "log.h"
|
|
#include <string>
|
|
#include <regex>
|
|
|
|
using json = nlohmann::ordered_json;
|
|
|
|
enum common_json_stack_element_type {
|
|
COMMON_JSON_STACK_ELEMENT_OBJECT,
|
|
COMMON_JSON_STACK_ELEMENT_KEY,
|
|
COMMON_JSON_STACK_ELEMENT_ARRAY,
|
|
};
|
|
|
|
struct common_json_stack_element {
|
|
common_json_stack_element_type type;
|
|
std::string key;
|
|
};
|
|
|
|
bool common_json_parse(
|
|
const std::string & input,
|
|
const std::string & healing_marker,
|
|
common_json & out)
|
|
{
|
|
std::string::const_iterator it = input.begin();
|
|
const auto end = input.end();
|
|
return common_json_parse(it, end, healing_marker, out);
|
|
}
|
|
|
|
bool common_json_parse(
|
|
std::string::const_iterator & it,
|
|
const std::string::const_iterator & end,
|
|
const std::string & healing_marker,
|
|
common_json & out)
|
|
{
|
|
// // https://json.nlohmann.me/features/parsing/sax_interface/
|
|
struct json_error_locator : public nlohmann::json_sax<json> {
|
|
std::size_t position;
|
|
bool found_error;
|
|
std::string last_token;
|
|
std::string exception_message;
|
|
std::vector<common_json_stack_element> stack;
|
|
|
|
json_error_locator() : position(0), found_error(false) {}
|
|
|
|
bool parse_error(std::size_t position, const std::string & last_token, const json::exception & ex) override { // NOLINT
|
|
this->position = position - 1;
|
|
this->found_error = true;
|
|
this->last_token = last_token;
|
|
this->exception_message = ex.what();
|
|
return false;
|
|
}
|
|
void close_value() {
|
|
if (!stack.empty() && (stack.back().type == COMMON_JSON_STACK_ELEMENT_KEY)) {
|
|
stack.pop_back();
|
|
}
|
|
}
|
|
bool null() override { // NOLINT
|
|
close_value();
|
|
return true;
|
|
}
|
|
bool boolean(bool) override { // NOLINT
|
|
close_value();
|
|
return true;
|
|
}
|
|
bool number_integer(number_integer_t) override { // NOLINT
|
|
close_value();
|
|
return true;
|
|
}
|
|
bool number_unsigned(number_unsigned_t) override { // NOLINT
|
|
close_value();
|
|
return true;
|
|
}
|
|
bool number_float(number_float_t, const string_t &) override { // NOLINT
|
|
close_value();
|
|
return true;
|
|
}
|
|
bool string(string_t &) override { // NOLINT
|
|
close_value();
|
|
return true;
|
|
}
|
|
bool binary(binary_t &) override { // NOLINT
|
|
close_value();
|
|
return true;
|
|
}
|
|
bool start_object(std::size_t) override { // NOLINT
|
|
stack.push_back({COMMON_JSON_STACK_ELEMENT_OBJECT, ""});
|
|
return true;
|
|
}
|
|
bool end_object() override {
|
|
GGML_ASSERT(!stack.empty() && stack.back().type == COMMON_JSON_STACK_ELEMENT_OBJECT);
|
|
stack.pop_back();
|
|
close_value();
|
|
return true;
|
|
}
|
|
bool key(string_t & key) override { // NOLINT
|
|
stack.push_back({COMMON_JSON_STACK_ELEMENT_KEY, key});
|
|
return true;
|
|
}
|
|
bool start_array(std::size_t) override { // NOLINT
|
|
stack.push_back({COMMON_JSON_STACK_ELEMENT_ARRAY, ""});
|
|
return true;
|
|
}
|
|
bool end_array() override {
|
|
GGML_ASSERT(!stack.empty() && stack.back().type == COMMON_JSON_STACK_ELEMENT_ARRAY);
|
|
stack.pop_back();
|
|
close_value();
|
|
return true;
|
|
}
|
|
};
|
|
json_error_locator err_loc;
|
|
auto start = it;
|
|
json::sax_parse(it, end, &err_loc);
|
|
|
|
if (err_loc.found_error) {
|
|
it = start;
|
|
auto temptative_end = it + err_loc.position;
|
|
// LOG_DBG("Error at position %zu (is_end = %s): %s\n", err_loc.position, temptative_end == end ? "true" : "false", err_loc.exception_message.c_str());
|
|
|
|
auto input = std::string(it, temptative_end);
|
|
try {
|
|
out.json = json::parse(input);
|
|
// out.json = json::parse(it, temptative_end);
|
|
it = temptative_end;
|
|
return true;
|
|
} catch (const std::exception & ex) {
|
|
// No, needs healing.
|
|
LOG("Failed to parse up to error: %s: <<<%s>>>\n", ex.what(), std::string(it, temptative_end).c_str());
|
|
}
|
|
auto can_parse = [](const std::string & str) {
|
|
try {
|
|
auto _ = json::parse(str); // NOLINT
|
|
return true;
|
|
} catch (const std::exception &) {
|
|
return false;
|
|
}
|
|
};
|
|
if (!healing_marker.empty() && !err_loc.stack.empty()) {
|
|
std::string str(it, temptative_end);
|
|
auto last_non_sp_pos = str.find_last_not_of(" \n\r\t");
|
|
if (last_non_sp_pos == std::string::npos) {
|
|
throw std::runtime_error("Cannot heal a truncated JSON that stopped in an unknown location");
|
|
}
|
|
auto last_non_sp_char = str[last_non_sp_pos];
|
|
// Used to detect stops on a number, which may not be complete.
|
|
auto was_maybe_number = [&]() {
|
|
if (!str.empty() && std::isspace(str.back())) {
|
|
return false;
|
|
}
|
|
return std::isdigit(last_non_sp_char) ||
|
|
last_non_sp_char == '.' ||
|
|
last_non_sp_char == 'e' ||
|
|
last_non_sp_char == 'E' ||
|
|
last_non_sp_char == '-';
|
|
};
|
|
|
|
std::string closing;
|
|
for (size_t i = err_loc.stack.size(); i > 0; i--) {
|
|
auto & el = err_loc.stack[i - 1];
|
|
if (el.type == COMMON_JSON_STACK_ELEMENT_OBJECT) {
|
|
closing += "}";
|
|
} else if (el.type == COMMON_JSON_STACK_ELEMENT_ARRAY) {
|
|
closing += "]";
|
|
} else if (el.type != COMMON_JSON_STACK_ELEMENT_KEY) {
|
|
throw std::runtime_error("Unexpected stack element type");
|
|
}
|
|
}
|
|
|
|
// Matches a potentially partial unicode escape sequence, e.g. \u, \uX, \uXX, \uXXX, \uXXXX
|
|
static const std::regex partial_unicode_regex(R"(\\u(?:[0-9a-fA-F](?:[0-9a-fA-F](?:[0-9a-fA-F](?:[0-9a-fA-F])?)?)?)?$)");
|
|
|
|
auto is_high_surrogate = [&](const std::string & s) {
|
|
// Check if a partial of a high surrogate (U+D800-U+DBFF)
|
|
return s.length() >= 4 &&
|
|
s[0] == '\\' && s[1] == 'u' &&
|
|
std::tolower(s[2]) == 'd' &&
|
|
(s[3] == '8' || s[3] == '9' || std::tolower(s[3]) == 'a' || std::tolower(s[3]) == 'b');
|
|
};
|
|
|
|
// Initialize the unicode marker to a low surrogate to handle the edge case
|
|
// where a high surrogate (U+D800-U+DBFF) is immediately followed by a
|
|
// backslash (\)
|
|
std::string unicode_marker_padding = "udc00";
|
|
std::smatch last_unicode_seq;
|
|
|
|
if (std::regex_search(str, last_unicode_seq, partial_unicode_regex)) {
|
|
std::smatch second_last_seq;
|
|
std::string prelude = str.substr(0, last_unicode_seq.position());
|
|
|
|
// Pad the escape sequence with 0s until it forms a complete sequence of 6 characters
|
|
unicode_marker_padding = std::string(6 - last_unicode_seq.length(), '0');
|
|
|
|
if (is_high_surrogate(last_unicode_seq.str())) {
|
|
// If the sequence is a partial match for a high surrogate, add a low surrogate (U+DC00-U+UDFF)
|
|
unicode_marker_padding += "\\udc00";
|
|
} else if (std::regex_search(prelude, second_last_seq, partial_unicode_regex)) {
|
|
if (is_high_surrogate(second_last_seq.str())) {
|
|
// If this follows a high surrogate, pad it to be a low surrogate
|
|
if (last_unicode_seq.length() == 2) {
|
|
unicode_marker_padding = "dc00";
|
|
} else if (last_unicode_seq.length() == 3) {
|
|
unicode_marker_padding = "c00";
|
|
} else {
|
|
// The original unicode_marker_padding is already padded with 0s
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const auto & magic_seed = out.healing_marker.marker = healing_marker;//"$llama.cpp.json$";
|
|
|
|
if (err_loc.stack.back().type == COMMON_JSON_STACK_ELEMENT_KEY) {
|
|
// We're inside an object value
|
|
if (last_non_sp_char == ':' && can_parse(str + "1" + closing)) {
|
|
// Was about to create an object value
|
|
str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
} else if (can_parse(str + ": 1" + closing)) {
|
|
str += (out.healing_marker.json_dump_marker = ":\"" + magic_seed) + "\"" + closing;
|
|
} else if (last_non_sp_char == '{' && can_parse(str + closing)) {
|
|
// Was about to create an object
|
|
str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\": 1" + closing;
|
|
} else if (can_parse(str + "\"" + closing)) {
|
|
// Was inside an object value string
|
|
str += (out.healing_marker.json_dump_marker = magic_seed) + "\"" + closing;
|
|
} else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\"" + closing)) {
|
|
// Was inside an object value string after an escape
|
|
str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\"" + closing;
|
|
} else if (can_parse(str + unicode_marker_padding + "\"" + closing)) {
|
|
// Was inside an object value string after a partial unicode escape
|
|
str += (out.healing_marker.json_dump_marker = unicode_marker_padding + magic_seed) + "\"" + closing;
|
|
} else {
|
|
// find last :
|
|
auto last_pos = str.find_last_of(':');
|
|
if (last_pos == std::string::npos) {
|
|
throw std::runtime_error("Cannot heal a truncated JSON that stopped in an unknown location");
|
|
}
|
|
// Cutting back to opening : for object value
|
|
str = str.substr(0, last_pos + 1) + (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
}
|
|
} else if (err_loc.stack.back().type == COMMON_JSON_STACK_ELEMENT_ARRAY) {
|
|
if ((last_non_sp_char == ',' || last_non_sp_char == '[') && can_parse(str + "1" + closing)) {
|
|
// Was about to create an array value
|
|
str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
} else if (can_parse(str + "\"" + closing)) {
|
|
// Was inside an array value string
|
|
str += (out.healing_marker.json_dump_marker = magic_seed) + "\"" + closing;
|
|
} else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\"" + closing)) {
|
|
// Was inside an array value string after an escape
|
|
str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\"" + closing;
|
|
} else if (can_parse(str + unicode_marker_padding + "\"" + closing)) {
|
|
// Was inside an array value string after a partial unicode escape
|
|
str += (out.healing_marker.json_dump_marker = unicode_marker_padding + magic_seed) + "\"" + closing;
|
|
} else if (!was_maybe_number() && can_parse(str + ", 1" + closing)) {
|
|
// Had just finished a value
|
|
str += (out.healing_marker.json_dump_marker = ",\"" + magic_seed) + "\"" + closing;
|
|
} else {
|
|
auto last_pos = str.find_last_of("[,");
|
|
if (last_pos == std::string::npos) {
|
|
throw std::runtime_error("Cannot heal a truncated JSON array stopped in an unknown location");
|
|
}
|
|
// Cutting back to last [ or , for array value
|
|
str = str.substr(0, last_pos + 1) + (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
}
|
|
} else if (err_loc.stack.back().type == COMMON_JSON_STACK_ELEMENT_OBJECT) {
|
|
if ((last_non_sp_char == '{' && can_parse(str + closing)) ||
|
|
(last_non_sp_char == ',' && can_parse(str + "\"\": 1" + closing))) {
|
|
// Was about to create an object key+value
|
|
str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\": 1" + closing;
|
|
} else if (!was_maybe_number() && can_parse(str + ",\"\": 1" + closing)) {
|
|
// Was about to create an object key+value
|
|
str += (out.healing_marker.json_dump_marker = ",\"" + magic_seed) + "\": 1" + closing;
|
|
} else if (can_parse(str + "\": 1" + closing)) {
|
|
// Was inside an object key string
|
|
str += (out.healing_marker.json_dump_marker = magic_seed) + "\": 1" + closing;
|
|
} else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\": 1" + closing)) {
|
|
// Was inside an object key string after an escape
|
|
str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\": 1" + closing;
|
|
} else if (can_parse(str + unicode_marker_padding + "\": 1" + closing)) {
|
|
// Was inside an object key string after a partial unicode escape
|
|
str += (out.healing_marker.json_dump_marker = unicode_marker_padding + magic_seed) + "\": 1" + closing;
|
|
} else {
|
|
auto last_pos = str.find_last_of(':');
|
|
if (last_pos == std::string::npos) {
|
|
throw std::runtime_error("Cannot heal a truncated JSON object stopped in an unknown location");
|
|
}
|
|
// fprintf(stderr, "Cutting back to last : for object key+value\n");
|
|
str = str.substr(0, last_pos + 1) + (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
}
|
|
} else {
|
|
throw std::runtime_error("Cannot heal a truncated JSON object stopped in an unknown location");
|
|
}
|
|
// fprintf(stderr, "HEALED:\nSTRING <<<\n%s\n>>>\n\nmagic_cut: <<<\n%s\n>>>\n\n", str.c_str(), out.healing_marker.json_dump_marker.c_str());
|
|
out.json = json::parse(str);
|
|
it = temptative_end;
|
|
return true;
|
|
}
|
|
// handle unclosed top-level primitive
|
|
if (err_loc.position != 0 && !healing_marker.empty() && err_loc.stack.empty()) {
|
|
std::string str(it, temptative_end);
|
|
const auto & magic_seed = out.healing_marker.marker = healing_marker;
|
|
if (can_parse(str + "\"")) {
|
|
// Was inside an string
|
|
str += (out.healing_marker.json_dump_marker = magic_seed) + "\"";
|
|
} else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\"")) {
|
|
// Was inside an string after an escape
|
|
str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\"";
|
|
} else {
|
|
// TODO: handle more unclosed top-level primitive if the stack was empty but we got an error (e.g. "tru", "\"", etc...)
|
|
// fprintf(stderr, "Closing: TODO\n");
|
|
return false;
|
|
}
|
|
out.json = json::parse(str);
|
|
it = temptative_end;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
out.json = json::parse(it, end);
|
|
it = end;
|
|
return true;
|
|
}
|