Exercise: Build and deploy an Azure AI Document Intelligence custom skill

Completed

In this exercise, you'll create and deploy a web service that acts as a custom skill in a AI Search pipeline. The web skill calls Azure AI Document Intelligence to extract invoice fields, such as customer names, so that they can be included in the index.

Run Cloud Shell

To start the exercise, let's connect to Cloud Shell, which you'll use to run the setup script:

  1. In the Azure portal, select the [>_] (Cloud Shell) button at the top of the page to the right of the search box. This opens a Cloud Shell pane at the bottom of the portal.

    Screenshot showing how to open Cloud Shell in the Azure portal.

  2. The first time you open the Cloud Shell, you might be prompted to choose the type of shell you want to use (Bash or PowerShell). Select Bash. If you don't see this option, skip the step.

  3. If you're prompted to create storage for your Cloud Shell, ensure your subscription is specified and select Create storage. Then wait a minute or so for the storage to be created.

  4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is switched to Bash. If it's PowerShell, switch to Bash by using the drop-down menu.

  5. Wait for Bash to start.

Set up resources

We'll use a script to create the Azure Forms Recognizer resource, a resource group, and the AI Search resource:

  1. In the Cloud Shell, to clone the code repository, enter this command:

    rm -r doc-intelligence -f
    git clone https://github.com/MicrosoftLearning/mslearn-ai-document-intelligence doc-intelligence
    

    Tip

    If you recently used this command in another lab to clone the doc-intelligence repository, you can skip this step.

  2. Change to the 04-custom-skill directory and then execute the setup script:

    cd doc-intelligence/Labfiles/04-custom-skill/
    bash setup.sh
    

Create an Azure Function

You'll host your custom skill in an Azure Function App. Let's create that Function App:

  1. In the Azure portal, select Create a resource.

  2. In the Search services and marketplace textbox, type Function App and then select Function App.

  3. In the Function App page, select Create.

  4. In the Subscription drop-down list, select your subscription.

  5. In the Resource group drop-down list, select FormsRecognizerResources.

  6. In the Function app name textbox, type a unique name.

  7. Next to Publish select Code.

  8. In the Runtime stack drop-down list, select Python.

  9. In the Version drop-down list, select 3.7.

  10. In the Region drop-down list, select West US.

  11. In the Plan type drop-down list, select Consumption (Serverless) and then select Review + create.

    Screenshot showing how to create a new Function App in the Azure portal.

  12. Review your configuration and then select Create. Azure creates the function app.

Configure the deployed Function

The Python code that you'll deploy needs to know the endpoint and API key for your Azure AI Document Intelligence resource. We'll obtain those values from the Azure portal and use the Function Apps application settings to configure them:

  1. In the Azure portal, select All resources and then select FormsRecognizer.

  2. Select Keys and Endpoint tab. To the right of the Endpoint textbox, select the Copy to clipboard button.

  3. Open Notepad and paste the endpoint value.

  4. In the Azure portal, to the right of the KEY 1 textbox, select the Copy to clipboard button.

  5. Switch to Notepad and paste the key on a new line.

  6. In the Azure portal, select All resources and then select the function app you created in the previous section.

  7. Under Settings, select Configuration and then select + New application setting.

  8. In the Add/Edit application setting dialog, in the Name textbox, type FORMS_RECOGNIZER_ENDPOINT.

  9. Copy the endpoint value from Notepad, paste it in the Value textbox, and then select OK.

  10. Select + New application setting.

  11. In the Add/Edit application setting dialog, in the Name textbox, type FORMS_RECOGNIZER_KEY.

  12. Copy the key from Notepad, paste it in the Value textbox, and then select OK.

  13. At the top of the Configuration page, select Save and then select Continue. The function app configuration is complete.

    Screenshot showing application settings in the Azure portal.

Publish the Function

Next, deploy the Python code for the Function App:

  1. In the Cloud Shell, change to the customskill folder:

    cd ~/doc-intelligence/Labfiles/04-custom-skill/customskill
    
  2. To use the Azure Functions Core Tools to deploy the Python code to your Function App, type this command and substitute <FunctionName> for the name you used when you created the Function App:

    func azure functionapp publish <FunctionName> --python
    
  3. When the deployment is complete, in the Azure portal, select All resources and then select your Function App.

  4. Under Functions, select Functions. If no functions are displayed in the list, wait for a minute and then select Refresh.

    Screenshot showing three published functions in the Azure portal.

    Azure displays three functions: AnalyzeForm, AnalyzeInvoice, and ExtractTables.

