page_type: sample languages:
This sample shows how to take text documents as a input via BlobTrigger, does Text Summarization processing using the AI Congnitive Language Service and ExtractiveSummarize operations, then computes sentiment scores, and then outputs to another text document using BlobOutput binding.
1) .NET 8 SDK 2) Azure Functions Core Tools 3) Azurite
The easiest way to install Azurite is using a Docker container or the support built into Visual Studio:
docker run -d -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite
4) Azure Developer CLI 5) Once you have your Azure subscription, run the following in a new terminal window to create all the AI Language and other resources needed:
azd provision
Take note of the value of TEXT_ANALYTICS_ENDPOINT
which can be found in ./.azure/<env name from azd provision>/.env
. It will look something like:
TEXT_ANALYTICS_ENDPOINT="https://<unique string>.cognitiveservices.azure.com/"
Alternatively you can create a Language resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource and view the Endpoint value.
5) Azure Storage Explorer or storage explorer features of Azure Portal
6) Add this local.settings.json
file to the ./text_summarization
folder to simplify local development. Fill in the TEXT_ANALYTICS_ENDPOINT value per step 4. This file will be gitignored to protectfrom committing to your repo.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"TEXT_ANALYTICS_ENDPOINT": "<insert from step 4>"
}
}
1) Open text_summarization.sln
using Visual Studio 2022 or later
2) Ensure local.settings.json
exists already using steps above
3) Press Run (F5
) to run in the debugger
4) Open Storage Explorer, Storage Accounts -> Emulator -> Blob Containers -> and create a container unprocessed-text
if it does not already exists
5) Copy any .txt document file with text into the unprocessed-text
container
You will see AI analysis happen in the Terminal standard out. The analysis will be saved in a .txt file in the processed-text
blob container.
1) Open the root folder in VS Code:
code .
2) Ensure local.settings.json
exists already using steps above
3) Run and Debug by pressing F5
4) Open Storage Explorer, Storage Accounts -> Emulator -> Blob Containers -> and create a container unprocessed-text
if it does not already exists
5) Copy any .txt document file with text into the unprocessed-text
container
You will see AI analysis happen in the Terminal standard out. The analysis will be saved in a .txt file in the processed-text
blob container.
1) Ensure local.settings.json
exists already using steps above
2) Open a new terminal and do the following:
cd text_summarization
func start
3) Open Storage Explorer, Storage Accounts -> Emulator -> Blob Containers -> and create a container test-samples-trigger
if it does not already exists
4) Copy any .txt document file with text into the test-samples-trigger
container
You will see AI analysis happen in the Terminal standard out. The analysis will be saved in a .txt file in the test-samples-output
blob container.
The easiest way to deploy this app is using the Azure Developer CLI. If you open this repo in GitHub CodeSpaces the AZD tooling is already preinstalled.
To provision and deploy: 1) Open a new terminal and do the following from root folder:
azd up
The main operation of the code starts with the summarize_function
function in summarize_function.cs. The function is triggered by a Blob uploaded event using BlobTrigger, your code runs to do the processing with AI, and then the output is returned as another blob file simply by returning a value and using the BlobOutput binding.
[Function("summarize_function")]
[BlobOutput("processed-text/{name}-output.txt")]
public async Task<string> Run(
[BlobTrigger("unprocessed-text/{name}", Source = BlobTriggerSource.EventGrid)] string myTriggerItem,
FunctionContext context)
{
var logger = context.GetLogger("summarize_function");
logger.LogInformation($"Triggered Item = {myTriggerItem}");
// Create client using Entra User or Managed Identity (no longer AzureKeyCredential)
// This requires a sub domain name to be set in endpoint URL for Managed Identity support
// See https://learn.microsoft.com/en-us/azure/ai-services/authentication#authenticate-with-microsoft-entra-id
var client = new TextAnalyticsClient(endpoint, new DefaultAzureCredential());
// analyze document text using Azure Cognitive Language Services
var summarizedText = await AISummarizeText(client, myTriggerItem, logger);
logger.LogInformation(Newline() + "*****Summary*****" + Newline() + summarizedText);
// Blob Output
return summarizedText;
}
The AISummarizeText
helper function does the heavy lifting for summary extraction and sentiment analysis using the TextAnalyticsClient
SDK from the AI Language Services:
static async Task<string> AISummarizeText(TextAnalyticsClient client, string document, ILogger logger)
{
// ...
// Start analysis process.
ExtractiveSummarizeOperation operation = client.ExtractiveSummarize(WaitUntil.Completed, batchInput);
// View operation status.
summarizedText += $"AnalyzeActions operation has completed" + Newline();
summarizedText += $"Created On : {operation.CreatedOn}" + Newline();
summarizedText += $"Expires On : {operation.ExpiresOn}" + Newline();
summarizedText += $"Id : {operation.Id}" + Newline();
summarizedText += $"Status : {operation.Status}" + Newline();
// ...
// Perform sentiment analysis on document summary
var sentimentResult = await client.AnalyzeSentimentAsync(summarizedText);
Console.WriteLine($"\nSentiment: {sentimentResult.Value.Sentiment}");
Console.WriteLine($"Positive Score: {sentimentResult.Value.ConfidenceScores.Positive}");
Console.WriteLine($"Negative Score: {sentimentResult.Value.ConfidenceScores.Negative}");
Console.WriteLine($"Neutral Score: {sentimentResult.Value.ConfidenceScores.Neutral}");
var summaryWithSentiment = summarizedText + $"Sentiment: {sentimentResult.Value.Sentiment}" + Newline();
return summaryWithSentiment;
}