diff --git a/llama_cpp/llama_chat_format.py b/llama_cpp/llama_chat_format.py index 69ed6019a7..bb9683129b 100644 --- a/llama_cpp/llama_chat_format.py +++ b/llama_cpp/llama_chat_format.py @@ -1853,6 +1853,8 @@ def __init__(self, clip_model_path: str, verbose: bool = False): self.clip_model_path.encode(), 0 ) + self.current_image_embed = None + def __del__(self): with suppress_stdout_stderr(disable=self.verbose): if self.clip_ctx is not None and self._clip_free is not None: @@ -1943,6 +1945,10 @@ def __call__( ) ) if content["type"] == "image_url": + if self.current_image_embed is not None: + with suppress_stdout_stderr(disable=self.verbose): + self._llava_cpp.llava_image_embed_free(self.current_image_embed) + image_bytes = ( self.load_image(content["image_url"]["url"]) if isinstance(content["image_url"], dict) @@ -1963,6 +1969,7 @@ def __call__( len(image_bytes), ) ) + self.current_image_embed = embed try: n_past = ctypes.c_int(llama.n_tokens) n_past_p = ctypes.pointer(n_past) @@ -1975,9 +1982,26 @@ def __call__( ) assert llama.n_ctx() >= n_past.value llama.n_tokens = n_past.value - finally: + except: with suppress_stdout_stderr(disable=self.verbose): self._llava_cpp.llava_image_embed_free(embed) + if content["type"] == "image_current": + try: + n_past = ctypes.c_int(llama.n_tokens) + n_past_p = ctypes.pointer(n_past) + with suppress_stdout_stderr(disable=self.verbose): + self._llava_cpp.llava_eval_image_embed( + ctx_llama=llama.ctx, + embed=self.current_image_embed, + n_batch=llama.n_batch, + n_past=n_past_p, + ) + assert llama.n_ctx() >= n_past.value + llama.n_tokens = n_past.value + except: + with suppress_stdout_stderr(disable=self.verbose): + self._llava_cpp.llava_image_embed_free(self.current_image_embed) + if message["role"] == "assistant" and message["content"] is not None: llama.eval( llama.tokenize(