Skip to content

Commit 41aaf16

Browse files
authored
Merge pull request #17 from happyadam73/feature/remove-sql-tags-in-response
Remove ```sql tags from response
2 parents 4f888de + 720d4a7 commit 41aaf16

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

sql/TSqlChatGPT.sql

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
-- Revision History: 1.1 (Error handling improved, and no longer need to find/replace APIM URL)
99
-- 1.2 (AWB, 17/08/2023) - Added natural query language support with the usp_ExecuteNaturalQuery stored procedure
1010
-- 1.3 (AWB, 23/08/2023) - Bugs fixes and support for Views
11+
-- 1.4 (AWB, 27/05/2024) - GPT models returning ```sql tags even if prompted otherwise
1112
--
1213
--
1314
-- DISCLAIMER:
@@ -236,6 +237,10 @@ BEGIN
236237
IF @response IS NULL
237238
THROW 50000, 'It was not possible to extract a message response from the OpenAI Chat API', 1;
238239

240+
-- Remove any preceding ```sql and ``` from the response - this seems to be a change in behaviour of
241+
-- some GPT models (such as 3.5 Turbo) even if the system/user prompt explicitly forbids this
242+
SET @response = REPLACE(REPLACE(@response, '```sql', ''), '```', '');
243+
239244
-- Print response if required (and not in debug mode)
240245
IF @print_response = 1 AND @debug = 0
241246
EXEC [dbo].[usp_PrintMax] @response;

sql/TSqlChatGPTForAOAI.sql

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
-- 1.1 (AWB, 16/08/2023) - default model is 16K token version (available in UK South), and amend system message to support CODEONLY response
1010
-- 1.2 (AWB, 17/08/2023) - Added natural query language support with the usp_ExecuteNaturalQuery stored procedure
1111
-- 1.3 (AWB, 23/08/2023) - Bugs fixes and support for Views
12+
-- 1.4 (AWB, 27/05/2024) - GPT models returning ```sql tags even if prompted otherwise
1213
--
1314
--
1415
-- DISCLAIMER:
@@ -263,6 +264,10 @@ BEGIN
263264
IF @response IS NULL
264265
THROW 50000, 'It was not possible to extract a message response from the Azure OpenAI Chat API', 1;
265266

267+
-- Remove any preceding ```sql and ``` from the response - this seems to be a change in behaviour of
268+
-- some GPT models (such as 3.5 Turbo) even if the system/user prompt explicitly forbids this
269+
SET @response = REPLACE(REPLACE(@response, '```sql', ''), '```', '');
270+
266271
-- Print response if required (and not in debug mode)
267272
IF @print_response = 1 AND @debug = 0
268273
EXEC [dbo].[usp_PrintMax] @response;

0 commit comments

Comments
 (0)