|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# pylint: disable=protected-access,bad-continuation,missing-function-docstring |
| 16 | + |
| 17 | +from tests.unit.vertexai.genai.replays import pytest_helper |
| 18 | +from vertexai import types |
| 19 | +import datetime |
| 20 | +import pytest |
| 21 | + |
| 22 | + |
| 23 | +def test_get_eval_item_response(client): |
| 24 | + """Tests that get_evaluation_item() returns a correctly structured EvaluationItem.""" |
| 25 | + evaluation_item_name = "projects/503583131166/locations/us-central1/evaluationItems/1486082323915997184" |
| 26 | + evaluation_item = client.evals.get_evaluation_item(name=evaluation_item_name) |
| 27 | + assert isinstance(evaluation_item, types.EvaluationItem) |
| 28 | + check_item_1486082323915997184(evaluation_item, evaluation_item_name) |
| 29 | + |
| 30 | + |
| 31 | +def test_get_eval_item_request(client): |
| 32 | + """Tests that get_evaluation_item() returns a correctly structured EvaluationItem with request.""" |
| 33 | + evaluation_item_name = "projects/503583131166/locations/us-central1/evaluationItems/4813679498589372416" |
| 34 | + evaluation_item = client.evals.get_evaluation_item(name=evaluation_item_name) |
| 35 | + assert isinstance(evaluation_item, types.EvaluationItem) |
| 36 | + check_item_4813679498589372416(evaluation_item, evaluation_item_name) |
| 37 | + |
| 38 | + |
| 39 | +pytest_plugins = ("pytest_asyncio",) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.asyncio |
| 43 | +async def test_get_eval_item_response_async(client): |
| 44 | + """Tests that get_evaluation_item() returns a correctly structured EvaluationItem.""" |
| 45 | + eval_item_id = "1486082323915997184" |
| 46 | + evaluation_item_name = ( |
| 47 | + f"projects/503583131166/locations/us-central1/evaluationItems/{eval_item_id}" |
| 48 | + ) |
| 49 | + evaluation_item = await client.aio.evals.get_evaluation_item(name=eval_item_id) |
| 50 | + check_item_1486082323915997184(evaluation_item, evaluation_item_name) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.asyncio |
| 54 | +async def test_get_eval_item_request_async(client): |
| 55 | + """Tests that get_evaluation_item() returns a correctly structured EvaluationItem with request.""" |
| 56 | + eval_item_id = "4813679498589372416" |
| 57 | + evaluation_item_name = ( |
| 58 | + f"projects/503583131166/locations/us-central1/evaluationItems/{eval_item_id}" |
| 59 | + ) |
| 60 | + evaluation_item = await client.aio.evals.get_evaluation_item(name=eval_item_id) |
| 61 | + check_item_4813679498589372416(evaluation_item, evaluation_item_name) |
| 62 | + |
| 63 | + |
| 64 | +def check_item_1486082323915997184( |
| 65 | + evaluation_item: types.EvaluationItem, evaluation_item_name: str |
| 66 | +): |
| 67 | + assert evaluation_item.name == evaluation_item_name |
| 68 | + assert evaluation_item.display_name == "universal result for 7119522507803066368" |
| 69 | + assert evaluation_item.evaluation_item_type == types.EvaluationItemType.RESULT |
| 70 | + assert ( |
| 71 | + evaluation_item.gcs_uri |
| 72 | + == "gs://lakeyk-limited-bucket/agora_eval_080525/result_1486082323915997184.json" |
| 73 | + ) |
| 74 | + assert evaluation_item.create_time == datetime.datetime( |
| 75 | + 2025, 9, 8, 20, 55, 46, 713792, tzinfo=datetime.timezone.utc |
| 76 | + ) |
| 77 | + assert isinstance(evaluation_item.evaluation_response, types.EvaluationItemResult) |
| 78 | + assert ( |
| 79 | + evaluation_item.evaluation_response.evaluation_request |
| 80 | + == "projects/503583131166/locations/us-central1/evaluationItems/7119522507803066368" |
| 81 | + ) |
| 82 | + assert ( |
| 83 | + evaluation_item.evaluation_response.evaluation_run |
| 84 | + == "projects/503583131166/locations/us-central1/evaluationRuns/1957799200510967808" |
| 85 | + ) |
| 86 | + # Check the first candidate result. |
| 87 | + candidate_result = evaluation_item.evaluation_response.candidate_results[0] |
| 88 | + assert candidate_result.candidate == "gemini-2.0-flash-001@default" |
| 89 | + assert candidate_result.metric == "universal" |
| 90 | + assert candidate_result.score == 0.2857143 |
| 91 | + # Check the first rubric verdict. |
| 92 | + rubric_verdict = candidate_result.rubric_verdicts[0] |
| 93 | + assert rubric_verdict.verdict |
| 94 | + assert ( |
| 95 | + rubric_verdict.reasoning |
| 96 | + == "The entire response is written in the English language." |
| 97 | + ) |
| 98 | + assert rubric_verdict.evaluated_rubric.type == "LANGUAGE:PRIMARY_RESPONSE_LANGUAGE" |
| 99 | + assert rubric_verdict.evaluated_rubric.importance == "HIGH" |
| 100 | + assert ( |
| 101 | + rubric_verdict.evaluated_rubric.content.property.description |
| 102 | + == "The response is in English." |
| 103 | + ) |
| 104 | + # Check the request. |
| 105 | + request = evaluation_item.evaluation_response.request |
| 106 | + assert ( |
| 107 | + "There is a wide range of potato varieties to choose from" |
| 108 | + in request.prompt.text |
| 109 | + ) |
| 110 | + assert request.candidate_responses[0].candidate == "gemini-2.0-flash-001@default" |
| 111 | + assert "Pick out your potato variety" in request.candidate_responses[0].text |
| 112 | + |
| 113 | + |
| 114 | +def check_item_4813679498589372416( |
| 115 | + evaluation_item: types.EvaluationItem, evaluation_item_name: str |
| 116 | +): |
| 117 | + assert evaluation_item.name == evaluation_item_name |
| 118 | + assert evaluation_item.display_name == "4813679498589372416" |
| 119 | + assert evaluation_item.evaluation_item_type == types.EvaluationItemType.REQUEST |
| 120 | + assert ( |
| 121 | + evaluation_item.gcs_uri |
| 122 | + == "gs://lakeyk-limited-bucket/agora_eval_080525/request_4813679498589372416.json" |
| 123 | + ) |
| 124 | + assert evaluation_item.create_time == datetime.datetime( |
| 125 | + 2025, 9, 8, 20, 55, 46, 338353, tzinfo=datetime.timezone.utc |
| 126 | + ) |
| 127 | + assert isinstance(evaluation_item.evaluation_request, types.EvaluationItemRequest) |
| 128 | + # Check the request. |
| 129 | + request = evaluation_item.evaluation_request |
| 130 | + assert ( |
| 131 | + "If your ball is curving during flight from left to right" |
| 132 | + in request.prompt.text |
| 133 | + ) |
| 134 | + # Check the first candidate response. |
| 135 | + assert request.candidate_responses[0].candidate == "gemini-2.0-flash-001@default" |
| 136 | + assert ( |
| 137 | + "Keep your knees bent during the backswing" |
| 138 | + in request.candidate_responses[0].text |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +pytestmark = pytest_helper.setup( |
| 143 | + file=__file__, |
| 144 | + globals_for_file=globals(), |
| 145 | + test_method="evals.get_evaluation_item", |
| 146 | +) |
0 commit comments