liblloyal 1.0.0
Branched Inference for llama.cpp
Loading...
Searching...
No Matches
grammar.hpp File Reference

Grammar-Constrained Sampling. More...

#include "common.hpp"
#include "tokenizer.hpp"
#include <llama/llama.h>
#include <json-schema-to-grammar.h>
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <string>
#include <vector>

Go to the source code of this file.

Namespaces

namespace  lloyal
 Boundary Tracker Stub for OSS liblloyal.
 
namespace  lloyal::grammar
 

Functions

std::string lloyal::grammar::from_json_schema (const std::string &schema_json)
 Convert JSON schema to GBNF (Grammar BNF) format.
 
llama_sampler * lloyal::grammar::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.
 
llama_sampler * lloyal::grammar::init_lazy_sampler (const llama_model *model, const std::string &grammar_str, const std::vector< std::string > &trigger_patterns, const std::vector< llama_token > &trigger_tokens, const std::string &root_rule="root")
 Initialize a lazy grammar sampler from GBNF grammar string.
 
llama_sampler * lloyal::grammar::clone_sampler (llama_sampler *smpl)
 Clone a grammar sampler (for fork/branching).
 
void lloyal::grammar::free_sampler (llama_sampler *smpl)
 Free a grammar sampler.
 
void lloyal::grammar::apply (llama_sampler *smpl, llama_token_data_array *cur_p)
 Apply grammar constraint to candidates.
 
void lloyal::grammar::accept (llama_sampler *smpl, llama_token token)
 Accept a token into grammar state.
 

Detailed Description

Grammar-Constrained Sampling.

Provides JSON schema to GBNF grammar conversion for structured output generation. Uses json_schema_to_grammar() from llama.cpp's common library.

Architecture:

  • Calls json_schema_to_grammar() from common/json-schema-to-grammar.h
  • Adds error handling, logging, and consistent API
  • Manages grammar sampler lifecycle

Definition in file grammar.hpp.