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 .claude/rules/subproject-llama-cpp-bindings-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ paths:

# `llama-cpp-bindings-types` Context

- The purposse of `llama-cpp-bindings-types` is to provide a thin layer of types that do not need to rely on `llama.cpp` vendored library itself
- The purposse of `llama-cpp-bindings-types` is to provide a thin layer of types that do not need to rely on the `llama.cpp` library itself
- `llama-cpp-bindings-types` must not depend on llama.cpp bindings themselves
27 changes: 14 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ members = [

[workspace.package]
edition = "2024"
version = "0.14.0"
version = "0.15.0"
license = "Apache-2.0"
repository = "https://github.com/intentee/llama-cpp-bindings"

Expand All @@ -33,21 +33,22 @@ find_cuda_helper = "=0.2.0"
hf-hub = "=0.5.0"
inventory = "=0.3.24"
libtest-mimic = "=0.8.2"
llama-cpp-bindings = { path = "llama-cpp-bindings", version = "=0.14.0" }
llama-cpp-bindings-build = { path = "llama-cpp-bindings-build", version = "=0.14.0" }
llama-cpp-bindings-sys = { path = "llama-cpp-bindings-sys", version = "=0.14.0" }
llama-cpp-bindings-types = { path = "llama-cpp-bindings-types", version = "=0.14.0" }
llama-cpp-error-recorder = { path = "llama-cpp-error-recorder", version = "=0.14.0" }
llama-cpp-ffi-status = { path = "llama-cpp-ffi-status", version = "=0.14.0" }
llama-cpp-gbnf = { path = "llama-cpp-gbnf", version = "=0.14.0" }
llama-cpp-log-decoder = { path = "llama-cpp-log-decoder", version = "=0.14.0" }
llama-cpp-test-harness = { path = "llama-cpp-test-harness", version = "=0.14.0" }
llama-cpp-test-harness-macros = { path = "llama-cpp-test-harness-macros", version = "=0.14.0" }
llama-cpp-bindings = { path = "llama-cpp-bindings", version = "=0.15.0" }
llama-cpp-bindings-build = { path = "llama-cpp-bindings-build", version = "=0.15.0" }
llama-cpp-bindings-sys = { path = "llama-cpp-bindings-sys", version = "=0.15.0" }
llama-cpp-bindings-types = { path = "llama-cpp-bindings-types", version = "=0.15.0" }
llama-cpp-error-recorder = { path = "llama-cpp-error-recorder", version = "=0.15.0" }
llama-cpp-ffi-status = { path = "llama-cpp-ffi-status", version = "=0.15.0" }
llama-cpp-gbnf = { path = "llama-cpp-gbnf", version = "=0.15.0" }
llama-cpp-log-decoder = { path = "llama-cpp-log-decoder", version = "=0.15.0" }
llama-cpp-test-harness = { path = "llama-cpp-test-harness", version = "=0.15.0" }
llama-cpp-test-harness-macros = { path = "llama-cpp-test-harness-macros", version = "=0.15.0" }
llama-cpp-wrapper-error-fixture = { path = "llama-cpp-wrapper-error-fixture" }
llama-cpp-wrapper-sources = { path = "llama-cpp-wrapper-sources", version = "=0.14.0" }
llama-cpp-wrapper-sources = { path = "llama-cpp-wrapper-sources", version = "=0.15.0" }
llguidance = "=1.7.0"
log = "=0.4.29"
nom = "=8.0.0"
once_cell = "1.21"
proc-macro2 = "=1.0.106"
quote = "=1.0.45"
serde = { version = "=1.0.228", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ WRAPPER_SOURCES_CRATE_FILES = \
EMIT_WRAPPER_BUILD_INPUTS = cargo run --quiet --package $(WRAPPER_SOURCES_CRATE) -- \
$(CURDIR)/llama-cpp-bindings-sys $(COMPILE_COMMANDS) $(WRAPPER_SOURCES_RESPONSE_FILE)

VENDORED_SUPPRESSIONS = \
LLAMA_CPP_AND_GSL_SUPPRESSIONS = \
--suppress='*:*llama-cpp-bindings-sys/llama.cpp/*' \
--suppress='*:*llama-cpp-bindings-sys/GSL/*'

Expand Down Expand Up @@ -115,7 +115,7 @@ lint.cpp.clang-tidy: $(COMPILE_COMMANDS) $(WRAPPER_SOURCES_RESPONSE_FILE)
lint.cpp.cppcheck: $(COMPILE_COMMANDS) $(CPPCHECK)
$(CPPCHECK) --project=$(COMPILE_COMMANDS) --enable=all --inconclusive \
--check-level=exhaustive --error-exitcode=1 \
$(VENDORED_SUPPRESSIONS) \
$(LLAMA_CPP_AND_GSL_SUPPRESSIONS) \
--suppress=missingIncludeSystem

.PHONY: test
Expand Down
93 changes: 59 additions & 34 deletions llama-cpp-bindings-sys/wrapper_chat_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,61 @@ void dup_or_set_alloc_flag(const std::string & source, char ** out_dup, bool * o
*out_dup = llama_rs_dup_string(source);
*out_alloc_failed = (*out_dup == nullptr);
}

auto report_current_parse_exception(
char ** out_error,
llama_rs_parse_chat_message_status thrown_status) -> llama_rs_parse_chat_message_status {
try {
throw;
} catch (const std::bad_alloc &) {
return LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_OUT_OF_MEMORY;
} catch (const std::exception & ex) {
*out_error = llama_rs_dup_string(std::string(ex.what()));
} catch (...) {
*out_error = llama_rs_dup_string(std::string("unknown c++ exception"));
}
if (*out_error == nullptr) {
return LLAMA_RS_PARSE_CHAT_MESSAGE_ERROR_STRING_ALLOCATION_FAILED;
}
return thrown_status;
}

auto build_tools_parser(const llama_rs_chat_parser & parser, const char * tools_json) -> common_peg_arena {
autoparser::generation_params inputs;

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

return parser.parser.build_parser(inputs, std::string());
}

auto parse_with_tools_parser(
const common_peg_arena & chat_parser,
const char * input,
int is_partial,
llama_rs_parsed_chat_handle * out_handle,
char ** out_error) -> llama_rs_parse_chat_message_status {
try {
common_chat_parser_params parser_params;
parser_params.format = COMMON_CHAT_FORMAT_PEG_NATIVE;

common_chat_msg parsed =
common_chat_peg_parse(chat_parser, input, is_partial != 0, parser_params);

auto handle = std::make_unique<llama_rs_parsed_chat>();
handle->message = std::move(parsed);

*out_handle = handle.release();

return LLAMA_RS_PARSE_CHAT_MESSAGE_OK;
} catch (...) {
return report_current_parse_exception(
out_error, LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_THREW_CXX_EXCEPTION);
}
}
} // namespace

extern "C" auto llama_rs_chat_parser_create(
Expand Down Expand Up @@ -148,42 +203,12 @@ extern "C" auto llama_rs_parse_chat_message(
}

try {
autoparser::generation_params inputs;

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

common_peg_arena const chat_parser = parser->parser.build_parser(inputs, std::string());

common_chat_parser_params parser_params;
parser_params.format = COMMON_CHAT_FORMAT_PEG_NATIVE;

common_chat_msg parsed =
common_chat_peg_parse(chat_parser, input, is_partial != 0, parser_params);
common_peg_arena const chat_parser = build_tools_parser(*parser, tools_json);

auto handle = std::make_unique<llama_rs_parsed_chat>();
handle->message = std::move(parsed);

*out_handle = handle.release();

return LLAMA_RS_PARSE_CHAT_MESSAGE_OK;
} catch (const std::bad_alloc &) {
return LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_OUT_OF_MEMORY;
} catch (const std::exception & ex) {
*out_error = llama_rs_dup_string(std::string(ex.what()));
if (*out_error == nullptr) {
return LLAMA_RS_PARSE_CHAT_MESSAGE_ERROR_STRING_ALLOCATION_FAILED;
}
return LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_THREW_CXX_EXCEPTION;
return parse_with_tools_parser(chat_parser, input, is_partial, out_handle, out_error);
} catch (...) {
*out_error = llama_rs_dup_string(std::string("unknown c++ exception"));
if (*out_error == nullptr) {
return LLAMA_RS_PARSE_CHAT_MESSAGE_ERROR_STRING_ALLOCATION_FAILED;
}
return LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_THREW_CXX_EXCEPTION;
return report_current_parse_exception(
out_error, LLAMA_RS_PARSE_CHAT_MESSAGE_TOOLS_PARSER_BUILD_THREW_CXX_EXCEPTION);
}
}

Expand Down
1 change: 1 addition & 0 deletions llama-cpp-bindings-sys/wrapper_chat_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ typedef enum llama_rs_parse_chat_message_status {
LLAMA_RS_PARSE_CHAT_MESSAGE_ERROR_STRING_ALLOCATION_FAILED,
LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_OUT_OF_MEMORY,
LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_THREW_CXX_EXCEPTION,
LLAMA_RS_PARSE_CHAT_MESSAGE_TOOLS_PARSER_BUILD_THREW_CXX_EXCEPTION,
} llama_rs_parse_chat_message_status;

llama_rs_parse_chat_message_status llama_rs_parse_chat_message(
Expand Down
Loading
Loading