Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ebe769f

Browse filesBrowse files
committed
added optimizations
1 parent bbb6065 commit ebe769f
Copy full SHA for ebe769f

File tree

2 files changed

+34
-6
lines changed
Filter options

2 files changed

+34
-6
lines changed

‎vector-embeddings/03-find-similar-articles.sql

Copy file name to clipboardExpand all lines: vector-embeddings/03-find-similar-articles.sql
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,39 @@ group by
6565
order by
6666
cosine_distance desc;
6767

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 on r.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+
drop table if exists #results;
87+
select top(50)
88+
v2.article_id,
89+
sum(v1.[vector_value] * v2.[vector_value]) as cosine_distance
90+
into
91+
#results
92+
from
93+
#t v1
94+
inner join
95+
dbo.wikipedia_articles_embeddings_contents_vector v2 on v1.vector_value_id = v2.vector_value_id
96+
group by
97+
v2.article_id
98+
order by
99+
cosine_distance desc;
100+
68101
select
69102
a.id,
70103
a.title,

‎vector-embeddings/04-sample-function.sql

Copy file name to clipboardExpand all lines: vector-embeddings/04-sample-function.sql
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ cteSimilar as
1616
(
1717
select top (50)
1818
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
2520
from
2621
cteVector v1
2722
inner join

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.