src/Private/Report/AI/Get-MCSMetroAIImplications.ps1
|
function Get-MCSMetroAIImplications { Param( $CSV, $MetroAgent, $ExtraRequest, $Debug ) if ($Debug.IsPresent) { $DebugPreference = "Continue" } else { $DebugPreference = "SilentlyContinue" } Write-Debug ((get-date -Format 'yyyy-MM-dd HH_mm_ss')+' - '+"Getting Recommendations to be processed by AI") $Recommendations = $CSV | Select-Object -Property "Recommendation Title","Description","Priority" -Unique $AIRecommendations = @() Write-Debug ((get-date -Format 'yyyy-MM-dd HH_mm_ss')+' - '+"Setting Initial Prompt for contextualizing the AI") $Conversation = New-MCSSfMCConversation -MetroAgent $MetroAgent -ExtraRequest $ExtraRequest -Debug $Debug Write-Debug ((get-date -Format 'yyyy-MM-dd HH_mm_ss')+' - '+"Setting Recommendations to be processed by AI") Foreach ($Recommendation in $Recommendations) { if (![string]::IsNullOrEmpty($Recommendation.'Recommendation Title')) { Write-Host "Getting AI explanation for: " -NoNewline Write-Host $Recommendation.'Recommendation Title' -ForegroundColor Blue $Prompt = "Recommendation: '$($Recommendation.'Recommendation Title')', with the following deeper description: '$($Recommendation.Description)'." try { $turn = Invoke-MetroAIConversation -AgentId $MetroAgent.id -ConversationId $Conversation.id -UserInput $Prompt -Debug:$false -ErrorAction Stop Write-Debug ((get-date -Format 'yyyy-MM-dd HH_mm_ss')+' - '+"AI Response ID: $($turn.ResponseId)") } catch { if ($_.Exception.Message -like "*429*") { Write-Warning "Rate limit hit when invoking AI for recommendation. Waiting for 30 seconds before retrying..." Start-Sleep -Seconds 30 Invoke-MetroAIApiCall -Service 'openai/conversations' -Operation 'conversation' -Path $Conversation.id -Method Delete | Out-Null $Conversation = New-MCSSfMCConversation -MetroAgent $MetroAgent -ExtraRequest $ExtraRequest -Debug $Debug Write-Host "Trying to get AI explanation again for: " -NoNewline Write-Host $Recommendation.'Recommendation Title' -ForegroundColor Blue $turn = Invoke-MetroAIConversation -AgentId $MetroAgent.id -ConversationId $Conversation.id -UserInput $Prompt -Debug:$false Write-Debug ((get-date -Format 'yyyy-MM-dd HH_mm_ss')+' - '+"AI Response ID: $($turn.ResponseId)") } elseif ($_.Exception.Message -like "*500*") { Write-Warning "Exception 500 hit. Waiting for 30 seconds before retrying..." Start-Sleep -Seconds 30 Invoke-MetroAIApiCall -Service 'openai/conversations' -Operation 'conversation' -Path $Conversation.id -Method Delete | Out-Null $Conversation = New-MCSSfMCConversation -MetroAgent $MetroAgent -ExtraRequest $ExtraRequest -Debug $Debug Write-Host "Trying to get AI explanation again for: " -NoNewline Write-Host $Recommendation.'Recommendation Title' -ForegroundColor Blue $turn = Invoke-MetroAIConversation -AgentId $MetroAgent.id -ConversationId $Conversation.id -UserInput $Prompt -Debug:$false Write-Debug ((get-date -Format 'yyyy-MM-dd HH_mm_ss')+' - '+"AI Response ID: $($turn.ResponseId)") } elseif ($_.Exception.Message -like "*400*") { Write-Warning "Exception 400 hit. Waiting for 60 seconds before retrying..." Start-Sleep -Seconds 60 Invoke-MetroAIApiCall -Service 'openai/conversations' -Operation 'conversation' -Path $Conversation.id -Method Delete | Out-Null $Conversation = New-MCSSfMCConversation -MetroAgent $MetroAgent -ExtraRequest $ExtraRequest -Debug $Debug Write-Host "Trying to get AI explanation again for: " -NoNewline Write-Host $Recommendation.'Recommendation Title' -ForegroundColor Blue $turn = Invoke-MetroAIConversation -AgentId $MetroAgent.id -ConversationId $Conversation.id -UserInput $Prompt -Debug:$false Write-Debug ((get-date -Format 'yyyy-MM-dd HH_mm_ss')+' - '+"AI Response ID: $($turn.ResponseId)") } else { Write-Error "Error invoking AI conversation for recommendation: $($Recommendation.'Recommendation Title'). Error: $_" $turn = @{ AssistantText = "Error retrieving AI implication: $_" } } } $Answer = @{ "Recommendation" = $Recommendation.'Recommendation Title' "Description" = $Recommendation.Description "Priority" = $Recommendation.Priority "Implication" = $turn.AssistantText } $AIRecommendations += $Answer Start-Sleep -Seconds 1 } } Invoke-MetroAIApiCall -Service 'openai/conversations' -Operation 'conversation' -Path $Conversation.id -Method Delete | Out-Null return $AIRecommendations } |