forked from getomni-ai/benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmistral.ts
More file actions
57 lines (45 loc) · 1.24 KB
/
mistral.ts
File metadata and controls
57 lines (45 loc) · 1.24 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
import { Mistral } from '@mistralai/mistralai';
import { ModelProvider } from './base';
// $1.00 per 1000 images
const COST_PER_IMAGE = 0.001;
export class MistralProvider extends ModelProvider {
private client: Mistral;
constructor() {
super('mistral-ocr');
const apiKey = process.env.MISTRAL_API_KEY;
if (!apiKey) {
throw new Error('Missing required Mistral API key');
}
this.client = new Mistral({
apiKey,
});
}
async ocr(imagePath: string) {
try {
const start = performance.now();
const response = await this.client.ocr.process({
model: 'mistral-ocr-latest',
document: {
imageUrl: imagePath,
},
includeImageBase64: true,
});
const text = response.pages.map((page) => page.markdown).join('\n');
const end = performance.now();
const imageBase64s = response.pages.flatMap((page) =>
page.images.map((image) => image.imageBase64).filter((base64) => base64),
);
return {
text,
imageBase64s,
usage: {
duration: end - start,
totalCost: COST_PER_IMAGE,
},
};
} catch (error) {
console.error('Mistral OCR Error:', error);
throw error;
}
}
}