-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpython_udf_evaluation.sql
More file actions
160 lines (149 loc) · 4.43 KB
/
Copy pathpython_udf_evaluation.sql
File metadata and controls
160 lines (149 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
-- Copyright 2026 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.
-- Example: Session evaluation using Python UDF score kernels.
--
-- This example shows the SQL + UDF split pattern: SQL pre-aggregates
-- per-session metrics, then UDF score kernels compute 0.0-1.0 scores.
--
-- Prerequisites:
-- Register UDFs from deploy/python_udf/register.sql
--
-- Replace PROJECT, DATASET, and UDF_DATASET with your values.
-- ------------------------------------------------------------------ --
-- 1. Score all sessions on latency, error rate, and turn count --
-- ------------------------------------------------------------------ --
-- Note: COALESCE guards are required because Python UDFs receive NULL
-- as None, which fails numeric comparisons. The SDK evaluator path
-- catches these via exception handling (evaluators.py:211), but the
-- UDF kernels are pure functions with no such fallback.
WITH session_summary AS (
SELECT
session_id,
COALESCE(AVG(
CAST(
JSON_VALUE(latency_ms, '$.total_ms') AS FLOAT64
)
), 0.0) AS avg_latency_ms,
COUNTIF(event_type = 'TOOL_STARTING') AS tool_calls,
COUNTIF(event_type = 'TOOL_ERROR') AS tool_errors,
COUNTIF(event_type = 'USER_MESSAGE_RECEIVED') AS turn_count
FROM
`PROJECT.DATASET.agent_events`
GROUP BY
session_id
)
SELECT
session_id,
avg_latency_ms,
tool_calls,
tool_errors,
turn_count,
`PROJECT.UDF_DATASET.bqaa_score_latency`(
avg_latency_ms, 5000.0
) AS latency_score,
`PROJECT.UDF_DATASET.bqaa_score_error_rate`(
tool_calls, tool_errors, 0.1
) AS error_rate_score,
`PROJECT.UDF_DATASET.bqaa_score_turn_count`(
turn_count, 10
) AS turn_count_score
FROM
session_summary
ORDER BY
latency_score ASC
LIMIT 100;
-- ------------------------------------------------------------------ --
-- 2. Multi-metric pass/fail gate --
-- ------------------------------------------------------------------ --
-- Sessions pass only if ALL scores are above 0.5.
WITH session_summary AS (
SELECT
session_id,
COALESCE(AVG(
CAST(
JSON_VALUE(latency_ms, '$.total_ms') AS FLOAT64
)
), 0.0) AS avg_latency_ms,
COUNTIF(event_type = 'TOOL_STARTING') AS tool_calls,
COUNTIF(event_type = 'TOOL_ERROR') AS tool_errors,
COUNTIF(event_type = 'USER_MESSAGE_RECEIVED') AS turn_count
FROM
`PROJECT.DATASET.agent_events`
GROUP BY
session_id
),
scored AS (
SELECT
session_id,
`PROJECT.UDF_DATASET.bqaa_score_latency`(
avg_latency_ms, 5000.0
) AS latency_score,
`PROJECT.UDF_DATASET.bqaa_score_error_rate`(
tool_calls, tool_errors, 0.1
) AS error_rate_score,
`PROJECT.UDF_DATASET.bqaa_score_turn_count`(
turn_count, 10
) AS turn_count_score
FROM
session_summary
)
SELECT
session_id,
latency_score,
error_rate_score,
turn_count_score,
(latency_score >= 0.5
AND error_rate_score >= 0.5
AND turn_count_score >= 0.5) AS passed
FROM
scored
ORDER BY
passed, session_id;
-- ------------------------------------------------------------------ --
-- 3. Cost evaluation with custom pricing --
-- ------------------------------------------------------------------ --
WITH session_tokens AS (
SELECT
session_id,
SUM(COALESCE(
CAST(JSON_VALUE(
attributes, '$.usage_metadata.prompt_token_count'
) AS INT64), 0
)) AS input_tokens,
SUM(COALESCE(
CAST(JSON_VALUE(
attributes, '$.usage_metadata.candidates_token_count'
) AS INT64), 0
)) AS output_tokens
FROM
`PROJECT.DATASET.agent_events`
WHERE
event_type = 'LLM_RESPONSE'
GROUP BY
session_id
)
SELECT
session_id,
input_tokens,
output_tokens,
`PROJECT.UDF_DATASET.bqaa_score_cost`(
input_tokens, output_tokens,
-- max $2.00 per session, Gemini 2.5 Flash pricing
2.0, 0.00015, 0.0006
) AS cost_score
FROM
session_tokens
ORDER BY
cost_score ASC
LIMIT 50;