feat: ADK cli allows developers to create an eval set and add an eval case

Agent developers can now create an eval set and add eval cases through command line itself. Adding an eval case is limited only to specifying conversation scenarios.

Sample comamnds:
- Create an eval set:
adk eval_set create \
    contributing/samples/hello_world \
    set_01

- Add an eval case with scenario file
Content of scenarios.json file:
'{"scenarios": [{"starting_prompt": "hello", "conversation_plan": "world"}]}'

adk eval_set add_eval_case \
    contributing/samples/hello_world \
    set_01 \
    --scenarios scenarios.json

PiperOrigin-RevId: 817456117
This commit is contained in:
Ankur Sharma
2025-10-09 20:31:01 -07:00
committed by Copybara-Service
parent 9939e0b087
commit ae139bb461
4 changed files with 400 additions and 9 deletions
@@ -0,0 +1,51 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests for utilities in cli_eval."""
from __future__ import annotations
from types import SimpleNamespace
from unittest import mock
def test_get_eval_sets_manager_local(monkeypatch):
mock_local_manager = mock.MagicMock()
monkeypatch.setattr(
"google.adk.evaluation.local_eval_sets_manager.LocalEvalSetsManager",
lambda *a, **k: mock_local_manager,
)
from google.adk.cli.cli_eval import get_eval_sets_manager
manager = get_eval_sets_manager(eval_storage_uri=None, agents_dir="some/dir")
assert manager == mock_local_manager
def test_get_eval_sets_manager_gcs(monkeypatch):
mock_gcs_manager = mock.MagicMock()
mock_create_gcs = mock.MagicMock()
mock_create_gcs.return_value = SimpleNamespace(
eval_sets_manager=mock_gcs_manager
)
monkeypatch.setattr(
"google.adk.cli.utils.evals.create_gcs_eval_managers_from_uri",
mock_create_gcs,
)
from google.adk.cli.cli_eval import get_eval_sets_manager
manager = get_eval_sets_manager(
eval_storage_uri="gs://bucket", agents_dir="some/dir"
)
assert manager == mock_gcs_manager
mock_create_gcs.assert_called_once_with("gs://bucket")
@@ -18,6 +18,7 @@
from __future__ import annotations
import builtins
import json
from pathlib import Path
from types import SimpleNamespace
from typing import Any
@@ -620,6 +621,177 @@ def test_cli_eval_with_eval_set_id(
assert len(eval_set_results) == 2
def test_cli_create_eval_set(tmp_path: Path):
app_name = "test_app"
eval_set_id = "test_eval_set"
agent_path = tmp_path / app_name
agent_path.mkdir()
(agent_path / "__init__.py").touch()
runner = CliRunner()
result = runner.invoke(
cli_tools_click.main,
["eval_set", "create", str(agent_path), eval_set_id],
)
assert result.exit_code == 0
eval_set_file = agent_path / f"{eval_set_id}.evalset.json"
assert eval_set_file.exists()
with open(eval_set_file, "r") as f:
eval_set_data = json.load(f)
assert eval_set_data["eval_set_id"] == eval_set_id
assert eval_set_data["eval_cases"] == []
def test_cli_add_eval_case_no_session(tmp_path: Path):
app_name = "test_app_add_1"
eval_set_id = "test_eval_set_add_1"
agent_path = tmp_path / app_name
agent_path.mkdir()
(agent_path / "__init__.py").touch()
scenarios_file = tmp_path / "scenarios1.json"
scenarios_file.write_text(
'{"scenarios": [{"starting_prompt": "hello", "conversation_plan":'
' "world"}]}'
)
runner = CliRunner()
runner.invoke(
cli_tools_click.main,
["eval_set", "create", str(agent_path), eval_set_id],
catch_exceptions=False,
)
result = runner.invoke(
cli_tools_click.main,
[
"eval_set",
"add_eval_case",
str(agent_path),
eval_set_id,
"--scenarios_file",
str(scenarios_file),
],
catch_exceptions=False,
)
assert result.exit_code == 0
eval_set_file = agent_path / f"{eval_set_id}.evalset.json"
assert eval_set_file.exists()
with open(eval_set_file, "r") as f:
eval_set_data = json.load(f)
assert len(eval_set_data["eval_cases"]) == 1
eval_case = eval_set_data["eval_cases"][0]
assert eval_case["eval_id"] == "0a1a5048"
assert eval_case["conversation_scenario"]["starting_prompt"] == "hello"
assert "session_input" not in eval_case
def test_cli_add_eval_case_with_session(tmp_path: Path):
app_name = "test_app_add_2"
eval_set_id = "test_eval_set_add_2"
agent_path = tmp_path / app_name
agent_path.mkdir()
(agent_path / "__init__.py").touch()
scenarios_file = tmp_path / "scenarios2.json"
scenarios_file.write_text(
'{"scenarios": [{"starting_prompt": "hello", "conversation_plan":'
' "world"}]}'
)
session_file = tmp_path / "session2.json"
session_file.write_text(
'{"app_name": "test_app_add_2", "user_id": "test_user", "state": {}}'
)
runner = CliRunner()
runner.invoke(
cli_tools_click.main,
["eval_set", "create", str(agent_path), eval_set_id],
catch_exceptions=False,
)
result = runner.invoke(
cli_tools_click.main,
[
"eval_set",
"add_eval_case",
str(agent_path),
eval_set_id,
"--scenarios_file",
str(scenarios_file),
"--session_input_file",
str(session_file),
],
catch_exceptions=False,
)
assert result.exit_code == 0
eval_set_file = agent_path / f"{eval_set_id}.evalset.json"
assert eval_set_file.exists()
with open(eval_set_file, "r") as f:
eval_set_data = json.load(f)
assert len(eval_set_data["eval_cases"]) == 1
eval_case = eval_set_data["eval_cases"][0]
assert eval_case["eval_id"] == "0a1a5048"
assert eval_case["session_input"]["app_name"] == "test_app_add_2"
def test_cli_add_eval_case_skip_existing(tmp_path: Path):
app_name = "test_app_add_3"
eval_set_id = "test_eval_set_add_3"
agent_path = tmp_path / app_name
agent_path.mkdir()
(agent_path / "__init__.py").touch()
scenarios_file = tmp_path / "scenarios3.json"
scenarios_file.write_text(
'{"scenarios": [{"starting_prompt": "hello", "conversation_plan":'
' "world"}]}'
)
runner = CliRunner()
runner.invoke(
cli_tools_click.main,
["eval_set", "create", str(agent_path), eval_set_id],
catch_exceptions=False,
)
result1 = runner.invoke(
cli_tools_click.main,
[
"eval_set",
"add_eval_case",
str(agent_path),
eval_set_id,
"--scenarios_file",
str(scenarios_file),
],
catch_exceptions=False,
)
eval_set_file = agent_path / f"{eval_set_id}.evalset.json"
with open(eval_set_file, "r") as f:
eval_set_data1 = json.load(f)
result2 = runner.invoke(
cli_tools_click.main,
[
"eval_set",
"add_eval_case",
str(agent_path),
eval_set_id,
"--scenarios_file",
str(scenarios_file),
],
catch_exceptions=False,
)
with open(eval_set_file, "r") as f:
eval_set_data2 = json.load(f)
assert result1.exit_code == 0
assert result2.exit_code == 0
assert len(eval_set_data1["eval_cases"]) == 1
assert len(eval_set_data2["eval_cases"]) == 1
def test_cli_deploy_cloud_run_gcloud_arg_conflict(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None: