# Package evaluation (1.153.1)

1.153.1 (latest) 1.153.0 1.152.0 1.151.0 1.150.0 1.149.0 1.148.1 1.147.0 1.146.0 1.145.0 1.144.0 1.143.0 1.142.0 1.141.0 1.140.0 1.139.0 1.138.0 1.137.0 1.136.0 1.135.0 1.134.0 1.133.0 1.132.0 1.131.0 1.130.0 1.129.0 1.122.0 1.121.0 1.120.0 1.119.0 1.118.0 1.117.0 1.95.1 1.94.0 1.93.1 1.92.0 1.91.0 1.90.0 1.89.0 1.88.0 1.87.0 1.86.0 1.85.0 1.84.0 1.83.0 1.82.0 1.81.0 1.80.0 1.79.0 1.78.0 1.77.0 1.76.0 1.75.0 1.74.0 1.73.0 1.72.0 1.71.1 1.70.0 1.69.0 1.68.0 1.67.1 1.66.0 1.65.0 1.63.0 1.62.0 1.60.0 1.59.0 API documentation for `evaluation` package.

## Classes

### [CustomMetric](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.CustomMetric)

The custom evaluation metric.

A fully-customized CustomMetric that can be used to evaluate a single model
by defining a metric function for a computation-based metric. The
CustomMetric is computed on the client-side using the user-defined metric
function in SDK only, not by the Vertex Gen AI Evaluation Service.

Attributes:
name: The name of the metric.
metric_function: The user-defined evaluation function to compute a metric
score. Must use the dataset row dictionary as the metric function
input and return per-instance metric result as a dictionary output.
The metric score must mapped to the name of the CustomMetric as key.

### [EvalResult](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.EvalResult)

Evaluation result.

### [EvalTask](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.EvalTask)

A class representing an EvalTask.

An evaluation task assesses the ability of a Gen AI model, agent or
application to perform a specific task in response to prompts.
Each evaluation task includes an evaluation dataset, which can be a set of
test cases and a set of metrics for assessment. These tasks provide the
framework for running evaluations in a standardized and repeatable way,
allowing for comparative assessment with varying run-specific parameters.

Dataset Details:

    Default dataset column names:
        * prompt_column_name: "prompt"
        * reference_column_name: "reference"
        * response_column_name: "response"
        * baseline_model_response_column_name: "baseline_model_response"
        * rubrics_column_name: "rubrics"


    Requirement for different use cases:
      * Bring-your-own-response (BYOR): You already have the data that you
          want to evaluate stored in the dataset. Response column name can be
          customized by providing `response_column_name` parameter, or in the
          `metric_column_mapping`. For BYOR pairwise evaluation, the baseline
          model response column name can be customized by providing
          `baseline_model_response_column_name` parameter, or
          in the `metric_column_mapping`. If the `response` column or
          `baseline_model_response` column is present while the
          corresponding model is specified, an error will be raised.

      * Perform model/agent inference without a prompt template: You have a dataset
          containing the input prompts to the model/agent and want to perform
          inference before evaluation. A column named `prompt` is required
          in the evaluation dataset and is used directly as input to the model/agent.

      * Perform model/agent inference with a prompt template: You have a dataset
          containing the input variables to the prompt template and want to
          assemble the prompts for inference. Evaluation dataset
          must contain column names corresponding to the variable names in
          the prompt template. For example, if prompt template is
          "Instruction: {instruction}, context: {context}", the dataset must
          contain `instruction` and `context` columns.

