def rank()

in experiments/google/cloud/ml/applied/categories/category.py [0:0]


def rank(desc: str, candidates: list[list[str]]) -> list[list[str]]:
    """Use an LLM to rank candidates by description.

    Args:
      desc: user provided description of product
      candidates: list of categories. Each category is in list form
        e.g. ['level 1 category', 'level 2 category'] so it's a list of lists

    Returns:
      The candidates ranked by the LLM from most to least relevant. If there are
      duplicate candidates the list is deduped prior to returning
    """
    try:
        return _rank(desc, candidates)
    except ValueError as e:
        logging.error(e)
        logging.error("Falling back to original candidate ranking.")
        return list(dict.fromkeys([tuple(list_item) for list_item in candidates]))