Low-level Guidance (llguidance) is a tool that can enforce arbitrary context-free grammar on the output of an LLM.

"Given a context-free grammar, a tokenizer, and a prefix of tokens, llguidance computes a token mask - a set of tokens from the tokenizer - that, when added to the current token prefix, can lead to a valid string in the language defined by the grammar. Mask computation takes approximately 1ms of single-core CPU time for a tokenizer with 100k tokens. While this timing depends on the exact grammar, it holds, for example, for grammars derived from JSON schemas. There is no significant startup cost."

"The library implements a context-free grammar parser using Earley's algorithm on top of a lexer based on derivatives of regular expressions. Mask computation is achieved by traversing the prefix tree (trie) of all possible tokens, leveraging highly optimized code."

guidance-ai / llguidance

#solidstatelife #ai #genai #llms

There are no comments yet.