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 c21edb6

Browse filesBrowse files
authored
Do not set grammar to None for new LlamaGrammar objects (abetlen#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
Copy full SHA for c21edb6

File tree

Expand file treeCollapse file tree

2 files changed

+14
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+14
-1
lines changed

‎llama_cpp/llama_grammar.py

Copy file name to clipboardExpand all lines: llama_cpp/llama_grammar.py
+1-1Lines 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

Copy file name to clipboard
+13Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.