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).
      """
      # First truncate the text so it's within `CohereClient.TOKENIZE_MAX_TEXT_LENGTH` length.
      text = text[: CohereClient.TOKENIZE_API_MAX_TEXT_LENGTH]
  
      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 text, since Cohere technically only supports English at the moment.
      while not self.fits_within_context_window(
          result, expected_completion_token_length
      ):
          result = result[:-1]
  
      return result
  
imports: from typing import List, Optional
  from helm.proxy.clients.cohere_client import CohereClient
  from .local_window_service import LocalWindowService
  from .tokenizer_service import TokenizerService
  from .window_service import EncodeResult
  from helm.common.tokenization_request import TokenizationRequest, TokenizationRequestResult, TokenizationToken
label_desc: Use image node_id 11 for calling the CohereWindowService obj's underlying member method code with example usage: obj.truncate_from_right(text, expected_completion_token_length) and returns: result