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
package/dist/backends/gemini.js
CHANGED
|
@@ -1,74 +1,56 @@
|
|
|
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
|
-
totalTokenCount: o.usageMetadata?.totalTokenCount || 0
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
})();
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Counts tokens.
|
|
61
|
-
* @param {Array} contents - The content to count.
|
|
62
|
-
* @returns {Promise<number>} Total tokens.
|
|
63
|
-
*/
|
|
64
|
-
async countTokens(t) {
|
|
65
|
-
const { totalTokens: e } = await this.#n.models.countTokens({
|
|
66
|
-
model: this.#t,
|
|
67
|
-
contents: t
|
|
68
|
-
});
|
|
69
|
-
return e;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
export {
|
|
73
|
-
l as default
|
|
1
|
+
import { n as e, t } from "../chunks/defaults-B5W7MP9T.js";
|
|
2
|
+
import { GoogleGenAI as n } from "@google/genai";
|
|
3
|
+
//#region backends/gemini.js
|
|
4
|
+
var r = class extends e {
|
|
5
|
+
#e;
|
|
6
|
+
#t;
|
|
7
|
+
#n;
|
|
8
|
+
constructor(e) {
|
|
9
|
+
super(e.modelName || t.gemini.modelName), this.#e = new n({ apiKey: e.apiKey });
|
|
10
|
+
}
|
|
11
|
+
createSession(e, t) {
|
|
12
|
+
return this.#n = t, this.#t = e.modelName || this.modelName, {
|
|
13
|
+
model: this.#t,
|
|
14
|
+
params: t
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
async generateContent(e) {
|
|
18
|
+
let t = {
|
|
19
|
+
systemInstruction: this.#n.systemInstruction,
|
|
20
|
+
...this.#n.generationConfig
|
|
21
|
+
}, n = await this.#e.models.generateContent({
|
|
22
|
+
model: this.#t,
|
|
23
|
+
contents: e,
|
|
24
|
+
config: t
|
|
25
|
+
}), r = n.usageMetadata?.promptTokenCount || 0;
|
|
26
|
+
return {
|
|
27
|
+
text: n.text,
|
|
28
|
+
usage: r
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async generateContentStream(e) {
|
|
32
|
+
let t = {
|
|
33
|
+
systemInstruction: this.#n.systemInstruction,
|
|
34
|
+
...this.#n.generationConfig
|
|
35
|
+
}, n = await this.#e.models.generateContentStream({
|
|
36
|
+
model: this.#t,
|
|
37
|
+
contents: e,
|
|
38
|
+
config: t
|
|
39
|
+
});
|
|
40
|
+
return (async function* () {
|
|
41
|
+
for await (let e of n) yield {
|
|
42
|
+
text: () => e.text,
|
|
43
|
+
usageMetadata: { totalTokenCount: e.usageMetadata?.totalTokenCount || 0 }
|
|
44
|
+
};
|
|
45
|
+
})();
|
|
46
|
+
}
|
|
47
|
+
async countTokens(e) {
|
|
48
|
+
let { totalTokens: t } = await this.#e.models.countTokens({
|
|
49
|
+
model: this.#t,
|
|
50
|
+
contents: e
|
|
51
|
+
});
|
|
52
|
+
return t;
|
|
53
|
+
}
|
|
74
54
|
};
|
|
55
|
+
//#endregion
|
|
56
|
+
export { r as default };
|
package/dist/backends/openai.js
CHANGED
|
@@ -1,217 +1,164 @@
|
|
|
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
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
text: () => i,
|
|
163
|
-
usageMetadata: { totalTokenCount: 0 }
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
})();
|
|
167
|
-
} catch (a) {
|
|
168
|
-
throw console.error("OpenAI Generate Content Stream Error:", a), a;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Counts tokens.
|
|
173
|
-
* @param {Array} contents - The content to count.
|
|
174
|
-
* @returns {Promise<number>} Total tokens.
|
|
175
|
-
*/
|
|
176
|
-
async countTokens(t) {
|
|
177
|
-
let s = "";
|
|
178
|
-
if (Array.isArray(t)) {
|
|
179
|
-
for (const o of t)
|
|
180
|
-
if (o.parts)
|
|
181
|
-
for (const e of o.parts)
|
|
182
|
-
e.text ? s += e.text : e.inlineData && (s += " ".repeat(1e3));
|
|
183
|
-
}
|
|
184
|
-
return Math.ceil(s.length / 4);
|
|
185
|
-
}
|
|
186
|
-
#o(t, s) {
|
|
187
|
-
const o = [];
|
|
188
|
-
s && o.push({
|
|
189
|
-
role: "system",
|
|
190
|
-
content: s
|
|
191
|
-
});
|
|
192
|
-
for (const e of t) {
|
|
193
|
-
const r = e.role === "model" ? "assistant" : "user", a = [];
|
|
194
|
-
for (const n of e.parts)
|
|
195
|
-
if (n.text)
|
|
196
|
-
a.push({ type: "text", text: n.text });
|
|
197
|
-
else if (n.inlineData) {
|
|
198
|
-
const { data: i, mimeType: p } = n.inlineData;
|
|
199
|
-
p.startsWith("image/") ? a.push({
|
|
200
|
-
type: "image_url",
|
|
201
|
-
image_url: { url: `data:${p};base64,${i}` }
|
|
202
|
-
}) : p.startsWith("audio/") && a.push({
|
|
203
|
-
type: "input_audio",
|
|
204
|
-
input_audio: {
|
|
205
|
-
data: i,
|
|
206
|
-
format: p.split("/")[1] === "mpeg" ? "mp3" : "wav"
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
o.push({ role: r, content: a });
|
|
211
|
-
}
|
|
212
|
-
return { messages: o };
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
export {
|
|
216
|
-
y as default
|
|
1
|
+
import { n as e, t } from "../chunks/defaults-B5W7MP9T.js";
|
|
2
|
+
import n from "openai";
|
|
3
|
+
//#region backends/openai.js
|
|
4
|
+
var r = class extends e {
|
|
5
|
+
#e;
|
|
6
|
+
constructor(e) {
|
|
7
|
+
super(e.modelName || t.openai.modelName), this.config = e, this.openai = new n({
|
|
8
|
+
apiKey: e.apiKey,
|
|
9
|
+
dangerouslyAllowBrowser: !0
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
static availability(e = {}) {
|
|
13
|
+
if (e.expectedInputs) {
|
|
14
|
+
let t = e.expectedInputs.some((e) => e.type === "audio"), n = e.expectedInputs.some((e) => e.type === "image");
|
|
15
|
+
if (t && n) return "unavailable";
|
|
16
|
+
}
|
|
17
|
+
return "available";
|
|
18
|
+
}
|
|
19
|
+
convertSchema(e) {
|
|
20
|
+
if (typeof e != "object" || !e) return {
|
|
21
|
+
schema: e,
|
|
22
|
+
wrapped: !1
|
|
23
|
+
};
|
|
24
|
+
let t = (e) => {
|
|
25
|
+
if (e.type === "object") if (e.properties) {
|
|
26
|
+
e.additionalProperties = !1, e.required = Object.keys(e.properties);
|
|
27
|
+
for (let n in e.properties) t(e.properties[n]);
|
|
28
|
+
} else e.additionalProperties = !1, e.required = [];
|
|
29
|
+
else e.type === "array" && e.items && t(e.items);
|
|
30
|
+
return e;
|
|
31
|
+
}, n = JSON.parse(JSON.stringify(e));
|
|
32
|
+
return n.type === "object" ? {
|
|
33
|
+
wrapped: !1,
|
|
34
|
+
schema: t(n)
|
|
35
|
+
} : {
|
|
36
|
+
wrapped: !0,
|
|
37
|
+
schema: {
|
|
38
|
+
type: "object",
|
|
39
|
+
properties: { value: n },
|
|
40
|
+
required: ["value"],
|
|
41
|
+
additionalProperties: !1
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
createSession(e, t) {
|
|
46
|
+
this.#e = {
|
|
47
|
+
model: e.modelName || this.modelName,
|
|
48
|
+
systemInstruction: t.systemInstruction
|
|
49
|
+
};
|
|
50
|
+
let n = t.generationConfig || {};
|
|
51
|
+
if (n.responseSchema) {
|
|
52
|
+
let { schema: e, wrapped: t } = n.responseSchema;
|
|
53
|
+
this.#e.response_format = {
|
|
54
|
+
type: "json_schema",
|
|
55
|
+
json_schema: {
|
|
56
|
+
name: "response",
|
|
57
|
+
strict: !0,
|
|
58
|
+
schema: e
|
|
59
|
+
}
|
|
60
|
+
}, this.#e.response_wrapped = t;
|
|
61
|
+
} else n.responseMimeType === "application/json" && (this.#e.response_format = { type: "json_object" });
|
|
62
|
+
return this.#e;
|
|
63
|
+
}
|
|
64
|
+
#t(e) {
|
|
65
|
+
let t = !1, n = !1;
|
|
66
|
+
for (let r of e) if (Array.isArray(r.content)) for (let e of r.content) e.type === "image_url" && (t = !0), e.type === "input_audio" && (n = !0);
|
|
67
|
+
if (t && n) throw Error("OpenAI backend does not support mixing images and audio in the same session. Please start a new session.");
|
|
68
|
+
return {
|
|
69
|
+
hasImage: t,
|
|
70
|
+
hasAudio: n
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
#n(e) {
|
|
74
|
+
return this.#e.model === this.modelName ? e ? `${this.modelName}-audio-preview` : this.modelName : this.#e.model;
|
|
75
|
+
}
|
|
76
|
+
async generateContent(e) {
|
|
77
|
+
let { messages: t } = this.#r(e, this.#e.systemInstruction), { hasAudio: n } = this.#t(t), r = this.#n(n);
|
|
78
|
+
if (r === `${this.modelName}-audio-preview` && this.#e.response_format) throw new DOMException(`OpenAI audio model ('${r}') does not support structured outputs (responseConstraint).`, "NotSupportedError");
|
|
79
|
+
let i = {
|
|
80
|
+
model: r,
|
|
81
|
+
messages: t
|
|
82
|
+
};
|
|
83
|
+
this.#e.temperature > 0 && (i.temperature = this.#e.temperature), this.#e.response_format && (i.response_format = this.#e.response_format);
|
|
84
|
+
try {
|
|
85
|
+
let e = await this.openai.chat.completions.create(i), t = e.choices[0].message.content;
|
|
86
|
+
if (this.#e.response_wrapped && t) try {
|
|
87
|
+
let e = JSON.parse(t);
|
|
88
|
+
e && typeof e == "object" && "value" in e && (t = JSON.stringify(e.value));
|
|
89
|
+
} catch {}
|
|
90
|
+
let n = e.usage?.prompt_tokens || 0;
|
|
91
|
+
return {
|
|
92
|
+
text: t,
|
|
93
|
+
usage: n
|
|
94
|
+
};
|
|
95
|
+
} catch (e) {
|
|
96
|
+
throw console.error("OpenAI Generate Content Error:", e), e;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async generateContentStream(e) {
|
|
100
|
+
let { messages: t } = this.#r(e, this.#e.systemInstruction), { hasAudio: n } = this.#t(t), r = this.#n(n);
|
|
101
|
+
if (r === `${this.modelName}-audio-preview` && this.#e.response_format) throw new DOMException(`OpenAI audio model ('${r}') does not support structured outputs (responseConstraint).`, "NotSupportedError");
|
|
102
|
+
let i = {
|
|
103
|
+
model: r,
|
|
104
|
+
messages: t,
|
|
105
|
+
stream: !0
|
|
106
|
+
};
|
|
107
|
+
this.#e.temperature > 0 && (i.temperature = this.#e.temperature), this.#e.response_format && (i.response_format = this.#e.response_format);
|
|
108
|
+
try {
|
|
109
|
+
let e = await this.openai.chat.completions.create(i);
|
|
110
|
+
return (async function* () {
|
|
111
|
+
for await (let t of e) {
|
|
112
|
+
let e = t.choices[0]?.delta?.content;
|
|
113
|
+
e && (yield {
|
|
114
|
+
text: () => e,
|
|
115
|
+
usageMetadata: { totalTokenCount: 0 }
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
})();
|
|
119
|
+
} catch (e) {
|
|
120
|
+
throw console.error("OpenAI Generate Content Stream Error:", e), e;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async countTokens(e) {
|
|
124
|
+
let t = "";
|
|
125
|
+
if (Array.isArray(e)) {
|
|
126
|
+
for (let n of e) if (n.parts) for (let e of n.parts) e.text ? t += e.text : e.inlineData && (t += " ".repeat(1e3));
|
|
127
|
+
}
|
|
128
|
+
return Math.ceil(t.length / 4);
|
|
129
|
+
}
|
|
130
|
+
#r(e, t) {
|
|
131
|
+
let n = [];
|
|
132
|
+
t && n.push({
|
|
133
|
+
role: "system",
|
|
134
|
+
content: t
|
|
135
|
+
});
|
|
136
|
+
for (let t of e) {
|
|
137
|
+
let e = t.role === "model" ? "assistant" : "user", r = [];
|
|
138
|
+
for (let e of t.parts) if (e.text) r.push({
|
|
139
|
+
type: "text",
|
|
140
|
+
text: e.text
|
|
141
|
+
});
|
|
142
|
+
else if (e.inlineData) {
|
|
143
|
+
let { data: t, mimeType: n } = e.inlineData;
|
|
144
|
+
n.startsWith("image/") ? r.push({
|
|
145
|
+
type: "image_url",
|
|
146
|
+
image_url: { url: `data:${n};base64,${t}` }
|
|
147
|
+
}) : n.startsWith("audio/") && r.push({
|
|
148
|
+
type: "input_audio",
|
|
149
|
+
input_audio: {
|
|
150
|
+
data: t,
|
|
151
|
+
format: n.split("/")[1] === "mpeg" ? "mp3" : "wav"
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
n.push({
|
|
156
|
+
role: e,
|
|
157
|
+
content: r
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return { messages: n };
|
|
161
|
+
}
|
|
217
162
|
};
|
|
163
|
+
//#endregion
|
|
164
|
+
export { r as default };
|