docstr: Encodes the input text to tokens.
docstr_tok: ["Encodes","the","input","text","to","tokens","."]
code: def encode(
      self,
      text: str,
      truncation: bool = False,
      max_length: Optional[int] = None,
  ) -> EncodeResult:
      """
      Encodes the input text to tokens.
      """
      if max_length is None:
          max_length = self.max_request_length
  
      response: TokenizationRequestResult
      tokens: List[TokenizationToken] = []
      if (
          truncation
          or len(text) <= CohereClient.TOKENIZE_API_MAX_TEXT_LENGTH
      ):
          response = self.service.tokenize(
              TokenizationRequest(
                  text,
                  tokenizer=self.tokenizer_name,
                  # The Cohere API does not support decoding, so set `encode` to False to get the value of tokens
                  # as strings so we can simply concatenate them when we need to decode.
                  encode=False,
                  truncation=truncation,
                  max_length=max_length,
              )
          )
          tokens = response.tokens
      else:
          # Perform chunk encoding: Cohere doesn't support long sequences, so break it up into chunks
          # and make a request for each chunk.
          # This can potentially break up valid tokens at the end of the chunk, but the chunk size
          # is large enough that this happens infrequently.
          chunk_size: int = CohereClient.TOKENIZE_API_MAX_TEXT_LENGTH
          for i in range(0, len(text), chunk_size):
              chunk: str = text[i : chunk_size + i]
              response = self.service.tokenize(
                  TokenizationRequest(
                      chunk,
                      tokenizer=self.tokenizer_name,
                      encode=False,
                      truncation=False,
                  )
              )
              tokens.extend(response.tokens)
  
      return EncodeResult(text=text, tokens=tokens)
  
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 7 for calling the CohereWindowService obj's underlying member method code with example usage: obj.encode(text, truncation, max_length) and returns: EncodeResult