-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAzureAIDocumentIntelligenceMarkdownConverter.cs
43 lines (38 loc) · 1.39 KB
/
AzureAIDocumentIntelligenceMarkdownConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Text;
using Azure;
using Azure.AI.DocumentIntelligence;
namespace EvaluationTests.Shared.Markdown;
public class AzureAIDocumentIntelligenceMarkdownConverter(DocumentIntelligenceClient client)
: IDocumentMarkdownConverter
{
public Task<byte[]?> FromUriAsync(string documentUri, CancellationToken cancellationToken = default)
{
return ToMarkdownAsync(
new AnalyzeDocumentContent { UrlSource = new Uri(documentUri) },
cancellationToken);
}
public Task<byte[]?> FromByteArrayAsync(byte[] documentBytes, CancellationToken cancellationToken = default)
{
return ToMarkdownAsync(
new AnalyzeDocumentContent { Base64Source = BinaryData.FromBytes(documentBytes) },
cancellationToken);
}
private async Task<byte[]?> ToMarkdownAsync(
AnalyzeDocumentContent content,
CancellationToken cancellationToken = default)
{
try
{
var operation = await client.AnalyzeDocumentAsync(
WaitUntil.Completed,
"prebuilt-layout",
content,
outputContentFormat: ContentFormat.Markdown, cancellationToken: cancellationToken);
return operation is { HasValue: true } ? Encoding.UTF8.GetBytes(operation.Value.Content) : default;
}
catch (Exception)
{
return default;
}
}
}