code: def __call__(
      self,
      *,
      llama: llama.Llama,
      messages: List[llama_types.ChatCompletionRequestMessage],
      functions: Optional[
          List[llama_types.ChatCompletionFunction]
      ] = None,
      function_call: Optional[
          llama_types.ChatCompletionRequestFunctionCall
      ] = None,
      tools: Optional[List[llama_types.ChatCompletionTool]] = None,
      tool_choice: Optional[
          llama_types.ChatCompletionToolChoiceOption
      ] = None,
      temperature: float = 0.2,
      top_p: float = 0.95,
      top_k: int = 40,
      min_p: float = 0.05,
      typical_p: float = 1.0,
      stream: bool = False,
      stop: Optional[Union[str, List[str]]] = [],
      response_format: Optional[
          llama_types.ChatCompletionRequestResponseFormat
      ] = None,
      max_tokens: Optional[int] = None,
      presence_penalty: float = 0.0,
      frequency_penalty: float = 0.0,
      repeat_penalty: float = 1.1,
      tfs_z: float = 1.0,
      mirostat_mode: int = 0,
      mirostat_tau: float = 5.0,
      mirostat_eta: float = 0.1,
      model: Optional[str] = None,
      logits_processor: Optional[llama.LogitsProcessorList] = None,
      grammar: Optional[llama.LlamaGrammar] = None,
      **kwargs,  # type: ignore
  ) -> Union[
      llama_types.CreateChatCompletionResponse,
      Iterator[llama_types.CreateChatCompletionStreamResponse],
  ]:
      assert (
          llama.context_params.logits_all is True
      )  # BUG: logits_all=True is required for llava
      assert self.clip_ctx is not None
      system_prompt = _get_system_message(messages)
      system_prompt = (
          system_prompt
          if system_prompt != ""
          else "A chat between a curious human and an artificial intelligence assistant.  The assistant gives helpful, detailed, and polite answers to the human's questions."
      )
      user_role = "\nUSER:"
      assistant_role = "\nASSISTANT:"
      llama.reset()
      llama.eval(
          llama.tokenize(system_prompt.encode("utf8"), add_bos=True)
      )
      for message in messages:
          if (
              message["role"] == "user"
              and message["content"] is not None
          ):
              if isinstance(message["content"], str):
                  llama.eval(
                      llama.tokenize(
                          f"{user_role} {message['content']}".encode(
                              "utf8"
                          ),
                          add_bos=False,
                      )
                  )
              else:
                  assert isinstance(message["content"], list)
                  llama.eval(
                      llama.tokenize(
                          f"{user_role} ".encode("utf8"), add_bos=False
                      )
                  )
                  for content in message["content"]:
                      if content["type"] == "text":
                          llama.eval(
                              llama.tokenize(
                                  f"{content['text']}".encode("utf8"),
                                  add_bos=False,
                              )
                          )
                      if content["type"] == "image_url":
                          image_bytes = (
                              self.load_image(
                                  content["image_url"]["url"]
                              )
                              if isinstance(content["image_url"], dict)
                              else self.load_image(content["image_url"])
                          )
                          import array
  
                          data_array = array.array("B", image_bytes)
                          c_ubyte_ptr = (
                              ctypes.c_ubyte * len(data_array)
                          ).from_buffer(data_array)
                          with suppress_stdout_stderr(
                              disable=self.verbose
                          ):
                              embed = self._llava_cpp.llava_image_embed_make_with_bytes(
                                  ctx_clip=self.clip_ctx,
                                  n_threads=llama.context_params.n_threads,
                                  image_bytes=c_ubyte_ptr,
                                  image_bytes_length=len(image_bytes),
                              )
                          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=embed,
                                      n_batch=llama.n_batch,
                                      n_past=n_past_p,
                                  )
                              assert llama.n_ctx() >= n_past.value
                              llama.n_tokens = n_past.value
                          finally:
                              with suppress_stdout_stderr(
                                  disable=self.verbose
                              ):
                                  self._llava_cpp.llava_image_embed_free(
                                      embed
                                  )
          if (
              message["role"] == "assistant"
              and message["content"] is not None
          ):
              llama.eval(
                  llama.tokenize(
                      f"ASSISTANT: {message['content']}".encode("utf8"),
                      add_bos=False,
                  )
              )
              assert llama.n_ctx() >= llama.n_tokens
      llama.eval(
          llama.tokenize(
              f"{assistant_role}".encode("utf8"), add_bos=False
          )
      )
      assert llama.n_ctx() >= llama.n_tokens
  
      prompt = llama.input_ids[: llama.n_tokens].tolist()
  
      if (
          response_format is not None
          and response_format["type"] == "json_object"
      ):
          with suppress_stdout_stderr(disable=self.verbose):
              grammar = llama_grammar.LlamaGrammar.from_string(
                  llama_grammar.JSON_GBNF
              )
  
      return _convert_completion_to_chat(
          llama.create_completion(
              prompt=prompt,
              temperature=temperature,
              top_p=top_p,
              top_k=top_k,
              min_p=min_p,
              typical_p=typical_p,
              stream=stream,
              stop=stop,
              max_tokens=max_tokens,
              presence_penalty=presence_penalty,
              frequency_penalty=frequency_penalty,
              repeat_penalty=repeat_penalty,
              tfs_z=tfs_z,
              mirostat_mode=mirostat_mode,
              mirostat_tau=mirostat_tau,
              mirostat_eta=mirostat_eta,
              model=model,
              logits_processor=logits_processor,
              grammar=grammar,
          ),
          stream=stream,
      )
  
imports: from __future__ import annotations
  import os
  import json
  import ctypes
  import dataclasses
  from typing import Any, Dict, Iterator, List, Optional, Tuple, Union, Protocol
  import llama_cpp.llama
  import llama_cpp.llama_types
  import llama_cpp.llama_grammar
  from ._utils import suppress_stdout_stderr
label_desc: Use image node_id 4 for calling the Llava15ChatHandler obj's underlying member method code with example usage: obj.__call__() and returns: _convert_completion_to_chat