-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API + Model: Add support for JSON schema constraints
Add the ability to constrain the return value of a model to be JSON. Built using the JSON schema standard to define the properties of what the model should return. This feature should be more accurate than using GBNF/EBNF to yield the same results due to the use of lmformatenforcer. GBNF/EBNF will be added in a different commit/branch. Signed-off-by: kingbri <[email protected]>
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from common.logger import init_logger | ||
from exllamav2 import ExLlamaV2, ExLlamaV2Tokenizer | ||
from exllamav2.generator import ExLlamaV2Sampler | ||
|
||
# Temporary, remove once the exllama version is bumped | ||
try: | ||
from exllamav2.generator.filters import ExLlamaV2PrefixFilter | ||
|
||
_exllama_filter_available = True | ||
except ImportError: | ||
_exllama_filter_available = False | ||
|
||
try: | ||
from lmformatenforcer import JsonSchemaParser | ||
from lmformatenforcer.integrations.exllamav2 import ExLlamaV2TokenEnforcerFilter | ||
|
||
_lmformatenforcer_available = True | ||
except ImportError: | ||
_lmformatenforcer_available = False | ||
|
||
|
||
logger = init_logger(__name__) | ||
|
||
|
||
class ExLlamaV2Grammar: | ||
"""ExLlamaV2 class for various grammar filters/parsers.""" | ||
|
||
def add_json_schema_filter( | ||
self, | ||
json_schema: dict, | ||
gen_settings: ExLlamaV2Sampler.Settings, | ||
model: ExLlamaV2, | ||
tokenizer: ExLlamaV2Tokenizer, | ||
): | ||
"""Adds an ExllamaV2 filter based on a JSON schema.""" | ||
|
||
# Check if the required dependencies can be imported | ||
if not _exllama_filter_available: | ||
logger.warning( | ||
"ExllamaV2PrefixFilter is not available " | ||
"in the currently installed ExllamaV2 version." | ||
) | ||
|
||
return | ||
|
||
if not _lmformatenforcer_available: | ||
logger.error( | ||
"lmformatenforcer must be installed to parse a json schema.\n" | ||
"Please run the following command: pip install lm-format-enforcer" | ||
) | ||
|
||
return | ||
|
||
# Create the parser | ||
schema_parser = JsonSchemaParser(json_schema) | ||
lmfilter = ExLlamaV2TokenEnforcerFilter(schema_parser, tokenizer) | ||
prefix_filter = ExLlamaV2PrefixFilter(model, tokenizer, "{") | ||
|
||
# Append the filters | ||
gen_settings.filters += [lmfilter, prefix_filter] | ||
gen_settings.filter_prefer_eos = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters