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.
- package/dist/backends/firebase.js +1384 -1877
- package/dist/backends/gemini.js +55 -73
- package/dist/backends/openai.js +163 -216
- package/dist/backends/transformers.js +133 -179
- package/dist/chunks/defaults-B5W7MP9T.js +35 -0
- package/dist/prompt-api-polyfill.js +675 -1006
- package/package.json +9 -7
- package/dist/chunks/defaults-CzvdT-At.js +0 -72
|
@@ -1,186 +1,140 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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 };
|