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 be94333

Browse filesBrowse files
committed
added FT hybrid search sample
1 parent 0537d73 commit be94333
Copy full SHA for be94333

File tree

3 files changed

+94
-1
lines changed
Filter options

3 files changed

+94
-1
lines changed

‎vector-embeddings/04-create-get-embeddings-procedure.sql

Copy file name to clipboardExpand all lines: vector-embeddings/04-create-get-embeddings-procedure.sql
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ create or alter procedure dbo.get_embedding
88
as
99
declare @retval int, @response nvarchar(max);
1010
declare @payload nvarchar(max) = json_object('input': @inputText);
11-
declare @url nvarchar(1000) = 'https://<your-api-name>.openai.azure.com/openai/deployments/' + @deployedModelName + '/embeddings?api-version=2023-03-15-preview'
11+
declare @url nvarchar(1000) = 'https://<your-api-name>.openai.azure.com/openai/deployments/' + @deployedModelName + '/embeddings?api-version=2024-08-01-preview'
1212
exec @retval = sp_invoke_external_rest_endpoint
1313
@url = @url,
1414
@method = 'POST',
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
if not exists(select * from sys.fulltext_catalogs where [name] = 'FullTextCatalog')
2+
begin
3+
create fulltext catalog [FullTextCatalog] as default;
4+
end
5+
go
6+
7+
create fulltext index on dbo.wikipedia_articles_embeddings ([text]) key index pk__wikipedia_articles_embeddings;
8+
go
9+
10+
alter fulltext index on dbo.wikipedia_articles_embeddings enable;
11+
go
12+
13+
select * from sys.fulltext_catalogs
14+
go
+79Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Get the embeddings for the input text by calling the OpenAI API
3+
and then search the most similar articles (by title)
4+
Note: <deployment-id> needs to be replaced with the deployment name of your embedding model in Azure OpenAI
5+
*/
6+
7+
DECLARE @q NVARCHAR(1000) = 'the foundation series by isaac asimov';
8+
DECLARE @k INT = 10
9+
10+
DECLARE @r INT, @e VECTOR(1536);
11+
12+
EXEC @r = dbo.get_embedding '<deployment-id>', @q, @e OUTPUT;
13+
IF (@r != 0) SELECT @r;
14+
15+
WITH keyword_search AS (
16+
SELECT TOP(@k)
17+
id,
18+
RANK() OVER (ORDER BY ft_rank DESC) AS rank,
19+
title,
20+
[text]
21+
FROM
22+
(
23+
SELECT TOP(@k)
24+
id,
25+
ftt.[RANK] AS ft_rank,
26+
title,
27+
[text]
28+
FROM
29+
dbo.wikipedia_articles_embeddings w
30+
INNER JOIN
31+
FREETEXTTABLE(dbo.wikipedia_articles_embeddings, *, @q) AS ftt ON w.id = ftt.[KEY]
32+
ORDER BY
33+
ft_rank DESC
34+
) AS freetext_documents
35+
ORDER BY
36+
rank ASC
37+
),
38+
semantic_search AS
39+
(
40+
SELECT TOP(@k)
41+
id,
42+
RANK() OVER (ORDER BY cosine_distance) AS rank
43+
FROM
44+
(
45+
SELECT
46+
id,
47+
VECTOR_DISTANCE('cosine', @e, content_vector_ada2) AS cosine_distance
48+
FROM
49+
dbo.wikipedia_articles_embeddings w
50+
) AS similar_documents
51+
),
52+
result AS (
53+
SELECT TOP(@k)
54+
COALESCE(ss.id, ks.id) AS id,
55+
ss.rank AS semantic_rank,
56+
ks.rank AS keyword_rank,
57+
COALESCE(1.0 / (@k + ss.rank), 0.0) +
58+
COALESCE(1.0 / (@k + ks.rank), 0.0) AS score -- Reciprocal Rank Fusion (RRF)
59+
FROM
60+
semantic_search ss
61+
FULL OUTER JOIN
62+
keyword_search ks ON ss.id = ks.id
63+
ORDER BY
64+
score DESC
65+
)
66+
SELECT
67+
w.id,
68+
cast(score * 1000 as int) as rrf_score,
69+
rank() OVER(ORDER BY cast(score * 1000 AS INT) DESC) AS rrf_rank,
70+
semantic_rank,
71+
keyword_rank,
72+
w.title,
73+
w.[text]
74+
FROM
75+
result AS r
76+
INNER JOIN
77+
dbo.wikipedia_articles_embeddings AS w ON r.id = w.id
78+
ORDER BY
79+
rrf_rank

0 commit comments

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