Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 2,112 Bytes
1979653 |
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 |
import { page } from "$app/state";
import { atLeastNDecimals } from "$lib/utils/number.js";
import type { PageData } from "../../routes/$types.js";
interface RouterProvider {
provider: string;
status: string;
context_length?: number;
pricing?: {
input: number;
output: number;
};
supports_tools?: boolean;
supports_structured_output?: boolean;
}
interface RouterModel {
id: string;
providers: RouterProvider[];
}
interface RouterData {
data: RouterModel[];
}
const pageData = $derived(page.data as PageData & { routerData: RouterData });
class Pricing {
routerData = $derived(pageData.routerData as RouterData);
getPricing(modelId: string, provider: string) {
const model = this.routerData?.data?.find((m: RouterModel) => m.id === modelId);
if (!model) return null;
const providerData = model.providers.find((p: RouterProvider) => p.provider === provider);
return providerData?.pricing || null;
}
getContextLength(modelId: string, provider: string) {
const model = this.routerData?.data?.find((m: RouterModel) => m.id === modelId);
if (!model) return null;
const providerData = model.providers.find((p: RouterProvider) => p.provider === provider);
return providerData?.context_length || null;
}
formatPricing(pricing: { input: number; output: number } | null) {
if (!pricing) return null;
const inputCost = atLeastNDecimals(pricing.input, 2);
const outputCost = atLeastNDecimals(pricing.output, 2);
return {
input: `$${inputCost}/1M`,
output: `$${outputCost}/1M`,
inputRaw: pricing.input,
outputRaw: pricing.output,
};
}
estimateCost(modelId: string, provider: string, inputTokens: number, outputTokens: number = 0) {
const pricing = this.getPricing(modelId, provider);
if (!pricing) return null;
const inputCost = (inputTokens / 1000000) * pricing.input;
const outputCost = (outputTokens / 1000000) * pricing.output;
const totalCost = inputCost + outputCost;
return {
input: inputCost,
output: outputCost,
total: totalCost,
formatted: `$${totalCost.toFixed(6)}`,
};
}
}
export const pricing = new Pricing();
|