def preprocess()

in community-content/vertex_model_garden/model_oss/peft/handler.py [0:0]


  def preprocess(self, data: Any) -> Any:
    """Preprocesses input data."""
    # Assumes that the parameters are same in one request. We parse the
    # parameters from the first instance for all instances in one request.
    # For generation length: `max_length` defines the maximum length of the
    # sequence to be generated, including both input and output tokens.
    # `max_length` is overridden by `max_new_tokens` if also set.
    # `max_new_tokens` defines the maximum number of new tokens to generate,
    # ignoring the current number of tokens.
    # Reference:
    # https://github.com/huggingface/transformers/blob/574a5384557b1aaf98ddb13ea9eb0a0ee8ff2cb2/src/transformers/generation/configuration_utils.py#L69-L73
    max_length = _MAX_LENGTH_DEFAULT
    max_tokens = _MAX_TOKENS_DEFAULT
    temperature = _TEMPERATURE_DEFAULT
    top_p = _TOP_P_DEFAULT
    top_k = _TOP_K_DEFAULT

    prompts = [item["prompt"] for item in data]
    if "max_length" in data[0]:
      max_length = data[0]["max_length"]
    if "max_tokens" in data[0]:
      max_tokens = data[0]["max_tokens"]
    if "temperature" in data[0]:
      temperature = data[0]["temperature"]
    if "top_p" in data[0]:
      top_p = data[0]["top_p"]
    if "top_k" in data[0]:
      top_k = data[0]["top_k"]

    return prompts, max_length, max_tokens, temperature, top_p, top_k