1818from tests .unit .vertexai .genai .replays import pytest_helper
1919from vertexai ._genai import types
2020import pandas as pd
21+ import json
2122
2223
2324def test_bleu_metric (client ):
@@ -34,6 +35,78 @@ def test_bleu_metric(client):
3435 assert len (response .bleu_results .bleu_metric_values ) == 1
3536
3637
38+ def test_exact_match_metric (client ):
39+ """Tests the _evaluate_instances method with ExactMatchInput."""
40+ test_exact_match_input = types .ExactMatchInput (
41+ instances = [
42+ types .ExactMatchInstance (
43+ prediction = "The quick brown fox jumps over the lazy dog." ,
44+ reference = "The quick brown fox jumps over the lazy dog." ,
45+ )
46+ ],
47+ metric_spec = types .ExactMatchSpec (),
48+ )
49+ response = client .evals ._evaluate_instances (
50+ exact_match_input = test_exact_match_input
51+ )
52+ assert len (response .exact_match_results .exact_match_metric_values ) == 1
53+
54+
55+ def test_rouge_metric (client ):
56+ """Tests the _evaluate_instances method with RougeInput."""
57+ test_rouge_input = types .RougeInput (
58+ instances = [
59+ types .RougeInstance (
60+ prediction = "A fast brown fox leaps over a lazy dog." ,
61+ reference = "The quick brown fox jumps over the lazy dog." ,
62+ )
63+ ],
64+ metric_spec = types .RougeSpec (rouge_type = "rougeL" ),
65+ )
66+ response = client .evals ._evaluate_instances (rouge_input = test_rouge_input )
67+ assert len (response .rouge_results .rouge_metric_values ) == 1
68+
69+
70+ def test_pointwise_metric (client ):
71+ """Tests the _evaluate_instances method with PointwiseMetricInput."""
72+ instance_dict = {"prompt" : "What is the capital of France?" , "response" : "Paris" }
73+ json_instance = json .dumps (instance_dict )
74+
75+ test_input = types .PointwiseMetricInput (
76+ instance = types .PointwiseMetricInstance (json_instance = json_instance ),
77+ metric_spec = types .PointwiseMetricSpec (
78+ metric_prompt_template = "Evaluate if the response '{response}' correctly answers the prompt '{prompt}'."
79+ ),
80+ )
81+ response = client .evals ._evaluate_instances (pointwise_metric_input = test_input )
82+ assert response .pointwise_metric_result is not None
83+ assert response .pointwise_metric_result .score is not None
84+
85+
86+ def test_pairwise_metric_with_autorater (client ):
87+ """Tests the _evaluate_instances method with PairwiseMetricInput and AutoraterConfig."""
88+
89+ instance_dict = {
90+ "baseline_response" : "Short summary." ,
91+ "candidate_response" : "A longer, more detailed summary." ,
92+ }
93+ json_instance = json .dumps (instance_dict )
94+
95+ test_input = types .PairwiseMetricInput (
96+ instance = types .PairwiseMetricInstance (json_instance = json_instance ),
97+ metric_spec = types .PairwiseMetricSpec (
98+ metric_prompt_template = "Which response is a better summary? Baseline: '{baseline_response}' or Candidate: '{candidate_response}'"
99+ ),
100+ )
101+ autorater_config = types .AutoraterConfig (sampling_count = 2 )
102+
103+ response = client .evals ._evaluate_instances (
104+ pairwise_metric_input = test_input , autorater_config = autorater_config
105+ )
106+ assert response .pairwise_metric_result is not None
107+ assert response .pairwise_metric_result .pairwise_choice is not None
108+
109+
37110def test_run_inference_with_string_model (client ):
38111 test_df = pd .DataFrame ({"prompt" : ["test prompt" ]})
39112
0 commit comments