Test the Function

We can use the Azure portal Code + Test tool to submit a test invoice to the function and see whether the inputs and output are as expected by AI Search:

  1. In a new browser tab, browse to the sample invoice and examine the PDF file: https://github.com/MicrosoftLearning/mslearn-ai-document-intelligence/blob/main/Labfiles/04-custom-skill/SampleInvoices/Invoice_1.pdf

  2. In the Azure portal, in the Function App, under Functions select Functions and then select AnalyzeInvoice.

  3. Under Developer select Code + Test.

  4. At the top of the code window, select Test/Run.

  5. In the HTTP method drop-down list, select POST.

  6. In the Body textbox, enter this JSON code, and then select Run:

    {
        "values": [
            {
                "recordId": "record1",
                "data": { 
                    "formUrl": "https://raw.githubusercontent.com/MicrosoftLearning/mslearn-ai-document-intelligence/main/Labfiles/04-custom-skill/SampleInvoices/Invoice_1.pdf",
                    "formSasToken": "?st=sasTokenThatWillBeGeneratedByCognitiveSearch"
                }
            }
        ]
    }
    
  7. If the function is configured correctly, the output displayed includes values for CustomerAddress, CustomerAddressRecipient, and other keys. Compare the values returned with the original PDF file you opened above.

Add the Function to the AI Search skillset

To configure Azure AI Services to use the new Azure Function that calls Azure AI Document Intelligence, you must add a skillset:

  1. In the Azure portal, select All resources and then select the Azure Function App you created in the previous section.

  2. Next to the URL field, select the Copy to clipboard button.

  3. Switch to Notepad and paste the URL on a new line.

  4. In the Azure portal, under Functions, select App keys and then select Show values.

  5. To the right of the default host key, select the Copy to clipboard button.

  6. Switch to Notepad and paste the key on a new line.

  7. In the Azure portal, select All resources and then select enrichedcognitivesearch.

  8. On the Overview page for the AI Search resource, select the Skillsets tab and then select Add skillset.

  9. Copy the following JSON code and then paste it into the Skillset definition (JSON) textbox, replacing the default content:

    { 
        "name": "[SkillsetName]",
        "description":"Skillset that calls an Azure AI Document Intelligence custom skill",
        "skills":[ 
            { 
                "@odata.type":"#Microsoft.Skills.Custom.WebApiSkill",
                "name":"formrecognizer",
                "description":"Analyze invoices and extract fields.",
                "uri":"[EndpointUrl]/api/analyze-form?code=[AzureFunctionDefaultHostKey]",
                "httpMethod":"POST",
                "timeout":"PT30S",
                "context":"/document",
                "batchSize":1,
                "inputs":[ 
                    { 
                        "name":"formUrl",
                        "source":"/document/metadata_storage_path"
                    },
                    { 
                        "name":"formSasToken",
                        "source":"/document/metadata_storage_sas_token"
                    }
                ],
                "outputs":[ 
                    { 
                        "name":"address",
                        "targetName":"address"
                    },
                    { 
                        "name":"recipient",
                        "targetName":"recipient"
                    }
                ]
            }
        ],
        "cognitiveServices": {
            "@odata.type": "#Microsoft.Azure.Search.DefaultCognitiveServices"
        }
    }
    
  10. Replace [SkillsetName] with a unique name for your skillset.

  11. From Notepad, copy the Function App URL and paste it into the JSON code, replacing [EndpointUrl].

  12. From Notepad, copy the default host key and paste it into the JSON code, replacing [AzureFunctionDefaultHostKey].

  13. At the top left, select Save. You've completed the integration of AI Search with Forms Recognizer.

Clean up

Let's remove the exercise resources from your Azure subscription:

  1. In Azure portal, select Resource groups.
  2. Select FormsRecognizerResources and then select Delete resource group.
  3. In the TYPE RESOURCE GROUP NAME textbox, type FormsRecognizerResources and then select Delete. Azure removes the resource group, the Azure AI Document Intelligence resource, the AI Search resource, and their associated resources.

Learn more