UNPKG

llm-hooks

Version:

A collection of useful React hooks for llm-related functionality

69 lines (68 loc) 1.89 kB
import { useState as _, useEffect as L, useCallback as f } from "react"; import { env as i, TextStreamer as w, pipeline as h } from "@huggingface/transformers"; const m = globalThis; let t = m.__LLM || { generator: null }; m.__LLM = t; const x = ({ envConfig: a, modelConfig: n = { modelName: "onnx-community/gemma-3-270m-it-ONNX", dtype: "fp32", device: "webgpu" } }) => { const [p, s] = _({ isLoading: !1, isReady: !!t.generator, error: null, progress: t.generator ? 100 : 0 }); L(() => { a && (i.remoteHost = a.remoteHost, i.remotePathTemplate = a.remotePathTemplate, i.backends.onnx.wasm.wasmPaths = a.wasmPaths); }, [a]); const d = async () => { if (t.generator) return t.generator; s((e) => ({ ...e, isLoading: !0, error: null, progress: 0 })); try { const e = await h("text-generation", n.modelName, { dtype: n.dtype, device: n.device, progress_callback: (r) => { r.status === "progress" && r.file?.endsWith?.("onnx_data") && s((o) => ({ ...o, progress: r.progress || 0 })); } }); return t.generator = e, s((r) => ({ ...r, isLoading: !1, isReady: !0, progress: 100 })), e; } catch (e) { throw s((r) => ({ ...r, isLoading: !1, error: e instanceof Error ? e.message : "Failed to load model" })), e; } }, g = f(async (e, r, o) => { const l = t.generator; if (!l) throw new Error("Model not loaded. Call load() first."); const u = new w(l.tokenizer, { skip_prompt: !0, skip_special_tokens: !0, callback_function: (c) => { r?.(c), o?.push(c); } }); await l(e, { max_new_tokens: 1024, do_sample: !1, streamer: u }), o?.close(); }, []); return { ...p, load: d, generate: g }; }; export { x as useLLM };