Do not set grammar to None for new LlamaGrammar objects (#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
This commit is contained in:
Maarten ter Huurne 2023-11-21 06:23:18 +01:00 committed by GitHub
parent ef65fc5ff4
commit c21edb6908
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View file

@ -58,7 +58,7 @@ class LlamaGrammar:
) # type: std.vector[std.vector[LlamaGrammarElement]]
self._n_rules = self._grammar_rules.size() # type: int
self._start_rule_index = parsed_grammar.symbol_ids.at("root") # type: int
self.grammar = self.init()
self.init()
@classmethod
def from_string(cls, grammar: str, verbose: bool = True) -> "LlamaGrammar":

13
tests/test_grammar.py Normal file
View file

@ -0,0 +1,13 @@
import llama_cpp
tree = """
leaf ::= "."
node ::= leaf | "(" node node ")"
root ::= node
"""
def test_grammar_from_string():
grammar = llama_cpp.LlamaGrammar.from_string(tree)
assert grammar._n_rules == 3
assert grammar._start_rule_index == 2
assert grammar.grammar is not None