I'm trying to get a code sample to use Azure Document Intelligence with files located on an in-house server but I'm getting the following error:
Azure.RequestFailedException: 'Resource not found
Status: 404 (Not Found)
ErrorCode: 404
Content:
{"error":{"code":"404","message":"Resource not found"}}
Headers:
Content-Length: 55
Content-Type: application/json
Date: Thu, 08 Aug 2024 14:08:41 GMT
I'm following the steps provided in this articleGet started with Document Intelligence but I've adjusted it to use a local file instead of a uri.
This is my code:
string endpoint = "https://<MyServiceInstance>.cognitiveservices.azure.com/";
string apiKey = "<MyApiKey>";
string filename = "C:\\..\\MyFile.jpg";
var client = new DocumentIntelligenceClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
using (FileStream stream = new FileStream(filename, FileMode.Open))
{
var result = AnalyzeDocumentAsync(client, stream).GetAwaiter().GetResult();
Console.WriteLine(result);
}
static async Task<Operation<AnalyzeResult>> AnalyzeDocumentAsync(
DocumentIntelligenceClient client,
Stream documentStream)
{
string modelId = "MyModelId";
var content = new AnalyzeDocumentContent()
{
Base64Source = BinaryData.FromStream(documentStream)
};
var operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, modelId, content);
AnalyzeResult result = operation.Value;
Console.WriteLine($"Document was analyzed with model with ID: {result.ModelId}");
foreach (AnalyzedDocument document in result.Documents)
{
Console.WriteLine($"Document of type: {document.DocType}");
foreach (KeyValuePair<string, DocumentField> fieldKvp in document.Fields)
{
string fieldName = fieldKvp.Key;
DocumentField field = fieldKvp.Value;
Console.WriteLine($"Field '{fieldName}': ");
Console.WriteLine($" Content: '{field.Content}'");
Console.WriteLine($" Confidence: '{field.Confidence}'");
}
}
return operation;
}
The line that's throwing the exception is:
var operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, modelId, content);
I'm getting my settings from Azure as per instructed in article:
and my model is a "Custom Model" and I'm using the name of the Id I used when training the model.
Anyone has any ideas what I'm missing?
DocumentIntelligenceClient
is an outdated configuration Instead of it use DocumentAnalysisClient
, which is the recommended class for interacting with Azure Form Recognizer and Document Intelligence services.Using
AnalyzeDocumentAsync
withAnalyzeDocumentContent
andBase64Source
, which is not the correct approach for current SDK methods.
AnalyzeDocumentAsync
with a Stream
directly, which aligns with the latest SDK methods.Code:
using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;
using System;
using System.IO;
using System.Threading.Tasks;
namespace ConsoleApp9
{
internal class Program
{
static async Task Main(string[] args)
{
string endpoint = "https://<YourServiceInstance>.cognitiveservices.azure.com/";
string apiKey = "<YourApiKey>";
string filename = "C:\\path\\to\\your\\file.pdf"; // Update with your file path
string modelId = "<YourModelId>"; // Ensure this is the correct model ID
var client = new DocumentAnalysisClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
using (FileStream stream = new FileStream(filename, FileMode.Open))
{
var result = await AnalyzeDocumentAsync(client, modelId, stream);
Console.WriteLine($"Model ID used: {result.ModelId}");
}
}
static async Task<AnalyzeResult> AnalyzeDocumentAsync(
DocumentAnalysisClient client,
string modelId,
Stream documentStream)
{
try
{
// Analyze the document with the specified model
AnalyzeDocumentOperation operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, modelId, documentStream);
AnalyzeResult result = operation.Value;
Console.WriteLine($"Document analyzed with model ID: {result.ModelId}");
// Process the analyzed document
foreach (AnalyzedDocument document in result.Documents)
{
Console.WriteLine($"Document has the following fields:");
foreach (KeyValuePair<string, DocumentField> fieldKvp in document.Fields)
{
string fieldName = fieldKvp.Key;
DocumentField field = fieldKvp.Value;
Console.WriteLine($"Field '{fieldName}': ");
Console.WriteLine($" Content: '{field.Content}'");
Console.WriteLine($" Confidence: '{field.Confidence}'");
}
}
return result;
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Error: {ex.Message}");
throw;
}
}
}
}
Model test in portal for reference:
Result: