Skip to content

Instantly share code, notes, and snippets.

@ehrnst
Last active April 24, 2023 12:36
Show Gist options
  • Save ehrnst/d53a48da5cee16165612e12ed2cac93b to your computer and use it in GitHub Desktop.
Save ehrnst/d53a48da5cee16165612e12ed2cac93b to your computer and use it in GitHub Desktop.
Azure OpenAI Slack summary using PowerShell
# slack test
$slackKey = Get-AzKeyVaultsecret -VaultName "" -Name "" -AsPlainText
$azOpenAiKey = Get-AzKeyVaultsecret -VaultName "" -Name "" -AsPlainText
$slackChannelId = ""
$slackThreadId = ""
$openAiUrl = ""
$slackUrl = "https://slack.com/api/conversations.replies?channel=$slackChannelId&ts=$slackThreadId&pretty=1"
$slackHeaders= @{
"Authorization" = "Bearer $slackKey"
"content-type" = "authorization/x-www-form-urlencoded"
}
$incidentThread = Invoke-RestMethod -uri $slackUrl -Method Get -Headers $slackHeaders
$incidentMessages = ""
foreach ($message in $incidentThread.messages) {
$messageText = $message.text
$messageUser = $message.user
$messageUserUrl = "https://slack.com/api/users.info?user=$messageUser&pretty=1"
$messageUser = Invoke-RestMethod -uri $messageUserUrl -Method Get -Headers $slackHeaders
$messageUser = $messageUser.user.profile.real_name_normalized
$messageDate = (([System.DateTimeOffset]::FromUnixTimeSeconds($message.ts)).DateTime).ToString("yyyy-MM-dd HH:mm")
$messageText = $messageText.Replace("`n", "")
$messageText = $messageText.Replace("`r", "")
$messageText = $messageText.Replace(">", "")
$messageText = $messageText.Split("https://")[0]
$incidentMessages+="$messageDate|$messageUser|$messageText|`n"
}
$openAiHeaders = @{
"Content-Type" = "application/json"
"api-key" = $azOpenAiKey
"accept" = "application/json, text/plain, */*"
"accept-language" = "en"
}
$messages = $incidentMessages |Out-String
$prompt = @"
summarize the incident chat log in the following format returning a structured markdown document. Make sure to include the following fields as headings
Start time:
End time:
Observed issue:
Root cause:
Action items:
Participants:
$messages
summary:
"@
$body = @{
"prompt" = "$prompt"
"max_tokens" = 500
"temperature" = 0.3
"top_p" = 1
"frequency_penalty" = 0.2
"presence_penalty" = 0.2
"stop" = "tokens"
} | ConvertTo-Json -EscapeHandling EscapeNonAscii
$summary = Invoke-RestMethod -uri $openAiUrl -Method Post -Headers $openAiHeaders -Body $body
$summary.choices.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment