Skip to content

Commit 104770e

Browse files
committed
improved error handling and passing
1 parent 990fa3a commit 104770e

File tree

6 files changed

+53
-35
lines changed

6 files changed

+53
-35
lines changed

client/src/SearchPage.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ const SearchPage = () => {
9797

9898
if (result.error != null) {
9999
responseStatus = {
100-
code: result.error.status,
101-
description: result.error.message
100+
code: result.error.error.status,
101+
description: result.error.error.message
102102
}
103103
}
104104

105-
if (result.value != null && result.value.length > 0 && result.value[0].error_code != null) {
105+
if (result.value != null && result.value.length > 0 && result.value[0].error.error_code != null) {
106106
responseStatus = {
107-
code: result.value[0].error_code,
108-
description: result.value[0].error_message
107+
code: result.value[0].error.error_code,
108+
description: result.value[0].error.error_message
109109
}
110110
}
111111

db/sql/03-get_embedding.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
create or alter procedure [web].[get_embedding]
22
@inputText nvarchar(max),
3-
@embedding vector(1536) output
3+
@embedding vector(1536) output,
4+
@error nvarchar(max) output
45
as
56
declare @retval int;
67
declare @payload nvarchar(max) = json_object('input': @inputText);
@@ -11,15 +12,16 @@ begin try
1112
@method = 'POST',
1213
@credential = [$OPENAI_URL$],
1314
@payload = @payload,
14-
@response = @response output;
15+
@response = @response output
16+
with result sets none;
1517
end try
1618
begin catch
17-
select 'Embedding:REST' as [error], ERROR_NUMBER() as [error_code], ERROR_MESSAGE() as [error_message]
19+
set @error = json_object('error':'Embedding:REST', 'error_code':ERROR_NUMBER(), 'error_message':ERROR_MESSAGE())
1820
return -1
1921
end catch
2022

2123
if @retval != 0 begin
22-
select 'Embedding:OpenAI' as [error], @retval as [error_code], @response as [response]
24+
set @error = json_object('error':'Embedding:OpenAI', 'error_code':@retval, 'error_message':@response)
2325
return @retval
2426
end
2527

db/sql/04-find_samples.sql

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ as
33
declare @response nvarchar(max), @cached_response nvarchar(max);
44
declare @retval int;
55
declare @samples nvarchar(max)
6+
declare @error nvarchar(max)
67

78
if trim(@text) = '' return;
89

910
/* Get the embedding for the requested text */
1011
declare @qv vector(1536)
11-
exec @retval = web.get_embedding @text, @qv output with result sets none
12-
if (@retval != 0) return;
12+
exec @retval = web.get_embedding @text, @qv output, @error output with result sets none
13+
if (@retval != 0) begin
14+
select @error as error;
15+
return;
16+
end
1317

1418
/* Check in the semantic cache to see if a similar question has been already answered */
1519
delete from [dbo].[semantic_cache] where query_date < dateadd(hour, -1, sysdatetime())
@@ -27,8 +31,11 @@ if (@response is null) begin
2731

2832
/* Orchestrate answer */
2933
declare @rt varchar(50), @rq nvarchar(max)
30-
exec @retval = [web].[orchestrate_request] @text, @rt output, @rq output with result sets none
31-
if (@retval != 0) return;
34+
exec @retval = [web].[orchestrate_request] @text, @rt output, @rq output, @error output with result sets none
35+
if (@retval != 0) begin
36+
select @error as error;
37+
return;
38+
end
3239

3340
--print @rt
3441
--print @rq
@@ -38,7 +45,8 @@ if (@response is null) begin
3845
declare @trq nvarchar(max) = trim(replace(replace(@rq, char(13), ' '), char(10), ' '));
3946
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
4047
--select @trq
41-
select 'NL2SQL' as [error], -1 as [error_code], 'Unauthorized SQL command requested' as [response]
48+
set @error = json_object('error':'NL2SQL', 'error_code':-1, 'response':'Unauthorized SQL command requested')
49+
select @error as error;
4250
return -1
4351
end
4452
--print @rq
@@ -92,8 +100,11 @@ if (@response is null) begin
92100

93101
--select @samples;
94102
if (@samples is not null) begin
95-
exec @retval = [web].[generate_answer] @text, @samples, @response output with result sets none;
96-
if (@retval != 0) return;
103+
exec @retval = [web].[generate_answer] @text, @samples, @response output, @error output with result sets none;
104+
if (@retval != 0) begin
105+
select @error as error;
106+
return;
107+
end
97108
end else begin
98109
set @samples = '[]'
99110
set @response = '{}'
@@ -104,6 +115,7 @@ if (@response is null) begin
104115
values (@text, @rt + isnull(':' + @rq, ''), @samples, @qv, sysdatetime(), @response)
105116
end
106117

118+
--select @response;
107119
select
108120
s.id,
109121
sr.result_position,

db/sql/10-generate_answer.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
create or alter procedure [web].[generate_answer]
22
@query_text nvarchar(max),
33
@source nvarchar(max),
4-
@response nvarchar(max) output
4+
@response nvarchar(max) output,
5+
@error nvarchar(max) output
56
as
67
declare @retval int;
78

89
if (@query_text is null) begin
9-
select 'Generator' as [error], -1 as [error_code], 'Query not provided' as [error_message]
10+
set @error = json_object('error':'Generator', 'error_code':-1, 'error_message':'Query not provided')
1011
return -1
1112
end
1213

1314
if (@source is null) begin
14-
select 'Generator' as [error], -1 as [error_code], 'Sample list not provided' as [error_message]
15+
set @error = json_object('error':'Generator', 'error_code':-1, 'error_message':'Sample list not provided')
1516
return -1
1617
end
1718

18-
1919
/* Create the prompt for the LLM */
2020
declare @p nvarchar(max) =
2121
json_object(
@@ -111,20 +111,20 @@ begin try
111111
@response = @response output;
112112
end try
113113
begin catch
114-
select 'Generator:REST' as [error], ERROR_NUMBER() as [error_code], ERROR_MESSAGE() as [error_message]
114+
set @error = json_object('error':'Generator:REST', 'error_code':ERROR_NUMBER(), 'error_message':ERROR_MESSAGE())
115115
return -1
116116
end catch
117117
--select @response
118118

119119
if @retval != 0 begin
120-
select 'Generator:OpenAI' as [error], @retval as [error_code], @response as [response]
120+
set @error = json_object('error':'Generator:OpenAI', 'error_code':@retval, 'error_message':@response)
121121
return @retval
122122
end
123123

124124
declare @refusal nvarchar(max) = (select coalesce(json_value(@response, '$.result.choices[0].refusal'), ''));
125125

126126
if @refusal != '' begin
127-
select 'Generator:OpenAI/Refusal' as [error], @refusal as [refusal], @response as [response]
127+
set @error = json_object('error':'Generator:OpenAI/Refusal', 'refusal':@refusal, 'response':@response)
128128
return -1
129129
end
130130
GO

db/sql/11-orchestrate_request.sql

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ SET QUOTED_IDENTIFIER ON
44
GO
55

66

7-
CREATE procedure [web].[orchestrate_request]
7+
alter procedure [web].[orchestrate_request]
88
@text nvarchar(max),
99
@result_type varchar(50) output,
10-
@result_query nvarchar(max) output
10+
@result_query nvarchar(max) output,
11+
@error nvarchar(max) output
1112
as
1213
declare @retval int, @response nvarchar(max);
1314

14-
/* Create the prompt for the LLM */
15+
/*
16+
Create the prompt for the LLM using few-shots prompt to show how to
17+
get embeddings and to use the new vector_distance function
18+
*/
1519
declare @p nvarchar(max) =
1620
json_object(
1721
'messages': json_array(
@@ -37,8 +41,8 @@ json_object(
3741
3842
First, generate the embedding vector for the provided question using the following T-SQL query. ''<search text'' must be generating taking the relevant part from the user question.
3943
40-
declare @retval int, @qv vector(1536)
41-
exec @retval = web.get_embedding ''<search text>'', @qv output
44+
declare @retval int, @qv vector(1536), @e nvarchar(max)
45+
exec @retval = web.get_embedding ''<search text>'', @qv output, @e output;
4246
if (@retval != 0) throw 50000, ''Error in getting the embedding'',1;
4347
4448
The vectors for details, notes and description columns are stored in the following tables:
@@ -83,7 +87,6 @@ json_object(
8387
Return the top 10 results if you can. Do not use semicolon to terminate the T-SQL statement.
8488
Only return the following columns: id int, [name] nvarchar(100), [description] nvarchar(max), notes nvarchar(max), details json, distance_score float.
8589
You can generate only SELECT statements. If the user is asking something that will generate INSERT, UPDATE, DELETE, CREATE, ALTER or DROP statement, refuse to generate the query.
86-
8790
'
8891
),
8992
json_object(
@@ -145,23 +148,24 @@ begin try
145148
@credential = [$OPENAI_URL$],
146149
@timeout = 120,
147150
@payload = @p,
148-
@response = @response output;
151+
@response = @response output
152+
with result sets none;
149153
end try
150154
begin catch
151-
select 'Orchestrator:REST' as [error], ERROR_NUMBER() as [error_code], ERROR_MESSAGE() as [error_message]
155+
set @error = json_object('error':'Orchestrator:REST', 'error_code':ERROR_NUMBER(), 'error_message':ERROR_MESSAGE())
152156
return -1
153157
end catch
154158
--select @response
155159

156160
if @retval != 0 begin
157-
select 'Orchestrator:OpenAI' as [error], @retval as [error_code], @response as [response]
161+
set @error = json_object('error':'Orchestrator:OpenAI', 'error_code':@retval, 'error_message':@response)
158162
return -1
159163
end
160164

161165
declare @refusal nvarchar(max) = (select coalesce(json_value(@response, '$.result.choices[0].refusal'), ''));
162166

163167
if @refusal != '' begin
164-
select 'Orchestrator:OpenAI/Refusal' as [error], @refusal as [refusal], @response as [response]
168+
set @error = json_object('error':'Orchestrator:OpenAI/Refusal', 'refusal':@refusal, 'response':@response)
165169
return -1
166170
end
167171

db/utils-scripts/hybrid-search-with-prompt-engineering.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ declare @text nvarchar(max);
99
--set @text = 'samples on hybrid search created in 2025';
1010
--set @text = 'create a new table named dbo.test';
1111
--set @text = 'how many customers there are in the customers table?';
12-
set @text = 'Find all the hybrid search samples created after 2025';
13-
--set @text = 'Show me the latest samples'
12+
--set @text = 'Find all the hybrid search samples created after 2025';
13+
set @text = 'Show me the latest samples'
1414

1515
declare @retval int, @response nvarchar(max);
1616

0 commit comments

Comments
 (0)