def _ensure_prompt_not_too_long(
    prompt: str,
    num_response_tokens: int,
    guess_chars_per_token: int = _GUESS_CHARS_PER_TOKEN) -> str:
  r"""Ensures the prompt is not too long for Together AI\'s Gemma-2 models."""
  num_initial_chars = _NUM_INITIAL_TOKENS * guess_chars_per_token
  max_prompt_tokens = _MAX_ALLOWED_TOKENS - num_response_tokens
  if max_prompt_tokens <= 0:
    raise ValueError(
        f'Cannot reserve {num_response_tokens} of {_MAX_ALLOWED_TOKENS} tokens.'
    )
  max_prompt_chars = max_prompt_tokens * guess_chars_per_token
  if len(prompt) <= max_prompt_chars:
    return prompt

  # Keep the first _NUM_INITIAL_TOKENS tokens and then skip to the last tokens
  # and take as many as we can from the end.
  if max_prompt_chars > num_initial_chars:
    num_final_chars = max_prompt_chars - num_initial_chars
    new_prompt = prompt[:num_initial_chars] + prompt[-num_final_chars:]
    logging.info('Prompt too long, trimmed it down, while keeping start and '
                 'end, resulting in %d characters', len(new_prompt))
    logging.debug('Trimmed prompt: %s', new_prompt)
    return new_prompt

  # This happens if len(prompt) > max_prompt_chars <= num_initial_chars.
  new_prompt = prompt[-max_prompt_chars:]
  logging.info(
      'Prompt too long, truncated it to last %d characters.',
      max_prompt_chars
  )
  logging.debug('Truncated prompt: %s', new_prompt)
  return new_prompt
Ensures the prompt is not too long for Together AI\'s Gemma-2 models.
concordia/language_model/together_ai.py