def top_k_mentions(
    name: str,
    query: Optional[str] = None,
    compute_for: Union[str, ComputeFor] = Predictions,
    k: int = 100,
    threshold: int = 2,
    post_label_filter: Optional[Set[str]] = None,
):
    """Computes the consistency for top k mentions in the dataset.

    Entity consistency defines the label variability for a given mention. For example, a mention `first` identified
    in the whole dataset as `Cardinal`, `Person` and `Time` is less consistent than a mention `Peter` identified as
    `Person` in the dataset.

    Args:
        name: The dataset name.
        query: An ElasticSearch query with the
            `query string syntax <https://docs.v1.argilla.io/en/latest/practical_guides/filter_dataset.html>`_
        compute_for: Metric can be computed for annotations or predictions. Accepted values are
            ``Annotations`` and ``Predictions``. Default to ``Predictions``
        k: The number of mentions to retrieve.
        threshold: The entity variability threshold (must be greater or equal to 1).
        post_label_filter: A set of labels used for filtering the results. This filter may affect to the expected
        number of mentions

    Returns:
        The summary top k mentions distribution

    Examples:
        >>> from argilla_v1.metrics.token_classification import top_k_mentions
        >>> summary = top_k_mentions(name="example-dataset")
        >>> summary.visualize()
    """

    threshold = max(1, threshold)
    metric = singleton.active_api().compute_metric(
        name,
        metric=f"{_check_compute_for(compute_for)}_top_k_mentions_consistency",
        query=query,
        size=k,
        interval=threshold,
    )

    filtered_mentions, mention_values = [], []
    for mention in metric.results["mentions"]:
        entities = mention["entities"]
        if post_label_filter:
            entities = [entity for entity in entities if entity["label"] in post_label_filter]
        if entities:
            mention["entities"] = entities
            filtered_mentions.append(mention)
            mention_values.append(mention["mention"])

    entities = {}
    for mention in filtered_mentions:
        for entity in mention["entities"]:
            label = entity["label"]
            mentions_for_label = entities.get(label, [0] * len(filtered_mentions))
            mentions_for_label[mention_values.index(mention["mention"])] = entity["count"]
            entities[label] = mentions_for_label

    return MetricSummary.new_summary(
        data={"mentions": filtered_mentions},
        visualization=lambda: helpers.stacked_bar(
            x=mention_values,
            y_s=entities,
            title=metric.description,
        ),
    )
Computes the consistency for top k mentions in the dataset.

    Entity consistency defines the label variability for a given mention. For example, a mention `first` identified
    in the whole dataset as `Cardinal`, `Person` and `Time` is less consistent than a mention `Peter` identified as
    `Person` in the dataset.

    Args:
        name: The dataset name.
        query: An ElasticSearch query with the
            `query string syntax <https://docs.v1.argilla.io/en/latest/practical_guides/filter_dataset.html>`_
        compute_for: Metric can be computed for annotations or predictions. Accepted values are
            ``Annotations`` and ``Predictions``. Default to ``Predictions``
        k: The number of mentions to retrieve.
        threshold: The entity variability threshold (must be greater or equal to 1).
        post_label_filter: A set of labels used for filtering the results. This filter may affect to the expected
        number of mentions

    Returns:
        The summary top k mentions distribution

    Examples:
        >>> from argilla_v1.metrics.token_classification import top_k_mentions
        >>> summary = top_k_mentions(name="example-dataset")
        >>> summary.visualize()
    
argilla-v1/src/argilla_v1/metrics/token_classification/metrics.py