|
| 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 | + |
0 commit comments