prompt-api-polyfill 1.14.0 → 1.14.2

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,186 +1,140 @@
1
- import { env as p, pipeline as m, TextStreamer as h } from "@huggingface/transformers";
2
- import { P as f, D as c } from "../chunks/defaults-CzvdT-At.js";
3
- class w extends f {
4
- #e;
5
- #t;
6
- #r;
7
- #a;
8
- #n;
9
- constructor(t = {}) {
10
- if (super(t.modelName || c.transformers.modelName), this.#r = t.device || c.transformers.device, this.#a = t.dtype || c.transformers.dtype, t.isDefault && console.log(
11
- `Polyfill: No backend configuration found. Defaulting to Transformers.js with model: ${this.modelName}`
12
- ), p.experimental_useCrossOriginStorage = !0, t.env) {
13
- const e = (n, o) => {
14
- for (const [r, s] of Object.entries(o))
15
- s && typeof s == "object" && !Array.isArray(s) && n[r] && typeof n[r] == "object" ? e(n[r], s) : n[r] = s;
16
- };
17
- e(p, t.env);
18
- }
19
- }
20
- /**
21
- * Loaded models can be large, so we initialize them lazily.
22
- * @param {EventTarget} [monitorTarget] - The event target to dispatch download progress events to.
23
- * @returns {Promise<Object>} The generator.
24
- */
25
- async #s(t) {
26
- if (!this.#e) {
27
- const e = (o) => {
28
- if (!t)
29
- return;
30
- const r = 1 / 65536, s = Math.floor(o / r) * r;
31
- s <= t.__lastProgressLoaded || (t.dispatchEvent(
32
- new ProgressEvent("downloadprogress", {
33
- loaded: s,
34
- total: 1,
35
- lengthComputable: !0
36
- })
37
- ), t.__lastProgressLoaded = s);
38
- }, n = (o) => {
39
- o.status === "progress_total" ? e(o.progress / 100) : o.status === "ready" && e(1);
40
- };
41
- e(0), this.#e = await m("text-generation", this.modelName, {
42
- device: this.#r,
43
- dtype: this.#a,
44
- progress_callback: n
45
- }), this.#t = this.#e.tokenizer;
46
- }
47
- return this.#e;
48
- }
49
- /**
50
- * Checks if the backend is available given the options.
51
- * @param {Object} options - LanguageModel options.
52
- * @returns {string} 'available' or 'unavailable'.
53
- */
54
- static availability(t) {
55
- if (t?.expectedInputs && Array.isArray(t.expectedInputs)) {
56
- for (const e of t.expectedInputs)
57
- if (e.type === "audio" || e.type === "image")
58
- return "unavailable";
59
- }
60
- return "available";
61
- }
62
- /**
63
- * Creates a new session.
64
- * @param {Object} options - LanguageModel options.
65
- * @param {Object} sessionParams - Session parameters.
66
- * @param {EventTarget} [monitorTarget] - The event target to dispatch download progress events to.
67
- * @returns {Promise<Object>} The generator.
68
- */
69
- async createSession(t, e, n) {
70
- return await this.#s(n), this.generationConfig = {
71
- max_new_tokens: 512,
72
- // Default limit
73
- do_sample: !1,
74
- return_full_text: !1
75
- }, this.#n = e.systemInstruction, this.responseSchema = e.generationConfig?.responseSchema, this.responseSchema && console.warn(
76
- "Polyfill: `responseConstraint` is not natively supported by the Transformers.js backend and is implemented via prompt engineering, which may fail. For better results, consider adding few-shot examples to your prompt."
77
- ), this.#e;
78
- }
79
- /**
80
- * Generates content (non-streaming).
81
- * @param {Array} contents - The history + new message content.
82
- * @returns {Promise<{text: string, usage: number}>}
83
- */
84
- async generateContent(t) {
85
- const e = await this.#s(), n = this.#o(t), o = this.#t.apply_chat_template(n, {
86
- tokenize: !1,
87
- add_generation_prompt: !0
88
- }), s = (await e(o, {
89
- ...this.generationConfig,
90
- add_special_tokens: !1
91
- }))[0].generated_text, i = await this.countTokens(t);
92
- return { text: s, usage: i };
93
- }
94
- /**
95
- * Generates content stream.
96
- * @param {Array} contents - The history + new content.
97
- * @returns {Promise<AsyncIterable>} Stream of chunks.
98
- */
99
- async generateContentStream(t) {
100
- const e = await this.#s(), n = this.#o(t), o = this.#t.apply_chat_template(n, {
101
- tokenize: !1,
102
- add_generation_prompt: !0
103
- }), r = [];
104
- let s, i = new Promise((a) => s = a), l = !1;
105
- const u = (a) => {
106
- r.push(a), s && (s(), s = null);
107
- }, d = new h(this.#t, {
108
- skip_prompt: !0,
109
- skip_special_tokens: !0,
110
- callback_function: u
111
- });
112
- return e(o, {
113
- ...this.generationConfig,
114
- add_special_tokens: !1,
115
- streamer: d
116
- }).then(() => {
117
- l = !0, s && (s(), s = null);
118
- }).catch((a) => {
119
- console.error("[Transformers.js] Generation error:", a), l = !0, s && (s(), s = null);
120
- }), (async function* () {
121
- for (; ; ) {
122
- for (r.length === 0 && !l && (s || (i = new Promise((a) => s = a)), await i); r.length > 0; ) {
123
- const a = r.shift();
124
- yield {
125
- text: () => a,
126
- usageMetadata: { totalTokenCount: 0 }
127
- };
128
- }
129
- if (l)
130
- break;
131
- }
132
- })();
133
- }
134
- /**
135
- * Counts tokens.
136
- * @param {Array} contents - The content to count.
137
- * @returns {Promise<number>} Total tokens.
138
- */
139
- async countTokens(t) {
140
- await this.#s();
141
- const e = this.#o(t);
142
- return this.#t.apply_chat_template(e, {
143
- tokenize: !0,
144
- add_generation_prompt: !1,
145
- return_tensor: !1
146
- }).length;
147
- }
148
- #o(t) {
149
- const e = t.map((n) => {
150
- let o = n.role === "model" ? "assistant" : n.role === "system" ? "system" : "user";
151
- const r = n.parts.map((s) => s.text).join("");
152
- return { role: o, content: r };
153
- });
154
- if (this.#n && !e.some((n) => n.role === "system") && e.unshift({ role: "system", content: this.#n }), this.#i(e), this.modelName.toLowerCase().includes("gemma")) {
155
- const n = e.findIndex((o) => o.role === "system");
156
- if (n !== -1) {
157
- const o = e[n], r = e.findIndex(
158
- (s, i) => s.role === "user" && i > n
159
- );
160
- r !== -1 ? (e[r].content = o.content + `
161
-
162
- ` + e[r].content, e.splice(n, 1)) : (o.content += `
163
-
164
- `, o.role = "user");
165
- }
166
- }
167
- return e;
168
- }
169
- #i(t) {
170
- if (this.responseSchema) {
171
- const e = `Respond ONLY with a raw JSON object matching this JSON Schema:
1
+ import { n as e, t } from "../chunks/defaults-B5W7MP9T.js";
2
+ import { TextStreamer as n, env as r, pipeline as i } from "@huggingface/transformers";
3
+ //#region backends/transformers.js
4
+ var a = class extends e {
5
+ #e;
6
+ #t;
7
+ #n;
8
+ #r;
9
+ #i;
10
+ constructor(e = {}) {
11
+ if (super(e.modelName || t.transformers.modelName), this.#n = e.device || t.transformers.device || "webgpu", this.#r = e.dtype || t.transformers.dtype || "q4f16", e.isDefault && console.log(`Polyfill: No backend configuration found. Defaulting to Transformers.js with model: ${this.modelName}`), r.experimental_useCrossOriginStorage = !0, e.env) {
12
+ let t = (e, n) => {
13
+ for (let [r, i] of Object.entries(n)) i && typeof i == "object" && !Array.isArray(i) && e[r] && typeof e[r] == "object" ? t(e[r], i) : e[r] = i;
14
+ };
15
+ t(r, e.env);
16
+ }
17
+ }
18
+ async #a(e) {
19
+ if (!this.#e) {
20
+ let t = (t) => {
21
+ if (!e) return;
22
+ let n = 1 / 65536, r = Math.floor(t / n) * n;
23
+ r <= e.__lastProgressLoaded || (e.dispatchEvent(new ProgressEvent("downloadprogress", {
24
+ loaded: r,
25
+ total: 1,
26
+ lengthComputable: !0
27
+ })), e.__lastProgressLoaded = r);
28
+ };
29
+ t(0), this.#e = await i("text-generation", this.modelName, {
30
+ device: this.#n,
31
+ dtype: this.#r,
32
+ progress_callback: (e) => {
33
+ e.status === "progress_total" ? t(e.progress / 100) : e.status === "ready" && t(1);
34
+ }
35
+ }), this.#t = this.#e.tokenizer;
36
+ }
37
+ return this.#e;
38
+ }
39
+ static availability(e) {
40
+ if (e?.expectedInputs && Array.isArray(e.expectedInputs)) {
41
+ for (let t of e.expectedInputs) if (t.type === "audio" || t.type === "image") return "unavailable";
42
+ }
43
+ return "available";
44
+ }
45
+ async createSession(e, t, n) {
46
+ return await this.#a(n), this.generationConfig = {
47
+ max_new_tokens: 512,
48
+ do_sample: !1,
49
+ return_full_text: !1
50
+ }, this.#i = t.systemInstruction, this.responseSchema = t.generationConfig?.responseSchema, this.responseSchema && console.warn("Polyfill: `responseConstraint` is not natively supported by the Transformers.js backend and is implemented via prompt engineering, which may fail. For better results, consider adding few-shot examples to your prompt."), this.#e;
51
+ }
52
+ async generateContent(e) {
53
+ let t = await this.#a(), n = this.#o(e);
54
+ return {
55
+ text: (await t(this.#t.apply_chat_template(n, {
56
+ tokenize: !1,
57
+ add_generation_prompt: !0
58
+ }), {
59
+ ...this.generationConfig,
60
+ add_special_tokens: !1
61
+ }))[0].generated_text,
62
+ usage: await this.countTokens(e)
63
+ };
64
+ }
65
+ async generateContentStream(e) {
66
+ let t = await this.#a(), r = this.#o(e), i = this.#t.apply_chat_template(r, {
67
+ tokenize: !1,
68
+ add_generation_prompt: !0
69
+ }), a = [], o, s = new Promise((e) => o = e), c = !1, l = new n(this.#t, {
70
+ skip_prompt: !0,
71
+ skip_special_tokens: !0,
72
+ callback_function: (e) => {
73
+ a.push(e), o &&= (o(), null);
74
+ }
75
+ });
76
+ return t(i, {
77
+ ...this.generationConfig,
78
+ add_special_tokens: !1,
79
+ streamer: l
80
+ }).then(() => {
81
+ c = !0, o &&= (o(), null);
82
+ }).catch((e) => {
83
+ console.error("[Transformers.js] Generation error:", e), c = !0, o &&= (o(), null);
84
+ }), (async function* () {
85
+ for (;;) {
86
+ for (a.length === 0 && !c && (o || (s = new Promise((e) => o = e)), await s); a.length > 0;) {
87
+ let e = a.shift();
88
+ yield {
89
+ text: () => e,
90
+ usageMetadata: { totalTokenCount: 0 }
91
+ };
92
+ }
93
+ if (c) break;
94
+ }
95
+ })();
96
+ }
97
+ async countTokens(e) {
98
+ await this.#a();
99
+ let t = this.#o(e);
100
+ return this.#t.apply_chat_template(t, {
101
+ tokenize: !0,
102
+ add_generation_prompt: !1,
103
+ return_tensor: !1
104
+ }).length;
105
+ }
106
+ #o(e) {
107
+ let t = e.map((e) => ({
108
+ role: e.role === "model" ? "assistant" : e.role === "system" ? "system" : "user",
109
+ content: e.parts.map((e) => e.text).join("")
110
+ }));
111
+ if (this.#i && !t.some((e) => e.role === "system") && t.unshift({
112
+ role: "system",
113
+ content: this.#i
114
+ }), this.#s(t), this.modelName.toLowerCase().includes("gemma")) {
115
+ let e = t.findIndex((e) => e.role === "system");
116
+ if (e !== -1) {
117
+ let n = t[e], r = t.findIndex((t, n) => t.role === "user" && n > e);
118
+ r === -1 ? (n.content += "\n\n", n.role = "user") : (t[r].content = n.content + "\n\n" + t[r].content, t.splice(e, 1));
119
+ }
120
+ }
121
+ return t;
122
+ }
123
+ #s(e) {
124
+ if (this.responseSchema) {
125
+ let t = `Respond ONLY with a raw JSON object matching this JSON Schema:
172
126
 
173
127
  \`\`\`json
174
128
  ${JSON.stringify(this.responseSchema, null, 2)}
175
129
  \`\`\`
176
130
 
177
131
  DO NOT include Markdown code blocks, explanations, or any other text.`;
178
- t.length > 0 && t[0].role === "system" ? t[0].content = e + `
179
-
180
- ` + t[0].content : t.unshift({ role: "system", content: e });
181
- }
182
- }
183
- }
184
- export {
185
- w as default
132
+ e.length > 0 && e[0].role === "system" ? e[0].content = t + "\n\n" + e[0].content : e.unshift({
133
+ role: "system",
134
+ content: t
135
+ });
136
+ }
137
+ }
186
138
  };
139
+ //#endregion
140
+ export { a as default };
@@ -0,0 +1,35 @@
1
+ //#region backends/base.js
2
+ var e = class {
3
+ constructor(e) {
4
+ this.modelName = e;
5
+ }
6
+ static availability(e) {
7
+ return "available";
8
+ }
9
+ createSession(e, t, n) {
10
+ throw Error("Not implemented");
11
+ }
12
+ async generateContent(e) {
13
+ throw Error("Not implemented");
14
+ }
15
+ async generateContentStream(e) {
16
+ throw Error("Not implemented");
17
+ }
18
+ async countTokens(e) {
19
+ throw Error("Not implemented");
20
+ }
21
+ convertSchema(e) {
22
+ return e;
23
+ }
24
+ }, t = {
25
+ firebase: { modelName: "gemini-2.5-flash-lite" },
26
+ gemini: { modelName: "gemini-2.5-flash-lite" },
27
+ openai: { modelName: "gpt-4o" },
28
+ transformers: {
29
+ modelName: "onnx-community/gemma-3-1b-it-ONNX-GQA",
30
+ device: "webgpu",
31
+ dtype: "q4f16"
32
+ }
33
+ };
34
+ //#endregion
35
+ export { e as n, t };