liblloyal 1.0.0
Composable primitives for llama.cpp inference
Loading...
Searching...
No Matches
lloyal::detail Namespace Reference

Classes

struct  BatchGuard
 RAII guard for automatic batch cleanup Ensures llama_batch_free is called even if exceptions occur. More...
 
struct  BuiltinRule
 
struct  is_optional
 Type trait to detect std::optional<T> More...
 
struct  is_optional< std::optional< T > >
 
class  SchemaConverter
 

Functions

void add_tokens_to_batch (llama_batch &batch, const llama_token *tokens, int32_t start_idx, int32_t n_eval, int32_t n_past, int32_t capacity, llama_seq_id seq_id=0)
 Add tokens to batch with position info.
 
std::string common_token_to_piece (const struct llama_vocab *vocab, llama_token token, bool special)
 
std::string get_token_safe (const llama_model *model, llama_token token)
 
const char * get_chatml_template ()
 
std::string apply_chat_template_helper (const std::string &template_str, const nlohmann::ordered_json &messages, const std::string &bos_token, const std::string &eos_token, bool add_generation_prompt, bool add_bos, bool add_eos)
 
bool is_reserved_name (const std::string &name)
 Check if name conflicts with GBNF reserved keywords.
 
std::regex INVALID_RULE_CHARS_RE ("[^a-zA-Z0-9-]+")
 
std::regex GRAMMAR_LITERAL_ESCAPE_RE ("[\r\n\"]")
 
std::regex GRAMMAR_RANGE_LITERAL_ESCAPE_RE ("[\r\n\"\\]\\-\\\\]")
 
std::string build_repetition (const std::string &item_rule, int min_items, int max_items, const std::string &separator_rule="")
 
void _build_min_max_int (int min_value, int max_value, std::stringstream &out, int decimals_left=16, bool top_level=true)
 
std::string replacePattern (const std::string &input, const std::regex &regex, const std::function< std::string(const std::smatch &)> &replacement)
 
std::string format_literal (const std::string &literal)
 
template<class X , class T >
constexpr T as_value (const X &x, T def)
 Extract value from either T or std::optional<T> with fallback.
 

Variables

constexpr const char * SPACE_RULE = "| \" \" | \"\\n\"{1,2} [ \\t]{0,20}"
 
const std::unordered_map< std::string, BuiltinRulePRIMITIVE_RULES
 Built-in grammar rules for JSON primitives.
 
const std::unordered_map< std::string, BuiltinRuleSTRING_FORMAT_RULES
 Grammar rules for string format validation.
 
const std::unordered_map< char, std::string > GRAMMAR_LITERAL_ESCAPES
 
const std::unordered_set< char > NON_LITERAL_SET
 
const std::unordered_set< char > ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS
 

Function Documentation

◆ _build_min_max_int()

void lloyal::detail::_build_min_max_int ( int  min_value,
int  max_value,
std::stringstream &  out,
int  decimals_left = 16,
bool  top_level = true 
)
inline

Definition at line 216 of file json-schema-to-grammar.hpp.

◆ add_tokens_to_batch()

void lloyal::detail::add_tokens_to_batch ( llama_batch &  batch,
const llama_token *  tokens,
int32_t  start_idx,
int32_t  n_eval,
int32_t  n_past,
int32_t  capacity,
llama_seq_id  seq_id = 0 
)
inline

Add tokens to batch with position info.

Definition at line 59 of file decoder.hpp.

◆ apply_chat_template_helper()

std::string lloyal::detail::apply_chat_template_helper ( const std::string &  template_str,
const nlohmann::ordered_json &  messages,
const std::string &  bos_token = "",
const std::string &  eos_token = "",
bool  add_generation_prompt = true,
bool  add_bos = false,
bool  add_eos = false 
)
inline

Definition at line 523 of file helpers.hpp.

◆ as_value()

template<class X , class T >
constexpr T lloyal::detail::as_value ( const X &  x,
def 
)
constexpr

Extract value from either T or std::optional<T> with fallback.

Handles both:

  • Direct values (T): cast to target type
  • Optional values (std::optional<T>): unwrap with value_or(default)

Used by sampler to accept Nitrogen-generated params (which use std::optional)

Definition at line 50 of file sampler.hpp.

◆ build_repetition()

std::string lloyal::detail::build_repetition ( const std::string &  item_rule,
int  min_items,
int  max_items,
const std::string &  separator_rule = "" 
)
inline

Definition at line 183 of file json-schema-to-grammar.hpp.

◆ common_token_to_piece()

std::string lloyal::detail::common_token_to_piece ( const struct llama_vocab *  vocab,
llama_token  token,
bool  special 
)
inline

Definition at line 479 of file helpers.hpp.

◆ format_literal()

std::string lloyal::detail::format_literal ( const std::string &  literal)
inline

Definition at line 431 of file json-schema-to-grammar.hpp.

◆ get_chatml_template()

const char * lloyal::detail::get_chatml_template ( )
inline

Definition at line 509 of file helpers.hpp.

◆ get_token_safe()

std::string lloyal::detail::get_token_safe ( const llama_model *  model,
llama_token  token 
)
inline

Definition at line 498 of file helpers.hpp.

◆ GRAMMAR_LITERAL_ESCAPE_RE()

