prompt-api-polyfill 1.16.0 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { n as e, t } from "../chunks/defaults-B5W7MP9T.js";
1
+ import { n as e, t } from "../chunks/defaults-DMD-8IKq.js";
2
2
  import { GoogleGenAI as n } from "@google/genai";
3
3
  //#region backends/gemini.js
4
4
  var r = class extends e {
@@ -1,4 +1,4 @@
1
- import { n as e, t } from "../chunks/defaults-B5W7MP9T.js";
1
+ import { n as e, t } from "../chunks/defaults-DMD-8IKq.js";
2
2
  import n from "openai";
3
3
  //#region backends/openai.js
4
4
  var r = class extends e {
@@ -1,4 +1,4 @@
1
- import { n as e, t } from "../chunks/defaults-B5W7MP9T.js";
1
+ import { n as e, t } from "../chunks/defaults-DMD-8IKq.js";
2
2
  import { TextStreamer as n, env as r, pipeline as i } from "@huggingface/transformers";
3
3
  //#region backends/transformers.js
4
4
  var a = class extends e {
@@ -25,13 +25,13 @@ var a = class extends e {
25
25
  total: 1,
26
26
  lengthComputable: !0
27
27
  })), e.__lastProgressLoaded = r);
28
+ }, n = (e) => {
29
+ e.status === "progress_total" ? t(e.progress / 100) : e.status === "ready" && t(1);
28
30
  };
29
31
  t(0), this.#e = await i("text-generation", this.modelName, {
30
32
  device: this.#n,
31
33
  dtype: this.#r,
32
- progress_callback: (e) => {
33
- e.status === "progress_total" ? t(e.progress / 100) : e.status === "ready" && t(1);
34
- }
34
+ progress_callback: n
35
35
  }), this.#t = this.#e.tokenizer;
36
36
  }
37
37
  return this.#e;
@@ -0,0 +1,107 @@
1
+ import { n as e, t } from "../chunks/defaults-DMD-8IKq.js";
2
+ import { CreateMLCEngine as n, prebuiltAppConfig as r } from "@mlc-ai/web-llm";
3
+ //#region backends/webllm.js
4
+ var i = class extends e {
5
+ #e;
6
+ #t;
7
+ #n = 0;
8
+ constructor(e = {}) {
9
+ super(e.modelName || t.webllm.modelName);
10
+ }
11
+ async #r(e) {
12
+ if (!this.#e) {
13
+ let t = (t) => {
14
+ if (!e) return;
15
+ let n = 1 / 65536, r = Math.floor(t / n) * n;
16
+ r <= e.__lastProgressLoaded || (e.dispatchEvent(new ProgressEvent("downloadprogress", {
17
+ loaded: r,
18
+ total: 1,
19
+ lengthComputable: !0
20
+ })), e.__lastProgressLoaded = r);
21
+ };
22
+ t(0);
23
+ let i = {
24
+ ...r,
25
+ cacheBackend: "cross-origin"
26
+ };
27
+ this.#e = await n(this.modelName, {
28
+ appConfig: i,
29
+ initProgressCallback: (e) => {
30
+ t(e.progress);
31
+ }
32
+ }), t(1);
33
+ }
34
+ return this.#e;
35
+ }
36
+ static availability(e) {
37
+ if (e?.expectedInputs && Array.isArray(e.expectedInputs)) {
38
+ for (let t of e.expectedInputs) if (t.type === "audio" || t.type === "image") return "unavailable";
39
+ }
40
+ return "available";
41
+ }
42
+ async createSession(e, t, n) {
43
+ return await this.#r(n), this.generationConfig = { max_tokens: 512 }, this.#t = t.systemInstruction, this.responseSchema = t.generationConfig?.responseSchema, this.#e;
44
+ }
45
+ async generateContent(e) {
46
+ let t = await this.#r(), n = {
47
+ messages: this.#i(e),
48
+ ...this.generationConfig
49
+ };
50
+ this.responseSchema && (n.response_format = {
51
+ type: "json_object",
52
+ schema: JSON.stringify(this.responseSchema)
53
+ });
54
+ let r = await t.chat.completions.create(n), i = r.choices[0].message.content;
55
+ return this.#n += (r.usage?.prompt_tokens ?? 0) + (r.usage?.completion_tokens ?? 0), {
56
+ text: i,
57
+ usage: this.#n
58
+ };
59
+ }
60
+ async generateContentStream(e) {
61
+ let t = await this.#r(), n = {
62
+ messages: this.#i(e),
63
+ ...this.generationConfig,
64
+ stream: !0,
65
+ stream_options: { include_usage: !0 }
66
+ };
67
+ this.responseSchema && (n.response_format = {
68
+ type: "json_object",
69
+ schema: JSON.stringify(this.responseSchema)
70
+ });
71
+ let r = await t.chat.completions.create(n), i = this;
72
+ return (async function* () {
73
+ let e = null;
74
+ for await (let t of r) {
75
+ let n = t.choices[0]?.delta?.content ?? "";
76
+ n && (yield {
77
+ text: () => n,
78
+ usageMetadata: { totalTokenCount: 0 }
79
+ }), t.usage && (e = t.usage);
80
+ }
81
+ i.#n += (e?.prompt_tokens ?? 0) + (e?.completion_tokens ?? 0), yield {
82
+ text: () => "",
83
+ usageMetadata: { totalTokenCount: i.#n }
84
+ };
85
+ })();
86
+ }
87
+ async countTokens(e) {
88
+ let t = "";
89
+ for (let n of e ?? []) for (let e of n?.parts ?? []) t += e.text ?? "";
90
+ return Math.ceil(t.length / 4);
91
+ }
92
+ #i(e) {
93
+ let t = e.map((e) => ({
94
+ role: e.role === "model" ? "assistant" : e.role === "system" ? "system" : "user",
95
+ content: e.parts.map((e) => e.text).join("")
96
+ }));
97
+ return this.#t && !t.some((e) => e.role === "system") && t.unshift({
98
+ role: "system",
99
+ content: this.#t
100
+ }), this.responseSchema && (t.length > 0 && t[0].role === "system" ? t[0].content += "\n\nRespond with valid JSON." : t.unshift({
101
+ role: "system",
102
+ content: "Respond with valid JSON."
103
+ })), t;
104
+ }
105
+ };
106
+ //#endregion
107
+ export { i as default };
@@ -29,7 +29,8 @@ var e = class {
29
29
  modelName: "onnx-community/gemma-3-1b-it-ONNX-GQA",
30
30
  device: "webgpu",
31
31
  dtype: "q4f16"
32
- }
32
+ },
33
+ webllm: { modelName: "Llama-3.2-3B-Instruct-q4f32_1-MLC" }
33
34
  };
