Last active
March 2, 2023 09:02
-
-
Save anoopt/807b9da3480058c510237833f20a6f39 to your computer and use it in GitHub Desktop.
Multilingual SharePoint Page Summarization with Open AI API and Microsoft Graph PowerShell SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A script to get summary of a SharePoint site page using GPT-3.5 turbo | |
# The script accepts the following parameters: | |
# SiteAbsoluteUrl, PageName | |
Param( | |
[Parameter(Mandatory = $true)] | |
[string]$SiteAbsoluteUrl, | |
[Parameter(Mandatory = $true)] | |
[string]$PageName | |
) | |
# function to connect to Microsoft Graph | |
# See https://learn.microsoft.com/en-us/powershell/microsoftgraph/get-started?view=graph-powershell-1.0 for more details | |
function Connect-Graph { | |
Write-Host "Connecting to Microsoft Graph."; | |
# Select Microsoft Graph beta endpoint | |
Select-MgProfile -Name "beta" | |
# Connect to Microsoft Graph | |
Connect-MgGraph -Scopes "Sites.Read.All" | |
} | |
function Get-SiteId { | |
Write-Host "Getting site Id."; | |
# remove trailing slash from site absolute url | |
$SiteAbsoluteUrl = $SiteAbsoluteUrl.TrimEnd("/"); | |
# SiteAbsoluteUrl is of the form https://<tenant>.sharepoint.com/sites/<site> | |
# We need to get a string of the form <tenant>.sharepoint.com:/sites/<site> | |
$siteUri = New-Object System.Uri($SiteAbsoluteUrl); | |
$siteId = $siteUri.Host + ":" + $siteUri.AbsolutePath; | |
$site = Get-MgSite -SiteId $siteId; | |
if($null -eq $site) { | |
Write-Host "Error: Unable to get site Id. Check if the site exists." -ForegroundColor Red; | |
Exit; | |
} | |
return $site.Id; | |
} | |
# function to get page content | |
function Get-PageContent { | |
param( | |
[Parameter(Mandatory = $true)] | |
[string]$SiteId | |
) | |
Write-Host "Getting page content."; | |
$pageContent = ""; | |
# Get content from all the text web parts in the page | |
$page = Get-MgSitePage -SiteId $SiteId -Filter "name eq '$PageName'" ` | |
-ExpandProperty "webparts(`$filter=(isof('microsoft.graph.textWebPart')))"; | |
if($null -eq $page) { | |
Write-Host "Error: Unable to get page content. Check if the page exists." -ForegroundColor Red; | |
Exit; | |
} | |
$page.WebParts | ForEach-Object { | |
$pageContent += $_.AdditionalProperties.innerHtml | |
} | |
$pageContent = $pageContent -replace "<[^>]+>", ""; | |
$pageContent = $pageContent -replace " ", " "; | |
return $pageContent; | |
} | |
# function to get page summary using openai api | |
function Get-PageSummary { | |
param( | |
[Parameter(Mandatory = $true)] | |
[string]$PageContent | |
) | |
$openai_api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
$openai_api_endpoint = "https://api.openai.com/v1/chat/completions"; | |
$data = @{} | |
$data["model"] = "gpt-3.5-turbo"; | |
$data["messages"] = @(@{}); | |
$data["messages"][0]["role"] = "user"; | |
$messageContent = "1 short sentence summary in English, French and Spanish of this: {{content}}. Output format: 'English: 'xxx' `n French: 'xxx' `n Spanish: 'xxx'"; | |
$data["messages"][0]["content"] = $messageContent.Replace("{{content}}", $PageContent); | |
$headers = @{ | |
"Content-Type" = "application/json" | |
"Authorization" = "Bearer " + $openai_api_key | |
} | |
Write-Host "Calling OpenAI API to get page summary."; | |
$response = Invoke-WebRequest -Method Post -Uri $openai_api_endpoint -Headers $headers -Body ($data | ConvertTo-Json); | |
if ($response -and $response.StatusCode -eq 200) { | |
$result = $response.Content | ConvertFrom-Json | Select-Object -ExpandProperty choices | Select-Object -ExpandProperty message | Select-Object -ExpandProperty content; | |
} else { | |
$result = $null; | |
} | |
return $result; | |
} | |
# main | |
$start = Get-Date; | |
Connect-Graph; | |
$siteId = Get-SiteId; | |
$pageContent = Get-PageContent -SiteId $siteId; | |
$pageSummary = Get-PageSummary -PageContent $pageContent; | |
If($null -eq $pageSummary) { | |
Write-Host "Error: Unable to get page summary." -ForegroundColor Red; | |
Exit; | |
} | |
Write-Host "### Page summary ###" | |
Write-Host $pageSummary -ForegroundColor Green | |
Write-Host "`n ### End of page summary ###" | |
$end = Get-Date; | |
$timespan = New-TimeSpan $start $end | |
$seconds = $timespan.TotalSeconds | |
Write-Host "Time taken: $seconds seconds" -ForegroundColor Green | |
# end of script | |
<# | |
Example usage: .\get-page-summary-using-gpt-3.5-turbo.ps1 -SiteAbsoluteUrl "https://contoso.sharepoint.com/sites/teamsite" -PageName "UK-News-1.aspx" | |
Welcome To Microsoft Graph! | |
Getting site Id. | |
Getting page content. | |
Calling OpenAI API to get page summary. | |
### Page summary ### | |
English: The UK and EU are close to reaching a deal on revising post-Brexit trade rules for Northern Ireland, but an agreement is not yet guaranteed. | |
French: Le Royaume-Uni et l'UE sont sur le point de conclure un accord sur la révision des règles commerciales post-Brexit pour l'Irlande du Nord, mais un accord n'est pas encore garanti. | |
Spanish: El Reino Unido y la UE están cerca de llegar a un acuerdo sobre la revisión de las reglas comerciales posteriores al Brexit para Irlanda del Norte, pero aún no se garantiza el acuerdo. | |
### End of page summary ### | |
Time taken: 2.7298925 seconds | |
#> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A script to get summary of a SharePoint site page | |
# The script accepts the following parameters: | |
# SiteAbsoluteUrl, PageName | |
Param( | |
[Parameter(Mandatory = $true)] | |
[string]$SiteAbsoluteUrl, | |
[Parameter(Mandatory = $true)] | |
[string]$PageName | |
) | |
# function to connect to Microsoft Graph | |
# See https://learn.microsoft.com/en-us/powershell/microsoftgraph/get-started?view=graph-powershell-1.0 for more details | |
function Connect-Graph { | |
Write-Host "Connecting to Microsoft Graph."; | |
# Select Microsoft Graph beta endpoint | |
Select-MgProfile -Name "beta" | |
# Connect to Microsoft Graph | |
Connect-MgGraph -Scopes "Sites.Read.All" | |
} | |
function Get-SiteId { | |
Write-Host "Getting site Id."; | |
# remove trailing slash from site absolute url | |
$SiteAbsoluteUrl = $SiteAbsoluteUrl.TrimEnd("/"); | |
# SiteAbsoluteUrl is of the form https://<tenant>.sharepoint.com/sites/<site> | |
# We need to get a string of the form <tenant>.sharepoint.com:/sites/<site> | |
$siteUri = New-Object System.Uri($SiteAbsoluteUrl); | |
$siteId = $siteUri.Host + ":" + $siteUri.AbsolutePath; | |
$site = Get-MgSite -SiteId $siteId; | |
if($null -eq $site) { | |
Write-Host "Error: Unable to get site Id. Check if the site exists." -ForegroundColor Red; | |
Exit; | |
} | |
return $site.Id; | |
} | |
# function to get page content | |
function Get-PageContent { | |
param( | |
[Parameter(Mandatory = $true)] | |
[string]$SiteId | |
) | |
Write-Host "Getting page content."; | |
$pageContent = ""; | |
# Get content from all the text web parts in the page | |
$page = Get-MgSitePage -SiteId $SiteId -Filter "name eq '$PageName'" ` | |
-ExpandProperty "webparts(`$filter=(isof('microsoft.graph.textWebPart')))"; | |
if($null -eq $page) { | |
Write-Host "Error: Unable to get page content. Check if the page exists." -ForegroundColor Red; | |
Exit; | |
} | |
$page.WebParts | ForEach-Object { | |
$pageContent += $_.AdditionalProperties.innerHtml | |
} | |
$pageContent = $pageContent -replace "<[^>]+>", ""; | |
$pageContent = $pageContent -replace " ", " "; | |
return $pageContent; | |
} | |
# function to get page summary using openai api | |
function Get-PageSummary { | |
param( | |
[Parameter(Mandatory = $true)] | |
[string]$PageContent | |
) | |
$openai_api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
$openai_api_endpoint = "https://api.openai.com/v1/completions"; | |
$data = @{ | |
"model" = "text-davinci-003"; | |
"prompt" = "1 short sentence summary in English, French and Spanish of this: {{content}}.'English: 'xxx' \n French: 'xxx' \n Spanish: 'xxx'"; | |
"temperature" = 0.7; | |
"max_tokens" = 256; | |
"top_p" = 1.0; | |
"frequency_penalty" = 0.0; | |
"presence_penalty" = 0.; | |
} | |
$data["prompt"] = $data["prompt"].Replace("{{content}}", $PageContent); | |
$headers = @{ | |
"Content-Type" = "application/json" | |
"Authorization" = "Bearer " + $openai_api_key | |
} | |
Write-Host "Calling OpenAI API to get page summary."; | |
$response = Invoke-WebRequest -Method Post -Uri $openai_api_endpoint -Headers $headers -Body ($data | ConvertTo-Json); | |
if ($response -and $response.StatusCode -eq 200) { | |
$result = $response.Content | ConvertFrom-Json | Select-Object -ExpandProperty "choices" | Select-Object -ExpandProperty "text"; | |
} else { | |
$result = $null; | |
} | |
return $result; | |
} | |
# main | |
Connect-Graph; | |
$siteId = Get-SiteId; | |
$pageContent = Get-PageContent -SiteId $siteId; | |
$pageSummary = Get-PageSummary -PageContent $pageContent; | |
If($null -eq $pageSummary) { | |
Write-Host "Error: Unable to get page summary." -ForegroundColor Red; | |
Exit; | |
} | |
Write-Host "### Page summary ###" | |
Write-Host $pageSummary -ForegroundColor Green | |
Write-Host "`n ### End of page summary ###" | |
# end of script | |
<# | |
Example usage: .\get-page-summary.ps1 -SiteAbsoluteUrl "https://contoso.sharepoint.com/sites/teamsite" -PageName "UK-News-1.aspx" | |
Sample output: | |
Welcome To Microsoft Graph! | |
Getting site Id. | |
Getting page content. | |
Calling OpenAI API to get page summary. | |
### Page summary ### | |
English: UK and EU are progressing towards an agreement on post-Brexit trade rules in Northern Ireland. | |
French: Le Royaume-Uni et l'Union européenne progressent vers un accord sur les règles commerciales post-Brexit en Irlande du Nord. | |
Spanish: Reino Unido y la UE están avanzando hacia un acuerdo sobre las normas comerciales post-Brexit en Irlanda del Norte. | |
### End of page summary ### | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment