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:
parent
ef65fc5ff4
commit
c21edb6908
2 changed files with 14 additions and 1 deletions
|
@ -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
13
tests/test_grammar.py
Normal 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
|
Loading…
Reference in a new issue