Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llama-cpp-bindings-sys/llama.cpp
Submodule llama.cpp updated 251 files
7 changes: 3 additions & 4 deletions llama-cpp-bindings-sys/wrapper_chat_apply.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include "wrapper_chat_apply.h"
#include "nlohmann/json_fwd.hpp"
#include "wrapper_token_text.h"

#include "llama.cpp/common/chat-auto-parser.h"
#include "llama.cpp/common/chat.h"
#include "llama.cpp/common/json.h"
#include "llama.cpp/include/llama.h"
#include "wrapper_utils.h"

#include <cstddef>
#include <exception>
#include <gsl/span>
#include <new>
#include <nlohmann/json.hpp>
#include <string>
#include <utility>

Expand Down Expand Up @@ -60,7 +59,7 @@ extern "C" auto llama_rs_apply_chat_template(

common_chat_template const tmpl(template_src, bos_token, eos_token);

nlohmann::ordered_json messages = nlohmann::ordered_json::array();
common_json messages = common_json::array();
const gsl::span<const char * const> role_span(roles, n_messages);
const gsl::span<const char * const> content_span(contents, n_messages);
for (size_t index = 0; index < n_messages; index++) {
Expand All @@ -72,7 +71,7 @@ extern "C" auto llama_rs_apply_chat_template(

autoparser::generation_params inputs;
inputs.messages = std::move(messages);
inputs.tools = nlohmann::ordered_json::array();
inputs.tools = common_json::array();
inputs.add_generation_prompt = add_generation_prompt != 0;
inputs.enable_thinking = enable_thinking != 0;

Expand Down
7 changes: 3 additions & 4 deletions llama-cpp-bindings-sys/wrapper_chat_parse.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "wrapper_chat_parse.h"
#include <nlohmann/json.hpp> // IWYU pragma: keep
#include <nlohmann/json_fwd.hpp>
#include "peg-parser.h"
#include "wrapper_token_text.h"

#include "llama.cpp/common/chat-auto-parser.h"
#include "llama.cpp/common/chat.h"
#include "llama.cpp/common/json.h"
#include "llama.cpp/include/llama.h"
#include "wrapper_utils.h"

Expand Down Expand Up @@ -152,9 +151,9 @@ extern "C" auto llama_rs_parse_chat_message(
autoparser::generation_params inputs;

if ((tools_json != nullptr) && *tools_json != '\0') {
inputs.tools = nlohmann::ordered_json::parse(tools_json);
inputs.tools = common_json::parse(tools_json);
} else {
inputs.tools = nlohmann::ordered_json::array();
inputs.tools = common_json::array();
}

common_peg_arena const chat_parser = parser->parser.build_parser(inputs, std::string());
Expand Down
5 changes: 2 additions & 3 deletions llama-cpp-bindings-sys/wrapper_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
#include <string>

#include "llama.cpp/common/json-schema-to-grammar.h"
#include "llama.cpp/common/json.h"
#include "llama.cpp/include/llama.h"
#include <nlohmann/json.hpp> // IWYU pragma: keep
#include <nlohmann/json_fwd.hpp>
#include "wrapper_utils.h"

extern "C" auto llama_rs_json_schema_to_grammar(
Expand All @@ -38,7 +37,7 @@ extern "C" auto llama_rs_json_schema_to_grammar(
}

try {
const auto schema = nlohmann::ordered_json::parse(schema_json);
const auto schema = common_json::parse(schema_json);
const auto grammar = json_schema_to_grammar(schema, force_gbnf);
*out_grammar = llama_rs_dup_string(grammar);
if (*out_grammar == nullptr) {
Expand Down
22 changes: 21 additions & 1 deletion llama-cpp-bindings-sys/wrapper_fit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern "C" auto llama_rs_fit_params(
struct llama_model_tensor_buft_override * tensor_buft_overrides,
size_t * margins,
uint32_t n_ctx_min,
const llama_rs_fit_extra_model * extra,
enum ggml_log_level log_level,
int32_t * out_unrecognized_status_code,
char ** out_error) -> llama_rs_fit_params_status {
Expand All @@ -42,11 +43,30 @@ extern "C" auto llama_rs_fit_params(
if (out_error == nullptr) {
return LLAMA_RS_FIT_PARAMS_NULL_OUT_ERROR_ARG;
}
if (extra != nullptr) {
if (extra->path_model == nullptr) {
return LLAMA_RS_FIT_PARAMS_NULL_EXTRA_PATH_MODEL_ARG;
}
if (extra->mparams == nullptr) {
return LLAMA_RS_FIT_PARAMS_NULL_EXTRA_MPARAMS_ARG;
}
if (extra->cparams == nullptr) {
return LLAMA_RS_FIT_PARAMS_NULL_EXTRA_CPARAMS_ARG;
}
}

try {
common_fit_extra_model vendored_extra{};
if (extra != nullptr) {
vendored_extra.path_model = extra->path_model;
vendored_extra.mparams = extra->mparams;
vendored_extra.cparams = extra->cparams;
vendored_extra.shares_model = extra->shares_model;
}

const common_params_fit_status status = common_fit_params(
path_model, mparams, cparams, tensor_split, tensor_buft_overrides,
margins, n_ctx_min, log_level);
margins, n_ctx_min, extra == nullptr ? nullptr : &vendored_extra, log_level);
switch (status) {
case COMMON_PARAMS_FIT_STATUS_SUCCESS:
return LLAMA_RS_FIT_PARAMS_OK;
Expand Down
12 changes: 12 additions & 0 deletions llama-cpp-bindings-sys/wrapper_fit.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "llama.cpp/ggml/include/ggml.h"
#include "llama.cpp/include/llama.h"

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Expand All @@ -23,8 +24,18 @@ typedef enum llama_rs_fit_params_status {
LLAMA_RS_FIT_PARAMS_ERROR_STRING_ALLOCATION_FAILED,
LLAMA_RS_FIT_PARAMS_VENDORED_OUT_OF_MEMORY,
LLAMA_RS_FIT_PARAMS_VENDORED_THREW_CXX_EXCEPTION,
LLAMA_RS_FIT_PARAMS_NULL_EXTRA_PATH_MODEL_ARG,
LLAMA_RS_FIT_PARAMS_NULL_EXTRA_MPARAMS_ARG,
LLAMA_RS_FIT_PARAMS_NULL_EXTRA_CPARAMS_ARG,
} llama_rs_fit_params_status;

typedef struct llama_rs_fit_extra_model {
const char * path_model;
struct llama_model_params * mparams;
struct llama_context_params * cparams;
bool shares_model;
} llama_rs_fit_extra_model;

llama_rs_fit_params_status llama_rs_fit_params(
const char * path_model,
struct llama_model_params * mparams,
Expand All @@ -33,6 +44,7 @@ llama_rs_fit_params_status llama_rs_fit_params(
struct llama_model_tensor_buft_override * tensor_buft_overrides,
size_t * margins,
uint32_t n_ctx_min,
const llama_rs_fit_extra_model * extra,
enum ggml_log_level log_level,
int32_t * out_unrecognized_status_code,
char ** out_error);
Expand Down
7 changes: 3 additions & 4 deletions llama-cpp-bindings-sys/wrapper_reasoning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

#include "llama.cpp/common/chat-auto-parser.h"
#include "llama.cpp/common/chat.h"
#include "llama.cpp/common/json.h"
#include "llama.cpp/include/llama.h"
#include <nlohmann/json.hpp> // IWYU pragma: keep
#include <nlohmann/json_fwd.hpp>
#include "wrapper_utils.h"

#include <cstddef>
Expand Down Expand Up @@ -36,8 +35,8 @@ auto find_reasoning_markers(
probe_params.is_inference = false;
probe_params.add_inference = false;
probe_params.mark_input = false;
probe_params.messages = nlohmann::ordered_json::array({
nlohmann::ordered_json{ { "role", "user" }, { "content", "ping" } },
probe_params.messages = common_json::array({
common_json{ { "role", "user" }, { "content", "ping" } },
});

const std::string tmpl_src_str = tmpl_src;
Expand Down
111 changes: 36 additions & 75 deletions llama-cpp-bindings-sys/wrapper_tool_calls.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "wrapper_tool_calls.h"
#include <nlohmann/json.hpp> // IWYU pragma: keep
#include <nlohmann/json_fwd.hpp>
#include "wrapper_token_text.h"

#include "llama.cpp/common/chat-auto-parser.h"
#include "llama.cpp/common/chat-auto-parser-helpers.h"
#include "llama.cpp/common/chat.h"
#include "llama.cpp/common/json.h"
#include "llama.cpp/include/llama.h"
#include "wrapper_utils.h"

Expand All @@ -18,18 +17,21 @@ using wrapper_helpers::token_text_or_empty;

namespace {

auto detect_tool_call_haystack(
const common_chat_template & tmpl,
const autoparser::analyze_reasoning & reasoning) -> std::string {
nlohmann::ordered_json const user_msg = {
struct tool_call_probe_params {
template_params without_tool_calls;
template_params with_tool_calls;
};

auto build_tool_call_probe_params() -> tool_call_probe_params {
common_json const user_msg = {
{ "role", "user" },
{ "content", "Please use the tool" }
};
nlohmann::ordered_json const assistant_no_tools = {
common_json const assistant_no_tools = {
{ "role", "assistant" },
{ "content", "Sure, calling." }
};
nlohmann::ordered_json const first_tool_call = {
common_json const first_tool_call = {
{ "id", "call_001" },
{ "type", "function" },
{ "function", {
Expand All @@ -40,12 +42,12 @@ auto detect_tool_call_haystack(
}}
}}
};
nlohmann::ordered_json const assistant_with_tools = {
{ "role", "assistant" },
{ "content", "" },
{ "tool_calls", nlohmann::ordered_json::array({ first_tool_call }) }
common_json const assistant_with_tools = {
{ "role", "assistant" },
{ "content", "" },
{ "tool_calls", common_json::array({ first_tool_call }) }
};
nlohmann::ordered_json const tool_definition = {
common_json const tool_definition = {
{ "type", "function" },
{ "function", {
{ "name", "tool_first" },
Expand All @@ -56,23 +58,30 @@ auto detect_tool_call_haystack(
{ "arg_first", { { "type", "string" }, { "description", "first arg" } } },
{ "arg_second", { { "type", "string" }, { "description", "second arg" } } },
}},
{ "required", nlohmann::ordered_json::array({ "arg_first", "arg_second" }) },
{ "required", common_json::array({ "arg_first", "arg_second" }) },
}}
}}
};

template_params params_no_tools;
params_no_tools.messages = nlohmann::ordered_json::array({ user_msg, assistant_no_tools });
params_no_tools.tools = nlohmann::ordered_json::array({ tool_definition });
params_no_tools.add_generation_prompt = false;
params_no_tools.enable_thinking = true;
tool_call_probe_params probe;
probe.without_tool_calls.messages = common_json::array({ user_msg, assistant_no_tools });
probe.without_tool_calls.tools = common_json::array({ tool_definition });
probe.without_tool_calls.add_generation_prompt = false;
probe.without_tool_calls.enable_thinking = true;

probe.with_tool_calls = probe.without_tool_calls;
probe.with_tool_calls.messages = common_json::array({ user_msg, assistant_with_tools });

return probe;
}

template_params params_with_tools = params_no_tools;
params_with_tools.messages =
nlohmann::ordered_json::array({ user_msg, assistant_with_tools });
auto detect_tool_call_haystack(
const common_chat_template & tmpl,
const autoparser::analyze_reasoning & reasoning) -> std::string {
tool_call_probe_params const probe = build_tool_call_probe_params();

std::string const output_no_tools = autoparser::apply_template(tmpl, params_no_tools);
std::string const output_with_tools = autoparser::apply_template(tmpl, params_with_tools);
std::string const output_no_tools = autoparser::apply_template(tmpl, probe.without_tool_calls);
std::string const output_with_tools = autoparser::apply_template(tmpl, probe.with_tool_calls);

if (output_no_tools.empty() || output_with_tools.empty()) {
return {};
Expand Down Expand Up @@ -210,58 +219,10 @@ extern "C" auto llama_rs_diagnose_tool_call_synthetic_renders(

common_chat_template const tmpl(tmpl_src, bos_token, eos_token);

nlohmann::ordered_json const user_msg = {
{ "role", "user" },
{ "content", "Please use the tool" }
};
nlohmann::ordered_json const assistant_no_tools = {
{ "role", "assistant" },
{ "content", "Sure, calling." }
};
nlohmann::ordered_json const first_tool_call = {
{ "id", "call_001" },
{ "type", "function" },
{ "function", {
{ "name", "tool_first" },
{ "arguments", {
{ "arg_first", "XXXX" },
{ "arg_second", "YYYY" },
}}
}}
};
nlohmann::ordered_json const assistant_with_tools = {
{ "role", "assistant" },
{ "content", "" },
{ "tool_calls", nlohmann::ordered_json::array({ first_tool_call }) }
};
nlohmann::ordered_json const tool_definition = {
{ "type", "function" },
{ "function", {
{ "name", "tool_first" },
{ "description", "First test tool" },
{ "parameters", {
{ "type", "object" },
{ "properties", {
{ "arg_first", { { "type", "string" }, { "description", "first arg" } } },
{ "arg_second", { { "type", "string" }, { "description", "second arg" } } },
}},
{ "required", nlohmann::ordered_json::array({ "arg_first", "arg_second" }) },
}}
}}
};

template_params params_no_tools;
params_no_tools.messages = nlohmann::ordered_json::array({ user_msg, assistant_no_tools });
params_no_tools.tools = nlohmann::ordered_json::array({ tool_definition });
params_no_tools.add_generation_prompt = false;
params_no_tools.enable_thinking = true;

template_params params_with_tools = params_no_tools;
params_with_tools.messages =
nlohmann::ordered_json::array({ user_msg, assistant_with_tools });
tool_call_probe_params const probe = build_tool_call_probe_params();

std::string const output_a = autoparser::apply_template(tmpl, params_no_tools);
std::string const output_b = autoparser::apply_template(tmpl, params_with_tools);
std::string const output_a = autoparser::apply_template(tmpl, probe.without_tool_calls);
std::string const output_b = autoparser::apply_template(tmpl, probe.with_tool_calls);

std::unique_ptr<char[]> a_dup(llama_rs_dup_string(output_a));
std::unique_ptr<char[]> b_dup(llama_rs_dup_string(output_b));
Expand Down
Loading
Loading