Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c647f01

Browse filesBrowse files
committed
Add from_json_schema to LlamaGrammar
1 parent be1f64d commit c647f01
Copy full SHA for c647f01

File tree

Expand file treeCollapse file tree

1 file changed

+10
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+10
-0
lines changed

‎llama_cpp/llama_grammar.py

Copy file name to clipboardExpand all lines: llama_cpp/llama_grammar.py
+10Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.