code: def limit_prompt(
      prompt_text, tokenizer, max_prompt_length=None, buffer=256
  ):
      if prompt_text is None:
          prompt_text = ""
      verbose = bool(int(os.getenv("VERBOSE_PIPELINE", "0")))
  
      if hasattr(tokenizer, "model_max_length"):
          # model_max_length only defined for generate.py, not raw use of h2oai_pipeline.py
          model_max_length = int(tokenizer.model_max_length)
          if max_prompt_length is not None:
              model_max_length = int(
                  min(model_max_length, max_prompt_length)
              )
          # cut at some upper likely limit to avoid excessive tokenization etc
          # upper bound of 10 chars/token, e.g. special chars sometimes are long
          if len(prompt_text) > model_max_length * 10:
              len0 = len(prompt_text)
              prompt_text = prompt_text[-model_max_length * 10 :]
              if verbose:
                  print(
                      "Cut of input: %s -> %s"
                      % (len0, len(prompt_text)),
                      flush=True,
                  )
      elif max_prompt_length is not None:
          model_max_length = max_prompt_length
      else:
          # unknown
          model_max_length = None
  
      num_prompt_tokens = None
      if model_max_length is not None:
          # can't wait for "hole" if not plain prompt_type, since would lose prefix like <human>:
          # For https://github.com/h2oai/h2ogpt/issues/192
          for trial in range(0, 5):
              if prompt_text:
                  num_prompt_tokens = (
                      H2OTextGenerationPipeline.get_token_count(
                          prompt_text, tokenizer
                      )
                  )
              else:
                  num_prompt_tokens = 0
              if (
                  num_prompt_tokens > model_max_length
                  and num_prompt_tokens > 0
              ):
                  # conservative by using int()
                  chars_per_token = len(prompt_text) / num_prompt_tokens
                  # keep tail, where question is if using langchain
                  model_max_length_with_buffer = (
                      model_max_length - buffer
                  )
                  prompt_text = prompt_text[
                      -int(
                          model_max_length_with_buffer * chars_per_token
                      ) :
                  ]
                  if verbose:
                      print(
                          "reducing %s tokens, assuming average of %s chars/token for %s characters"
                          % (
                              num_prompt_tokens,
                              chars_per_token,
                              len(prompt_text),
                          ),
                          flush=True,
                      )
              else:
                  if verbose:
                      print(
                          "using %s tokens with %s chars"
                          % (num_prompt_tokens, len(prompt_text)),
                          flush=True,
                      )
                  break
          if (
              num_prompt_tokens is not None
              and num_prompt_tokens > model_max_length
          ):
              print(
                  "Failed to reduce %s tokens with %s chars: %s"
                  % (num_prompt_tokens, len(prompt_text), prompt_text),
                  flush=True,
              )
  
      return prompt_text, num_prompt_tokens
  
file_path: h2ogpt/src/h2oai_pipeline.py
imports: import os
  from transformers import TextGenerationPipeline
  from transformers.pipelines.text_generation import ReturnType
  from stopping import get_stopping
  from prompter import Prompter
label_desc: Use image node_id 3 for calling the H2OTextGenerationPipeline obj's underlying member method code with example usage: obj.limit_prompt(prompt_text, tokenizer, max_prompt_length, buffer) and returns: prompt_text,  num_prompt_tokens