std::regex lloyal::detail::GRAMMAR_LITERAL_ESCAPE_RE ( ""  [\r\n\"])
inline

◆ GRAMMAR_RANGE_LITERAL_ESCAPE_RE()

std::regex lloyal::detail::GRAMMAR_RANGE_LITERAL_ESCAPE_RE ( "\\-\\\\]"  [\r\n\"\\])
inline

◆ INVALID_RULE_CHARS_RE()

std::regex lloyal::detail::INVALID_RULE_CHARS_RE ( "+"  [^a-zA-Z0-9-])
inline

◆ is_reserved_name()

bool lloyal::detail::is_reserved_name ( const std::string &  name)
inline

Check if name conflicts with GBNF reserved keywords.

Tests whether a rule name would collide with built-in primitives ("root", "boolean", "number", "string", etc.) or format rules ("date", "time", "uuid", etc.). Used during schema conversion to append "-" suffix to conflicting names.

Parameters
nameRule name to check
Returns
True if name is reserved, false otherwise
Note
Uses lazy-initialized static set for O(1) lookup after first call

Definition at line 156 of file json-schema-to-grammar.hpp.

◆ replacePattern()

std::string lloyal::detail::replacePattern ( const std::string &  input,
const std::regex &  regex,
const std::function< std::string(const std::smatch &)> &  replacement 
)
inline

Definition at line 411 of file json-schema-to-grammar.hpp.

Variable Documentation

◆ ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS

const std::unordered_set<char> lloyal::detail::ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS
inline
Initial value:
= {
'^', '$', '.', '[', ']', '(', ')', '|', '{', '}', '*', '+', '?'}

Definition at line 178 of file json-schema-to-grammar.hpp.

◆ GRAMMAR_LITERAL_ESCAPES

const std::unordered_map<char, std::string> lloyal::detail::GRAMMAR_LITERAL_ESCAPES
inline
Initial value:
= {
{'\r', "\\r"}, {'\n', "\\n"}, {'"', "\\\""}, {'-', "\\-"}, {']', "\\]"}}

Definition at line 173 of file json-schema-to-grammar.hpp.

◆ NON_LITERAL_SET

const std::unordered_set<char> lloyal::detail::NON_LITERAL_SET
inline
Initial value:
= {
'|', '.', '(', ')', '[', ']', '{', '}', '*', '+', '?'}

Definition at line 176 of file json-schema-to-grammar.hpp.

◆ PRIMITIVE_RULES

lloyal::detail::PRIMITIVE_RULES
inline
Initial value:
= {
{"boolean", {"(\"true\" | \"false\") space", {}}},
{"decimal-part", {"[0-9]{1,16}", {}}},
{"integral-part", {"[0] | [1-9] [0-9]{0,15}", {}}},
{"number",
{"(\"-\"? integral-part) (\".\" decimal-part)? ([eE] [-+]? "
"integral-part)? space",
{"integral-part", "decimal-part"}}},
{"integer", {"(\"-\"? integral-part) space", {"integral-part"}}},
{"value",
{"object | array | string | number | boolean | null",
{"object", "array", "string", "number", "boolean", "null"}}},
{"object",
{"\"{\" space ( string \":\" space value (\",\" space string \":\" space "
"value)* )? \"}\" space",
{"string", "value"}}},
{"array",
{"\"[\" space ( value (\",\" space value)* )? \"]\" space", {"value"}}},
{"uuid",
{"\"\\\"\" [0-9a-fA-F]{8} \"-\" [0-9a-fA-F]{4} \"-\" [0-9a-fA-F]{4} \"-\" "
"[0-9a-fA-F]{4} \"-\" [0-9a-fA-F]{12} \"\\\"\" space",
{}}},
{"char",
{"[^\"\\\\\\x7F\\x00-\\x1F] | [\\\\] ([\"\\\\bfnrt] | \"u\" "
"[0-9a-fA-F]{4})",
{}}},
{"string", {"\"\\\"\" char* \"\\\"\" space", {"char"}}},
{"null", {"\"null\" space", {}}},
}

Built-in grammar rules for JSON primitives.

Defines GBNF rules for basic JSON types: boolean, number, integer, string, array, object, null, uuid, and character escaping. Used as building blocks for schema conversion.

Definition at line 91 of file json-schema-to-grammar.hpp.

◆ SPACE_RULE

constexpr const char* lloyal::detail::SPACE_RULE = "| \" \" | \"\\n\"{1,2} [ \\t]{0,20}"
inlineconstexpr

Definition at line 77 of file json-schema-to-grammar.hpp.

◆ STRING_FORMAT_RULES

lloyal::detail::STRING_FORMAT_RULES
inline
Initial value:
=
{{"date",
{"[0-9]{4} \"-\" ( \"0\" [1-9] | \"1\" [0-2] ) \"-\" ( \"0\" [1-9] | "
"[1-2] [0-9] | \"3\" [0-1] )",
{}}},
{"time",
{"([01] [0-9] | \"2\" [0-3]) \":\" [0-5] [0-9] \":\" [0-5] [0-9] ( \".\" "
"[0-9]{3} )? ( \"Z\" | ( \"+\" | \"-\" ) ( [01] [0-9] | \"2\" [0-3] ) "
"\":\" [0-5] [0-9] )",
{}}},
{"date-time", {"date \"T\" time", {"date", "time"}}},
{"date-string", {"\"\\\"\" date \"\\\"\" space", {"date"}}},
{"time-string", {"\"\\\"\" time \"\\\"\" space", {"time"}}},
{"date-time-string",
{"\"\\\"\" date-time \"\\\"\" space", {"date-time"}}}}

Grammar rules for string format validation.

Defines GBNF rules for JSON Schema string formats: date, time, date-time, uri, email, uuid. Used when schema specifies "format" field for string validation.

Definition at line 128 of file json-schema-to-grammar.hpp.