@@ -23,30 +23,50 @@ def __init__(self, *args, _grammar: str, **kwargs):
2323 self ._grammar = _grammar
2424 self ._root = LLAMA_GRAMMAR_DEFAULT_ROOT
2525
26+ @property
27+ def grammar (self ) -> str :
28+ return self ._grammar
29+
2630 @classmethod
2731 def from_string (cls , grammar : str , verbose : bool = True ) -> "LlamaGrammar" :
2832 return cls (_grammar = grammar )
2933
3034 @classmethod
3135 def from_file (cls , file : Union [str , Path ], verbose : bool = True ) -> "LlamaGrammar" :
36+ file_path = Path (file )
37+
38+ if not file_path .exists ():
39+ raise FileNotFoundError (f"{ cls .__name__ } .from_file: file not found: { file_path } " )
40+
3241 try :
33- with open (file ) as f :
34- grammar = f .read ()
42+ grammar_content = file_path .read_text (encoding = 'utf-8' )
3543 except Exception as err :
36- raise Exception (
37- f"{ cls .from_file .__name__ } : error reading grammar file: { err } "
38- )
44+ raise IOError (f"{ cls .__name__ } .from_file: error reading grammar file: { err } " )
3945
40- if grammar :
41- return cls .from_string ( grammar , verbose = verbose )
46+ if not grammar_content . strip () :
47+ raise ValueError ( f" { cls .__name__ } .from_file: grammar file is empty" )
4248
43- raise ValueError (
44- f"{ cls .from_file .__name__ } : error parsing grammar file: params_grammer is empty"
45- )
49+ return cls .from_string (grammar_content , verbose = verbose )
4650
4751 @classmethod
48- def from_json_schema (cls , json_schema : str , verbose : bool = True ) -> "LlamaGrammar" :
49- return cls .from_string (json_schema_to_gbnf (json_schema ), verbose = verbose )
52+ def from_json_schema (
53+ cls ,
54+ json_schema : str ,
55+ prop_order : Optional [List [str ]] = None ,
56+ verbose : bool = True
57+ ) -> "LlamaGrammar" :
58+ """
59+ Create a syntax object from a JSON Schema.
60+
61+ json_schema: A JSON Schema string or dictionary.
62+ prop_order: Specifies the order in which fields are generated (helps improve the stability of small models).
63+ verbose: Whether to log.
64+ """
65+ try :
66+ gbnf_grammar_str = json_schema_to_gbnf (json_schema , prop_order = prop_order )
67+ return cls .from_string (gbnf_grammar_str , verbose = verbose )
68+ except Exception as e :
69+ raise ValueError (f"{ cls .__name__ } .from_json_schema: conversion failed: { e } " )
5070
5171
5272"""llama.cpp gbnf rules from vendor/llama.cpp/grammars"""
0 commit comments