A very specific and interesting question!
Short answer: Unfortunately, no, GBNF (Grammar-Based Normal Forms) in llama.cpp does not directly support regex (regular expressions).
Longer explanation:
llama.cpp is a C++ implementation of the LLaMA (Large Language Model Meta AI) model, and GBNF is a feature that allows specifying a grammar for constraining the output of the model. GBNF is essentially a formal grammar definition, similar to BNF (Backus-Naur Form) or ABNF (Augmented BNF).
The GBNF syntax in llama.cpp is based on a simplified variant of ABNF, which is a context-free grammar formalism. It allows defining rules using a sequence of terminals ( literal strings), non-terminals (rule references), and a few operators (e.g., * for repetition, ? for optionality, | for alternatives).
Here's a simple GBNF example from the llama.cpp documentation:
root ::= [0-9]+ // one or more digits
This rule matches one or more digits (0 through 9).
Now, regex is a different beast. Regular expressions are a powerful pattern-matching language, supporting more advanced features like:
- Character classes (e.g.,
\w, \d, \s)
- Quantifiers (e.g.,
*, +, ?, {n,m})
- Groups and captures
- Anchors (e.g.,
^, $)
- Lookahead and lookbehind assertions
- and more...
The GBNF syntax in llama.cpp does not support the full regex syntax. The main reasons are:
- Complexity: Integrating a full regex engine would add significant complexity to the
llama.cpp codebase.
- Performance: Evaluating regex patterns can be computationally expensive, which might impact the model's inference speed.
- Grammar constraints: GBNF is designed for generative grammar-based constraints, whereas regex is typically used for matching existing text. The two paradigms don't align perfectly.
That being said, there are some similarities between GBNF and regex:
- Character classes: GBNF supports simple character ranges (e.g.,
[0-9], [a-zA-Z]), similar to regex character classes (e.g., \d, [a-zA-Z]).
- Repetition: GBNF's
* operator (e.g., [0-9]*) is similar to regex's * quantifier (e.g., \d*).
If you need more advanced pattern-matching capabilities, you have a few workarounds:
- Post-processing: Run the generated text through a separate regex engine (e.g.,
std::regex in C++ or a dedicated regex library) to filter or validate the output.
- Grammar approximation: Try to approximate your regex pattern using GBNF rules. For example, instead of
\w+ (one or more word characters), use [a-zA-Z0-9_]+ in GBNF.
- Contribute to
llama.cpp: If you're comfortable with C++ and grammar implementations, you could propose an extension to the GBNF syntax to support a subset of regex features. This would require modifying the llama.cpp codebase.
Here's an example of approximating a simple regex pattern (\d{4}-\d{2}-\d{2} for a YYYY-MM-DD date format) using GBNF:
date ::= [0-9][0-9][0-9][0-9] "-" [0-9][0-9] "-" [0-9][0-9]
Not as elegant as regex, but it gets the job done.
In summary, while GBNF in llama.cpp doesn't support full regex syntax, you can often find creative workarounds or approximations using the existing GBNF features.
Hope this answers your question!