Module ffi

Structs§

LlgCommitResult
Represents result from llg_commit_token()
LlgConstraint
LlgConstraintInit
LlgConstraintStep
LlgMaskResult
LlgMatcher
LlgStopController
LlgTokenizer
LlgTokenizerInit

Constants§

LLG_DECODE_INCLUDE_SPECIAL
Include special tokens in the output. They may look like <|something|>, <something_else>, or <[12345]> if they don’t have a name.
LLG_DECODE_NONE
Do not include special tokens, and keep invalid UTF-8 as is.
LLG_DECODE_VALID_UTF8
Replace invalid UTF-8 with the replacement character.

Functions§

constraint_to_llg
llg_clone_constraint
Clone the constraint
llg_clone_matcher
Clone the matcher.
llg_clone_tokenizer
Clone a tokenizer. This increments a reference count and does a small allocation.
llg_commit_token
Commit the token sampled with the mask returned from llg_compute_mask(). Can be run on the critical path of sampling (is fast). Returns 0 on success and -1 on error (use llg_get_error() to get the exact error). When 0 is returned, the result is written to *res_p.
llg_compute_mask
Compute mask for the next token sampling It typically takes up to a millisecond for a 100k tokenizer, so should be called in background. Returns 0 on success and -1 on error (use llg_get_error() to get the exact error). When 0 is returned, the result is written to *res_p.
llg_constraint_init_set_defaults
Set the default values for the ConstraintInit Disables ff_tokens and backtracking, enables warnings on stderr and all logging to the buffer (get with llg_flush_logs()). You need to set the tokenizer field manually.
llg_decode_tokens
Return a string representation of the tokens, useful for debugging. The output is NUL-terminated. Returns the number of bytes that would be written to output if output_len was large enough. flags is one of LLG_DECODE_*
llg_flush_logs
Get the logs from the constraint, since last call to this function. The logs are null-terminated. The logs are kept in the constraint until the next call to this function or until the constraint is freed.
llg_free_constraint
Free the constraint
llg_free_matcher
Free the matcher.
llg_free_stop_controller
Free the stop-sequence controller
llg_free_tokenizer
Free the tokenizer. Should NOT be called while there are still constraints using it.
llg_get_error
Get the error message from the constraint or null if there is no error. After it returns a non-null value, it will always return it until the constraint is freed using llg_free_constraint() (at which point the pointer will be invalid).
llg_get_temperature
Get the current temperature of the constraint. It is updated by mask computation.
llg_is_stopped
Check if constraint is stopped (cannot be extended further).
llg_matcher_compute_ff_tokens
Compute the fast-forward (forced) tokens for the current state. The result is written to output. Returns the number of tokens written to output (which can be 0) or -1 on error.
llg_matcher_compute_mask
Compute the set of allowed tokens for the current state. Use llg_matcher_get_mask() to get the result. Returns 0 on success and -1 on error.
llg_matcher_compute_mask_into
Compute the set of allowed tokens for the current state. The result is written to mask_dest. mask_byte_len must be equal to llg_matcher_get_mask_byte_size(). Returns 0 on success and -1 on error.
llg_matcher_consume_token
Advance the matcher by one token. Returns 0 on success and -1 on error.
llg_matcher_consume_tokens
Advance the matcher by several tokens. Returns 0 on success and -1 on error.
llg_matcher_get_error
Get the error message from the matcher or null if there is no error. After it returns a non-null value, it will always return it until the matcher is freed using llg_free_matcher() (at which point the pointer will be invalid).
llg_matcher_get_mask
Return pointer to the mask computed by llg_matcher_compute_mask(), if any.
llg_matcher_get_mask_byte_size
Return the size of the mask in bytes.
llg_matcher_is_accepting
Check if the grammar can fully accept the input.
llg_matcher_is_error
Check if the matcher is in an error state.
llg_matcher_is_stopped
Check if the matcher will force EOS token. This returns true also in error state, as that is a forced stop.
llg_matcher_reset
Resets the matcher to the initial state. A matcher in error state cannot be reset. Returns 0 on success and -1 on error.
llg_matcher_rollback
Backtracks the matcher states by num_tokens. Returns 0 on success and -1 on error.
llg_matcher_validate_tokens
Check how many tokens can be consumed from the given tokens. Returns the number of tokens that can be consumed, or -1 on error.
llg_new_constraint
Create a new constraint from a grammar JSON string Always returns a non-null value. Call llg_get_error() on the result to check for errors.
llg_new_constraint_any
Create a new constraint with specified type Type can be one of “regex”, “json_schema” (or “json”), “lark”, “llguidance” (or “guidance”) Always returns a non-null value. Call llg_get_error() on the result to check for errors.
llg_new_constraint_json
Create a new constraint from a given JSON schema Always returns a non-null value. Call llg_get_error() on the result to check for errors.
llg_new_constraint_lark
Create a new constraint from a given lark grammar Always returns a non-null value. Call llg_get_error() on the result to check for errors.
llg_new_constraint_regex
Create a new constraint from a given regular expression Always returns a non-null value. Call llg_get_error() on the result to check for errors.
llg_new_matcher
Create a new matcher from the given ConstraintInit Always returns a non-null value. Call llg_matcher_get_error() on the result to check for errors. init.ff_tokens_ok and init.backtrack_ok are ignored (backtracking is always disabled, and ff_tokens can be retrieved using llg_matcher_compute_ff_tokens()). The data is of different format, depending on constraint_type:
llg_new_stop_controller
Create a new stop-sequence controller
llg_new_tokenizer
Construct a new tokenizer from the given TokenizerInit
llg_par_compute_mask
Compute mask for several constraints in parallel.
llg_stop_commit_token
Commit a token to the stop-sequence controller. Returns a valid utf8 string to be returned to the user (which can be empty) and whether the sequence should be then finished. The string is valid until the next call to this function, or until the stop-sequence controller is freed.
llg_stringify_tokens
Return a string representation of the tokens, useful for debugging. The output is NUL-terminated. Returns the number of bytes that would be written to output if output_len was large enough.
llg_tokenize_bytes
Tokenize the given bytes and return the tokens. Always returns the number of tokens that would be written to output_tokens if output_tokens_len was large enough.
llg_tokenize_bytes_marker
Tokenize the given bytes and return the tokens. Special tokens will be tokenized, if they follow 0xFF byte prefix. Always returns the number of tokens that would be written to output_tokens if output_tokens_len was large enough.
llg_validate_grammar
Check if given grammar is valid. This about twice as fast as creating a matcher (which also validates). See llg_new_matcher() for the grammar format. Returns 0 on success and -1 on error and 1 on warning. The error message or warning is written to message, which is message_len bytes long. It’s always NUL-terminated.
save_error_string
Save the error string to the given pointer. The string is NUL-terminated. The function will write at most error_string_len bytes (including the NUL).

Type Aliases§

LlgCallback
Function which llg calls when an operation is done.
LlgToken
LlgTokenizeFn
Tokenization function Will not write more than output_tokens_len tokens (which can be 0) Returns the total number of tokens (which can be more than output_tokens_len) This function has to be thread-safe!