You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vector-embeddings/03-find-similar-articles.sql
+33Lines changed: 33 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -65,6 +65,39 @@ group by
65
65
order by
66
66
cosine_distance desc;
67
67
68
+
select
69
+
a.id,
70
+
a.title,
71
+
a.url,
72
+
r.cosine_distance
73
+
from
74
+
#results r
75
+
inner join
76
+
dbo.wikipedia_articles_embeddings a onr.article_id=a.id
77
+
order by
78
+
cosine_distance desc;
79
+
go
80
+
81
+
82
+
/*
83
+
Optimization: since vectors are normalized (as per OpenAI documentation: https://platform.openai.com/docs/guides/embeddings/which-distance-function-should-i-use),
84
+
we can simplify the cosine distance calculation by removing magnitude calculation
85
+
*/
86
+
droptableifexists #results;
87
+
selecttop(50)
88
+
v2.article_id,
89
+
sum(v1.[vector_value] * v2.[vector_value]) as cosine_distance
Copy file name to clipboardExpand all lines: vector-embeddings/04-sample-function.sql
+1-6Lines changed: 1 addition & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -16,12 +16,7 @@ cteSimilar as
16
16
(
17
17
selecttop (50)
18
18
v2.article_id,
19
-
sum(v1.[vector_value] * v2.[vector_value]) /
20
-
(
21
-
sqrt(sum(v1.[vector_value] * v1.[vector_value]))
22
-
*
23
-
sqrt(sum(v2.[vector_value] * v2.[vector_value]))
24
-
) as cosine_distance
19
+
sum(v1.[vector_value] * v2.[vector_value]) as cosine_distance -- Optimized as per https://platform.openai.com/docs/guides/embeddings/which-distance-function-should-i-use
0 commit comments