std::string gbnf = lloyal::grammar::from_json_schema(schemaJsonString); // Pass to sampler::sample_with_params() via grammarSampler parameter
std::string gbnf = lloyal::grammar::from_json_schema(schemaJsonString); // Pass to sampler::sample_with_params() via grammarSampler parameter
#pragma once
#include <llama/llama.h>
#include <lloyal/nlohmann/json.hpp>
#include <stdexcept>
#include <string>
"[grammar::from_json_schema] Converting JSON schema (%zu bytes)",
schema_json.size());
try {
nlohmann::ordered_json schema = nlohmann::ordered_json::parse(schema_json);
"json_schema_to_grammar");
if (grammar.empty()) {
"empty grammar");
throw std::runtime_error("Grammar conversion produced empty result");
}
"[grammar::from_json_schema] Generated GBNF grammar (%zu bytes)",
grammar.size());
return grammar;
} catch (const nlohmann::json::parse_error &e) {
std::string errorMsg = std::string("JSON parse error: ") + e.what();
throw std::runtime_error(errorMsg);
} catch (const std::exception &e) {
std::string errorMsg =
std::string("Grammar conversion failed: ") + e.what();
throw std::runtime_error(errorMsg);
}
}
inline llama_sampler *
init_sampler(
const llama_model *model,
const std::string &grammar_str,
const std::string &root_rule = "root") {
if (!model) {
return nullptr;
}
if (grammar_str.empty()) {
return nullptr;
}
if (!vocab) {
return nullptr;
}
"(grammar: %zu bytes, root: %s)",
grammar_str.size(), root_rule.c_str());
llama_sampler *sampler =
llama_sampler_init_grammar(vocab, grammar_str.c_str(), root_rule.c_str());
if (!sampler) {
"llama_sampler_init_grammar returned null");
}
return sampler;
}
if (!smpl) {
return nullptr;
}
llama_sampler *cloned = llama_sampler_clone(smpl);
if (!cloned) {
} else {
}
return cloned;
}
if (smpl) {
llama_sampler_free(smpl);
}
}
inline void apply(llama_sampler* smpl, llama_token_data_array* cur_p) {
if (smpl && cur_p) {
llama_sampler_apply(smpl, cur_p);
}
}
inline void accept(llama_sampler* smpl, llama_token token) {
if (smpl) {
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.