docstr: Truncates text from the right to fit within the context window given by `max_request_length`
  minus the expected completion length (defaults to 0).
docstr_tok: ["Truncates","text","from","the","right","to","fit","within","the","context","window","given","by","`","max_request_length","`","minus","the","expected","completion","length","(","defaults","to","0",")","."]
code: def truncate_from_right(
      self, text: str, expected_completion_token_length: int = 0
  ) -> str:
      """
      Truncates text from the right to fit within the context window given by `max_request_length`
      minus the expected completion length (defaults to 0).
      """
      max_length: int = (
          self.max_request_length - expected_completion_token_length
      )
      result: str = self.decode(
          self.encode(
              text, truncation=True, max_length=max_length
          ).tokens
      )
  
      # HACK: For the vast majority of cases, the above logic works, but it sometimes doesn't work
      # for non-English, non-Chinese text (e.g., Japanese text from NaturalQA -
      # followed up in https://github.com/THUDM/icetk/issues/3).
      # Truncate by removing character by character until the prompt fits within the context window.
      while not self.fits_within_context_window(
          result, expected_completion_token_length
      ):
          result = result[:-1]
  
      return result
  
file_path: H2O/h2o_hf/helm/src/helm/benchmark/window_services/ice_window_service.py
imports: from .local_window_service import LocalWindowService
  from .tokenizer_service import TokenizerService
label_desc: Use image node_id 7 for calling the ICEWindowService obj's underlying member method code with example usage: obj.truncate_from_right(text, expected_completion_token_length) and returns: result