Skip to content

Commit c21edb6

Browse files
authored
Do not set grammar to None for new LlamaGrammar objects (ggml-org#834)
* Do not set `grammar` to `None` for new `LlamaGrammar` objects The `grammar` attribute is written by `init()`, but that method always returns `None`, so `__init__()` would then discard the previously written object. * Add minimal test for grammar parsing
1 parent ef65fc5 commit c21edb6

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

llama_cpp/llama_grammar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(
5858
) # type: std.vector[std.vector[LlamaGrammarElement]]
5959
self._n_rules = self._grammar_rules.size() # type: int
6060
self._start_rule_index = parsed_grammar.symbol_ids.at("root") # type: int
61-
self.grammar = self.init()
61+
self.init()
6262

6363
@classmethod
6464
def from_string(cls, grammar: str, verbose: bool = True) -> "LlamaGrammar":

tests/test_grammar.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import llama_cpp
2+
3+
tree = """
4+
leaf ::= "."
5+
node ::= leaf | "(" node node ")"
6+
root ::= node
7+
"""
8+
9+
def test_grammar_from_string():
10+
grammar = llama_cpp.LlamaGrammar.from_string(tree)
11+
assert grammar._n_rules == 3
12+
assert grammar._start_rule_index == 2
13+
assert grammar.grammar is not None

0 commit comments

Comments
 (0)