liblloyal 1.0.0
Composable primitives for llama.cpp inference
Loading...
Searching...
No Matches
grammar.hpp File Reference

Grammar-Constrained Sampling. More...

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

Go to the source code of this file.

Namespaces

namespace  lloyal
 JSON Schema to Grammar Converter (Header-Only)
 
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::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. Wraps json-schema-to-grammar.hpp conversion logic with error handling and logging.

Architecture:

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

Definition in file grammar.hpp.