MCPcopy Create free account
hub / github.com/abetlen/llama-cpp-python / LlamaGrammar

Class LlamaGrammar

llama_cpp/llama_grammar.py:19–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17
18
19class LlamaGrammar:
20 def __init__(self, *args, _grammar: str, **kwargs):
21 self._grammar = _grammar
22 self._root = LLAMA_GRAMMAR_DEFAULT_ROOT
23
24 @classmethod
25 def from_string(cls, grammar: str, verbose: bool = True) -> "LlamaGrammar":
26 return cls(_grammar=grammar)
27
28 @classmethod
29 def from_file(cls, file: Union[str, Path], verbose: bool = True) -> "LlamaGrammar":
30 try:
31 with open(file) as f:
32 grammar = f.read()
33 except Exception as err:
34 raise Exception(
35 f"{cls.from_file.__name__}: error reading grammar file: {err}"
36 )
37
38 if grammar:
39 return cls.from_string(grammar, verbose=verbose)
40
41 raise ValueError(
42 f"{cls.from_file.__name__}: error parsing grammar file: params_grammer is empty"
43 )
44
45 @classmethod
46 def from_json_schema(cls, json_schema: str, verbose: bool = True) -> "LlamaGrammar":
47 return cls.from_string(json_schema_to_gbnf(json_schema), verbose=verbose)
48
49
50"""llama.cpp gbnf rules from vendor/llama.cpp/grammars"""

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…