-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
389 lines (324 loc) · 9.59 KB
/
index.js
File metadata and controls
389 lines (324 loc) · 9.59 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"use strict";
const PRICING = require("./src/pricing");
const CJK_PATTERN =
/[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/g;
const WORD_PATTERN = /[A-Za-z]+(?:'[A-Za-z]+)*/g;
const NUMBER_PATTERN = /\d+(?:[.,:/-]\d+)*/g;
const PUNCTUATION_PATTERN = /[!-/:-@[-`{-~]/g;
const EMOJI_PATTERN =
/[\u{1f300}-\u{1f5ff}\u{1f600}-\u{1f64f}\u{1f680}-\u{1f6ff}\u{1f900}-\u{1f9ff}\u{1fa70}-\u{1faff}]/gu;
const MODEL_PROFILES = {
openai: {
family: "openai",
contextWindow: 128000,
supportsMessages: true,
matchers: ["gpt", "o1", "o3", "o4", "text-embedding", "openai"]
},
gemini: {
family: "gemini",
contextWindow: 1000000,
supportsMessages: true,
matchers: ["gemini", "google"]
},
claude: {
family: "claude",
contextWindow: 200000,
supportsMessages: true,
matchers: ["claude", "anthropic", "haiku", "sonnet", "opus"]
}
};
const PRICING_PROVIDER_MAP = {
openai: "openai",
claude: "anthropic",
gemini: "google"
};
function normalizeModelLike(model) {
return model.toLowerCase().replace(/[\s_/.:]+/g, "-");
}
function normalizeModel(model) {
if (typeof model !== "string" || model.trim() === "") {
throw new TypeError("model must be a non-empty string");
}
return normalizeModelLike(model);
}
function resolveModelProfile(model) {
const normalizedModel = normalizeModel(model);
for (const [provider, profile] of Object.entries(MODEL_PROFILES)) {
for (const matcher of profile.matchers) {
if (normalizedModel.includes(matcher)) {
return {
provider,
profile,
normalizedModel,
matchedBy: matcher
};
}
}
}
throw new RangeError(
"Unsupported model. Use an OpenAI, Gemini, or Claude model name."
);
}
function detectProvider(model) {
return resolveModelProfile(model).provider;
}
function getModelInfo(model) {
const { provider, profile, normalizedModel, matchedBy } =
resolveModelProfile(model);
return {
provider,
family: profile.family,
normalizedModel,
contextWindow: profile.contextWindow,
supportsMessages: profile.supportsMessages,
matchedBy
};
}
function countMatches(text, pattern) {
const matches = text.match(pattern);
return matches ? matches.length : 0;
}
function resolvePricing(provider, normalizedModel) {
const pricingProvider = PRICING_PROVIDER_MAP[provider];
const providerPricing = PRICING[pricingProvider] || {};
const defaultPricing = providerPricing.default || { input: 0, output: 0 };
for (const [modelName, price] of Object.entries(providerPricing)) {
if (modelName === "default") {
continue;
}
if (normalizeModelLike(modelName) === normalizedModel) {
return price;
}
}
let bestMatch = null;
for (const [modelName, price] of Object.entries(providerPricing)) {
if (modelName === "default") {
continue;
}
const normalizedCandidate = normalizeModelLike(modelName);
if (
normalizedModel.includes(normalizedCandidate) ||
normalizedCandidate.includes(normalizedModel)
) {
if (!bestMatch || normalizedCandidate.length > bestMatch.length) {
bestMatch = {
length: normalizedCandidate.length,
price
};
}
}
}
return bestMatch ? bestMatch.price : defaultPricing;
}
function countWordTokens(text) {
const words = text.match(WORD_PATTERN) || [];
let total = 0;
for (const word of words) {
const length = word.length;
if (length <= 4) {
total += 1;
continue;
}
total += Math.ceil(length / 4);
if (/[A-Z]/.test(word) && /[a-z]/.test(word)) {
total += 0.15;
}
}
return total;
}
function estimateBaseTokens(text) {
const cjkCount = countMatches(text, CJK_PATTERN);
const punctuationCount = countMatches(text, PUNCTUATION_PATTERN);
const numberGroups = countMatches(text, NUMBER_PATTERN);
const emojiCount = countMatches(text, EMOJI_PATTERN);
const whitespaceGroups = countMatches(text, /\s+/g);
const wordTokens = countWordTokens(text);
const visibleChars = text.replace(/\s/g, "").length;
const latinLikeChars = Math.max(
0,
visibleChars - cjkCount - punctuationCount - emojiCount
);
const residualChars = Math.max(0, latinLikeChars - countMatches(text, WORD_PATTERN));
return {
cjkCount,
punctuationCount,
numberGroups,
emojiCount,
whitespaceGroups,
wordTokens,
residualChars
};
}
const MAX_INPUT_LENGTH = 10 * 1024 * 1024; // 10 MB
function countTokens(text, model) {
if (typeof text !== "string") {
throw new TypeError("text must be a string");
}
if (text.length === 0) {
return 0;
}
if (text.length > MAX_INPUT_LENGTH) {
throw new RangeError(
"input exceeds maximum length of " + MAX_INPUT_LENGTH + " characters"
);
}
const provider = detectProvider(model);
const {
cjkCount,
punctuationCount,
numberGroups,
emojiCount,
whitespaceGroups,
wordTokens,
residualChars
} = estimateBaseTokens(text);
const openAiEstimate =
wordTokens +
cjkCount * 1.55 +
numberGroups * 0.9 +
punctuationCount * 0.33 +
whitespaceGroups * 0.12 +
emojiCount * 2.2 +
residualChars * 0.25 +
0.5;
const claudeEstimate =
wordTokens * 0.96 +
cjkCount * 1.4 +
numberGroups * 0.85 +
punctuationCount * 0.28 +
whitespaceGroups * 0.1 +
emojiCount * 2 +
residualChars * 0.22 +
0.35;
const geminiEstimate =
wordTokens * 0.98 +
cjkCount * 1.48 +
numberGroups * 0.88 +
punctuationCount * 0.3 +
whitespaceGroups * 0.11 +
emojiCount * 2.1 +
residualChars * 0.23 +
0.4;
let estimate = claudeEstimate;
if (provider === "openai") {
estimate = openAiEstimate;
} else if (provider === "gemini") {
estimate = geminiEstimate;
}
return Math.max(1, Math.ceil(estimate));
}
function countMessages(messages, model) {
if (!Array.isArray(messages)) {
throw new TypeError("messages must be an array");
}
if (messages.length === 0) {
return 0;
}
detectProvider(model);
let total = 0;
for (const message of messages) {
if (!message || typeof message !== "object") {
throw new TypeError("each message must be an object");
}
const role = Object.prototype.hasOwnProperty.call(message, "role") &&
typeof message.role === "string" ? message.role : "";
const content = Object.prototype.hasOwnProperty.call(message, "content")
? message.content
: undefined;
if (typeof content !== "string") {
throw new TypeError("each message content must be a string");
}
// Chat payloads include some structure beyond raw text, so add a small
// overhead for each message plus a tiny role cost.
total += countTokens(content, model);
total += 3;
if (role) {
total += Math.max(1, Math.ceil(role.length / 8));
}
if (Object.prototype.hasOwnProperty.call(message, "name") &&
typeof message.name === "string") {
total += Math.max(1, Math.ceil(message.name.length / 8));
}
}
return total;
}
function fitsContextWindow(input, model, maxOutputTokens) {
const modelInfo = getModelInfo(model);
const reservedOutputTokens =
maxOutputTokens === undefined ? 0 : maxOutputTokens;
if (!Number.isInteger(reservedOutputTokens) || reservedOutputTokens < 0) {
throw new TypeError(
"maxOutputTokens must be a non-negative integer when provided"
);
}
let inputTokens;
if (typeof input === "string") {
inputTokens = countTokens(input, model);
} else if (Array.isArray(input)) {
inputTokens = countMessages(input, model);
} else {
throw new TypeError("input must be a string or an array of messages");
}
const availableInputTokens = Math.max(
0,
modelInfo.contextWindow - reservedOutputTokens
);
return {
fits: inputTokens <= availableInputTokens,
inputTokens,
reservedOutputTokens,
availableInputTokens,
contextWindow: modelInfo.contextWindow,
model: modelInfo.normalizedModel,
provider: modelInfo.provider
};
}
function estimateCost(input, model, options) {
const modelInfo = getModelInfo(model);
const resolvedOptions = options === undefined ? {} : options;
if (
resolvedOptions === null ||
typeof resolvedOptions !== "object" ||
Array.isArray(resolvedOptions)
) {
throw new TypeError("options must be an object when provided");
}
const outputTokens =
resolvedOptions.outputTokens === undefined ? 0 : resolvedOptions.outputTokens;
if (!Number.isInteger(outputTokens) || outputTokens < 0) {
throw new TypeError("options.outputTokens must be a non-negative integer");
}
let inputTokens;
if (typeof input === "string") {
inputTokens = countTokens(input, model);
} else if (Array.isArray(input)) {
inputTokens = countMessages(input, model);
} else {
throw new TypeError("input must be a string or an array of messages");
}
const pricing = resolvePricing(modelInfo.provider, modelInfo.normalizedModel);
const PRECISION = 1e9;
const estimatedInputCost =
Math.round(inputTokens * pricing.input * PRECISION) / PRECISION;
const estimatedOutputCost =
Math.round(outputTokens * pricing.output * PRECISION) / PRECISION;
const estimatedTotalCost =
Math.round((estimatedInputCost + estimatedOutputCost) * PRECISION) / PRECISION;
return {
provider: modelInfo.provider,
model: modelInfo.normalizedModel,
inputTokens,
outputTokensReserved: outputTokens,
totalTokensEstimated: inputTokens + outputTokens,
estimatedInputCost,
estimatedOutputCost,
estimatedTotalCost
};
}
module.exports = {
countTokens,
countMessages,
getModelInfo,
fitsContextWindow,
estimateCost
};