(
self,
*,
path_model: str,
params: llama_cpp.llama_model_params,
verbose: bool = True,
)
| 34 | NOTE: For stability it's recommended you use the Llama class instead.""" |
| 35 | |
| 36 | def __init__( |
| 37 | self, |
| 38 | *, |
| 39 | path_model: str, |
| 40 | params: llama_cpp.llama_model_params, |
| 41 | verbose: bool = True, |
| 42 | ): |
| 43 | self.path_model = path_model |
| 44 | self.params = params |
| 45 | self.verbose = verbose |
| 46 | self._exit_stack = ExitStack() |
| 47 | # LlamaModel does not use samplers, but close() can run after partial init. |
| 48 | self.sampler = None |
| 49 | self.custom_samplers = [] |
| 50 | |
| 51 | model = None |
| 52 | |
| 53 | if not os.path.exists(path_model): |
| 54 | raise ValueError(f"Model path does not exist: {path_model}") |
| 55 | |
| 56 | with suppress_stdout_stderr(disable=verbose): |
| 57 | model = llama_cpp.llama_model_load_from_file( |
| 58 | self.path_model.encode("utf-8"), self.params |
| 59 | ) |
| 60 | |
| 61 | if model is None: |
| 62 | raise ValueError(f"Failed to load model from file: {path_model}") |
| 63 | |
| 64 | vocab = llama_cpp.llama_model_get_vocab(model) |
| 65 | |
| 66 | if vocab is None: |
| 67 | raise ValueError(f"Failed to get vocab from model: {path_model}") |
| 68 | |
| 69 | self.model = model |
| 70 | self.vocab = vocab |
| 71 | |
| 72 | def free_model(): |
| 73 | if self.model is None: |
| 74 | return |
| 75 | llama_cpp.llama_model_free(self.model) |
| 76 | self.model = None |
| 77 | |
| 78 | self._exit_stack.callback(free_model) |
| 79 | |
| 80 | def close(self): |
| 81 | if self.sampler is not None: |
nothing calls this directly
no test coverage detected