Designed for developers familiar with Azure and generative AI, the guide walks you through the process of harnessing the power of the prebuilt model for document intelligence.
The Microsoft team has recently launched an Open AI challenge in which a developer can learn how to build Azure AI solutions and apps.
The article demonstrates the use of a Prebuilt Model — Invoice in the document intelligence service.
Use a predefined model to identify and collect standard elements from particular types of documents. In this instance, we employ the built-in invoice model to process and extract information from an invoice.
The guide will cover the following features.
Reference: Sample document
Search for Azure Document Intelligence and fill out the following details
Enable system-assigned identity to grant the resource access to other existing resources. For this demonstration, we don't require identity.
For this exercise, tag names are not required. But in a production environment, it should be added as it’s a best practice.
Post validation checks by Azure Cloud and proceed with creating the resource. Make sure you review the details entered in the previous steps.
Please find below the sample document highlighting the fields that will be processed as part of this exercise using C# code.
The expected output should be as follows, along with the confidence score
Vendor Name: 'CONTOSO LTD.', with confidence 0.93.
Customer Name: 'MICROSOFT CORPORATION', with confidence 0.915.
Invoice Total: '$110', with confidence 0.97.
To test image generation, create a console application in Visual Studio or Visual Studio Code.
dotnet new console
Read the configuration from appsettings.json file
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
string? endpoint = config["AzureEndpoint"];
string? apiKey = config["AzureKey"];
string? documentUrl = config["DocumentUrl"];
var cred = new AzureKeyCredential(apiKey);
var client = new DocumentAnalysisClient(new Uri(endpoint), cred);
Install the NuGet package using the following command
dotnet add package Azure.AI.FormRecognizer
and add the following code
AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-invoice", fileUri);
await operation.WaitForCompletionAsync();
AnalyzeResult result = operation.Value;
The final step is to display the invoice information to the user using the following code snippet.
To keep things straightforward, we’re not displaying every key-value pair returned by the service here. For a comprehensive list of all fields supported and their respective types, please refer to the Invoice concept page.
foreach (AnalyzedDocument invoice in result.Documents)
{
if (invoice.Fields.TryGetValue("VendorName", out DocumentField? vendorNameField))
{
if (vendorNameField.FieldType == DocumentFieldType.String)
{
string vendorName = vendorNameField.Value.AsString();
Console.WriteLine($"Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}.");
}
}
if (invoice.Fields.TryGetValue("CustomerName", out DocumentField? customerNameField))
{
if (customerNameField.FieldType == DocumentFieldType.String)
{
string customerName = customerNameField.Value.AsString();
Console.WriteLine($"Customer Name: '{customerName}', with confidence {customerNameField.Confidence}.");
}
}
if (invoice.Fields.TryGetValue("InvoiceTotal", out DocumentField? invoiceTotalField))
{
if (invoiceTotalField.FieldType == DocumentFieldType.Currency)
{
CurrencyValue invoiceTotal = invoiceTotalField.Value.AsCurrency();
Console.WriteLine($"Invoice Total: '{invoiceTotal.Symbol}{invoiceTotal.Amount}', with confidence {invoiceTotalField.Confidence}.");
}
}
}
Console.WriteLine("\nAnalysis complete.\n");
Make sure the document URL is added to the appsettings.json file.
The URL for the example document is provided below.
// See https://aka.ms/new-console-template for more information
// Build a config object and retrieve user settings.
using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;
using Microsoft.Extensions.Configuration;
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
string? endpoint = config["AzureEndpoint"];
string? apiKey = config["AzureKey"];
string? documentUrl = config["DocumentUrl"];
Uri fileUri = new Uri(uriString: documentUrl);
Console.WriteLine("\nConnecting to Forms Recognizer at: {0}", endpoint);
Console.WriteLine("Analyzing invoice at: {0}\n", fileUri.ToString());
// Create the client
var cred = new AzureKeyCredential(apiKey);
var client = new DocumentAnalysisClient(new Uri(endpoint), cred);
// Analyze the invoice
AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-invoice", fileUri);
await operation.WaitForCompletionAsync();
AnalyzeResult result = operation.Value;
// Display invoice information to the user
foreach (AnalyzedDocument invoice in result.Documents)
{
if (invoice.Fields.TryGetValue("VendorName", out DocumentField? vendorNameField))
{
if (vendorNameField.FieldType == DocumentFieldType.String)
{
string vendorName = vendorNameField.Value.AsString();
Console.WriteLine($"Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}.");
}
}
if (invoice.Fields.TryGetValue("CustomerName", out DocumentField? customerNameField))
{
if (customerNameField.FieldType == DocumentFieldType.String)
{
string customerName = customerNameField.Value.AsString();
Console.WriteLine($"Customer Name: '{customerName}', with confidence {customerNameField.Confidence}.");
}
}
if (invoice.Fields.TryGetValue("InvoiceTotal", out DocumentField? invoiceTotalField))
{
if (invoiceTotalField.FieldType == DocumentFieldType.Currency)
{
CurrencyValue invoiceTotal = invoiceTotalField.Value.AsCurrency();
Console.WriteLine($"Invoice Total: '{invoiceTotal.Symbol}{invoiceTotal.Amount}', with confidence {invoiceTotalField.Confidence}.");
}
}
}
Console.WriteLine("\nAnalysis complete.\n");
Make sure to give it a star on GitHub and provide feedback on how to improve the tool further..!! To access the document intelligence sample, move to the directory samples > Azure.OpenAI.DocumentIntelligence
GitHub - ssukhpinder/AzureOpenAI: Sample for testing Azure Open AI GPT3 Turbo Model
Thank you for being a part of the C# community! Before you leave:
Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr
Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev
More content at C# Programming
Also published here.