|
| 1 | +# (C) 2026 GoodData Corporation. All rights reserved. |
| 2 | +# SPDX-License-Identifier: LicenseRef-GoodData-Enterprise |
| 3 | +"""Agentic evaluation runner for gd-eval CLI — handles multi-turn agentic test kinds.""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import time |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +from gooddata_eval.core.agentic._langfuse import HttpxLangfuseClient, make_langfuse_client |
| 11 | +from gooddata_eval.core.models import CreatedVisualization, DatasetItem |
| 12 | +from gooddata_eval.core.runner import EvalReport, ItemReport |
| 13 | + |
| 14 | +AGENTIC_TEST_KINDS = frozenset({ |
| 15 | + "vis_agentic", # production: expected_output.visualization (single/multi CreatedVisualization) |
| 16 | + "agentic_visualization", # experimental: expected_output.expected_outputs (multi-candidate) |
| 17 | + "agentic_metric_skill", |
| 18 | + "agentic_alert_skill", |
| 19 | + "agentic_search", |
| 20 | + "agentic_general_question", |
| 21 | + "agentic_guardrail", |
| 22 | + "agentic_conversation", |
| 23 | +}) |
| 24 | + |
| 25 | + |
| 26 | +def _parse_visualization_expected(expected_output: Any) -> list[CreatedVisualization]: |
| 27 | + """Parse expected_output into a list of CreatedVisualization candidates. |
| 28 | +
|
| 29 | + Accepts: |
| 30 | + {"expected_outputs": [{"visualization": {...}}, ...]} <- agentic fixture format |
| 31 | + {"visualization": {...}} or {"visualization": [{...}]} <- single/multi candidate |
| 32 | + [{"visualization": {...}}, ...] <- bare list |
| 33 | + """ |
| 34 | + if isinstance(expected_output, dict): |
| 35 | + raw_list = expected_output.get("expected_outputs") |
| 36 | + if raw_list is not None: |
| 37 | + return [ |
| 38 | + CreatedVisualization.model_validate(v.get("visualization", v) if isinstance(v, dict) else v) |
| 39 | + for v in raw_list |
| 40 | + ] |
| 41 | + raw_viz = expected_output.get("visualization") |
| 42 | + if raw_viz is not None: |
| 43 | + if isinstance(raw_viz, list): |
| 44 | + return [CreatedVisualization.model_validate(v) for v in raw_viz] |
| 45 | + return [CreatedVisualization.model_validate(raw_viz)] |
| 46 | + if isinstance(expected_output, list): |
| 47 | + return [ |
| 48 | + CreatedVisualization.model_validate(v.get("visualization", v) if isinstance(v, dict) else v) |
| 49 | + for v in expected_output |
| 50 | + ] |
| 51 | + raise ValueError( |
| 52 | + f"Cannot parse agentic_visualization expected_output: {type(expected_output).__name__}. " |
| 53 | + 'Expected {"expected_outputs": [...]} or {"visualization": {...}}.' |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def _dispatch_agentic( |
| 58 | + item: DatasetItem, |
| 59 | + host: str, |
| 60 | + token: str, |
| 61 | + workspace_id: str, |
| 62 | + k: int, |
| 63 | + langfuse: Any, |
| 64 | + run_ts: str, |
| 65 | + model_version_override: str | None, |
| 66 | +) -> None: |
| 67 | + """Call the appropriate evaluate_agentic_* function for the item's test_kind.""" |
| 68 | + kind = item.test_kind |
| 69 | + eo = item.expected_output |
| 70 | + lf_kw = dict( |
| 71 | + langfuse=langfuse, |
| 72 | + dataset_item_id=item.id, |
| 73 | + dataset_name=item.dataset_name, |
| 74 | + run_timestamp=run_ts, |
| 75 | + model_version_override=model_version_override, |
| 76 | + ) |
| 77 | + |
| 78 | + if kind in ("vis_agentic", "agentic_visualization"): |
| 79 | + from gooddata_eval.core.agentic.visualization import evaluate_agentic_visualization # noqa: PLC0415 |
| 80 | + evaluate_agentic_visualization( |
| 81 | + host=host, token=token, workspace_id=workspace_id, |
| 82 | + question=item.question, |
| 83 | + expected_outputs=_parse_visualization_expected(eo), |
| 84 | + k=k, |
| 85 | + **lf_kw, |
| 86 | + ) |
| 87 | + elif kind == "agentic_metric_skill": |
| 88 | + from gooddata_eval.core.agentic.metric_skill import evaluate_agentic_metric_skill # noqa: PLC0415 |
| 89 | + evaluate_agentic_metric_skill( |
| 90 | + host=host, token=token, workspace_id=workspace_id, |
| 91 | + question=item.question, |
| 92 | + expected_output=eo if isinstance(eo, dict) else {}, |
| 93 | + k=k, |
| 94 | + **lf_kw, |
| 95 | + ) |
| 96 | + elif kind == "agentic_alert_skill": |
| 97 | + from gooddata_eval.core.agentic.alert_skill import evaluate_agentic_alert_skill # noqa: PLC0415 |
| 98 | + evaluate_agentic_alert_skill( |
| 99 | + host=host, token=token, workspace_id=workspace_id, |
| 100 | + question=item.question, |
| 101 | + expected_output=eo if isinstance(eo, dict) else {}, |
| 102 | + k=k, |
| 103 | + **lf_kw, |
| 104 | + ) |
| 105 | + elif kind == "agentic_search": |
| 106 | + from gooddata_eval.core.agentic.search_tool import evaluate_agentic_search_tool # noqa: PLC0415 |
| 107 | + eo_dict = eo if isinstance(eo, dict) else {} |
| 108 | + tool_call = eo_dict.get("tool_call", {}) |
| 109 | + expected_args = tool_call.get("function_arguments", eo_dict) |
| 110 | + evaluate_agentic_search_tool( |
| 111 | + host=host, token=token, workspace_id=workspace_id, |
| 112 | + question=item.question, |
| 113 | + expected_tool_call=expected_args, |
| 114 | + k=k, |
| 115 | + **lf_kw, |
| 116 | + ) |
| 117 | + elif kind == "agentic_general_question": |
| 118 | + from gooddata_eval.core.agentic.general_question import evaluate_agentic_general_question # noqa: PLC0415 |
| 119 | + evaluate_agentic_general_question( |
| 120 | + host=host, token=token, workspace_id=workspace_id, |
| 121 | + question=item.question, |
| 122 | + expected_output=eo if isinstance(eo, str) else str(eo), |
| 123 | + k=k, |
| 124 | + **lf_kw, |
| 125 | + ) |
| 126 | + elif kind == "agentic_guardrail": |
| 127 | + from gooddata_eval.core.agentic.guardrail import evaluate_agentic_guardrail # noqa: PLC0415 |
| 128 | + evaluate_agentic_guardrail( |
| 129 | + host=host, token=token, workspace_id=workspace_id, |
| 130 | + question=item.question, |
| 131 | + expected_output=eo if isinstance(eo, str) else str(eo), |
| 132 | + k=k, |
| 133 | + **lf_kw, |
| 134 | + ) |
| 135 | + elif kind == "agentic_conversation": |
| 136 | + from gooddata_eval.core.agentic.conversation import ConversationFixture, evaluate_agentic_conversation # noqa: PLC0415 |
| 137 | + fixture_data = eo.get("fixture") or eo if isinstance(eo, dict) else {} |
| 138 | + evaluate_agentic_conversation( |
| 139 | + host=host, token=token, workspace_id=workspace_id, |
| 140 | + fixture=ConversationFixture.model_validate(fixture_data), |
| 141 | + **lf_kw, |
| 142 | + ) |
| 143 | + else: |
| 144 | + raise ValueError(f"Unknown agentic test kind: {kind!r}") |
| 145 | + |
| 146 | + |
| 147 | +def run_agentic_items( |
| 148 | + items: list[DatasetItem], |
| 149 | + host: str, |
| 150 | + token: str, |
| 151 | + workspace_id: str, |
| 152 | + *, |
| 153 | + k: int = 2, |
| 154 | + model_version: str | None = None, |
| 155 | + use_langfuse: bool = False, |
| 156 | + run_ts: str, |
| 157 | + on_item_start: Any = None, |
| 158 | + on_item_done: Any = None, |
| 159 | +) -> EvalReport: |
| 160 | + """Run agentic items through evaluate_agentic_* and return an EvalReport.""" |
| 161 | + langfuse = make_langfuse_client() if use_langfuse else None |
| 162 | + |
| 163 | + report = EvalReport(model=model_version) |
| 164 | + total = len(items) |
| 165 | + |
| 166 | + for index, item in enumerate(items, start=1): |
| 167 | + if on_item_start is not None: |
| 168 | + try: |
| 169 | + on_item_start(index, total, item) |
| 170 | + except Exception: |
| 171 | + pass |
| 172 | + |
| 173 | + item_report = ItemReport( |
| 174 | + id=item.id, |
| 175 | + dataset_name=item.dataset_name, |
| 176 | + test_kind=item.test_kind, |
| 177 | + question=item.question, |
| 178 | + ) |
| 179 | + t0 = time.perf_counter() |
| 180 | + try: |
| 181 | + _dispatch_agentic(item, host, token, workspace_id, k, langfuse, run_ts, model_version) |
| 182 | + item_report.pass_at_k = True |
| 183 | + item_report.runs = k |
| 184 | + except AssertionError as exc: |
| 185 | + item_report.pass_at_k = False |
| 186 | + item_report.runs = k |
| 187 | + print(f"[agentic] {item.id} FAIL: {exc}", flush=True) |
| 188 | + except Exception as exc: |
| 189 | + item_report.error = f"{type(exc).__name__}: {exc}" |
| 190 | + item_report.runs = 0 |
| 191 | + finally: |
| 192 | + item_report.latency_s = time.perf_counter() - t0 |
| 193 | + |
| 194 | + if on_item_done is not None: |
| 195 | + try: |
| 196 | + on_item_done(index, total, item_report) |
| 197 | + except Exception: |
| 198 | + pass |
| 199 | + |
| 200 | + report.items.append(item_report) |
| 201 | + |
| 202 | + if langfuse is not None: |
| 203 | + try: |
| 204 | + langfuse.flush() |
| 205 | + langfuse.close() |
| 206 | + except Exception: |
| 207 | + pass |
| 208 | + |
| 209 | + return report |
0 commit comments