|
| 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 | +import pytest |
| 18 | + |
| 19 | +from tests.unit.vertexai.genai.replays import pytest_helper |
| 20 | +from vertexai._genai import types |
| 21 | +from google.genai import types as genai_types |
| 22 | + |
| 23 | +pytest.importorskip( |
| 24 | + "google.adk", reason="google-adk not installed, skipping ADK agent tests" |
| 25 | +) |
| 26 | +from google.adk.agents import LlmAgent # noqa: E402 # pylint: disable=g-import-not-at-top,g-bad-import-order |
| 27 | + |
| 28 | + |
| 29 | +def test_inference_with_eval_cases_multi_turn_agent_data(client): |
| 30 | + """Tests run_inference with multi-turn agent_data in eval_cases. |
| 31 | +
|
| 32 | + Verifies that run_inference() accepts an EvaluationDataset with |
| 33 | + eval_cases containing agent_data (no eval_dataset_df). The agent_data |
| 34 | + has 2 turns: turn 0 is a completed user+agent exchange (history), |
| 35 | + turn 1 is a new user query. The agent should see the history and |
| 36 | + respond to the final query in context. |
| 37 | + """ |
| 38 | + agent = LlmAgent( |
| 39 | + name="test_agent", |
| 40 | + model="gemini-2.5-flash", |
| 41 | + instruction="You are a helpful assistant. Answer questions concisely.", |
| 42 | + ) |
| 43 | + |
| 44 | + eval_case = types.EvalCase( |
| 45 | + agent_data=types.evals.AgentData( |
| 46 | + turns=[ |
| 47 | + types.evals.ConversationTurn( |
| 48 | + turn_index=0, |
| 49 | + events=[ |
| 50 | + types.evals.AgentEvent( |
| 51 | + author="user", |
| 52 | + content=genai_types.Content( |
| 53 | + role="user", |
| 54 | + parts=[genai_types.Part(text="My name is Alice.")], |
| 55 | + ), |
| 56 | + ), |
| 57 | + types.evals.AgentEvent( |
| 58 | + author="test_agent", |
| 59 | + content=genai_types.Content( |
| 60 | + role="model", |
| 61 | + parts=[ |
| 62 | + genai_types.Part( |
| 63 | + text="Hello Alice! How can I help you?" |
| 64 | + ) |
| 65 | + ], |
| 66 | + ), |
| 67 | + ), |
| 68 | + ], |
| 69 | + ), |
| 70 | + types.evals.ConversationTurn( |
| 71 | + turn_index=1, |
| 72 | + events=[ |
| 73 | + types.evals.AgentEvent( |
| 74 | + author="user", |
| 75 | + content=genai_types.Content( |
| 76 | + role="user", |
| 77 | + parts=[genai_types.Part(text="What is my name?")], |
| 78 | + ), |
| 79 | + ), |
| 80 | + ], |
| 81 | + ), |
| 82 | + ], |
| 83 | + ), |
| 84 | + ) |
| 85 | + eval_dataset = types.EvaluationDataset(eval_cases=[eval_case]) |
| 86 | + |
| 87 | + inference_result = client.evals.run_inference( |
| 88 | + agent=agent, |
| 89 | + src=eval_dataset, |
| 90 | + ) |
| 91 | + assert isinstance(inference_result, types.EvaluationDataset) |
| 92 | + assert inference_result.eval_dataset_df is not None |
| 93 | + assert "agent_data" in inference_result.eval_dataset_df.columns |
| 94 | + |
| 95 | + |
| 96 | +def test_inference_with_eval_cases_agent_engine_agent_data(client): |
| 97 | + """Tests N+1 inference with agent_data via remote Agent Engine.""" |
| 98 | + agent_engine = client.agent_engines.get( |
| 99 | + name="projects/977012026409/locations/us-central1" |
| 100 | + "/reasoningEngines/7188347537655332864" |
| 101 | + ) |
| 102 | + |
| 103 | + eval_case = types.EvalCase( |
| 104 | + agent_data=types.evals.AgentData( |
| 105 | + turns=[ |
| 106 | + types.evals.ConversationTurn( |
| 107 | + turn_index=0, |
| 108 | + events=[ |
| 109 | + types.evals.AgentEvent( |
| 110 | + author="user", |
| 111 | + content=genai_types.Content( |
| 112 | + role="user", |
| 113 | + parts=[genai_types.Part(text="My name is Bob.")], |
| 114 | + ), |
| 115 | + ), |
| 116 | + types.evals.AgentEvent( |
| 117 | + author="model", |
| 118 | + content=genai_types.Content( |
| 119 | + role="model", |
| 120 | + parts=[ |
| 121 | + genai_types.Part(text="Hi Bob! Nice to meet you.") |
| 122 | + ], |
| 123 | + ), |
| 124 | + ), |
| 125 | + ], |
| 126 | + ), |
| 127 | + types.evals.ConversationTurn( |
| 128 | + turn_index=1, |
| 129 | + events=[ |
| 130 | + types.evals.AgentEvent( |
| 131 | + author="user", |
| 132 | + content=genai_types.Content( |
| 133 | + role="user", |
| 134 | + parts=[genai_types.Part(text="What is my name?")], |
| 135 | + ), |
| 136 | + ), |
| 137 | + ], |
| 138 | + ), |
| 139 | + ], |
| 140 | + ), |
| 141 | + ) |
| 142 | + eval_dataset = types.EvaluationDataset(eval_cases=[eval_case]) |
| 143 | + |
| 144 | + inference_result = client.evals.run_inference( |
| 145 | + agent=agent_engine, |
| 146 | + src=eval_dataset, |
| 147 | + ) |
| 148 | + assert isinstance(inference_result, types.EvaluationDataset) |
| 149 | + assert inference_result.eval_dataset_df is not None |
| 150 | + assert "agent_data" in inference_result.eval_dataset_df.columns |
| 151 | + |
| 152 | + |
| 153 | +pytestmark = pytest_helper.setup( |
| 154 | + file=__file__, |
| 155 | + globals_for_file=globals(), |
| 156 | + test_method="evals.run_inference", |
| 157 | +) |
0 commit comments