Run Javascript Laugnauage AI PII to read and write to Azure Storage Container

Glen Sale 0 Reputation points
2024-02-26T23:30:12.45+00:00

Hi I am new to language AI.
How do I grab a textfile and upload the completed version back to azure storage container (read/write)

So far I have these code in javascript

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
 * This sample uses the PII-recognition endpoint to detect sensitive
 * personally identifiable information in documents (such as social security
 * numbers, addresses, and more). The API returns information about the
 * location of the sensitive information in the text, which we use to perform
 * redaction of the PII text.
 *
 * @summary detects personally-identifiable information
 */

const {
    TextAnalysisClient,
    AzureKeyCredential,
    KnownPiiEntityDomain,
    KnownPiiEntityCategory,
} = require("@azure/ai-language-text");


process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
// Load the .env file if it exists
require("dotenv").config();

async function main() {
    console.log(`Running recognizePii sample`);

    // You will need to set these environment variables or edit the following values
    const endpoint = "<>>"
    const apiKey = "<>>"

    const client = new TextAnalysisClient(endpoint, new AzureKeyCredential(apiKey));

    const documents = ["My phone number is 555-5555"];

    const [result] = await client.analyze("PiiEntityRecognition", documents, "en", {
        domainFilter: KnownPiiEntityDomain.Phi,
        categoriesFilter: [
            KnownPiiEntityCategory.PhoneNumber,
            KnownPiiEntityCategory.USSocialSecurityNumber,
        ],
    });

    if (!result.error) {
        console.log(`Redacted text: "${result.redactedText}"`);
        console.log("Pii Entities: ");
        for (const entity of result.entities) {
            console.log(`\t- "${entity.text}" of type ${entity.category}`);
        }
    }
}

main().catch((err) => {
    console.error("The sample encountered an error:", err);
});

module.exports = { main };

In CURL request they have something in JSON to call source/target of azure storage container. How do I do that in javascript?

{
    "displayName": "Extracting Location & US Region",
    "analysisInput": {
        "documents": [
            {
                "language": "en-US",
                "id": "Output-excel-file",
                "source": {
                    "location": "<>"
                },
                "target": {
                    "location": "<>"
                }
            }
        ]
    },
    "tasks": [
        {
            "kind": "PiiEntityRecognition",
            "parameters": {
                "excludePiiCategories": [
                    "PersonType",
                    "Category2",
                    "Category3"
                ],
                "redactionPolicy": "UseRedactionCharacterWithRefId"
            }
        }
    ]
}
Azure AI Language
Azure AI Language
An Azure service that provides natural language capabilities including sentiment analysis, entity extraction, and automated question answering.
355 questions
{count} votes

1 answer

Sort by: Most helpful
  1. navba-MSFT 17,115 Reputation points Microsoft Employee
    2024-02-27T06:50:52.95+00:00

    @Glen Sale Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    Here is the sample code which follows this documentation:

    As pre-requisites, you need to first follow the steps mentioned here.

    const fs = require('fs');
    const path = require('path');
     
    // Read the JSON file
    const data = fs.readFileSync(path.join(__dirname, 'pii-detection.json'), 'utf8');
     
    const postUrl = "https://MyLanguageAiService.cognitiveservices.azure.com/language/analyze-documents/jobs?api-version=2023-11-15-preview";
    const headers = {
        "Content-Type": "application/json",
        "Ocp-Apim-Subscription-Key": "84b4e3XXXXXXXXXXXXX3d6c360cc"
    };
     
    fetch(postUrl, {
        method: "POST",
        headers: headers,
        body: data  // use the data read from the file
    })
    .then(response => {
        const operationLocation = response.headers.get('operation-location');
        return operationLocation;
    })
    .then(operationLocation => {
        const intervalId = setInterval(() => {
            fetch(operationLocation, {
                method: "GET",
                headers: headers
            })
            .then(response => response.json())
            .then(data => {
                if (data.status !== 'running' && data.status !== 'notStarted') {
                    clearInterval(intervalId);
                    if (data.status === 'succeeded') {
                        data.results.documents.forEach(document => {
                            document.targets.forEach(target => {
                                console.log(target.location);
                            });
                        });
                    }
                }
            })
           .catch((error) => console.error('Error:', error));
        }, 1000);
    })
    .catch((error) => console.error('Error:', error));
    

    The above javascript sample code :

    • Reads a JSON file: It uses the fs (file system) module in Node.js to read the content of a JSON file named ‘pii-detection.json’ located in the same directory as the script. The content of the file is stored in the data variable.
    • Makes a POST request: It sends a POST request to the URL specified in postUrl (which appears to be an API endpoint for Azure’s Language AI Service). The headers for this request include a content type of ‘application/json’ and a subscription key. The body of the request is the data read from the ‘pii-detection.json’ file.
    • Handles the response: It then handles the response from the POST request. If the request is successful, it retrieves the ‘operation-location’ from the response headers.
    • Polls the operation location: It starts an interval that sends a GET request to the operation location every second. This is likely checking the status of a long-running operation that was started by the POST request.
    • Processes the operation results: Once the operation status is neither ‘running’ nor ‘notStarted’, it stops the interval. If the operation status is ‘succeeded’, it logs the ‘location’ property of each ‘target’ in each ‘document’ in the operation results.
    • Error handling: If any errors occur during the fetch operations, they are logged to the console.

    Note: I haven't tested the above code at my end, since I am waiting for the approval from the gating team to provide access to the Analyze Documents public preview feature.

    0 comments No comments