Download systems/openai-compat.mjs from PulseOS/budgetbench: direct link, hf CLI and curl.
- Browser
- Download file 5.64 kB
-
https://huggingface.co/datasets/PulseOS/budgetbench/resolve/main/systems/openai-compat.mjs
- Command line
-
hf download hf://datasets/PulseOS/budgetbench/systems/openai-compat.mjs
-
curl -L -o openai-compat.mjs https://huggingface.co/datasets/PulseOS/budgetbench/resolve/main/systems/openai-compat.mjs
5.64 kB
| // BudgetBench system: ANY MODEL behind an OpenAI-compatible chat endpoint, with the reference tools (tools.mjs). | |
| // Used for the stock open models (a local llama-server per GGUF, runner/serve-local.sh) and for any hosted model a | |
| // submitter runs with THEIR OWN key. BudgetBench itself publishes no hosted-model row. | |
| // Every model row gets the same system prompt, tools, temperature 0 and at most 8 tool rounds a turn. | |
| // BB_BASE the endpoint (http://127.0.0.1:8080/v1, https://api.openai.com/v1, ...) | |
| // BB_MODEL the model id the endpoint expects ("local" for llama-server) | |
| // BB_KEY the NAME of the env var that holds your own API key (default OPENAI_API_KEY); the key is read from the | |
| // process env only and never written to any file, log or transcript | |
| // BB_NO_THINK=1 ask a hybrid Qwen3 template not to think (as eval/run_model.py --no-think) | |
| import {loadView, makeTools, systemPromptFor} from "./tools.mjs"; | |
| export const name = "openai-compat"; | |
| export const variants = ["default"]; | |
| export const processPerHousehold = false; | |
| const MAX_ROUNDS = 8; | |
| export async function describe() { | |
| return {system: "model + reference tools", base: process.env.BB_BASE ?? null, model: process.env.BB_MODEL ?? null, no_think: process.env.BB_NO_THINK === "1"}; | |
| } | |
| async function post(body) { | |
| const base = process.env.BB_BASE; | |
| if (!base) throw new Error("BB_BASE is not set"); | |
| const keyName = process.env.BB_KEY ?? "OPENAI_API_KEY"; | |
| const key = /127\.0\.0\.1|localhost/.test(base) ? null : process.env[keyName]; | |
| if (!/127\.0\.0\.1|localhost/.test(base) && !key) throw new Error(`no key in ${keyName}`); | |
| for (let attempt = 0; ; attempt++) { | |
| const res = await fetch(base.replace(/\/$/, "") + "/chat/completions", {method: "POST", headers: {"Content-Type": "application/json", ...(key ? {Authorization: `Bearer ${key}`} : {})}, body: JSON.stringify(body)}); | |
| if (res.ok) return res.json(); | |
| const retry = res.status === 429 || res.status >= 500; | |
| if (!retry || attempt >= 4) { const e = new Error(`HTTP ${res.status}: ${(await res.text()).slice(0, 300)}`); e.failure_class = "serving_error"; throw e; } | |
| await new Promise((r) => setTimeout(r, (2 ** attempt) * 1000 * (0.5 + Math.random()))); | |
| } | |
| } | |
| export async function open(hhDir, setup) { | |
| const view = loadView(hhDir, setup); | |
| const {impl, schema} = makeTools(view); | |
| const system = systemPromptFor(setup, view); | |
| const usage = {input_tokens: 0, output_tokens: 0, calls: 0}; | |
| return { | |
| loaded: view.rows.length, | |
| model: process.env.BB_MODEL ?? "local", | |
| usage, | |
| async ask(history, message) { | |
| const t0 = Date.now(); | |
| const msgs = [{role: "system", content: system}, ...history, {role: "user", content: message}]; | |
| const calls = []; | |
| let served = null; | |
| for (let round = 0; round < MAX_ROUNDS; round++) { | |
| const body = {model: process.env.BB_MODEL ?? "local", messages: msgs, tools: schema, temperature: 0, max_tokens: 900}; | |
| if (process.env.BB_NO_THINK === "1") body.chat_template_kwargs = {enable_thinking: false}; | |
| const out = await post(body); | |
| served = out.model ?? served; | |
| usage.input_tokens += out.usage?.prompt_tokens ?? 0; usage.output_tokens += out.usage?.completion_tokens ?? 0; usage.calls++; | |
| const msg = out.choices?.[0]?.message ?? {}; | |
| const tc = msg.tool_calls ?? []; | |
| const content = String(msg.content ?? "").replace(/<think>[\s\S]*?<\/think>/g, "").trim(); | |
| if (!tc.length) { | |
| msgs.push({role: "assistant", content}); | |
| return {text: content, choice: null, history: msgs.slice(1), meta: {calls, served, ms: Date.now() - t0, stop: out.choices?.[0]?.finish_reason ?? null}}; | |
| } | |
| msgs.push({role: "assistant", content: msg.content ?? "", tool_calls: tc}); | |
| for (const c of tc) { | |
| let args = {}; | |
| try { args = JSON.parse(c.function?.arguments || "{}"); } catch { args = {}; } | |
| calls.push({name: c.function?.name, args}); | |
| if (c.function?.name === "ask_user") { | |
| // the turn ends with a choice; the person's tap is their next message | |
| const options = (Array.isArray(args.options) ? args.options : []).map(String).filter(Boolean).slice(0, 6); | |
| const text = `${String(args.question ?? "Which do you mean?")}${options.length ? "\n" + options.map((o) => `- ${o}`).join("\n") : ""}`; | |
| msgs.push({role: "tool", tool_call_id: c.id ?? "", content: JSON.stringify({shown: true})}); | |
| msgs.push({role: "assistant", content: text}); | |
| return {text, choice: options.length ? {labels: options} : null, history: msgs.slice(1), meta: {calls, served, ms: Date.now() - t0}}; | |
| } | |
| const fn = impl[c.function?.name]; | |
| let result; | |
| try { result = fn ? fn(args) : {error: "unknown tool"}; } catch (e) { result = {error: String(e.message)}; } | |
| msgs.push({role: "tool", tool_call_id: c.id ?? "", content: JSON.stringify(result).slice(0, 12000)}); | |
| } | |
| } | |
| msgs.push({role: "assistant", content: ""}); | |
| return {text: "", choice: null, history: msgs.slice(1), meta: {calls, served, ms: Date.now() - t0, stop: "tool_rounds"}}; | |
| } | |
| }; | |
| } | |