Spaces:
Running
Running
Download src/agent/llm.js from VectorW/xlerobot-sim: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/src/agent/llm.js
- Command line
-
hf download hf://spaces/VectorW/xlerobot-sim/src/agent/llm.js
-
curl -L -o llm.js https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/src/agent/llm.js
2.78 kB
| // OpenAI-compatible chat completions with tool calling, from the browser. Hugging Face's router | |
| // (Inference Providers) speaks this API, and so do local servers such as Ollama or LM Studio. | |
| export const HF_ROUTER = "https://router.huggingface.co/v1"; | |
| export const DEFAULT_MODEL = "Qwen/Qwen3.8-27B"; | |
| /** One completion: returns the assistant message ({content, tool_calls}). */ | |
| export async function complete({ baseUrl, apiKey, model, messages, tools, signal }) { | |
| const response = await fetch(`${baseUrl.replace(/\/$/, "")}/chat/completions`, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json", ...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}) }, | |
| body: JSON.stringify({ | |
| model, | |
| messages, | |
| tools: tools.map((t) => ({ type: "function", function: { name: t.name, description: t.description, parameters: t.parameters } })), | |
| tool_choice: "auto", | |
| temperature: 0.2, | |
| max_tokens: 1200, | |
| }), | |
| signal, | |
| }); | |
| if (response.status === 402) { | |
| throw new Error( | |
| "This Hugging Face account has no Inference Providers credits left (402). Add credits or PRO on huggingface.co, or use another OpenAI-compatible endpoint under “Model and endpoint” (for example a local Ollama server at http://localhost:11434/v1 started with OLLAMA_ORIGINS set to this page).", | |
| ); | |
| } | |
| if (response.status === 401) throw new Error("The token was rejected (401): sign in again or check the key under “Model and endpoint”."); | |
| if (!response.ok) { | |
| let detail = ""; | |
| try { | |
| const body = await response.json(); | |
| detail = body.error?.message ?? body.error ?? JSON.stringify(body); | |
| } catch { | |
| detail = await response.text().catch(() => ""); | |
| } | |
| throw new Error(`${response.status} ${typeof detail === "string" ? detail.slice(0, 300) : ""}`.trim()); | |
| } | |
| const data = await response.json(); | |
| const message = data.choices?.[0]?.message; | |
| if (!message) throw new Error("the model returned no message"); | |
| return message; | |
| } | |
| /** Models on Hugging Face's router that support tool calls, cheapest first: [{id, vision}]. */ | |
| export async function routerModels() { | |
| const response = await fetch(`${HF_ROUTER}/models`); | |
| if (!response.ok) throw new Error(`models: HTTP ${response.status}`); | |
| const { data } = await response.json(); | |
| return data | |
| .map((m) => { | |
| const live = (m.providers ?? []).filter((p) => p.supports_tools && p.status !== "offline"); | |
| if (!live.length) return null; | |
| const price = Math.min(...live.map((p) => p.pricing?.input ?? 99)); | |
| return { id: m.id, vision: (m.architecture?.input_modalities ?? []).includes("image"), price, providers: live.length }; | |
| }) | |
| .filter(Boolean) | |
| .sort((a, b) => a.price - b.price || b.providers - a.providers); | |
| } | |