forked from getomni-ai/benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure.ts
More file actions
68 lines (55 loc) · 2 KB
/
azure.ts
File metadata and controls
68 lines (55 loc) · 2 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { AzureKeyCredential } from '@azure/core-auth';
import DocumentIntelligence, {
DocumentIntelligenceClient,
getLongRunningPoller,
isUnexpected,
AnalyzeOperationOutput,
} from '@azure-rest/ai-document-intelligence';
import { ModelProvider } from './base';
// https://azure.microsoft.com/en-us/pricing/details/ai-document-intelligence/
// $10 per 1000 pages for the first 1M pages, Prebuilt-Layout model
const COST_PER_PAGE = 10 / 1000;
export class AzureDocumentIntelligenceProvider extends ModelProvider {
private client: DocumentIntelligenceClient;
constructor() {
super('azure-document-intelligence');
const endpoint = process.env.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT;
const apiKey = process.env.AZURE_DOCUMENT_INTELLIGENCE_KEY;
if (!endpoint || !apiKey) {
throw new Error('Missing required Azure Document Intelligence configuration');
}
this.client = DocumentIntelligence(endpoint, new AzureKeyCredential(apiKey));
}
async ocr(imagePath: string) {
try {
const start = performance.now();
const initialResponse = await this.client
.path('/documentModels/{modelId}:analyze', 'prebuilt-layout')
.post({
contentType: 'application/json',
body: {
urlSource: imagePath,
},
queryParameters: { outputContentFormat: 'markdown' },
});
if (isUnexpected(initialResponse)) {
throw initialResponse.body.error;
}
const poller = getLongRunningPoller(this.client, initialResponse);
const result = (await poller.pollUntilDone()).body as AnalyzeOperationOutput;
const analyzeResult = result.analyzeResult;
const text = analyzeResult?.content;
const end = performance.now();
return {
text,
usage: {
duration: end - start,
totalCost: COST_PER_PAGE, // the input is always 1 page.
},
};
} catch (error) {
console.error('Azure Document Intelligence Error:', error);
throw error;
}
}
}