Skip to content

Commit a4cc2f8

Browse files
committed
updates for FabCon 2025
1 parent 666de63 commit a4cc2f8

File tree

6 files changed

+248
-0
lines changed

6 files changed

+248
-0
lines changed

db/demo/00-orchestration.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
declare @result_type varchar(50)
3+
declare @result_query nvarchar(max)
4+
declare @error nvarchar(max)
5+
6+
exec [web].[orchestrate_request]
7+
--'find the samples created in 2025',
8+
--'show the last 5 samples',
9+
--'samples used by Davide at FabCon 2025',
10+
--'langchain samples',
11+
@result_type output,
12+
@result_query output,
13+
@error output
14+
15+
select @result_type as result_type, @result_query as result_query, @error as error
16+
print @result_query

db/demo/01-semantic_search.sql

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
declare @response nvarchar(max);
2+
declare @retval int;
3+
declare @samples nvarchar(max)
4+
declare @error nvarchar(max)
5+
6+
declare @text nvarchar(1000) =
7+
--'find the samples created in 2025';
8+
--'show the last 5 samples';
9+
--'samples used by Davide at FabCon 2025';
10+
--'langchain samples';
11+
--'agentic rag demos';
12+
13+
declare @qv vector(1536)
14+
exec @retval = web.get_embedding @text, @qv output, @error output with result sets none
15+
if (@retval != 0) begin
16+
select @error as error;
17+
return;
18+
end
19+
20+
select top(10)
21+
s.id,
22+
s.name,
23+
s.created_on,
24+
least(
25+
vector_distance('cosine', e.[embedding], @qv),
26+
vector_distance('cosine', ne.[embedding], @qv),
27+
vector_distance('cosine', de.[embedding], @qv)
28+
) as cosine_distance
29+
from
30+
dbo.samples s
31+
inner join
32+
dbo.samples_embeddings e on e.id = s.id
33+
left join
34+
dbo.samples_notes_embeddings ne on e.id = ne.id
35+
left join
36+
dbo.samples_details_embeddings de on e.id = de.id
37+
order by
38+
cosine_distance asc;

