-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathopenrouter.ts
71 lines (60 loc) · 1.89 KB
/
openrouter.ts
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
69
70
71
import OpenAI from 'openai';
import { ChatCompletionMessageParam } from 'openai/resources/chat/completions';
import { ModelProvider } from './base';
import { Usage } from '../types';
import { calculateTokenCost, OCR_SYSTEM_PROMPT } from './shared';
export class OpenRouterProvider extends ModelProvider {
private client: OpenAI;
constructor(model: string) {
super(model);
const apiKey = process.env.OPENROUTER_API_KEY;
if (!apiKey) {
throw new Error('Missing required OpenRouter API key');
}
this.client = new OpenAI({
baseURL: 'https://openrouter.ai/api/v1',
apiKey,
defaultHeaders: {
'HTTP-Referer': process.env.SITE_URL || 'https://github.com/omni-ai/benchmark',
'X-Title': 'OmniAI OCR Benchmark',
},
});
}
async ocr(imagePath: string): Promise<{
text: string;
imageBase64s?: string[];
usage: Usage;
}> {
const start = performance.now();
const messages: ChatCompletionMessageParam[] = [
{
role: 'user',
content: [
{ type: 'text', text: OCR_SYSTEM_PROMPT },
{ type: 'image_url', image_url: { url: imagePath } },
],
},
];
const response = await this.client.chat.completions.create({
model: this.model,
messages,
});
const end = performance.now();
const inputTokens = response.usage?.prompt_tokens || 0;
const outputTokens = response.usage?.completion_tokens || 0;
const inputCost = calculateTokenCost(this.model, 'input', inputTokens);
const outputCost = calculateTokenCost(this.model, 'output', outputTokens);
return {
text: response.choices[0].message.content || '',
usage: {
duration: end - start,
inputTokens,
outputTokens,
totalTokens: inputTokens + outputTokens,
inputCost,
outputCost,
totalCost: inputCost + outputCost,
},
};
}
}