code: def greedy_generate(
      model,
      tokenizer,
      input_ids,
      past_key_values,
      max_gen_len,
      stop_words=[],
  ):
      print(Fore.BLUE + "BigDL-LLM: " + Fore.RESET, end="")
      outputs = model(
          input_ids=input_ids,
          past_key_values=past_key_values,
          use_cache=True,
      )
      past_key_values = outputs.past_key_values
      pred_token_idx = (
          outputs.logits[:, -1, :].argmax(dim=-1).unsqueeze(1)
      )
      generated_ids = [pred_token_idx.item()]
      pos = 0
      stop = False
      for _ in range(max_gen_len - 1):
          outputs = model(
              input_ids=pred_token_idx,
              past_key_values=past_key_values,
              use_cache=True,
          )
          past_key_values = outputs.past_key_values
          pred_token_idx = (
              outputs.logits[:, -1, :].argmax(dim=-1).unsqueeze(1)
          )
          generated_ids.append(pred_token_idx.item())
  
          if stop_words is not None:
              for stop_str in stop_words:
                  if generated_ids[-1 * len(stop_str) :] == stop_str:
                      stop = True
                      break
              if stop:
                  break
  
          generated_text = tokenizer.decode(
              generated_ids,
              skip_special_tokens=True,
              clean_up_tokenization_spaces=True,
              spaces_between_special_tokens=False,
          )
  
          now = len(generated_text) - 1
          if now > pos:
              if "\n<" in generated_text:
                  break
              else:
                  print(
                      "".join(generated_text[pos:now]),
                      end="",
                      flush=True,
                  )
                  pos = now
  
          if pred_token_idx == tokenizer.eos_token_id:
              break
      print(" ".join(generated_text[pos:]).strip("\n<"), flush=True)
      return past_key_values
  
file_path: BigDL/python/llm/portable-zip/chat.py
imports: import torch
  import argparse
  import sys
  from transformers import AutoModel, AutoModelForCausalLM, AutoTokenizer, AutoConfig
  from transformers import TextIteratorStreamer
  from transformers.tools.agents import StopSequenceCriteria
  from transformers.generation.stopping_criteria import StoppingCriteriaList
  from colorama import Fore
  from bigdl.llm import optimize_model
  from kv_cache import StartRecentKVCache
label_desc: Use image node_id 2 for calling a global function with example usage: greedy_generate(model, tokenizer, input_ids, past_key_values, max_gen_len, stop_words) and returns: past_key_values