db/demo/02-find_samples.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exec [web].[find_samples]
2+
--'find the samples created in 2025';
3+
--'show the last 5 samples';
4+
--'samples used by Davide at FabCon 2025';
5+
'langchain samples';
6+
--'agentic rag demos';
7+
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
create or alter procedure [web].[find_samples] @text nvarchar(1000), @k int = null
2+
as
3+
set nocount on;
4+
declare @response nvarchar(max), @cached_response nvarchar(max);
5+
declare @retval int;
6+
declare @samples nvarchar(max)
7+
declare @error nvarchar(max)
8+
9+
if trim(@text) = '' return;
10+
11+
/* Get the embedding for the requested text */
12+
declare @qv vector(1536)
13+
exec @retval = web.get_embedding @text, @qv output, @error output with result sets none
14+
if (@retval != 0) begin
15+
select @error as error;
16+
return;
17+
end
18+
19+
/* Check in the semantic cache to see if a similar question has been already answered */
20+
delete from [dbo].[semantic_cache] where query_date < dateadd(hour, -1, sysdatetime())
21+
22+
select top(1) *, vector_distance('cosine', @qv, embedding) as d
23+
into #c
24+
from [dbo].[semantic_cache] order by d;
25+
--select * from #c
26+
27+
select top(1) @cached_response = response from #c where d < 0.25
28+
if (@cached_response is not null) set @response = @cached_response
29+
30+
/* If no cached response is available then generate a fresh answer */
31+
if (@response is null) begin
32+
33+
/* Orchestrate answer */
34+
declare @rt varchar(50), @rq nvarchar(max)
35+
exec @retval = [web].[orchestrate_request] @text, @rt output, @rq output, @error output with result sets none
36+
if (@retval != 0) begin
37+
select @error as error;
38+
return;
39+
end
40+
41+
--print @rt
42+
--print @rq
43+
44+
/* Find the samples using T-SQL */
45+
if (@rt = 'SQL') begin
46+
declare @trq nvarchar(max) = trim(replace(replace(@rq, char(13), ' '), char(10), ' '));
47+
if (@trq like '%INSERT %' or @trq like '%UPDATE %' or @trq like '%DELETE %' or @trq like '%DROP %' or @trq like '%ALTER %' or @trq like '%CREATE %') begin
48+
--select @trq
49+
select 'NL2SQL' as [error], -1 as [error_code], 'Unauthorized SQL command requested' as [response]
50+
return -1
51+
end
52+
--print @rq
53+
54+
create table #ts (id int, [name] nvarchar(100), [description] nvarchar(max), notes nvarchar(max), details json, distance_score float);
55+
insert into #ts exec sp_executesql @rq
56+
set @samples = cast((select * from #ts for json auto) as nvarchar(max))
57+
--print @samples
58+
59+
/* If not results coming from SQL execution, try SEMANTIC anyway */
60+
if (@samples is null) begin
61+
set @rt = 'SQL+SEMANTIC'
62+
end
63+
end
64+
65+
66+
/* Find the samples most similar to the requested topic */
67+
if (@rt like '%SEMANTIC%') begin
68+
set @k = coalesce(@k, 50)
69+
70+
-- Semantic Search
71+
drop table if exists #ss;
72+
select top(@k)
73+
s.id,
74+
[name], [description], [notes], [details],
75+
least(
76+
vector_distance('cosine', e.[embedding], @qv),
77+
vector_distance('cosine', ne.[embedding], @qv),
78+
vector_distance('cosine', de.[embedding], @qv)
79+
) as cosine_distance
80+
into
81+
#s
82+
from
83+
dbo.samples s
84+
inner join
85+
dbo.samples_embeddings e on e.id = s.id
86+
left join
87+
dbo.samples_notes_embeddings ne on e.id = ne.id
88+
left join
89+
dbo.samples_details_embeddings de on e.id = de.id
90+
order by
91+
cosine_distance asc;
92+
93+
/* Prepare the JSON string with relevant results to be sent to LLM for evaluation */
94+
set @samples = (
95+
select
96+
[id], [name], [description], [notes], [details],
97+
100 * (1-cosine_distance) as similiarity_score
98+
from #s
99+
order by similiarity_score desc for json path
100+
)
101+
end
102+
103+
--select @samples;
104+
if (@samples is not null) begin
105+
exec @retval = [web].[generate_answer] @text, @samples, @response output, @error output with result sets none;
106+
if (@retval != 0) begin
107+
select @error as error;
108+
return;
109+
end
110+
end else begin
111+
set @samples = '[]'
112+
set @response = '{}'
113+
end
114+
115+
/* Cache results */
116+
insert into dbo.semantic_cache (query, [action], samples, embedding, query_date, response)
117+
values (@text, @rt + isnull(':' + @rq, ''), @samples, @qv, sysdatetime(), @response)
118+
end
119+
120+
--select @response;
121+
select
122+
s.id,
123+
sr.result_position,
124+
s.[name],
125+
s.[description],
126+
sr.sample_summary,
127+
sr.thoughts,
128+
s.[url]--,
129+
--s.distance_score
130+
from
131+
openjson(@response, '$.result.choices[0].message') with (
132+
content nvarchar(max) '$.content'
133+
) m
134+
cross apply
135+
openjson(m.content, '$.samples') with (
136+
id int,
137+
result_position int,
138+
sample_summary nvarchar(max),
139+
thoughts nvarchar(max)
140+
) as sr
141+
inner join
142+
dbo.samples as s on s.id = sr.id
143+
order by
144+
sr.result_position
145+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
create or alter procedure [web].[find_samples_langchain] @text nvarchar(1000), @k int = 10
2+
as
3+
set nocount on;
4+
declare @response nvarchar(max), @cached_response nvarchar(max);
5+
declare @retval int;
6+
declare @samples nvarchar(max)
7+
declare @error nvarchar(max)
8+
9+
if trim(@text) = '' return;
10+
11+
-- Get the embedding for the requested text
12+
declare @qv vector(1536)
13+
exec @retval = web.get_embedding @text, @qv output, @error output with result sets none
14+
if (@retval != 0) begin
15+
select @error as error;
16+
return;
17+
end
18+
19+
-- Semantic Search
20+
select top(@k)
21+
s.id,
22+
[name], [description], [notes], [details],
23+
least(
24+
vector_distance('cosine', e.[embedding], @qv),
25+
vector_distance('cosine', ne.[embedding], @qv),
26+
vector_distance('cosine', de.[embedding], @qv)
27+
) as cosine_distance
28+
from
29+
dbo.samples s
30+
inner join
31+
dbo.samples_embeddings e on e.id = s.id
32+
left join
33+
dbo.samples_notes_embeddings ne on e.id = ne.id
34+
left join
35+
dbo.samples_details_embeddings de on e.id = de.id
36+
order by
37+
cosine_distance asc
38+
39+
40+

db/utils-scripts/test.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ go
1010
exec [web].[find_samples] 'rag sample'
1111
go
1212

13+
exec [web].[find_samples_langchain] 'any samples used at FabCon 2025?'
14+
go

0 commit comments

Comments
 (0)