Spaces:
Running
Running
Add savings calculator; run the router on CPU when WebGPU is missing
Browse files- app.js +4 -1
- evaluation.html +1 -0
- index.html +2 -1
- router-core.js +65 -0
- savings.html +95 -0
- savings.js +189 -0
- style.css +41 -0
app.js
CHANGED
|
@@ -7,6 +7,9 @@ const LOCAL = ["localhost", "127.0.0.1"].includes(location.hostname);
|
|
| 7 |
const MODEL_ID = "switchboardllm/switchboard-router-0.5b-ONNX";
|
| 8 |
const MAX_PROMPT_CHARS = 2000;
|
| 9 |
const TIERS = ["small", "mid", "frontier"];
|
|
|
|
|
|
|
|
|
|
| 10 |
const reduceMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
|
| 11 |
|
| 12 |
const $ = (id) => document.getElementById(id);
|
|
@@ -216,7 +219,7 @@ async function loadRouter() {
|
|
| 216 |
let lastError = null;
|
| 217 |
for (const device of devices) {
|
| 218 |
try {
|
| 219 |
-
model = await AutoModelForCausalLM.from_pretrained(MODEL_ID, { dtype:
|
| 220 |
return device;
|
| 221 |
} catch (err) {
|
| 222 |
lastError = err;
|
|
|
|
| 7 |
const MODEL_ID = "switchboardllm/switchboard-router-0.5b-ONNX";
|
| 8 |
const MAX_PROMPT_CHARS = 2000;
|
| 9 |
const TIERS = ["small", "mid", "frontier"];
|
| 10 |
+
// WebGPU runs the 660 MB file with a 4-bit embedding (GatherBlockQuantized). onnxruntime-web has no CPU kernel for
|
| 11 |
+
// that op, so the CPU path loads model_uint8.onnx (710 MB), the same model with an 8-bit embedding made of standard ops.
|
| 12 |
+
const DTYPE = { webgpu: "q8", wasm: "uint8" };
|
| 13 |
const reduceMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
|
| 14 |
|
| 15 |
const $ = (id) => document.getElementById(id);
|
|
|
|
| 219 |
let lastError = null;
|
| 220 |
for (const device of devices) {
|
| 221 |
try {
|
| 222 |
+
model = await AutoModelForCausalLM.from_pretrained(MODEL_ID, { dtype: DTYPE[device], device, progress_callback: onProgress });
|
| 223 |
return device;
|
| 224 |
} catch (err) {
|
| 225 |
lastError = err;
|
evaluation.html
CHANGED
|
@@ -21,6 +21,7 @@
|
|
| 21 |
<nav class="nav">
|
| 22 |
<a href="index.html#demo">Demo</a>
|
| 23 |
<a href="index.html#how">How it works</a>
|
|
|
|
| 24 |
<a href="evaluation.html" aria-current="page">Evaluation</a>
|
| 25 |
<a href="index.html#faq">FAQ</a>
|
| 26 |
<a href="https://huggingface.co/switchboardllm/switchboard-router-0.5b">Model card</a>
|
|
|
|
| 21 |
<nav class="nav">
|
| 22 |
<a href="index.html#demo">Demo</a>
|
| 23 |
<a href="index.html#how">How it works</a>
|
| 24 |
+
<a href="savings.html">Savings</a>
|
| 25 |
<a href="evaluation.html" aria-current="page">Evaluation</a>
|
| 26 |
<a href="index.html#faq">FAQ</a>
|
| 27 |
<a href="https://huggingface.co/switchboardllm/switchboard-router-0.5b">Model card</a>
|
index.html
CHANGED
|
@@ -23,6 +23,7 @@
|
|
| 23 |
<nav class="nav">
|
| 24 |
<a href="#demo">Demo</a>
|
| 25 |
<a href="#how">How it works</a>
|
|
|
|
| 26 |
<a href="evaluation.html">Evaluation</a>
|
| 27 |
<a href="#faq">FAQ</a>
|
| 28 |
<a href="https://huggingface.co/switchboardllm/switchboard-router-0.5b">Model card</a>
|
|
@@ -198,6 +199,6 @@ router.pick_model("Write a haiku about rain")
|
|
| 198 |
</main>
|
| 199 |
|
| 200 |
|
| 201 |
-
<script type="module" src="app.js?v=
|
| 202 |
</body>
|
| 203 |
</html>
|
|
|
|
| 23 |
<nav class="nav">
|
| 24 |
<a href="#demo">Demo</a>
|
| 25 |
<a href="#how">How it works</a>
|
| 26 |
+
<a href="savings.html">Savings</a>
|
| 27 |
<a href="evaluation.html">Evaluation</a>
|
| 28 |
<a href="#faq">FAQ</a>
|
| 29 |
<a href="https://huggingface.co/switchboardllm/switchboard-router-0.5b">Model card</a>
|
|
|
|
| 199 |
</main>
|
| 200 |
|
| 201 |
|
| 202 |
+
<script type="module" src="app.js?v=14"></script>
|
| 203 |
</body>
|
| 204 |
</html>
|
router-core.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Load the Switchboard router into this tab with transformers.js and classify prompts.
|
| 2 |
+
// Used by savings.html. (The home page demo in app.js has its own copy with extra UI handling.)
|
| 3 |
+
|
| 4 |
+
const TRANSFORMERS_URL = "https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.2.0";
|
| 5 |
+
const LOCAL = ["localhost", "127.0.0.1"].includes(location.hostname);
|
| 6 |
+
export const MODEL_ID = "switchboardllm/switchboard-router-0.5b-ONNX";
|
| 7 |
+
export const MAX_PROMPT_CHARS = 2000;
|
| 8 |
+
export const TIERS = ["small", "mid", "frontier"];
|
| 9 |
+
// WebGPU runs the 660 MB file with a 4-bit embedding (GatherBlockQuantized). onnxruntime-web has no CPU kernel for
|
| 10 |
+
// that op, so the CPU path loads model_uint8.onnx (710 MB), the same model with an 8-bit embedding made of standard ops.
|
| 11 |
+
const DTYPE = { webgpu: "q8", wasm: "uint8" };
|
| 12 |
+
|
| 13 |
+
async function retry(fn, label, attempts = 2) {
|
| 14 |
+
let last = null;
|
| 15 |
+
for (let i = 0; i < attempts; i += 1) {
|
| 16 |
+
try {
|
| 17 |
+
return await fn();
|
| 18 |
+
} catch (err) {
|
| 19 |
+
last = err;
|
| 20 |
+
console.warn(`${label} failed (attempt ${i + 1} of ${attempts})`, err);
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
throw last;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
/** Returns { classify(prompt), device }. `?device=wasm` in the page URL forces the CPU path. */
|
| 27 |
+
export async function loadRouter(onProgress = () => {}) {
|
| 28 |
+
const { AutoModelForCausalLM, AutoTokenizer, env } = await retry(() => import(TRANSFORMERS_URL), "loading transformers.js");
|
| 29 |
+
if (LOCAL) {
|
| 30 |
+
// transformers.js 4 only reads local folders on Node, so serve ./models/ the way Hugging Face would
|
| 31 |
+
env.allowLocalModels = false;
|
| 32 |
+
env.allowRemoteModels = true;
|
| 33 |
+
env.remoteHost = new URL("./models/", location.href).href;
|
| 34 |
+
env.remotePathTemplate = "{model}/";
|
| 35 |
+
}
|
| 36 |
+
const tokenizer = await retry(() => AutoTokenizer.from_pretrained(MODEL_ID), "loading the tokenizer");
|
| 37 |
+
const forced = new URLSearchParams(location.search).get("device");
|
| 38 |
+
const devices = forced ? [forced] : navigator.gpu ? ["webgpu", "wasm"] : ["wasm"];
|
| 39 |
+
let lastError = null;
|
| 40 |
+
for (const device of devices) {
|
| 41 |
+
try {
|
| 42 |
+
const model = await AutoModelForCausalLM.from_pretrained(MODEL_ID, { dtype: DTYPE[device], device, progress_callback: onProgress });
|
| 43 |
+
return { device, classify: (prompt) => classify(tokenizer, model, prompt) };
|
| 44 |
+
} catch (err) {
|
| 45 |
+
lastError = err;
|
| 46 |
+
console.warn(`load on ${device} failed`, err);
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
throw lastError;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
async function classify(tokenizer, model, prompt) {
|
| 53 |
+
const inputs = tokenizer.apply_chat_template(
|
| 54 |
+
[{ role: "user", content: prompt.slice(0, MAX_PROMPT_CHARS) }],
|
| 55 |
+
{ add_generation_prompt: true, return_dict: true },
|
| 56 |
+
);
|
| 57 |
+
const output = await model.generate({ ...inputs, max_new_tokens: 24, do_sample: false });
|
| 58 |
+
const promptLength = inputs.input_ids.dims[1];
|
| 59 |
+
const text = tokenizer.batch_decode(output.slice(null, [promptLength, null]), { skip_special_tokens: true })[0].trim();
|
| 60 |
+
try {
|
| 61 |
+
const parsed = JSON.parse(text);
|
| 62 |
+
if (TIERS.includes(parsed.tier)) return { text, tier: parsed.tier, domain: parsed.domain || "general" };
|
| 63 |
+
} catch { /* fall through to the safe default */ }
|
| 64 |
+
return { text, tier: "frontier", domain: "general", invalid: true };
|
| 65 |
+
}
|
savings.html
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<title>Savings Calculator · Switchboard</title>
|
| 7 |
+
<meta name="description" content="Paste the prompts your app sends. Switchboard routes each one in your browser and adds up what you'd pay, compared with sending everything to the frontier model.">
|
| 8 |
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
| 9 |
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 10 |
+
<link href="https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,500;9..144,600;9..144,700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;600&display=swap" rel="stylesheet">
|
| 11 |
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 24 24%22%3E%3Ccircle cx=%225%22 cy=%2212%22 r=%223%22 fill=%22%23ea580c%22/%3E%3Ccircle cx=%2219%22 cy=%225%22 r=%222.4%22 fill=%22%23ea580c%22/%3E%3Ccircle cx=%2219%22 cy=%2212%22 r=%222.4%22 fill=%22%23ea580c%22/%3E%3Ccircle cx=%2219%22 cy=%2219%22 r=%222.4%22 fill=%22%23ea580c%22/%3E%3C/svg%3E">
|
| 12 |
+
<link rel="stylesheet" href="style.css?v=10">
|
| 13 |
+
</head>
|
| 14 |
+
<body>
|
| 15 |
+
<header class="top">
|
| 16 |
+
<div class="top-inner">
|
| 17 |
+
<a class="brand" href="index.html">
|
| 18 |
+
<svg class="brand-mark" viewBox="0 0 24 24" aria-hidden="true"><circle cx="5" cy="12" r="3"/><circle cx="19" cy="5" r="2.4"/><circle cx="19" cy="12" r="2.4"/><circle cx="19" cy="19" r="2.4"/><path d="M8 12c4 0 5-7 8.6-7"/></svg>
|
| 19 |
+
Switchboard
|
| 20 |
+
</a>
|
| 21 |
+
<nav class="nav">
|
| 22 |
+
<a href="index.html#demo">Demo</a>
|
| 23 |
+
<a href="index.html#how">How it works</a>
|
| 24 |
+
<a href="savings.html" aria-current="page">Savings</a>
|
| 25 |
+
<a href="evaluation.html">Evaluation</a>
|
| 26 |
+
<a href="index.html#faq">FAQ</a>
|
| 27 |
+
</nav>
|
| 28 |
+
<a class="btn btn-primary btn-sm" href="#calc">Check my prompts</a>
|
| 29 |
+
</div>
|
| 30 |
+
</header>
|
| 31 |
+
|
| 32 |
+
<main>
|
| 33 |
+
<section class="band cream sv-hero">
|
| 34 |
+
<div class="wrap narrow center">
|
| 35 |
+
<h1 class="sv-title">What would Switchboard save you?</h1>
|
| 36 |
+
<p class="section-lede">Paste the prompts your app actually sends. The router runs in this tab, picks a tier for each one, and adds up two bills. Nothing you paste leaves your device.</p>
|
| 37 |
+
</div>
|
| 38 |
+
</section>
|
| 39 |
+
|
| 40 |
+
<section class="band white" id="calc">
|
| 41 |
+
<div class="wrap">
|
| 42 |
+
<div class="demo">
|
| 43 |
+
<label for="prompts" class="field-label">Your prompts, one per line</label>
|
| 44 |
+
<textarea id="prompts" rows="9" placeholder="Summarize this support ticket in two sentences Write a SQL query that finds duplicate emails Prove that the square root of 2 is irrational"></textarea>
|
| 45 |
+
<div class="sv-tools">
|
| 46 |
+
<label class="chip sv-file">Load a .txt file<input type="file" id="file" accept=".txt,.csv,text/plain" hidden></label>
|
| 47 |
+
<button type="button" class="chip" id="examples">Use 40 real test prompts</button>
|
| 48 |
+
<span class="sv-count" id="count">0 prompts</span>
|
| 49 |
+
</div>
|
| 50 |
+
<div class="sv-opts">
|
| 51 |
+
<label for="tokens" class="field-label">Average answer length</label>
|
| 52 |
+
<div class="sv-inline"><input id="tokens" type="number" min="1" max="100000" step="50" value="500"><span>output tokens per answer</span></div>
|
| 53 |
+
</div>
|
| 54 |
+
<div class="demo-actions">
|
| 55 |
+
<button type="button" id="run" class="btn btn-primary">Route my prompts</button>
|
| 56 |
+
<button type="button" id="stop" class="btn btn-ghost" hidden>Stop</button>
|
| 57 |
+
<p id="status" class="status" role="status" aria-live="polite">The first run downloads the router once (660 MB, or 710 MB without WebGPU). Up to 200 prompts per run.</p>
|
| 58 |
+
</div>
|
| 59 |
+
<div class="meter" id="meter" hidden><span id="meter-fill"></span></div>
|
| 60 |
+
</div>
|
| 61 |
+
|
| 62 |
+
<div class="sv-results" id="results" hidden>
|
| 63 |
+
<div class="sv-summary">
|
| 64 |
+
<aside class="bill" aria-labelledby="sv-bill-title">
|
| 65 |
+
<h3 id="sv-bill-title">Your <span id="sv-n"></span> prompts, two bills</h3>
|
| 66 |
+
<p id="sv-assume"></p>
|
| 67 |
+
<div class="bill-row"><span id="sv-all-label"></span><b id="sv-all"></b><span class="bill-bar"><i style="width:100%"></i></span></div>
|
| 68 |
+
<div class="bill-row ours"><span>With Switchboard picking the model</span><b id="sv-routed"></b><span class="bill-bar"><i id="sv-routed-bar" style="width:0%"></i></span></div>
|
| 69 |
+
</aside>
|
| 70 |
+
<div class="sv-saved">
|
| 71 |
+
<p class="sv-big" id="sv-pct"></p>
|
| 72 |
+
<p class="sv-sub" id="sv-sub"></p>
|
| 73 |
+
<div class="sv-mix" id="sv-mix" aria-label="Share of prompts per tier"></div>
|
| 74 |
+
<ul class="sv-legend" id="sv-legend"></ul>
|
| 75 |
+
</div>
|
| 76 |
+
</div>
|
| 77 |
+
<div class="sv-table-head">
|
| 78 |
+
<h3>Every decision</h3>
|
| 79 |
+
<button type="button" class="btn btn-ghost btn-sm" id="csv">Download CSV</button>
|
| 80 |
+
</div>
|
| 81 |
+
<div class="sv-table-wrap">
|
| 82 |
+
<table class="sv-table">
|
| 83 |
+
<thead><tr><th>#</th><th>Prompt</th><th>Tier</th><th>Model</th></tr></thead>
|
| 84 |
+
<tbody id="rows"></tbody>
|
| 85 |
+
</table>
|
| 86 |
+
</div>
|
| 87 |
+
<p class="fineprint" id="sv-fine"></p>
|
| 88 |
+
</div>
|
| 89 |
+
</div>
|
| 90 |
+
</section>
|
| 91 |
+
</main>
|
| 92 |
+
|
| 93 |
+
<script type="module" src="savings.js?v=3"></script>
|
| 94 |
+
</body>
|
| 95 |
+
</html>
|
savings.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { loadRouter, TIERS } from "./router-core.js?v=1";
|
| 2 |
+
|
| 3 |
+
const MAX_PROMPTS = 200;
|
| 4 |
+
const EXAMPLES = 40;
|
| 5 |
+
const $ = (id) => document.getElementById(id);
|
| 6 |
+
|
| 7 |
+
let tiers = null; // tier -> {model, price_out}, from results.json
|
| 8 |
+
let evalResults = null; // results.json, for the honesty note
|
| 9 |
+
let samples = []; // real held-out prompts, from samples.json
|
| 10 |
+
let router = null;
|
| 11 |
+
let stopRequested = false;
|
| 12 |
+
let lastRun = [];
|
| 13 |
+
|
| 14 |
+
const prettyModel = (id) => {
|
| 15 |
+
const name = id.split("/").pop().replace(/-(\d)/, " $1").split("-");
|
| 16 |
+
return name.map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join(" ");
|
| 17 |
+
};
|
| 18 |
+
const usd = (x) => (x >= 100 ? `$${x.toFixed(0)}` : x >= 1 ? `$${x.toFixed(2)}` : x > 0 ? `$${x.toPrecision(3)}` : "$0");
|
| 19 |
+
const pct = (x) => `${(x * 100).toFixed(1)}%`;
|
| 20 |
+
|
| 21 |
+
function prompts() {
|
| 22 |
+
return $("prompts").value.split(/\r?\n/).map((s) => s.trim()).filter(Boolean);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
function updateCount() {
|
| 26 |
+
const n = prompts().length;
|
| 27 |
+
$("count").textContent = n > MAX_PROMPTS ? `${n} prompts (the first ${MAX_PROMPTS} will be routed)` : `${n} prompt${n === 1 ? "" : "s"}`;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
function onProgress(e) {
|
| 31 |
+
if (e.status !== "progress" || !e.total || !String(e.file).endsWith(".onnx")) return;
|
| 32 |
+
$("meter").hidden = false;
|
| 33 |
+
$("meter-fill").style.width = `${(e.loaded / e.total) * 100}%`;
|
| 34 |
+
$("status").textContent = `Downloading the router: ${(e.loaded / 1e6).toFixed(0)} of ${(e.total / 1e6).toFixed(0)} MB.`;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
async function ensureRouter() {
|
| 38 |
+
if (router) return router;
|
| 39 |
+
$("status").textContent = "Starting the download.";
|
| 40 |
+
router = await loadRouter(onProgress);
|
| 41 |
+
return router;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
function tokensPerAnswer() {
|
| 45 |
+
const v = Number($("tokens").value);
|
| 46 |
+
return Number.isFinite(v) && v > 0 ? Math.min(v, 100000) : 500;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
function bills(rows) {
|
| 50 |
+
const per = tokensPerAnswer() / 1e6;
|
| 51 |
+
const all = rows.length * tiers.frontier.price_out * per;
|
| 52 |
+
const routed = rows.reduce((sum, r) => sum + tiers[r.tier].price_out * per, 0);
|
| 53 |
+
return { all, routed, saved: all ? 1 - routed / all : 0 };
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
function render(rows) {
|
| 57 |
+
const { all, routed, saved } = bills(rows);
|
| 58 |
+
$("results").hidden = false;
|
| 59 |
+
$("sv-n").textContent = rows.length.toLocaleString("en-US");
|
| 60 |
+
$("sv-assume").textContent = `Each answer ${tokensPerAnswer().toLocaleString("en-US")} output tokens, priced on Surplus.`;
|
| 61 |
+
$("sv-all-label").textContent = `Every prompt to ${prettyModel(tiers.frontier.model)}`;
|
| 62 |
+
$("sv-all").textContent = usd(all);
|
| 63 |
+
$("sv-routed").textContent = usd(routed);
|
| 64 |
+
$("sv-routed-bar").style.width = `${all ? (routed / all) * 100 : 0}%`;
|
| 65 |
+
$("sv-pct").textContent = `${pct(saved)} cheaper`;
|
| 66 |
+
const per1000 = rows.length ? ((all - routed) / rows.length) * 1000 : 0;
|
| 67 |
+
$("sv-sub").textContent = `About ${usd(per1000)} saved per 1,000 prompts like these.`;
|
| 68 |
+
|
| 69 |
+
const counts = Object.fromEntries(TIERS.map((t) => [t, rows.filter((r) => r.tier === t).length]));
|
| 70 |
+
$("sv-mix").innerHTML = TIERS.filter((t) => counts[t])
|
| 71 |
+
.map((t) => `<i class="t-${t}" style="flex:${counts[t]}"></i>`).join("");
|
| 72 |
+
$("sv-legend").innerHTML = TIERS.map((t) =>
|
| 73 |
+
`<li><span class="sw t-${t}"></span>${t[0].toUpperCase() + t.slice(1)}: ${counts[t]} (${prettyModel(tiers[t].model)})</li>`).join("");
|
| 74 |
+
|
| 75 |
+
const body = $("rows");
|
| 76 |
+
body.textContent = "";
|
| 77 |
+
rows.forEach((r, i) => {
|
| 78 |
+
const tr = document.createElement("tr");
|
| 79 |
+
const cells = [String(i + 1), r.prompt, r.tier + (r.invalid ? " (fallback)" : ""), prettyModel(tiers[r.tier].model)];
|
| 80 |
+
cells.forEach((text, k) => {
|
| 81 |
+
const td = document.createElement("td");
|
| 82 |
+
if (k === 2) {
|
| 83 |
+
const pill = document.createElement("span");
|
| 84 |
+
pill.className = `sv-pill t-${r.tier}`;
|
| 85 |
+
pill.textContent = text;
|
| 86 |
+
td.appendChild(pill);
|
| 87 |
+
} else {
|
| 88 |
+
td.textContent = text;
|
| 89 |
+
}
|
| 90 |
+
if (k === 1) td.title = r.prompt;
|
| 91 |
+
tr.appendChild(td);
|
| 92 |
+
});
|
| 93 |
+
body.appendChild(tr);
|
| 94 |
+
});
|
| 95 |
+
const under = evalResults ? pct(evalResults.under_routed) : "some";
|
| 96 |
+
$("sv-fine").textContent =
|
| 97 |
+
`An estimate, not an invoice: it prices output tokens only and assumes every answer is the same length. ` +
|
| 98 |
+
`The router also makes mistakes. On our 10,000-prompt test it sent ${under} of prompts to a cheaper model than they needed. ` +
|
| 99 |
+
`Full numbers are on the evaluation page.`;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
async function run() {
|
| 103 |
+
const list = prompts().slice(0, MAX_PROMPTS);
|
| 104 |
+
if (!list.length) { $("status").textContent = "Paste at least one prompt, or use the real test prompts."; return; }
|
| 105 |
+
if (!tiers) { $("status").textContent = "Prices couldn't be loaded. Reload the page and try again."; return; }
|
| 106 |
+
$("run").disabled = true;
|
| 107 |
+
stopRequested = false;
|
| 108 |
+
try {
|
| 109 |
+
await ensureRouter();
|
| 110 |
+
} catch (err) {
|
| 111 |
+
console.error(err);
|
| 112 |
+
$("status").textContent = navigator.onLine
|
| 113 |
+
? "The router couldn't load. Try again, or use a recent desktop Chrome or Edge."
|
| 114 |
+
: "You're offline. Reconnect and try again.";
|
| 115 |
+
$("run").disabled = false;
|
| 116 |
+
$("meter").hidden = true;
|
| 117 |
+
return;
|
| 118 |
+
}
|
| 119 |
+
$("stop").hidden = false;
|
| 120 |
+
$("meter").hidden = false;
|
| 121 |
+
const rows = [];
|
| 122 |
+
const started = performance.now();
|
| 123 |
+
for (let i = 0; i < list.length && !stopRequested; i += 1) {
|
| 124 |
+
$("status").textContent = router.device === "webgpu"
|
| 125 |
+
? `Routing ${i + 1} of ${list.length} on your GPU…`
|
| 126 |
+
: `Routing ${i + 1} of ${list.length} on your CPU. This browser has no WebGPU, so it takes several seconds per prompt; recent Chrome or Edge is much faster.`;
|
| 127 |
+
$("meter-fill").style.width = `${(i / list.length) * 100}%`;
|
| 128 |
+
try {
|
| 129 |
+
rows.push({ prompt: list[i], ...(await router.classify(list[i])) });
|
| 130 |
+
} catch (err) {
|
| 131 |
+
console.error(err);
|
| 132 |
+
rows.push({ prompt: list[i], text: "", tier: "frontier", domain: "general", invalid: true });
|
| 133 |
+
}
|
| 134 |
+
if (rows.length % 5 === 0 || rows.length === list.length) render(rows);
|
| 135 |
+
}
|
| 136 |
+
lastRun = rows;
|
| 137 |
+
if (rows.length) render(rows);
|
| 138 |
+
const secs = ((performance.now() - started) / 1000).toFixed(1);
|
| 139 |
+
$("status").textContent = stopRequested
|
| 140 |
+
? `Stopped after ${rows.length} prompts.`
|
| 141 |
+
: `Routed ${rows.length} prompts in ${secs} s on your ${router.device === "webgpu" ? "GPU" : "CPU"}.`;
|
| 142 |
+
$("meter").hidden = true;
|
| 143 |
+
$("stop").hidden = true;
|
| 144 |
+
$("run").disabled = false;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
function csvCell(s) {
|
| 148 |
+
return `"${String(s).replace(/"/g, '""')}"`;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
function downloadCsv() {
|
| 152 |
+
if (!lastRun.length) return;
|
| 153 |
+
const head = ["prompt", "tier", "domain", "model", "valid"];
|
| 154 |
+
const lines = lastRun.map((r) => [r.prompt, r.tier, r.domain, tiers[r.tier].model, !r.invalid].map(csvCell).join(","));
|
| 155 |
+
const blob = new Blob([[head.join(","), ...lines].join("\n")], { type: "text/csv" });
|
| 156 |
+
const a = document.createElement("a");
|
| 157 |
+
a.href = URL.createObjectURL(blob);
|
| 158 |
+
a.download = "switchboard-decisions.csv";
|
| 159 |
+
a.click();
|
| 160 |
+
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
async function init() {
|
| 164 |
+
$("prompts").addEventListener("input", updateCount);
|
| 165 |
+
$("run").addEventListener("click", run);
|
| 166 |
+
$("stop").addEventListener("click", () => { stopRequested = true; });
|
| 167 |
+
$("csv").addEventListener("click", downloadCsv);
|
| 168 |
+
$("tokens").addEventListener("change", () => { if (lastRun.length) render(lastRun); });
|
| 169 |
+
$("file").addEventListener("change", async (e) => {
|
| 170 |
+
const file = e.target.files?.[0];
|
| 171 |
+
if (!file) return;
|
| 172 |
+
$("prompts").value = (await file.text()).slice(0, 2_000_000);
|
| 173 |
+
updateCount();
|
| 174 |
+
});
|
| 175 |
+
$("examples").addEventListener("click", () => {
|
| 176 |
+
$("prompts").value = samples.slice(0, EXAMPLES).map((s) => s.prompt.replace(/\s+/g, " ")).join("\n");
|
| 177 |
+
updateCount();
|
| 178 |
+
});
|
| 179 |
+
const [results, routed] = await Promise.allSettled([
|
| 180 |
+
fetch("results.json", { cache: "no-store" }).then((r) => r.json()),
|
| 181 |
+
fetch("samples.json", { cache: "no-store" }).then((r) => r.json()),
|
| 182 |
+
]);
|
| 183 |
+
if (results.status === "fulfilled") { evalResults = results.value; tiers = results.value.tiers; }
|
| 184 |
+
if (routed.status === "fulfilled") samples = routed.value;
|
| 185 |
+
$("examples").disabled = !samples.length;
|
| 186 |
+
updateCount();
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
init();
|
style.css
CHANGED
|
@@ -355,3 +355,44 @@ code { font-family: var(--mono); font-size: 0.86em; }
|
|
| 355 |
.run-code .answer { color: #fbbf24; }
|
| 356 |
.etable td.build { width: 56%; }
|
| 357 |
.etable td.build small.note { color: var(--body); margin-top: 6px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
.run-code .answer { color: #fbbf24; }
|
| 356 |
.etable td.build { width: 56%; }
|
| 357 |
.etable td.build small.note { color: var(--body); margin-top: 6px; }
|
| 358 |
+
|
| 359 |
+
/* ---------- Savings calculator ---------- */
|
| 360 |
+
.sv-hero { padding: 72px 0 40px; }
|
| 361 |
+
.sv-title { margin: 0 0 14px; font-family: var(--serif); font-weight: 600; font-size: clamp(34px, 5vw, 52px); line-height: 1.08; letter-spacing: -0.02em; color: var(--ink-strong); }
|
| 362 |
+
.sv-hero .section-lede { margin-bottom: 0; }
|
| 363 |
+
.sv-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
|
| 364 |
+
.sv-file { display: inline-flex; align-items: center; }
|
| 365 |
+
.sv-count { margin-left: auto; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
|
| 366 |
+
.sv-opts { margin-top: 18px; }
|
| 367 |
+
.sv-inline { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--body); }
|
| 368 |
+
.sv-inline input { width: 110px; padding: 10px 12px; border-radius: 10px; border: 1px solid #e3dccb; font: 16px var(--sans); }
|
| 369 |
+
.sv-inline input:focus { outline: none; border-color: var(--orange); box-shadow: 0 0 0 3px #ea580c22; }
|
| 370 |
+
.sv-results { max-width: 1000px; margin: 40px auto 0; }
|
| 371 |
+
.sv-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 28px; align-items: start; }
|
| 372 |
+
.sv-saved { padding: 8px 4px; }
|
| 373 |
+
.sv-big { margin: 0; font-family: var(--serif); font-weight: 700; font-size: clamp(44px, 6vw, 64px); line-height: 1; letter-spacing: -0.02em; color: var(--orange-deep); }
|
| 374 |
+
.sv-sub { margin: 10px 0 22px; color: var(--body); }
|
| 375 |
+
.sv-mix { display: flex; height: 16px; border-radius: 999px; overflow: hidden; background: #f1ebdc; }
|
| 376 |
+
.sv-mix i { display: block; height: 100%; }
|
| 377 |
+
.t-small { background: #3d8b63; }
|
| 378 |
+
.t-mid { background: #d97706; }
|
| 379 |
+
.t-frontier { background: #ea580c; }
|
| 380 |
+
.sv-legend { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 6px; font-size: 14px; color: var(--body); }
|
| 381 |
+
.sv-legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 8px; }
|
| 382 |
+
.sv-table-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 40px 0 12px; }
|
| 383 |
+
.sv-table-head h3 { margin: 0; }
|
| 384 |
+
.sv-table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 14px; background: #fffdf8; }
|
| 385 |
+
.sv-table { width: 100%; border-collapse: collapse; font-size: 14px; }
|
| 386 |
+
.sv-table th { text-align: left; font-weight: 600; color: var(--muted); padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--cream); }
|
| 387 |
+
.sv-table td { padding: 10px 14px; border-bottom: 1px solid var(--line); color: var(--body); vertical-align: top; }
|
| 388 |
+
.sv-table td:nth-child(1) { color: var(--muted); font-variant-numeric: tabular-nums; width: 3em; }
|
| 389 |
+
.sv-table td:nth-child(2) { max-width: 520px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink); }
|
| 390 |
+
.sv-table td:nth-child(4) { white-space: nowrap; }
|
| 391 |
+
.sv-table tr:last-child td { border-bottom: 0; }
|
| 392 |
+
.sv-pill { display: inline-block; padding: 3px 10px; border-radius: 999px; color: #fff; font-size: 12px; font-weight: 600; white-space: nowrap; }
|
| 393 |
+
.sv-results .fineprint { margin-top: 16px; }
|
| 394 |
+
@media (max-width: 760px) {
|
| 395 |
+
.sv-summary { grid-template-columns: 1fr; }
|
| 396 |
+
.sv-count { margin-left: 0; width: 100%; }
|
| 397 |
+
.sv-table td:nth-child(2) { max-width: 180px; }
|
| 398 |
+
}
|