@@ -237,6 +237,7 @@ CREATE TABLE ##BlitzCacheProcs (
237237 Pattern NVARCHAR (20 ),
238238 ai_prompt NVARCHAR (MAX ),
239239 ai_advice NVARCHAR (MAX ),
240+ ai_payload NVARCHAR (MAX ),
240241 ai_raw_response NVARCHAR (MAX )
241242 );
242243GO
@@ -878,6 +879,7 @@ CREATE TABLE #ai_configuration
878879 AI_Parameters NVARCHAR (4000 ),
879880 Payload_Template_Override NVARCHAR (4000 ),
880881 Timeout_Seconds TINYINT ,
882+ Context INT ,
881883 DefaultModel BIT DEFAULT 0 );
882884
883885DECLARE
@@ -888,14 +890,15 @@ DECLARE
888890 @AIParameters NVARCHAR (4000 ),
889891 @AIPayloadTemplate NVARCHAR (MAX ),
890892 @AITimeoutSeconds TINYINT ,
891- @AIAdviceText NVARCHAR (MAX );
893+ @AIAdviceText NVARCHAR (MAX ),
894+ @AIContext INT ;
892895
893896
894897IF @AIConfig IS NOT NULL
895898BEGIN
896899 RAISERROR (N ' Reading values from AI Configuration Table' , 0 , 1 ) WITH NOWAIT ;
897- SET @config_sql = N' INSERT INTO #ai_configuration (Id, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, AI_System_Prompt_Override, AI_Parameters, Payload_Template_Override, Timeout_Seconds, DefaultModel)
898- SELECT Id, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, AI_System_Prompt_Override, AI_Parameters, Payload_Template_Override, Timeout_Seconds, DefaultModel FROM '
900+ SET @config_sql = N' INSERT INTO #ai_configuration (Id, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, AI_System_Prompt_Override, AI_Parameters, Payload_Template_Override, Timeout_Seconds, Context, DefaultModel)
901+ SELECT Id, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, AI_System_Prompt_Override, AI_Parameters, Payload_Template_Override, Timeout_Seconds, Context, DefaultModel FROM '
899902 + CASE WHEN @AIConfigDatabaseName IS NOT NULL THEN (QUOTENAME (@AIConfigDatabaseName) + N ' .' ) ELSE N ' ' END
900903 + CASE WHEN @AIConfigSchemaName IS NOT NULL THEN (QUOTENAME (@AIConfigSchemaName) + N ' .' ) ELSE N ' ' END
901904 + QUOTENAME (@AIConfigTableName) + N ' WHERE (@AIModel IS NULL AND DefaultModel = 1) OR @AIModel IN (AI_Model, Nickname) ; ' ;
@@ -928,6 +931,7 @@ IF @AI > 0
928931 @AISystemPrompt = AI_System_Prompt_Override,
929932 @AIParameters = AI_Parameters,
930933 @AITimeoutSeconds = COALESCE (Timeout_Seconds, 230 ),
934+ @AIContext = Context,
931935 @AIPayloadTemplate = Payload_Template_Override
932936 FROM #ai_configuration
933937 WHERE DefaultModel = 1
@@ -939,6 +943,7 @@ IF @AI > 0
939943 @AISystemPrompt = AI_System_Prompt_Override,
940944 @AIParameters = AI_Parameters,
941945 @AITimeoutSeconds = COALESCE (Timeout_Seconds, 230 ),
946+ @AIContext = Context,
942947 @AIPayloadTemplate = Payload_Template_Override
943948 FROM #ai_configuration
944949 ORDER BY Id;
@@ -947,7 +952,10 @@ IF @AI > 0
947952 SET @AIModel = N ' gpt-5-nano' ;
948953
949954 IF @AIURL IS NULL OR @AIURL NOT LIKE N ' http%'
950- SET @AIURL = N ' https://api.openai.com/v1/chat/completions' ;
955+ SET @AIURL = CASE
956+ WHEN @AIModel LIKE ' gemini%' THEN N ' https://generativelanguage.googleapis.com/v1beta/models/' + @AIModel + N ' :generateContent'
957+ ELSE N ' https://api.openai.com/v1/chat/completions' /* Default to ChatGPT */
958+ END ;
951959
952960 /* Try to guess the credential based on the root of their URL: */
953961 IF @AICredential IS NULL
@@ -963,7 +971,7 @@ IF @AI > 0
963971
964972 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.' ;
965973
966- IF @AIURL LIKE ' https://generativelanguage.googleapis.com %' AND @AIPayloadTemplate IS NULL
974+ IF @AIModel LIKE ' gemini %' AND @AIPayloadTemplate IS NULL
967975 SET @AIPayloadTemplate = N' {
968976 "contents": [
969977 {
@@ -990,20 +998,9 @@ IF @AI > 0
990998
991999 IF @Debug = 2 OR (@AI = 1 AND (@AIModel IS NULL OR @AIURL IS NULL OR @AISystemPrompt IS NULL OR @AICredential IS NULL OR @AIPayloadTemplate IS NULL ))
9921000 BEGIN
993- PRINT N ' @AIModel: ' ;
994- PRINT @AIModel;
995- PRINT N ' @AIURL: ' ;
996- PRINT @AIURL;
997- PRINT N ' @AICredential: ' ;
998- PRINT @AICredential;
999- PRINT N ' @AIParameters: ' ;
1000- PRINT @AIParameters;
1001- PRINT N ' @AITimeoutSeconds: ' ;
1002- PRINT @AITimeoutSeconds;
1003- PRINT N ' @AISystemPrompt: ' ;
1004- PRINT @AISystemPrompt;
1005- PRINT N ' @AIPayloadTemplate: ' ;
1006- PRINT @AIPayloadTemplate;
1001+ SELECT @AIModel AS AIModel, @AIURL AS AIUrl, @AICredential AS AICredential,
1002+ @AIContext AS AIContext, @AIParameters AS AIParameters, @AITimeoutSeconds AS AITimeoutSeconds,
1003+ @AISystemPrompt AS AISystemPrompt, @AIPayloadTemplate AS AIPayloadTemplate;
10071004 END ;
10081005
10091006 IF @AI = 1 AND (@AIModel IS NULL OR @AIURL IS NULL OR @AISystemPrompt IS NULL OR @AICredential IS NULL OR @AIPayloadTemplate IS NULL )
@@ -5290,7 +5287,7 @@ Thank you.'
52905287
52915288 /* Store the response in the the ai_advice column */
52925289 UPDATE ##BlitzCacheProcs
5293- SET ai_advice = @AIAdviceText, ai_raw_response = @AIResponseJSON
5290+ SET ai_advice = @AIAdviceText, ai_raw_response = @AIResponseJSON, ai_payload = @AIPayload
52945291 WHERE SPID = @@SPID
52955292 AND ((@CurrentSqlHandle IS NOT NULL AND SqlHandle = @CurrentSqlHandle)
52965293 OR (@CurrentSqlHandle IS NULL AND SqlHandle IS NULL ))
@@ -5311,7 +5308,7 @@ Thank you.'
53115308
53125309 -- Store the error message in ai_advice so the user knows what happened
53135310 UPDATE ##BlitzCacheProcs
5314- SET ai_advice = @AIErrorMessage, ai_raw_response = @AIResponseJSON
5311+ SET ai_advice = @AIErrorMessage, ai_raw_response = @AIResponseJSON, ai_payload = @AIPayload
53155312 WHERE SPID = @@SPID
53165313 AND ((@CurrentSqlHandle IS NOT NULL AND SqlHandle = @CurrentSqlHandle)
53175314 OR (@CurrentSqlHandle IS NULL AND SqlHandle IS NULL ))
@@ -5681,6 +5678,8 @@ BEGIN
56815678 [AI Prompt] = (
56825679 SELECT (@AISystemPrompt + NCHAR(13) + NCHAR(10) + NCHAR(13) + NCHAR(10) + ai_prompt) AS [text()] FOR XML PATH('' ai_prompt'' ), TYPE),' ELSE N ' ' END
56835680 + CASE WHEN @AI = 1 THEN N'
5681+ [AI Payload] = CASE WHEN ai_payload IS NULL THEN NULL ELSE (
5682+ SELECT ai_payload AS [text()] FOR XML PATH('' ai_payload'' ), TYPE) END,
56845683 [AI Raw Response] = CASE WHEN ai_raw_response IS NULL THEN NULL ELSE (
56855684 SELECT ai_raw_response AS [text()] FOR XML PATH('' ai_raw_response'' ), TYPE) END, ' ELSE N ' ' END + N'
56865685 [Remove Plan Handle From Cache],
0 commit comments