Add from_json_schema to LlamaGrammar

This commit is contained in:
Andrei Betlen 2023-11-23 00:27:00 -05:00
parent be1f64d569
commit c647f01609

View file

@ -64,6 +64,7 @@ class LlamaGrammar:
@classmethod @classmethod
def from_string(cls, grammar: str, verbose: bool = True) -> "LlamaGrammar": def from_string(cls, grammar: str, verbose: bool = True) -> "LlamaGrammar":
"""Convert a GBNF grammar to a Llama grammar."""
parsed_grammar = parse(const_char_p(grammar)) # type: parse_state parsed_grammar = parse(const_char_p(grammar)) # type: parse_state
if parsed_grammar.rules.empty(): if parsed_grammar.rules.empty():
raise ValueError( raise ValueError(
@ -75,6 +76,15 @@ class LlamaGrammar:
print(file=sys.stderr) print(file=sys.stderr)
return cls(parsed_grammar) return cls(parsed_grammar)
@classmethod
def from_json_schema(
cls,
json_schema: str,
verbose: bool = True,
) -> "LlamaGrammar":
"""Convert a JSON schema to a Llama grammar."""
return cls.from_string(json_schema_to_gbnf(json_schema), verbose=verbose)
@classmethod @classmethod
def from_file(cls, file: Union[str, Path], verbose: bool = True) -> "LlamaGrammar": def from_file(cls, file: Union[str, Path], verbose: bool = True) -> "LlamaGrammar":
try: try: