Get Chat GPT / Azure OpenAI responses based on documents in SharePoint / Teams

This is currently in preview, but here is the way to get started with having Chat GPT / Azure OpenAI giving responses based on your own internal documents you have store in a SharePoint document library.


Go to https://oai.azure.com/

If you already have access to Azure OpenAI, click Create a Resource.

Fill out the following and click Review/Create. The name you set here is the Resource name; you will need it in the PowerShell script further down. Click Review + create.

Click Create.

Wait while the resource is deploying.

Click Go to resource.

Click on the Model Deployment menu, give the deployment a name, and choose the model you want to use. In this case, we are using gpt-4. Click Save.

Click Keys and Endpoint.

Copy one of the keys and the endpoint URL. (take a look here to see other ways to authenticate)

Go to the Model Deployments menu, and click Manage Deployments.

We are now going to create a text with some HR information, that we can use to demo getting chat replies based on documents in SharePoint. Go to chat, and write the following prompt: Write me a document about Norwegian employee benefits.

Click Send.

Copy the text, and create a new Word document in a Teams team.

Click Open in SharePoint, and copy the SharePoint site URL.

Go to the Azure portal and search for Cognitive Search.

Click Create.

Choose your preferred pricing tier and setting. You cant use the free tier for this.

Click Rewview + Create.

Click Create.

When the deployment is complete, click Go to resource.

Copy the URL, and then go to the Keys menu and copy one of the admin keys.

Search for App Registrations.

Click New registration.

Give it a name and click register.

Copy the tenant id and application/client ID, the go to the API Permission menu.

Click Add Permission.

Click Microsoft Graph.

Select the Application permissions Files.Read.All and Sites.Read.All

Click Grant admin consent…

Go to the Certificates and secrets mene. Click New client secret.

Give it a description, and click Add.

Copy the value of the secret.

Run the following PowerShell script to add SharePoint as a data source in your Cognitive Search instance.

$AzureCognitiveSearchURL = "https://sharepointsearchinstance.search.windows.net"
$AzureCognitiveSearchAdminKey = "Wt1mZ0sjkKoXW913d2eJ8xXYJk4JHThk1UK8H0wZoIAzSeDjYTmC"
$SharePointSiteURL = "https://alexholmeset.sharepoint.com/sites/TestDemoTeam/"
$AzureAppregistrationID = "861eade5-bb96-41cc-af4b-55dce97b020e"
$AzureAppregistrationSecret = "8re8Q~pR6icm6oT5Fx8UwiDO~xgoeYp~8KGiXaF3"
$TenantID = "29594fb0-53f2-490c-a1be-8b2cca824805"
$Header = @{"api-key" = "$AzureCognitiveSearchAdminKey"
"content-type" = "application/json"
}
$Body = @"
{
"name" : "sharepoint-datasource",
"type" : "sharepoint",
"credentials" : { "connectionString" : "SharePointOnlineEndpoint=$SharePointSiteURL;ApplicationId=$AzureAppregistrationID;ApplicationSecret=$AzureAppregistrationSecret;TenantId=$TenantID" },
"container" : { "name" : "defaultSiteLibrary", "query" : null }
}
"@
$uri = "$AzureCognitiveSearchURL/datasources?api-version=2020-06-30-Preview"
invoke-restmethod -Method POST -Uri $uri -Body $body -Headers $Header

Run the following PowerShell script to add an index to your Cognitive Search instance.

$AzureCognitiveSearchURL = "https://sharepointsearchinstance.search.windows.net"
$AzureCognitiveSearchAdminKey = "Wt1mZ0sjkKoXW913d2eJ8xXYJk4JHThk1UK8H0wZoIAzSeDjYTmC"
$Header = @{"api-key" = "$AzureCognitiveSearchAdminKey"
"content-type" = "application/json"
}
$Body = @"
{
"name" : "sharepoint-index",
"fields": [
{ "name": "id", "type": "Edm.String", "key": true, "searchable": false },
{ "name": "metadata_spo_item_name", "type": "Edm.String", "key": false, "searchable": true, "filterable": false, "sortable": false, "facetable": false },
{ "name": "metadata_spo_item_path", "type": "Edm.String", "key": false, "searchable": false, "filterable": false, "sortable": false, "facetable": false },
{ "name": "metadata_spo_item_content_type", "type": "Edm.String", "key": false, "searchable": false, "filterable": true, "sortable": false, "facetable": true },
{ "name": "metadata_spo_item_last_modified", "type": "Edm.DateTimeOffset", "key": false, "searchable": false, "filterable": false, "sortable": true, "facetable": false },
{ "name": "metadata_spo_item_size", "type": "Edm.Int64", "key": false, "searchable": false, "filterable": false, "sortable": false, "facetable": false },
{ "name": "content", "type": "Edm.String", "searchable": true, "filterable": false, "sortable": false, "facetable": false }
]
}
"@
$uri = "$AzureCognitiveSearchURL/indexes?api-version=2020-06-30"
invoke-restmethod -Method POST -Uri $uri -Body $body -Headers $Header
view raw AddIndex.ps1 hosted with ❤ by GitHub

Run the following PowerShell script to add an indexer to your Cognitive Search instance.

$AzureCognitiveSearchURL = "https://sharepointsearchinstance.search.windows.net"
$AzureCognitiveSearchAdminKey = "Wt1mZ0sjkKoXW913d2eJ8xXYJk4JHThk1UK8H0wZoIAzSeDjYTmC"
$Header = @{"api-key" = "$AzureCognitiveSearchAdminKey"
"content-type" = "application/json"
}
$Body = @"
{
"name" : "sharepoint-indexer",
"dataSourceName" : "sharepoint-datasource",
"targetIndexName" : "sharepoint-index",
"parameters": {
"batchSize": null,
"maxFailedItems": null,
"maxFailedItemsPerBatch": null,
"base64EncodeKeys": null,
"configuration": {
"indexedFileNameExtensions" : ".pdf, .docx",
"excludedFileNameExtensions" : ".png, .jpg",
"dataToExtract": "contentAndMetadata"
}
},
"schedule" : { },
"fieldMappings" : [
{
"sourceFieldName" : "metadata_spo_site_library_item_id",
"targetFieldName" : "id",
"mappingFunction" : {
"name" : "base64Encode"
}
}
]
}
"@
$uri = "$AzureCognitiveSearchURL/indexers?api-version=2020-06-30"
invoke-restmethod -Method POST -Uri $uri -Body $body -Headers $Header
view raw AddIndexer.ps1 hosted with ❤ by GitHub

Run the following script with the question centered on your documents.

$Question = "How many sick days do i have?"
$AzureOpenAIEndpointURL = "https://sharepointdocumentschat.openai.azure.com/"
$AzureOpenAIEndpointKey = "7256974864a9499499ca4d3e70fa0c7b"
$AzureOpenAIModelDelpoymentName = "ChatGPT4"
$AzureCognitiveSearchURL = "https://sharepointsearchinstance.search.windows.net"
$AzureCognitiveSearchAdminKey = "Wt1mZ0sjkKoXW913d2eJ8xXYJk4JHThk1UK8H0wZoIAzSeDjYTmC"
$Header = @{"api-key" = "$AzureOpenAIEndpointKey"
"ChatGpt_Key" = "$AzureOpenAIEndpointKey "
"ChatGpt_Url" = "$AzureOpenAIEndpointURL"
"content-type" = "application/json"
}
$Body = @"
{
"dataSources": [
{
"type": "AzureCognitiveSearch",
"parameters": {
"endpoint": "$azurecognitivesearchurl",
"key": "$AzureCognitiveSearchAdminKey",
"indexName": "sharepoint-index"
}
}
],
"messages": [
{
"role": "user",
"content": "$Question"
}
]
}
"@
$uri = $AzureOpenAIEndpointURL + "openai/deployments/" + $AzureOpenAIModelDelpoymentName + "/extensions/chat/completions?api-version=2023-06-01-preview"
$meassage = invoke-restmethod -Method POST -Uri $uri -Body $body -Headers $Header
"Question: $Question"
"Answer: $($meassage.CHoices.messages[1].content)"
view raw Question.ps1 hosted with ❤ by GitHub

Here 2 questions with output based on the HR document we generated earlier. It also shows the name of the document it comes from.

This is a rough guide helping you to get started, there are other ways to authenticate and you can have the indexing running on a schedule also, so it picks up new documents. If you want to dig deeper, take a look at the following articles I have used to write this blog post:
https://learn.microsoft.com/en-gb/azure/search/search-howto-index-sharepoint-online
https://learn.microsoft.com/en-us/azure/ai-services/openai/use-your-data-quickstart?tabs=command-line&pivots=rest-api

5 thoughts on “Get Chat GPT / Azure OpenAI responses based on documents in SharePoint / Teams

  1. Hi Alex, good article, with a step-by-step guide to setting up Azure Cognitive search with a SharePoint indexer.

    Small point, the last script targets the Powershell script AddIndexer, instead of the script in the question. We can’t go through with the demo.

    I also have a few questions. Have you configured the link between Azure Open AI and Cognitive search in this article? The Azure Open AI record doesn’t seem to be used. Azure Open AI seems to be used only to generate the initial document. Am I wrong?

    Thanks in advance for your answers

    Like

    • Thanks.
      Ooops, a little copy paste error there of the wronge github code. Fixed it now 🙂

      In the now uploaded script at the end it does Azure OpenAI calls ,and uses the Cognitive Search key in the request towards Azure OpenAI.

      Like

  2. Hi Alex, Thanks for the step by step guide. Quick q: Can you please elaborate which $TenantID is used in the powershell script (Azure or SharePoint?. Does SharePoint and Azure need to be in the same Tenant. Thanks

    Like

    • Azure and SharePoint is in the same tenant is this blogpost. I think you can have them separate as you are using keys to authenticate, but i havent tested my self.

      Like

Leave a comment