From dd22010e85265ae840c76ec835d67a29ed852722 Mon Sep 17 00:00:00 2001 From: Andrei Betlen Date: Thu, 22 Feb 2024 00:09:45 -0500 Subject: [PATCH 1/3] fix: Raise exceptions when llama model or context fails to load --- llama_cpp/_internals.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/llama_cpp/_internals.py b/llama_cpp/_internals.py index 10c1a6f..8690843 100644 --- a/llama_cpp/_internals.py +++ b/llama_cpp/_internals.py @@ -51,6 +51,9 @@ class _LlamaModel: self.path_model.encode("utf-8"), self.params ) + if self.model is None: + raise ValueError(f"Failed to load model from file: {path_model}") + def __del__(self): if self.model is not None and self._llama_free_model is not None: self._llama_free_model(self.model) @@ -258,6 +261,9 @@ class _LlamaContext: self.model.model, self.params ) + if self.ctx is None: + raise ValueError("Failed to create llama_context") + def __del__(self): if self.ctx is not None and self._llama_free is not None: self._llama_free(self.ctx) From e6d6260a91b7831733f7d1f73c7af46a3e8185ed Mon Sep 17 00:00:00 2001 From: Andrei Betlen Date: Thu, 22 Feb 2024 00:10:23 -0500 Subject: [PATCH 2/3] fix: Update from_pretrained defaults to match hf_hub_download --- llama_cpp/llama.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/llama_cpp/llama.py b/llama_cpp/llama.py index 9fc4ec2..1226545 100644 --- a/llama_cpp/llama.py +++ b/llama_cpp/llama.py @@ -1885,8 +1885,9 @@ class Llama: cls, repo_id: str, filename: Optional[str], - local_dir: Optional[Union[str, os.PathLike[str]]] = ".", + local_dir: Optional[Union[str, os.PathLike[str]]] = None, local_dir_use_symlinks: Union[bool, Literal["auto"]] = "auto", + cache_dir: Optional[Union[str, os.PathLike[str]]] = None, **kwargs: Any, ) -> "Llama": """Create a Llama model from a pretrained model name or path. @@ -1945,18 +1946,29 @@ class Llama: subfolder = str(Path(matching_file).parent) filename = Path(matching_file).name - local_dir = "." - # download the file hf_hub_download( repo_id=repo_id, - local_dir=local_dir, filename=filename, subfolder=subfolder, + local_dir=local_dir, local_dir_use_symlinks=local_dir_use_symlinks, + cache_dir=cache_dir, ) - model_path = os.path.join(local_dir, filename) + if local_dir is None: + model_path = hf_hub_download( + repo_id=repo_id, + filename=filename, + subfolder=subfolder, + local_dir=local_dir, + local_dir_use_symlinks=local_dir_use_symlinks, + cache_dir=cache_dir, + local_files_only=True, + + ) + else: + model_path = os.path.join(local_dir, filename) return cls( model_path=model_path, From 3921e10770996d95a9eb22c8248bacef39f69365 Mon Sep 17 00:00:00 2001 From: Andrei Betlen Date: Thu, 22 Feb 2024 00:17:06 -0500 Subject: [PATCH 3/3] feat: support minItems/maxItems in JSON grammar converter (by @nopperl) --- llama_cpp/llama_grammar.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/llama_cpp/llama_grammar.py b/llama_cpp/llama_grammar.py index 3eb3b96..6a37857 100644 --- a/llama_cpp/llama_grammar.py +++ b/llama_cpp/llama_grammar.py @@ -1498,9 +1498,21 @@ class SchemaConverter: item_rule_name = self.visit( schema["items"], f'{name}{"-" if name else ""}item' ) - rule = ( - f'"[" space ({item_rule_name} ("," space {item_rule_name})*)? "]" space' - ) + list_item_operator = f'("," space {item_rule_name})' + successive_items = "" + min_items = schema.get("minItems", 0) + if min_items > 0: + first_item = f"({item_rule_name})" + successive_items = list_item_operator * (min_items - 1) + min_items -= 1 + else: + first_item = f"({item_rule_name})?" + max_items = schema.get("maxItems") + if max_items is not None and max_items > min_items: + successive_items += (list_item_operator + "?") * (max_items - min_items - 1) + else: + successive_items += list_item_operator + "*" + rule = f'"[" space {first_item} {successive_items} "]" space' return self._add_rule(rule_name, rule) else: