-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathai_similarity_validation.sql
More file actions
142 lines (138 loc) · 4.23 KB
/
Copy pathai_similarity_validation.sql
File metadata and controls
142 lines (138 loc) · 4.23 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
-- 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.
-- ai_similarity_validation.sql
--
-- Side-by-side validation: AI.EMBED + ML.DISTANCE vs AI.SIMILARITY
--
-- AI.SIMILARITY provides a convenient text-to-text similarity score, but
-- re-embeds both inputs on every call. In a cross join of N ground-truth
-- rows × M predicted rows this means O(N×M) embedding calls, compared to
-- O(N+M) when using AI.EMBED once per text and ML.DISTANCE on the
-- pre-computed vectors.
--
-- Use AI.SIMILARITY for:
-- • One-off pair comparisons or ad-hoc investigation
-- • Very small datasets where embedding cost is negligible
--
-- Use AI.EMBED + ML.DISTANCE for:
-- • Production drift workloads (the SDK's default)
-- • Any cross-join or large-batch scenario
--
-- Note: results are directionally similar but not guaranteed to be
-- numerically identical across implementations or model revisions.
--
-- Replace {project}, {dataset}, {endpoint}, and {table} with your values.
-- {endpoint} is the embedding model endpoint, e.g. 'text-embedding-005'.
-- 1. Current approach — AI.EMBED both sides, ML.DISTANCE on vectors
WITH ground_truth_embedded AS (
SELECT
session_id,
question,
AI.EMBED(
question,
endpoint => '{endpoint}'
) AS embedding
FROM `{project}.{dataset}.{table}_ground_truth`
),
predicted_embedded AS (
SELECT
session_id,
question,
AI.EMBED(
question,
endpoint => '{endpoint}'
) AS embedding
FROM `{project}.{dataset}.{table}_predicted`
),
embed_distance AS (
SELECT
g.session_id AS gt_session_id,
p.session_id AS pred_session_id,
ML.DISTANCE(g.embedding, p.embedding, 'COSINE') AS distance
FROM ground_truth_embedded g
CROSS JOIN predicted_embedded p
)
SELECT
gt_session_id,
pred_session_id,
distance,
1 - distance AS similarity_equiv
FROM embed_distance
ORDER BY gt_session_id, pred_session_id;
-- 2. AI.SIMILARITY approach — direct text-to-text comparison
SELECT
g.session_id AS gt_session_id,
p.session_id AS pred_session_id,
AI.SIMILARITY(
content1 => g.question,
content2 => p.question,
endpoint => '{endpoint}'
) AS similarity
FROM
`{project}.{dataset}.{table}_ground_truth` g
CROSS JOIN
`{project}.{dataset}.{table}_predicted` p
ORDER BY gt_session_id, pred_session_id;
-- 3. Agreement check — join both results, compute correlation & max diff
WITH ground_truth_embedded AS (
SELECT
session_id,
question,
AI.EMBED(
question,
endpoint => '{endpoint}'
) AS embedding
FROM `{project}.{dataset}.{table}_ground_truth`
),
predicted_embedded AS (
SELECT
session_id,
question,
AI.EMBED(
question,
endpoint => '{endpoint}'
) AS embedding
FROM `{project}.{dataset}.{table}_predicted`
),
embed_results AS (
SELECT
g.session_id AS gt_session_id,
p.session_id AS pred_session_id,
1 - ML.DISTANCE(g.embedding, p.embedding, 'COSINE') AS similarity_from_embed
FROM ground_truth_embedded g
CROSS JOIN predicted_embedded p
),
ai_sim_results AS (
SELECT
g.session_id AS gt_session_id,
p.session_id AS pred_session_id,
AI.SIMILARITY(
content1 => g.question,
content2 => p.question,
endpoint => '{endpoint}'
) AS similarity_from_ai
FROM
`{project}.{dataset}.{table}_ground_truth` g
CROSS JOIN
`{project}.{dataset}.{table}_predicted` p
)
SELECT
CORR(e.similarity_from_embed, a.similarity_from_ai) AS pearson_correlation,
MAX(ABS(e.similarity_from_embed - a.similarity_from_ai)) AS max_abs_difference,
AVG(ABS(e.similarity_from_embed - a.similarity_from_ai)) AS avg_abs_difference,
COUNT(*) AS num_pairs
FROM embed_results e
JOIN ai_sim_results a
ON e.gt_session_id = a.gt_session_id
AND e.pred_session_id = a.pred_session_id;