Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c647f01

Browse files
committed
Add from_json_schema to LlamaGrammar
1 parent be1f64d commit c647f01

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

llama_cpp/llama_grammar.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(
6464

6565
@classmethod
6666
def from_string(cls, grammar: str, verbose: bool = True) -> "LlamaGrammar":
67+
"""Convert a GBNF grammar to a Llama grammar."""
6768
parsed_grammar = parse(const_char_p(grammar)) # type: parse_state
6869
if parsed_grammar.rules.empty():
6970
raise ValueError(
@@ -75,6 +76,15 @@ def from_string(cls, grammar: str, verbose: bool = True) -> "LlamaGrammar":
7576
print(file=sys.stderr)
7677
return cls(parsed_grammar)
7778

79+
@classmethod
80+
def from_json_schema(
81+
cls,
82+
json_schema: str,
83+
verbose: bool = True,
84+
) -> "LlamaGrammar":
85+
"""Convert a JSON schema to a Llama grammar."""
86+
return cls.from_string(json_schema_to_gbnf(json_schema), verbose=verbose)
87+
7888
@classmethod
7989
def from_file(cls, file: Union[str, Path], verbose: bool = True) -> "LlamaGrammar":
8090
try:

0 commit comments

Comments
 (0)