8000 added sample function wrapper · sirmnemonic/azure-sql-db-openai@2aca9d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2aca9d4

Browse files
committed
added sample function wrapper
1 parent 4576441 commit 2aca9d4

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines 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 8000 +
*
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