diff --git a/common/common.h b/common/common.h index 0f0817e6d..0d34c962e 100644 --- a/common/common.h +++ b/common/common.h @@ -471,16 +471,17 @@ std::string llama_detokenize( // Chat template utils // +struct llama_chat_msg_tool_call { + std::string name; + std::string arguments; +}; + // same as llama_chat_message, but uses std::string and std::vector struct llama_chat_msg { std::string role; std::string content; std::string tool; - struct llama_tool_call { - std::string name; - std::string arguments; - }; - std::vector tool_calls; + std::vector tool_calls; }; // Check if the template supplied via "--chat-template" is supported or not. Returns true if it's valid @@ -571,8 +572,8 @@ private: // The Aho–Corasick algorithm allows efficient string matching with multiple patterns. // See https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm struct TrieNode { - std::unordered_map children; - TrieNode* fail = nullptr; + std::unordered_map children; + struct TrieNode* fail = nullptr; int output = -1; size_t depth = 0; diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index 9dfcedb4f..e57a3b1cc 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -1041,15 +1041,15 @@ std::string json_schema_to_grammar(const json & schema) { } std::string build_grammar(const std::function & cb) { - SchemaConverter converter([&](const std::string & name) { return json(); }, /* dotall= */ false); + SchemaConverter converter([&](const std::string &) { return json(); }, /* dotall= */ false); llama_grammar_builder builder { - .add_rule = [&](const std::string & name, const std::string & rule) { + /* .add_rule = */ [&](const std::string & name, const std::string & rule) { return converter.add_rule(name, rule); }, - .add_schema = [&](const std::string & name, const nlohmann::ordered_json & schema) { + /* .add_schema = */ [&](const std::string & name, const nlohmann::ordered_json & schema) { return converter.visit(schema, name); }, - .resolve_refs = [&](nlohmann::ordered_json & schema) { + /* .resolve_refs = */ [&](nlohmann::ordered_json & schema) { converter.resolve_refs(schema, ""); } }; diff --git a/common/minja.hpp b/common/minja.hpp index fef6d5fef..646b054b7 100644 --- a/common/minja.hpp +++ b/common/minja.hpp @@ -2160,7 +2160,7 @@ private: throw unterminated(**start); } children.emplace_back(nonstd_make_unique(token->location, std::move(macro_token->name), std::move(macro_token->params), std::move(body))); - } else if (auto comment_token = dynamic_cast(token.get())) { + } else if (dynamic_cast(token.get())) { // Ignore comments } else if (dynamic_cast(token.get()) || dynamic_cast(token.get()) diff --git a/common/tool-call.cpp b/common/tool-call.cpp index ea7753b4e..af2d95cf8 100644 --- a/common/tool-call.cpp +++ b/common/tool-call.cpp @@ -41,8 +41,7 @@ static bool parse_json(std::string::const_iterator & it, const std::string::cons 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 { - // LOG_WARNING("JSON error (Expected)", {{"position", position}, {"last_token", last_token}, {"error", ex.what()}}); + bool parse_error(std::size_t position, const std::string &, const json::exception &) override { this->position = position - 1; this->found_error = true; return false; @@ -70,13 +69,11 @@ static bool parse_json(std::string::const_iterator & it, const std::string::cons temptative_end = end; } std::string json_sub {it, temptative_end}; - // LOG_WARNING("Parsing json", {{"json_sub", json_sub}}); try { out = json::parse(json_sub); it = temptative_end; return true; - } catch (const std::exception & e) { - // LOG_WARNING("Failed to parse tool call", {{"json_sub", json_sub}, {"error", e.what()}}); + } catch (const std::exception &) { return false; } } diff --git a/common/tool-call.h b/common/tool-call.h index fd30f1f7c..de3958575 100644 --- a/common/tool-call.h +++ b/common/tool-call.h @@ -1,18 +1,14 @@ #pragma once #include "ggml.h" +#include "common.h" // Change JSON_ASSERT from assert() to GGML_ASSERT: #define JSON_ASSERT GGML_ASSERT #include "json.hpp" -struct llama_tool_call { - std::string name; - std::string arguments; -}; - struct llama_tool_calls { std::string content; - std::vector tool_calls; + std::vector tool_calls; }; struct llama_tool_call_handler { diff --git a/examples/server/server.cpp b/examples/server/server.cpp index 10fec4174..49c412f8b 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -662,7 +662,7 @@ struct server_context { bool validate_model_chat_template(bool use_jinja) const { llama_chat_message chat[] = {{"user", "test"}}; - const int res = llama_chat_apply_template(model, nullptr, chat, 1, true, nullptr, 0, use_jinja); + const int res = llama_chat_apply_template(model, nullptr, chat, 1, true, nullptr, 0, use_jinja, nullptr, nullptr, nullptr); return res > 0; } diff --git a/include/llama.h b/include/llama.h index 2345be47e..262142b96 100644 --- a/include/llama.h +++ b/include/llama.h @@ -378,17 +378,17 @@ extern "C" { // used in chat template - typedef struct llama_tool_call { + typedef struct llama_chat_message_tool_call { const char * name; const char * arguments; - } llama_tool_call; + } llama_chat_message_tool_call; typedef struct llama_chat_message { const char * role; const char * content; const char * tool; - const llama_tool_call * tool_calls; + const llama_chat_message_tool_call * tool_calls; uint32_t n_tool_calls; } llama_chat_message; diff --git a/src/llama.cpp b/src/llama.cpp index 0c0f6322d..ddaaa1f74 100644 --- a/src/llama.cpp +++ b/src/llama.cpp @@ -21081,8 +21081,9 @@ static int32_t llama_chat_apply_template_internal( context->set("tools", tools_val); } auto tmpl_root = minja::Parser::parse(tmpl, { - .trim_blocks = true, - .lstrip_blocks = true, + /* .trim_blocks = */ true, + /* .lstrip_blocks = */ true, + /* .keep_trailing_newline = */ false, }); try { dest = tmpl_root->render(context);