Metrics Details:

    The supported metrics descriptions, rating rubrics, and the required
    input variables can be found on the Vertex AI public documentation page.
    [Evaluation methods and metrics](https://cloud.google.com/vertex-ai/generative-ai/docs/models/determine-eval).

Usage Examples:

    1. To perform bring-your-own-response(BYOR) evaluation, provide the model
    responses in the `response` column in the dataset. If a pairwise metric is
    used for BYOR evaluation, provide the baseline model responses in the
    `baseline_model_response` column.

      ```
      eval_dataset = pd.DataFrame({
              "prompt"  : [...],
              "reference": [...],
              "response" : [...],
              "baseline_model_response": [...],
      })
      eval_task = EvalTask(
        dataset=eval_dataset,
        metrics=[
                "bleu",
                "rouge_l_sum",
                MetricPromptTemplateExamples.Pointwise.FLUENCY,
                MetricPromptTemplateExamples.Pairwise.SAFETY
        ],
        experiment="my-experiment",
      )
      eval_result = eval_task.evaluate(experiment_run_name="eval-experiment-run")
      ```

    2. To perform evaluation with Gemini model inference, specify the `model`
    parameter with a model name string. The input column name to the
    model is `prompt` and must be present in the dataset.

      ```
      eval_dataset = pd.DataFrame({
            "reference": [...],
            "prompt"  : [...],
      })
      result = EvalTask(
          dataset=eval_dataset,
          metrics=["exact_match", "bleu", "rouge_1", "rouge_l_sum"],
          experiment="my-experiment",
      ).evaluate(
          model="gemini-2.5-flash",
          experiment_run_name="gemini-eval-run"
      )
      ```

    3. If a `prompt_template` is specified, the `prompt` column is not required.
    Prompts can be assembled from the evaluation dataset, and all prompt
    template variable names must be present in the dataset columns.
      ```
      eval_dataset = pd.DataFrame({
          "context"    : [...],
          "instruction": [...],
      })
      result = EvalTask(
          dataset=eval_dataset,
          metrics=[MetricPromptTemplateExamples.Pointwise.SUMMARIZATION_QUALITY],
      ).evaluate(
          model="gemini-2.5-flash",
          prompt_template="{instruction}. Article: {context}. Summary:",
      )
      ```

    4. To perform evaluation with custom model inference, specify the `model`
    parameter with a custom inference function. The input column name to the
    custom inference function is `prompt` and must be present in the dataset.

      ```
      from openai import OpenAI
      client = OpenAI()
      def custom_model_fn(input: str) -> str:
        response = client.chat.completions.create(
          model="gpt-3.5-turbo",
          messages=[
            {"role": "user", "content": input}
          ]
        )
        return response.choices[0].message.content

      eval_dataset = pd.DataFrame({
            "prompt"  : [...],
            "reference": [...],
      })
      result = EvalTask(
          dataset=eval_dataset,
          metrics=[MetricPromptTemplateExamples.Pointwise.SAFETY],
          experiment="my-experiment",
      ).evaluate(
          model=custom_model_fn,
          experiment_run_name="gpt-eval-run"
      )
      ```

    5. To perform pairwise metric evaluation with model inference step, specify
    the `baseline_model` input to a `PairwiseMetric` instance and the candidate
    `model` input to the `EvalTask.evaluate()` function. The input column name
    to both models is `prompt` and must be present in the dataset.

      ```
      baseline_model = "gemini-2.5-pro"
      candidate_model = "gemini-2.5-flash"

      pairwise_groundedness = PairwiseMetric(
          metric_prompt_template=MetricPromptTemplateExamples.get_prompt_template(
              "pairwise_groundedness"
          ),
          baseline_model=baseline_model,
      )
      eval_dataset = pd.DataFrame({
            "prompt"  : [...],
      })
      result = EvalTask(
          dataset=eval_dataset,
          metrics=[pairwise_groundedness],
          experiment="my-pairwise-experiment",
      ).evaluate(
          model=candidate_model,
          experiment_run_name="gemini-pairwise-eval-run",
      )
      ```

### [MetricPromptTemplateExamples](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.MetricPromptTemplateExamples)

Examples of metric prompt templates for model-based evaluation.

### [PairwiseMetric](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.PairwiseMetric)

A Model-based Pairwise Metric.

A model-based evaluation metric that compares two generative models' responses
side-by-side, and allows users to A/B test their generative models to
determine which model is performing better.

For more details on when to use pairwise metrics, see
[Evaluation methods and
metrics](https://cloud.google.com/vertex-ai/generative-ai/docs/models/determine-eval#pointwise_versus_pairwise).

Result Details:

    * In `EvalResult.summary_metrics`, win rates for both the baseline and
    candidate model are computed. The win rate is computed as proportion of
    wins of one model's responses to total attempts as a decimal value
    between 0 and 1.

    * In `EvalResult.metrics_table`, a pairwise metric produces two
    evaluation results per dataset row:
        * `pairwise_choice`: The choice shows whether the candidate model or
          the baseline model performs better, or if they are equally good.
        * `explanation`: The rationale behind each verdict using
          chain-of-thought reasoning. The explanation helps users scrutinize
          the judgment and builds appropriate trust in the decisions.

    See [documentation
    page](https://cloud.google.com/vertex-ai/generative-ai/docs/models/determine-eval#understand-results)
    for more details on understanding the metric results.

Usage Examples:

    ```
    baseline_model = "gemini-2.5-pro"
    candidate_model = "gemini-2.5-flash"

    pairwise_groundedness = PairwiseMetric(
        metric_prompt_template=MetricPromptTemplateExamples.get_prompt_template(
            "pairwise_groundedness"
        ),
        baseline_model=baseline_model,
    )
    eval_dataset = pd.DataFrame({
          "prompt"  : [...],
    })
    pairwise_task = EvalTask(
        dataset=eval_dataset,
        metrics=[pairwise_groundedness],
        experiment="my-pairwise-experiment",
    )
    pairwise_result = pairwise_task.evaluate(
        model=candidate_model,
        experiment_run_name="gemini-pairwise-eval-run",
    )
    ```

### [PairwiseMetricPromptTemplate](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.PairwiseMetricPromptTemplate)

Pairwise metric prompt template for pairwise model-based metrics.

### [PointwiseMetric](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.PointwiseMetric)

A Model-based Pointwise Metric.

A model-based evaluation metric that evaluate a single generative model's
response.

For more details on when to use model-based pointwise metrics, see
[Evaluation methods and metrics](https://cloud.google.com/vertex-ai/generative-ai/docs/models/determine-eval).

Usage Examples:

    ```
    candidate_model = "gemini-2.5-pro"
    eval_dataset = pd.DataFrame({
        "prompt"  : [...],
    })
    fluency_metric = PointwiseMetric(
        metric="fluency",
        metric_prompt_template=MetricPromptTemplateExamples.get_prompt_template('fluency'),
    )
    pointwise_eval_task = EvalTask(
        dataset=eval_dataset,
        metrics=[
            fluency_metric,
            MetricPromptTemplateExamples.Pointwise.GROUNDEDNESS,
        ],
    )
    pointwise_result = pointwise_eval_task.evaluate(
        model=candidate_model,
    )
    ```

### [PointwiseMetricPromptTemplate](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.PointwiseMetricPromptTemplate)

Pointwise metric prompt template for pointwise model-based metrics.

### [PromptTemplate](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.PromptTemplate)

A prompt template for creating prompts with variables.

The `PromptTemplate` class allows users to define a template string with
variables represented in curly braces `{variable}`. The variable
names cannot contain spaces and must start with a letter or underscore,
followed by letters, digits, or underscore. These variables can be
replaced with specific values using the `assemble` method, providing
flexibility in generating dynamic prompts.

Usage:

    ```
    template_str = "Hello, {name}! Today is {day}. How are you?"
    prompt_template = PromptTemplate(template_str)
    completed_prompt = prompt_template.assemble(name="John", day="Monday")
    print(completed_prompt)
    ```

### [Rouge](https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.evaluation.Rouge)

The ROUGE Metric.

Calculates the recall of n-grams in prediction as compared to reference and
returns a score ranging between 0 and 1. Supported rouge types are
rougen\[1-9\], rougeL, and rougeLsum.