llama-cpp-gbnf 0.15.1
“Standalone, model-free GBNF grammar validator built on llama.cpp's real parser”
Release published within seven daysRust test marker or harness
SHA-2568edc8955da124b9ac5682ba052f2852f79fc99a72b9c3d5400b91749c3e98c73
MaleculeMd₂(In₃Pa)
Evidence
1use std::ffi::{CString, c_char};
2
3use llama_cpp_ffi_status::FfiContractError;
4use llama_cpp_ffi_status::FfiStatusError;
5use llama_cpp_ffi_status::read_and_free_cpp_string;
6
7use llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_EMPTY_RULE_SET;
8use llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_LEFT_RECURSION;
9use llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_OK;
10use llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_ROOT_SYMBOL_MISSING;
11use llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_SYNTAX_ERROR;
12use llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_THREW_CXX_EXCEPTION;
13use llama_cpp_bindings_sys::llama_rs_gbnf_validation_status;
14use llama_cpp_bindings_sys::llama_rs_validate_gbnf;
15
16use crate::gbnf_validation_error::GbnfValidationError;
17
18fn validation_status_to_result(
19 status: llama_rs_gbnf_validation_status,
20 root: &str,
79:73… interior
80/// NUL byte, or when the grammar parser rejects the grammar.
81pub fn validate_gbnf(gramma …
92:5… };
93
94 validation_status_to_result(status, root, out_error)
95}
96
97#[cfg(test)]
98mod tests {
99 use std::ffi::CString;
100
101 use llama_cpp_ffi_status::FfiContractError;
102 use llama_cpp_ffi_status::FfiStatusError;
103
104 use super::validate_gbnf;
105 use super::validation_status_to_result;
106 use crate::gbnf_validation_error::GbnfValidationError;
107 use llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_THREW_CXX_EXCEPTION;
108
109 #[test]
110 fn valid_grammar_is_accepted() {
111 assert_eq!(validate_gbnf(r#"root ::= "yes" | "no""#, "root"), Ok(()));
⋯4 lines
1use std::ffi::NulError;
2
3#[derive(Debug, thiserror::Error, PartialEq, Eq)]
⋯7 lines
11 #[error("grammar root name contains an interior NUL byte")]
12 RootContainsNul(#[source] NulError),
13 #[error("grammar has a syntax error and could not be parsed")]
14 SyntaxError,
15 #[error("grammar defines no rules")]
16 EmptyRuleSet,
17 #[error("grammar does not define the root symbol {root:?}")]
18 RootSymbolMissing { root: String },
19 #[error("grammar is left-recursive and cannot be compiled by llama.cpp")]
⋯8 lines
No evidence locations were recorded for this file. Raw result