34
35
  //#endregion
35
36
  export { e as n, t };
@@ -158,6 +158,10 @@ var e = class {
158
158
  {
159
159
  config: "TRANSFORMERS_CONFIG",
160
160
  path: "./backends/transformers.js"
161
+ },
162
+ {
163
+ config: "WEBLLM_CONFIG",
164
+ path: "./backends/webllm.js"
161
165
  }
162
166
  ];
163
167
  async function n(e) {
@@ -165,6 +169,7 @@ async function n(e) {
165
169
  if (e === "./backends/gemini.js") return (await import("./backends/gemini.js")).default;
166
170
  if (e === "./backends/openai.js") return (await import("./backends/openai.js")).default;
167
171
  if (e === "./backends/transformers.js") return (await import("./backends/transformers.js")).default;
172
+ if (e === "./backends/webllm.js") return (await import("./backends/webllm.js")).default;
168
173
  throw Error(`Unknown backend path "${e}"`);
169
174
  }
170
175
  //#endregion
@@ -550,7 +555,9 @@ var i = class i extends EventTarget {
550
555
  this.#r.systemInstruction && e.unshift({
551
556
  role: "system",
552
557
  parts: [{ text: this.#r.systemInstruction }]
553
- }), this.#a = await this.#e.countTokens(e) || 0;
558
+ });
559
+ let t = await this.#e.countTokens(e);
560
+ this.#a = t || 0;
554
561
  } catch {}
555
562
  this.#a > this.contextWindow && this.dispatchEvent(new Event("contextoverflow"));
556
563
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prompt-api-polyfill",
3
- "version": "1.16.0",
3
+ "version": "1.18.0",
4
4
  "description": "Polyfill for the Prompt API (`LanguageModel`) backed by Firebase AI Logic, Gemini API, OpenAI API, or Transformers.js.",
5
5
  "type": "module",
6
6
  "main": "./dist/prompt-api-polyfill.js",
@@ -47,19 +47,20 @@
47
47
  },
48
48
  "devDependencies": {
49
49
  "@eslint/js": "^10.0.1",
50
- "eslint": "^10.2.0",
50
+ "eslint": "^10.4.0",
51
51
  "eslint-config-prettier": "^10.1.8",
52
- "globals": "^17.5.0",
52
+ "globals": "^17.6.0",
53
53
  "node-addon-api": "^8.7.0",
54
- "node-gyp": "^12.2.0",
55
- "prettier": "^3.8.2",
54
+ "node-gyp": "^12.3.0",
55
+ "prettier": "^3.8.3",
56
56
  "prettier-plugin-curly": "^0.4.1",
57
- "vite": "^8.0.8"
57
+ "vite": "^8.0.13"
58
58
  },
59
59
  "dependencies": {
60
- "@google/genai": "^1.50.0",
61
- "@huggingface/transformers": "^4.0.1",
62
- "firebase": "^12.12.0",
63
- "openai": "^6.34.0"
60
+ "@google/genai": "^2.3.0",
61
+ "@huggingface/transformers": "^4.2.0",
62
+ "@mlc-ai/web-llm": "^0.2.83",
63
+ "firebase": "^12.13.0",
64
+ "openai": "^6.37.0"
64
65
  }
65
66
  }