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 2aca9d4

Browse filesBrowse files
committed
added sample function wrapper
1 parent 4576441 commit 2aca9d4
Copy full SHA for 2aca9d4

File tree

1 file changed

+58
-0
lines changed
Filter options

1 file changed

+58
-0
lines changed
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Create a sample function to reuse code
3+
*/
4+
create or alter function dbo.SimilarContentArticles(@vector nvarchar(max))
5+
returns table
6+
as
7+
return with cteVector as
8+
(
9+
select
10+
cast([key] as int) as [vector_value_id],
11+
cast([value] as float) as [vector_value]
12+
from
13+
openjson(@vector)
14+
),
15+
cteSimilar as
16+
(
17+
select top (50)
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
25+
from
26+
cteVector v1
27+
inner join
28+
dbo.wikipedia_articles_embeddings_contents_vector v2 on v1.vector_value_id = v2.vector_value_id
29+
group by
30+
v2.article_id
31+
order by
32+
cosine_distance desc
33+
)
34+
select
35+
a.id,
36+
a.title,
37+
a.url,
38+
r.cosine_distance
39+
from
40+
cteSimilar r
41+
inner join
42+
dbo.wikipedia_articles_embeddings a on r.article_id = a.id
43+
go
44+
45+
/*
46+
Test the function
47+
*/
48+
declare @response nvarchar(max);
49+
declare @payload nvarchar(max) = json_object('input': 'the foundation series by isaac asimov');
50+
51+
exec sp_invoke_external_rest_endpoint
52+
@url = 'https:/<your-app-name>.openai.azure.com/openai/deployments/embeddings/embeddings?api-version=2023-03-15-preview',
53+
@credential = [https://<your-app-name>.openai.azure.com],
54+
@payload = @payload,
55+
@response = @response output;
56+
57+
select * from dbo.SimilarContentArticles(json_query(@response, '$.result.data[0].embedding')) as r order by cosine_distance desc
58+
go

0 commit comments

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