From c647f016097eb1f3ac545bed1b1d257642ac76af Mon Sep 17 00:00:00 2001 From: Andrei Betlen Date: Thu, 23 Nov 2023 00:27:00 -0500 Subject: [PATCH] Add from_json_schema to LlamaGrammar --- llama_cpp/llama_grammar.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/llama_cpp/llama_grammar.py b/llama_cpp/llama_grammar.py index 89e7cb5..aec023c 100644 --- a/llama_cpp/llama_grammar.py +++ b/llama_cpp/llama_grammar.py @@ -64,6 +64,7 @@ class LlamaGrammar: @classmethod def from_string(cls, grammar: str, verbose: bool = True) -> "LlamaGrammar": + """Convert a GBNF grammar to a Llama grammar.""" parsed_grammar = parse(const_char_p(grammar)) # type: parse_state if parsed_grammar.rules.empty(): raise ValueError( @@ -75,6 +76,15 @@ class LlamaGrammar: print(file=sys.stderr) return cls(parsed_grammar) + @classmethod + def from_json_schema( + cls, + json_schema: str, + verbose: bool = True, + ) -> "LlamaGrammar": + """Convert a JSON schema to a Llama grammar.""" + return cls.from_string(json_schema_to_gbnf(json_schema), verbose=verbose) + @classmethod def from_file(cls, file: Union[str, Path], verbose: bool = True) -> "LlamaGrammar": try: