9#include <llama/llama.h>
10#include <lloyal/nlohmann/json.hpp>
47 "[grammar::from_json_schema] Converting JSON schema (%zu bytes)",
52 nlohmann::ordered_json schema = nlohmann::ordered_json::parse(schema_json);
55 "json_schema_to_grammar");
62 if (grammar.empty()) {
65 throw std::runtime_error(
"Grammar conversion produced empty result");
69 "[grammar::from_json_schema] Generated GBNF grammar (%zu bytes)",
73 }
catch (
const nlohmann::json::parse_error &e) {
74 std::string errorMsg = std::string(
"JSON parse error: ") + e.what();
76 throw std::runtime_error(errorMsg);
77 }
catch (
const std::exception &e) {
78 std::string errorMsg =
79 std::string(
"Grammar conversion failed: ") + e.what();
81 throw std::runtime_error(errorMsg);
106 const std::string &grammar_str,
107 const std::string &root_rule =
"root") {
113 if (grammar_str.empty()) {
125 "(grammar: %zu bytes, root: %s)",
126 grammar_str.size(), root_rule.c_str());
128 llama_sampler *sampler =
129 llama_sampler_init_grammar(vocab, grammar_str.c_str(), root_rule.c_str());
133 "llama_sampler_init_grammar returned null");
156 llama_sampler *cloned = llama_sampler_clone(smpl);
159 LLOYAL_LOG_DEBUG(
"[grammar::clone_sampler] ERROR: llama_sampler_clone failed");
174 llama_sampler_free(smpl);
186inline void apply(llama_sampler* smpl, llama_token_data_array* cur_p) {
188 llama_sampler_apply(smpl, cur_p);
200inline void accept(llama_sampler* smpl, llama_token token) {
202 llama_sampler_accept(smpl, token);
#define LLOYAL_LOG_DEBUG(...)
liblloyal - Common definitions and logging
void free_sampler(llama_sampler *smpl)
Free a grammar sampler.
llama_sampler * clone_sampler(llama_sampler *smpl)
Clone a grammar sampler (for fork/branching).
llama_sampler * init_sampler(const llama_model *model, const std::string &grammar_str, const std::string &root_rule="root")
Initialize a grammar sampler from GBNF grammar string.
std::string from_json_schema(const std::string &schema_json)
Convert JSON schema to GBNF (Grammar BNF) format.
void accept(llama_sampler *smpl, llama_token token)
Accept a token into grammar state.
void apply(llama_sampler *smpl, llama_token_data_array *cur_p)
Apply grammar constraint to candidates.
const llama_vocab * get_vocab(const llama_model *model)
Get vocabulary from model.
std::string json_schema_to_grammar(const json &schema, bool force_gbnf=false)
Convert JSON schema to GBNF grammar.
Text Tokenization Operations.