class_docstr: High-level Python wrapper for a llama.cpp model.
code: def _create_completion(
      self,
      prompt: str,
      suffix: Optional[str] = None,
      max_tokens: int = 16,
      temperature: float = 0.8,
      top_p: float = 0.95,
      logprobs: Optional[int] = None,
      echo: bool = False,
      stop: Optional[Union[str, List[str]]] = [],
      frequency_penalty: float = 0.0,
      presence_penalty: float = 0.0,
      repeat_penalty: float = 1.1,
      top_k: int = 40,
      stream: bool = False,
      tfs_z: float = 1.0,
      mirostat_mode: int = 0,
      mirostat_tau: float = 5.0,
      mirostat_eta: float = 0.1,
      model: Optional[str] = None,
  ) -> Union[Iterator[Completion], Iterator[CompletionChunk]]:
      invalidInputError(
          self.ctx is not None,
          "The attribute `ctx` of `Llama` object is None.",
      )
      completion_id: str = f"cmpl-{str(uuid.uuid4())}"
      created: int = int(time.time())
      completion_tokens: List[int] = []
      # Add blank space to start of prompt to match OG llama tokenizer
      prompt_tokens: List[int] = self.tokenize(
          b" " + prompt.encode("utf-8")
      )
      text: bytes = b""
      returned_tokens: int = 0
      stop = (
          stop
          if isinstance(stop, list)
          else [stop]
          if isinstance(stop, str)
          else []
      )
      model_name: str = model if model is not None else self.model_path
  
      if self.verbose:
          llama_cpp.llama_reset_timings(self.ctx)
  
      invalidInputError(
          len(prompt_tokens) + max_tokens
          <= int(llama_cpp.llama_n_ctx(self.ctx)),
          "Requested tokens exceed context window of "
          f"{llama_cpp.llama_n_ctx(self.ctx)}.",
      )
  
      if stop != []:
          stop_sequences = [s.encode("utf-8") for s in stop]
      else:
          stop_sequences = []
  
      if logprobs is not None and self.params.logits_all is False:
          invalidInputError(
              False,
              "logprobs is not supported for models created with logits_all=False",
          )
  
      if self.cache:
          try:
              cache_item = self.cache[prompt_tokens]
              cache_prefix_len = Llama.longest_token_prefix(
                  cache_item.eval_tokens, prompt_tokens
              )
              eval_prefix_len = Llama.longest_token_prefix(
                  self.eval_tokens, prompt_tokens
              )
              if cache_prefix_len > eval_prefix_len:
                  self.load_state(cache_item)
                  if self.verbose:
                      print(
                          "Llama._create_completion: cache hit",
                          file=sys.stderr,
                      )
          except KeyError:
              if self.verbose:
                  print(
                      "Llama._create_completion: cache miss",
                      file=sys.stderr,
                  )
  
      finish_reason = "length"
      multibyte_fix = 0
      for token in self._generate(
          prompt_tokens,
          top_k=top_k,
          top_p=top_p,
          temp=temperature,
          tfs_z=tfs_z,
          mirostat_mode=mirostat_mode,
          mirostat_tau=mirostat_tau,
          mirostat_eta=mirostat_eta,
          frequency_penalty=frequency_penalty,
          presence_penalty=presence_penalty,
          repeat_penalty=repeat_penalty,
      ):
          if token == self._token_eos:
              text = self.detokenize(completion_tokens)
              finish_reason = "stop"
              break
  
          completion_tokens.append(token)
  
          all_text = self.detokenize(completion_tokens)
  
          # Contains multi-byte UTF8
          for k, char in enumerate(all_text[-3:]):
              k = 3 - k
              for num, pattern in [(2, 192), (3, 224), (4, 240)]:
                  # Bitwise AND check
                  if num > k and pattern & char == pattern:
                      multibyte_fix = num - k
  
          # Stop incomplete bytes from passing
          if multibyte_fix > 0:
              multibyte_fix -= 1
              continue
  
          any_stop = [s for s in stop_sequences if s in all_text]
          if len(any_stop) > 0:
              first_stop = any_stop[0]
              text = all_text[: all_text.index(first_stop)]
              finish_reason = "stop"
              break
  
          if stream:
              # We want to avoid yielding any characters from
              # the generated text if they are part of a stop
              # sequence.
              first_stop_position = 0
              for s in stop_sequences:
                  for i in range(len(s), 0, -1):
                      if all_text.endswith(s[:i]):
                          if i > first_stop_position:
                              first_stop_position = i
                          break
  
              token_end_position = 0
              remaining_tokens = completion_tokens[returned_tokens:]
              remaining_length = len(self.detokenize(remaining_tokens))
              for token in remaining_tokens:
                  token_end_position += len(self.detokenize([token]))
                  # Check if stop sequence is in the token
                  if token_end_position >= (
                      remaining_length - first_stop_position - 1
                  ):
                      break
                  logprobs_or_none: Optional[CompletionLogprobs] = None
                  if logprobs is not None:
                      token_str = self.detokenize([token]).decode(
                          "utf-8", errors="ignore"
                      )
                      text_offset = len(prompt) + len(
                          self.detokenize(
                              completion_tokens[:returned_tokens]
                          )
                      )
                      token_offset = (
                          len(prompt_tokens) + returned_tokens
                      )
                      logits = self.eval_logits[token_offset - 1]
                      current_logprobs = Llama.logits_to_logprobs(
                          logits
                      )
                      sorted_logprobs = list(
                          sorted(
                              zip(
                                  current_logprobs,
                                  range(len(current_logprobs)),
                              ),
                              reverse=True,
                          )
                      )
                      top_logprob = {
                          self.detokenize([i]).decode(
                              "utf-8", errors="ignore"
                          ): logprob
                          for logprob, i in sorted_logprobs[:logprobs]
                      }
                      top_logprob.update(
                          {token_str: current_logprobs[int(token)]}
                      )
                      logprobs_or_none = {
                          "tokens": [
                              self.detokenize([token]).decode(
                                  "utf-8", errors="ignore"
                              )
                          ],
                          "text_offset": [text_offset],
                          "token_logprobs": [
                              sorted_logprobs[int(token)][0]
                          ],
                          "top_logprobs": [top_logprob],
                      }
                  returned_tokens += 1
                  yield {
                      "id": completion_id,
                      "object": "text_completion",
                      "created": created,
                      "model": model_name,
                      "choices": [
                          {
                              "text": self.detokenize([token]).decode(
                                  "utf-8", errors="ignore"
                              ),
                              "index": 0,
                              "logprobs": logprobs_or_none,
                              "finish_reason": None,
                          }
                      ],
                      "usage": {
                          "prompt_tokens": len(prompt_tokens),
                      },
                  }
  
          if len(completion_tokens) >= max_tokens:
              text = self.detokenize(completion_tokens)
              finish_reason = "length"
              break
  
      if self.cache:
          if self.verbose:
              print(
                  "Llama._create_completion: cache save",
                  file=sys.stderr,
              )
          self.cache[
              prompt_tokens + completion_tokens
          ] = self.save_state()
  
      if self.verbose:
          llama_cpp.llama_print_timings(self.ctx)
  
      if stream:
          remaining_tokens = completion_tokens[returned_tokens:]
          all_text = self.detokenize(remaining_tokens)
          any_stop = [s for s in stop_sequences if s in all_text]
          if len(any_stop) > 0:
              end = min(all_text.index(stop) for stop in any_stop)
          else:
              end = len(all_text)
  
          token_end_position = 0
          for token in remaining_tokens:
              token_end_position += len(self.detokenize([token]))
  
              logprobs_or_none: Optional[CompletionLogprobs] = None
              if logprobs is not None:
                  token_str = self.detokenize([token]).decode(
                      "utf-8", errors="ignore"
                  )
                  text_offset = len(prompt) + len(
                      self.detokenize(
                          completion_tokens[:returned_tokens]
                      )
                  )
                  token_offset = (
                      len(prompt_tokens) + returned_tokens - 1
                  )
                  logits = self.eval_logits[token_offset]
                  current_logprobs = Llama.logits_to_logprobs(logits)
                  sorted_logprobs = list(
                      sorted(
                          zip(
                              current_logprobs,
                              range(len(current_logprobs)),
                          ),
                          reverse=True,
                      )
                  )
                  top_logprob = {
                      self.detokenize([i]).decode(
                          "utf-8", errors="ignore"
                      ): logprob
                      for logprob, i in sorted_logprobs[:logprobs]
                  }
                  top_logprob.update(
                      {token_str: current_logprobs[int(token)]}
                  )
                  logprobs_or_none = {
                      "tokens": [
                          self.detokenize([token]).decode(
                              "utf-8", errors="ignore"
                          )
                      ],
                      "text_offset": [text_offset],
                      "token_logprobs": [
                          sorted_logprobs[int(token)][0]
                      ],
                      "top_logprobs": [top_logprob],
                  }
  
              if token_end_position >= end:
                  last_text = self.detokenize([token])
                  if token_end_position == end - 1:
                      break
                  returned_tokens += 1
                  yield {
                      "id": completion_id,
                      "object": "text_completion",
                      "created": created,
                      "model": model_name,
                      "choices": [
                          {
                              "text": last_text[
                                  : len(last_text)
                                  - (token_end_position - end)
                              ].decode("utf-8", errors="ignore"),
                              "index": 0,
                              "logprobs": logprobs_or_none,
                              "finish_reason": finish_reason,
                          }
                      ],
                      "usage": {
                          "prompt_tokens": len(prompt_tokens),
                      },
                  }
                  break
              returned_tokens += 1
              yield {
                  "id": completion_id,
                  "object": "text_completion",
                  "created": created,
                  "model": model_name,
                  "choices": [
                      {
                          "text": self.detokenize([token]).decode(
                              "utf-8", errors="ignore"
                          ),
                          "index": 0,
                          "logprobs": logprobs_or_none,
                          "finish_reason": finish_reason
                          if returned_tokens == len(completion_tokens)
                          else None,
                      }
                  ],
                  "usage": {
                      "prompt_tokens": len(prompt_tokens),
                  },
              }
          return
  
      text_str = text.decode("utf-8", errors="ignore")
  
      if echo:
          text_str = prompt + text_str
  
      if suffix is not None:
          text_str = text_str + suffix
  
      logprobs_or_none: Optional[CompletionLogprobs] = None
      if logprobs is not None:
          text_offset = 0 if echo else len(prompt)
          token_offset = 0 if echo else len(prompt_tokens[1:])
          text_offsets: List[int] = []
          token_logprobs: List[Optional[float]] = []
          tokens: List[str] = []
          top_logprobs: List[Optional[Dict[str, float]]] = []
  
          if echo:
              # Remove leading BOS token
              all_tokens = prompt_tokens[1:] + completion_tokens
          else:
              all_tokens = completion_tokens
  
          all_token_strs = [
              self.detokenize([token]).decode("utf-8", errors="ignore")
              for token in all_tokens
          ]
          all_logprobs = [
              Llama.logits_to_logprobs(list(map(float, row)))
              for row in self.eval_logits
          ][token_offset:]
          for token, token_str, logprobs_token in zip(
              all_tokens, all_token_strs, all_logprobs
          ):
              text_offsets.append(text_offset)
              text_offset += len(token_str)
              tokens.append(token_str)
              sorted_logprobs = list(
                  sorted(
                      zip(logprobs_token, range(len(logprobs_token))),
                      reverse=True,
                  )
              )
              token_logprobs.append(sorted_logprobs[int(token)][0])
              top_logprob: Optional[Dict[str, float]] = {
                  self.detokenize([i]).decode(
                      "utf-8", errors="ignore"
                  ): logprob
                  for logprob, i in sorted_logprobs[:logprobs]
              }
              top_logprob.update(
                  {token_str: logprobs_token[int(token)]}
              )
              top_logprobs.append(top_logprob)
          # Weird idosincracy of the OpenAI API where
          # token_logprobs and top_logprobs are null for
          # the first token.
          if echo and len(all_tokens) > 0:
              token_logprobs[0] = None
              top_logprobs[0] = None
          logprobs_or_none = {
              "tokens": tokens,
              "text_offset": text_offsets,
              "token_logprobs": token_logprobs,
              "top_logprobs": top_logprobs,
          }
  
      yield {
          "id": completion_id,
          "object": "text_completion",
          "created": created,
          "model": model_name,
          "choices": [
              {
                  "text": text_str,
                  "index": 0,
                  "logprobs": logprobs_or_none,
                  "finish_reason": finish_reason,
              }
          ],
          "usage": {
              "prompt_tokens": len(prompt_tokens),
              "completion_tokens": len(completion_tokens),
              "total_tokens": len(prompt_tokens)
              + len(completion_tokens),
          },
      }
  
imports: import os
  import sys
  import uuid
  import time
  import math
  import multiprocessing
  from typing import List, Optional, Union, Generator, Sequence, Iterator, Deque, Tuple
  from collections import deque, OrderedDict
  from bigdl.llm.utils.common import invalidInputError
  from bigdl.llm.ggml.model.generation import GenerationMixin
  from .None import llama_cpp
  from .llama_types import *
label_desc: Use image node_id 12 for calling the Llama obj's underlying member method code with example usage: obj._create_completion(prompt, suffix, max_tokens, temperature, top_p, logprobs, echo, stop, frequency_penalty, presence_penalty, repeat_penalty, top_k, stream, tfs_z, mirostat_mode, mirostat_tau, mirostat_eta, model) without return types