Skip to content

Commit feba8de

Browse files
committed
#3669 sp_BlitzCache AI 3
Trying to auto-detect database scoped config names, easier automatic runs with Gemini, defaulting to gpt-5-nano. Working on #3669.
1 parent 04100a6 commit feba8de

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

sp_BlitzCache.sql

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ ALTER PROCEDURE dbo.sp_BlitzCache
278278
@AI TINYINT = 0, /* 1 = ask for advice, 2 = build prompt but don't actually call AI. Only works with a single query plan: automatically sets @ExpertMode = 1, @KeepCRLF = 1. */
279279
@AIModel VARCHAR(200) = NULL, /* Defaults to gpt-4.1-mini */
280280
@AIURL VARCHAR(200) = NULL, /* Defaults to https://api.openai.com/v1/chat/completions */
281-
@AICredential VARCHAR(200) = NULL, /* Defaults to 'https://api.openai.com' */
281+
@AICredential VARCHAR(200) = NULL, /* Defaults to 'https://api.openai.com/' or the root of your AIURL, trailing slash included */
282282
@AIConfig NVARCHAR(500) = NULL, /* Table where AI config data is stored - can be in the format db.schema.table, schema.table, or just table. */
283283
@Version VARCHAR(30) = NULL OUTPUT,
284284
@VersionDate DATETIME = NULL OUTPUT,
@@ -492,7 +492,7 @@ IF @Help = 1
492492
UNION ALL
493493
SELECT N'@AICredential',
494494
N'VARCHAR(200)',
495-
N'The database scoped credential that you configured. Defaults to https://api.openai.com. Must be a subset of the @AIURL parameter.'
495+
N'The database scoped credential that you configured. Defaults to https://api.openai.com/ or the root URL of your @AIURL, trailing slash included. Must be a subset of the @AIURL parameter.'
496496

497497
UNION ALL
498498
SELECT N'@Version',
@@ -927,7 +927,7 @@ IF @AI > 0
927927
@AICredential = AI_Database_Scoped_Credential_Name,
928928
@AISystemPrompt = AI_System_Prompt_Override,
929929
@AIParameters = AI_Parameters,
930-
@AITimeoutSeconds = COALESCE(Timeout_Seconds, 30),
930+
@AITimeoutSeconds = COALESCE(Timeout_Seconds, 230),
931931
@AIPayloadTemplate = Payload_Template_Override
932932
FROM #ai_configuration
933933
WHERE DefaultModel = 1
@@ -938,22 +938,23 @@ IF @AI > 0
938938
@AICredential = COALESCE(@AICredential, AI_Database_Scoped_Credential_Name),
939939
@AISystemPrompt = AI_System_Prompt_Override,
940940
@AIParameters = AI_Parameters,
941-
@AITimeoutSeconds = COALESCE(Timeout_Seconds, 30),
941+
@AITimeoutSeconds = COALESCE(Timeout_Seconds, 230),
942942
@AIPayloadTemplate = Payload_Template_Override
943943
FROM #ai_configuration
944944
ORDER BY Id;
945945

946946
IF @AIModel IS NULL
947-
SET @AIModel = N'gpt-4.1-mini';
947+
SET @AIModel = N'gpt-5-nano';
948948

949949
IF @AIURL IS NULL OR @AIURL NOT LIKE N'http%'
950950
SET @AIURL = N'https://api.openai.com/v1/chat/completions';
951951

952-
IF @AICredential IS NULL OR @AICredential NOT LIKE 'http%'
953-
SET @AICredential = N'https://api.openai.com';
952+
/* Try to guess the credential based on the root of their URL: */
953+
IF @AICredential IS NULL
954+
SET @AICredential = LEFT(@AIURL, CHARINDEX('/', @AIURL, CHARINDEX('://', @AIURL) + 3));
954955

955956
IF @AITimeoutSeconds IS NULL OR @AITimeoutSeconds < 1 OR @AITimeoutSeconds > 230
956-
SET @AITimeoutSeconds = 30;
957+
SET @AITimeoutSeconds = 230;
957958

958959
IF @AISystemPrompt IS NULL OR @AISystemPrompt = N''
959960
SET @AISystemPrompt = N'You are a very senior database developer working with Microsoft SQL Server and Azure SQL DB. You focus on real-world, actionable advice that will make a big difference, quickly. You value everyone''s time, and while you are friendly and courteous, you do not waste time with pleasantries or emoji because you work in a fast-paced corporate environment.
@@ -962,7 +963,17 @@ IF @AI > 0
962963
963964
Do not offer followup options: the customer can only contact you once, so include all necessary information, tasks, and scripts in your initial reply. Render your output in Markdown, as it will be shown in plain text to the customer.';
964965

965-
IF @AIPayloadTemplate IS NULL
966+
IF @AIURL LIKE 'https://generativelanguage.googleapis.com%' AND @AIPayloadTemplate IS NULL
967+
SET @AIPayloadTemplate = N'{
968+
"contents": [
969+
{
970+
"parts": [
971+
{"text": "@AISystemPrompt @CurrentAIPrompt"}
972+
]
973+
}
974+
]
975+
}';
976+
ELSE IF @AIPayloadTemplate IS NULL /* Default to ChatGPT format */
966977
SET @AIPayloadTemplate = N'{
967978
"model": "@AIModel",
968979
"messages": [
@@ -5191,7 +5202,7 @@ Thank you.'
51915202
DECLARE @AIErrorMessage NVARCHAR(4000);
51925203

51935204
DECLARE ai_cursor CURSOR LOCAL FAST_FORWARD FOR
5194-
SELECT SqlHandle, QueryHash, PlanHandle, ai_prompt, COALESCE(QueryType, N'') + N' - ' + LEFT(QueryText, 100)
5205+
SELECT DISTINCT SqlHandle, QueryHash, PlanHandle, ai_prompt, COALESCE(QueryType, N'') + N' - ' + LEFT(QueryText, 100)
51955206
FROM ##BlitzCacheProcs
51965207
WHERE SPID = @@SPID
51975208
AND QueryPlan IS NOT NULL

0 commit comments

Comments
 (0)