@firebase/ai 0.0.1
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/README.md +5 -0
- package/dist/ai-public.d.ts +1972 -0
- package/dist/ai.d.ts +2073 -0
- package/dist/esm/index.esm2017.js +2624 -0
- package/dist/esm/index.esm2017.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/src/api.d.ts +120 -0
- package/dist/esm/src/backend.d.ts +74 -0
- package/dist/esm/src/constants.d.ts +23 -0
- package/dist/esm/src/errors.d.ts +35 -0
- package/dist/esm/src/googleai-mappers.d.ts +73 -0
- package/dist/esm/src/helpers.d.ts +30 -0
- package/dist/esm/src/index.d.ts +12 -0
- package/dist/esm/src/index.node.d.ts +7 -0
- package/dist/esm/src/logger.d.ts +18 -0
- package/dist/esm/src/methods/chat-session-helpers.d.ts +18 -0
- package/dist/esm/src/methods/chat-session.d.ts +50 -0
- package/dist/esm/src/methods/count-tokens.d.ts +19 -0
- package/dist/esm/src/methods/generate-content.d.ts +20 -0
- package/dist/esm/src/models/ai-model.d.ts +72 -0
- package/dist/esm/src/models/generative-model.d.ts +54 -0
- package/dist/esm/src/models/imagen-model.d.ts +102 -0
- package/dist/esm/src/models/index.d.ts +19 -0
- package/dist/esm/src/public-types.d.ts +106 -0
- package/dist/esm/src/requests/imagen-image-format.d.ts +61 -0
- package/dist/esm/src/requests/request-helpers.d.ts +28 -0
- package/dist/esm/src/requests/request.d.ts +43 -0
- package/dist/esm/src/requests/response-helpers.d.ts +53 -0
- package/dist/esm/src/requests/schema-builder.d.ts +145 -0
- package/dist/esm/src/requests/stream-reader.d.ts +38 -0
- package/dist/esm/src/service.d.ts +31 -0
- package/dist/esm/src/types/content.d.ts +144 -0
- package/dist/esm/src/types/enums.d.ts +263 -0
- package/dist/esm/src/types/error.d.ts +81 -0
- package/dist/esm/src/types/googleai.d.ts +56 -0
- package/dist/esm/src/types/imagen/index.d.ts +18 -0
- package/dist/esm/src/types/imagen/internal.d.ts +124 -0
- package/dist/esm/src/types/imagen/requests.d.ts +211 -0
- package/dist/esm/src/types/imagen/responses.d.ts +78 -0
- package/dist/esm/src/types/index.d.ts +24 -0
- package/dist/esm/src/types/internal.d.ts +33 -0
- package/dist/esm/src/types/requests.d.ts +213 -0
- package/dist/esm/src/types/responses.d.ts +257 -0
- package/dist/esm/src/types/schema.d.ts +102 -0
- package/dist/index.cjs.js +2652 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.node.cjs.js +2652 -0
- package/dist/index.node.cjs.js.map +1 -0
- package/dist/index.node.mjs +2624 -0
- package/dist/index.node.mjs.map +1 -0
- package/dist/src/api.d.ts +120 -0
- package/dist/src/backend.d.ts +74 -0
- package/dist/src/constants.d.ts +23 -0
- package/dist/src/errors.d.ts +35 -0
- package/dist/src/googleai-mappers.d.ts +73 -0
- package/dist/src/helpers.d.ts +30 -0
- package/dist/src/index.d.ts +12 -0
- package/dist/src/index.node.d.ts +7 -0
- package/dist/src/logger.d.ts +18 -0
- package/dist/src/methods/chat-session-helpers.d.ts +18 -0
- package/dist/src/methods/chat-session.d.ts +50 -0
- package/dist/src/methods/count-tokens.d.ts +19 -0
- package/dist/src/methods/generate-content.d.ts +20 -0
- package/dist/src/models/ai-model.d.ts +72 -0
- package/dist/src/models/generative-model.d.ts +54 -0
- package/dist/src/models/imagen-model.d.ts +102 -0
- package/dist/src/models/index.d.ts +19 -0
- package/dist/src/public-types.d.ts +106 -0
- package/dist/src/requests/imagen-image-format.d.ts +61 -0
- package/dist/src/requests/request-helpers.d.ts +28 -0
- package/dist/src/requests/request.d.ts +43 -0
- package/dist/src/requests/response-helpers.d.ts +53 -0
- package/dist/src/requests/schema-builder.d.ts +145 -0
- package/dist/src/requests/stream-reader.d.ts +38 -0
- package/dist/src/service.d.ts +31 -0
- package/dist/src/tsdoc-metadata.json +11 -0
- package/dist/src/types/content.d.ts +144 -0
- package/dist/src/types/enums.d.ts +263 -0
- package/dist/src/types/error.d.ts +81 -0
- package/dist/src/types/googleai.d.ts +56 -0
- package/dist/src/types/imagen/index.d.ts +18 -0
- package/dist/src/types/imagen/internal.d.ts +124 -0
- package/dist/src/types/imagen/requests.d.ts +211 -0
- package/dist/src/types/imagen/responses.d.ts +78 -0
- package/dist/src/types/index.d.ts +24 -0
- package/dist/src/types/internal.d.ts +33 -0
- package/dist/src/types/requests.d.ts +213 -0
- package/dist/src/types/responses.d.ts +257 -0
- package/dist/src/types/schema.d.ts +102 -0
- package/package.json +81 -0
|
@@ -0,0 +1,2652 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var app = require('@firebase/app');
|
|
6
|
+
var component = require('@firebase/component');
|
|
7
|
+
var util = require('@firebase/util');
|
|
8
|
+
var logger$1 = require('@firebase/logger');
|
|
9
|
+
var tslib = require('tslib');
|
|
10
|
+
|
|
11
|
+
var name = "@firebase/ai";
|
|
12
|
+
var version = "1.2.2";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @license
|
|
16
|
+
* Copyright 2024 Google LLC
|
|
17
|
+
*
|
|
18
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
19
|
+
* you may not use this file except in compliance with the License.
|
|
20
|
+
* You may obtain a copy of the License at
|
|
21
|
+
*
|
|
22
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
23
|
+
*
|
|
24
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
25
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
26
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
27
|
+
* See the License for the specific language governing permissions and
|
|
28
|
+
* limitations under the License.
|
|
29
|
+
*/
|
|
30
|
+
const AI_TYPE = 'AI';
|
|
31
|
+
const DEFAULT_LOCATION = 'us-central1';
|
|
32
|
+
const DEFAULT_BASE_URL = 'https://firebasevertexai.googleapis.com';
|
|
33
|
+
const DEFAULT_API_VERSION = 'v1beta';
|
|
34
|
+
const PACKAGE_VERSION = version;
|
|
35
|
+
const LANGUAGE_TAG = 'gl-js';
|
|
36
|
+
const DEFAULT_FETCH_TIMEOUT_MS = 180 * 1000;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @license
|
|
40
|
+
* Copyright 2024 Google LLC
|
|
41
|
+
*
|
|
42
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
43
|
+
* you may not use this file except in compliance with the License.
|
|
44
|
+
* You may obtain a copy of the License at
|
|
45
|
+
*
|
|
46
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
47
|
+
*
|
|
48
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
49
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
50
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
51
|
+
* See the License for the specific language governing permissions and
|
|
52
|
+
* limitations under the License.
|
|
53
|
+
*/
|
|
54
|
+
/**
|
|
55
|
+
* Possible roles.
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
const POSSIBLE_ROLES = ['user', 'model', 'function', 'system'];
|
|
59
|
+
/**
|
|
60
|
+
* Harm categories that would cause prompts or candidates to be blocked.
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
exports.HarmCategory = void 0;
|
|
64
|
+
(function (HarmCategory) {
|
|
65
|
+
HarmCategory["HARM_CATEGORY_HATE_SPEECH"] = "HARM_CATEGORY_HATE_SPEECH";
|
|
66
|
+
HarmCategory["HARM_CATEGORY_SEXUALLY_EXPLICIT"] = "HARM_CATEGORY_SEXUALLY_EXPLICIT";
|
|
67
|
+
HarmCategory["HARM_CATEGORY_HARASSMENT"] = "HARM_CATEGORY_HARASSMENT";
|
|
68
|
+
HarmCategory["HARM_CATEGORY_DANGEROUS_CONTENT"] = "HARM_CATEGORY_DANGEROUS_CONTENT";
|
|
69
|
+
})(exports.HarmCategory || (exports.HarmCategory = {}));
|
|
70
|
+
/**
|
|
71
|
+
* Threshold above which a prompt or candidate will be blocked.
|
|
72
|
+
* @public
|
|
73
|
+
*/
|
|
74
|
+
exports.HarmBlockThreshold = void 0;
|
|
75
|
+
(function (HarmBlockThreshold) {
|
|
76
|
+
/**
|
|
77
|
+
* Content with `NEGLIGIBLE` will be allowed.
|
|
78
|
+
*/
|
|
79
|
+
HarmBlockThreshold["BLOCK_LOW_AND_ABOVE"] = "BLOCK_LOW_AND_ABOVE";
|
|
80
|
+
/**
|
|
81
|
+
* Content with `NEGLIGIBLE` and `LOW` will be allowed.
|
|
82
|
+
*/
|
|
83
|
+
HarmBlockThreshold["BLOCK_MEDIUM_AND_ABOVE"] = "BLOCK_MEDIUM_AND_ABOVE";
|
|
84
|
+
/**
|
|
85
|
+
* Content with `NEGLIGIBLE`, `LOW`, and `MEDIUM` will be allowed.
|
|
86
|
+
*/
|
|
87
|
+
HarmBlockThreshold["BLOCK_ONLY_HIGH"] = "BLOCK_ONLY_HIGH";
|
|
88
|
+
/**
|
|
89
|
+
* All content will be allowed.
|
|
90
|
+
*/
|
|
91
|
+
HarmBlockThreshold["BLOCK_NONE"] = "BLOCK_NONE";
|
|
92
|
+
})(exports.HarmBlockThreshold || (exports.HarmBlockThreshold = {}));
|
|
93
|
+
/**
|
|
94
|
+
* This property is not supported in the Gemini Developer API ({@link GoogleAIBackend}).
|
|
95
|
+
*
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
98
|
+
exports.HarmBlockMethod = void 0;
|
|
99
|
+
(function (HarmBlockMethod) {
|
|
100
|
+
/**
|
|
101
|
+
* The harm block method uses both probability and severity scores.
|
|
102
|
+
*/
|
|
103
|
+
HarmBlockMethod["SEVERITY"] = "SEVERITY";
|
|
104
|
+
/**
|
|
105
|
+
* The harm block method uses the probability score.
|
|
106
|
+
*/
|
|
107
|
+
HarmBlockMethod["PROBABILITY"] = "PROBABILITY";
|
|
108
|
+
})(exports.HarmBlockMethod || (exports.HarmBlockMethod = {}));
|
|
109
|
+
/**
|
|
110
|
+
* Probability that a prompt or candidate matches a harm category.
|
|
111
|
+
* @public
|
|
112
|
+
*/
|
|
113
|
+
exports.HarmProbability = void 0;
|
|
114
|
+
(function (HarmProbability) {
|
|
115
|
+
/**
|
|
116
|
+
* Content has a negligible chance of being unsafe.
|
|
117
|
+
*/
|
|
118
|
+
HarmProbability["NEGLIGIBLE"] = "NEGLIGIBLE";
|
|
119
|
+
/**
|
|
120
|
+
* Content has a low chance of being unsafe.
|
|
121
|
+
*/
|
|
122
|
+
HarmProbability["LOW"] = "LOW";
|
|
123
|
+
/**
|
|
124
|
+
* Content has a medium chance of being unsafe.
|
|
125
|
+
*/
|
|
126
|
+
HarmProbability["MEDIUM"] = "MEDIUM";
|
|
127
|
+
/**
|
|
128
|
+
* Content has a high chance of being unsafe.
|
|
129
|
+
*/
|
|
130
|
+
HarmProbability["HIGH"] = "HIGH";
|
|
131
|
+
})(exports.HarmProbability || (exports.HarmProbability = {}));
|
|
132
|
+
/**
|
|
133
|
+
* Harm severity levels.
|
|
134
|
+
* @public
|
|
135
|
+
*/
|
|
136
|
+
exports.HarmSeverity = void 0;
|
|
137
|
+
(function (HarmSeverity) {
|
|
138
|
+
/**
|
|
139
|
+
* Negligible level of harm severity.
|
|
140
|
+
*/
|
|
141
|
+
HarmSeverity["HARM_SEVERITY_NEGLIGIBLE"] = "HARM_SEVERITY_NEGLIGIBLE";
|
|
142
|
+
/**
|
|
143
|
+
* Low level of harm severity.
|
|
144
|
+
*/
|
|
145
|
+
HarmSeverity["HARM_SEVERITY_LOW"] = "HARM_SEVERITY_LOW";
|
|
146
|
+
/**
|
|
147
|
+
* Medium level of harm severity.
|
|
148
|
+
*/
|
|
149
|
+
HarmSeverity["HARM_SEVERITY_MEDIUM"] = "HARM_SEVERITY_MEDIUM";
|
|
150
|
+
/**
|
|
151
|
+
* High level of harm severity.
|
|
152
|
+
*/
|
|
153
|
+
HarmSeverity["HARM_SEVERITY_HIGH"] = "HARM_SEVERITY_HIGH";
|
|
154
|
+
/**
|
|
155
|
+
* Harm severity is not supported.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* The GoogleAI backend does not support `HarmSeverity`, so this value is used as a fallback.
|
|
159
|
+
*/
|
|
160
|
+
HarmSeverity["HARM_SEVERITY_UNSUPPORTED"] = "HARM_SEVERITY_UNSUPPORTED";
|
|
161
|
+
})(exports.HarmSeverity || (exports.HarmSeverity = {}));
|
|
162
|
+
/**
|
|
163
|
+
* Reason that a prompt was blocked.
|
|
164
|
+
* @public
|
|
165
|
+
*/
|
|
166
|
+
exports.BlockReason = void 0;
|
|
167
|
+
(function (BlockReason) {
|
|
168
|
+
/**
|
|
169
|
+
* Content was blocked by safety settings.
|
|
170
|
+
*/
|
|
171
|
+
BlockReason["SAFETY"] = "SAFETY";
|
|
172
|
+
/**
|
|
173
|
+
* Content was blocked, but the reason is uncategorized.
|
|
174
|
+
*/
|
|
175
|
+
BlockReason["OTHER"] = "OTHER";
|
|
176
|
+
/**
|
|
177
|
+
* Content was blocked because it contained terms from the terminology blocklist.
|
|
178
|
+
*/
|
|
179
|
+
BlockReason["BLOCKLIST"] = "BLOCKLIST";
|
|
180
|
+
/**
|
|
181
|
+
* Content was blocked due to prohibited content.
|
|
182
|
+
*/
|
|
183
|
+
BlockReason["PROHIBITED_CONTENT"] = "PROHIBITED_CONTENT";
|
|
184
|
+
})(exports.BlockReason || (exports.BlockReason = {}));
|
|
185
|
+
/**
|
|
186
|
+
* Reason that a candidate finished.
|
|
187
|
+
* @public
|
|
188
|
+
*/
|
|
189
|
+
exports.FinishReason = void 0;
|
|
190
|
+
(function (FinishReason) {
|
|
191
|
+
/**
|
|
192
|
+
* Natural stop point of the model or provided stop sequence.
|
|
193
|
+
*/
|
|
194
|
+
FinishReason["STOP"] = "STOP";
|
|
195
|
+
/**
|
|
196
|
+
* The maximum number of tokens as specified in the request was reached.
|
|
197
|
+
*/
|
|
198
|
+
FinishReason["MAX_TOKENS"] = "MAX_TOKENS";
|
|
199
|
+
/**
|
|
200
|
+
* The candidate content was flagged for safety reasons.
|
|
201
|
+
*/
|
|
202
|
+
FinishReason["SAFETY"] = "SAFETY";
|
|
203
|
+
/**
|
|
204
|
+
* The candidate content was flagged for recitation reasons.
|
|
205
|
+
*/
|
|
206
|
+
FinishReason["RECITATION"] = "RECITATION";
|
|
207
|
+
/**
|
|
208
|
+
* Unknown reason.
|
|
209
|
+
*/
|
|
210
|
+
FinishReason["OTHER"] = "OTHER";
|
|
211
|
+
/**
|
|
212
|
+
* The candidate content contained forbidden terms.
|
|
213
|
+
*/
|
|
214
|
+
FinishReason["BLOCKLIST"] = "BLOCKLIST";
|
|
215
|
+
/**
|
|
216
|
+
* The candidate content potentially contained prohibited content.
|
|
217
|
+
*/
|
|
218
|
+
FinishReason["PROHIBITED_CONTENT"] = "PROHIBITED_CONTENT";
|
|
219
|
+
/**
|
|
220
|
+
* The candidate content potentially contained Sensitive Personally Identifiable Information (SPII).
|
|
221
|
+
*/
|
|
222
|
+
FinishReason["SPII"] = "SPII";
|
|
223
|
+
/**
|
|
224
|
+
* The function call generated by the model was invalid.
|
|
225
|
+
*/
|
|
226
|
+
FinishReason["MALFORMED_FUNCTION_CALL"] = "MALFORMED_FUNCTION_CALL";
|
|
227
|
+
})(exports.FinishReason || (exports.FinishReason = {}));
|
|
228
|
+
/**
|
|
229
|
+
* @public
|
|
230
|
+
*/
|
|
231
|
+
exports.FunctionCallingMode = void 0;
|
|
232
|
+
(function (FunctionCallingMode) {
|
|
233
|
+
/**
|
|
234
|
+
* Default model behavior; model decides to predict either a function call
|
|
235
|
+
* or a natural language response.
|
|
236
|
+
*/
|
|
237
|
+
FunctionCallingMode["AUTO"] = "AUTO";
|
|
238
|
+
/**
|
|
239
|
+
* Model is constrained to always predicting a function call only.
|
|
240
|
+
* If `allowed_function_names` is set, the predicted function call will be
|
|
241
|
+
* limited to any one of `allowed_function_names`, else the predicted
|
|
242
|
+
* function call will be any one of the provided `function_declarations`.
|
|
243
|
+
*/
|
|
244
|
+
FunctionCallingMode["ANY"] = "ANY";
|
|
245
|
+
/**
|
|
246
|
+
* Model will not predict any function call. Model behavior is same as when
|
|
247
|
+
* not passing any function declarations.
|
|
248
|
+
*/
|
|
249
|
+
FunctionCallingMode["NONE"] = "NONE";
|
|
250
|
+
})(exports.FunctionCallingMode || (exports.FunctionCallingMode = {}));
|
|
251
|
+
/**
|
|
252
|
+
* Content part modality.
|
|
253
|
+
* @public
|
|
254
|
+
*/
|
|
255
|
+
exports.Modality = void 0;
|
|
256
|
+
(function (Modality) {
|
|
257
|
+
/**
|
|
258
|
+
* Unspecified modality.
|
|
259
|
+
*/
|
|
260
|
+
Modality["MODALITY_UNSPECIFIED"] = "MODALITY_UNSPECIFIED";
|
|
261
|
+
/**
|
|
262
|
+
* Plain text.
|
|
263
|
+
*/
|
|
264
|
+
Modality["TEXT"] = "TEXT";
|
|
265
|
+
/**
|
|
266
|
+
* Image.
|
|
267
|
+
*/
|
|
268
|
+
Modality["IMAGE"] = "IMAGE";
|
|
269
|
+
/**
|
|
270
|
+
* Video.
|
|
271
|
+
*/
|
|
272
|
+
Modality["VIDEO"] = "VIDEO";
|
|
273
|
+
/**
|
|
274
|
+
* Audio.
|
|
275
|
+
*/
|
|
276
|
+
Modality["AUDIO"] = "AUDIO";
|
|
277
|
+
/**
|
|
278
|
+
* Document (for example, PDF).
|
|
279
|
+
*/
|
|
280
|
+
Modality["DOCUMENT"] = "DOCUMENT";
|
|
281
|
+
})(exports.Modality || (exports.Modality = {}));
|
|
282
|
+
/**
|
|
283
|
+
* Generation modalities to be returned in generation responses.
|
|
284
|
+
*
|
|
285
|
+
* @beta
|
|
286
|
+
*/
|
|
287
|
+
const ResponseModality = {
|
|
288
|
+
/**
|
|
289
|
+
* Text.
|
|
290
|
+
* @beta
|
|
291
|
+
*/
|
|
292
|
+
TEXT: 'TEXT',
|
|
293
|
+
/**
|
|
294
|
+
* Image.
|
|
295
|
+
* @beta
|
|
296
|
+
*/
|
|
297
|
+
IMAGE: 'IMAGE'
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @license
|
|
302
|
+
* Copyright 2024 Google LLC
|
|
303
|
+
*
|
|
304
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
305
|
+
* you may not use this file except in compliance with the License.
|
|
306
|
+
* You may obtain a copy of the License at
|
|
307
|
+
*
|
|
308
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
309
|
+
*
|
|
310
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
311
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
312
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
313
|
+
* See the License for the specific language governing permissions and
|
|
314
|
+
* limitations under the License.
|
|
315
|
+
*/
|
|
316
|
+
/**
|
|
317
|
+
* Contains the list of OpenAPI data types
|
|
318
|
+
* as defined by the
|
|
319
|
+
* {@link https://swagger.io/docs/specification/data-models/data-types/ | OpenAPI specification}
|
|
320
|
+
* @public
|
|
321
|
+
*/
|
|
322
|
+
exports.SchemaType = void 0;
|
|
323
|
+
(function (SchemaType) {
|
|
324
|
+
/** String type. */
|
|
325
|
+
SchemaType["STRING"] = "string";
|
|
326
|
+
/** Number type. */
|
|
327
|
+
SchemaType["NUMBER"] = "number";
|
|
328
|
+
/** Integer type. */
|
|
329
|
+
SchemaType["INTEGER"] = "integer";
|
|
330
|
+
/** Boolean type. */
|
|
331
|
+
SchemaType["BOOLEAN"] = "boolean";
|
|
332
|
+
/** Array type. */
|
|
333
|
+
SchemaType["ARRAY"] = "array";
|
|
334
|
+
/** Object type. */
|
|
335
|
+
SchemaType["OBJECT"] = "object";
|
|
336
|
+
})(exports.SchemaType || (exports.SchemaType = {}));
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* @license
|
|
340
|
+
* Copyright 2025 Google LLC
|
|
341
|
+
*
|
|
342
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
343
|
+
* you may not use this file except in compliance with the License.
|
|
344
|
+
* You may obtain a copy of the License at
|
|
345
|
+
*
|
|
346
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
347
|
+
*
|
|
348
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
349
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
350
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
351
|
+
* See the License for the specific language governing permissions and
|
|
352
|
+
* limitations under the License.
|
|
353
|
+
*/
|
|
354
|
+
/**
|
|
355
|
+
* A filter level controlling how aggressively to filter sensitive content.
|
|
356
|
+
*
|
|
357
|
+
* Text prompts provided as inputs and images (generated or uploaded) through Imagen on Vertex AI
|
|
358
|
+
* are assessed against a list of safety filters, which include 'harmful categories' (for example,
|
|
359
|
+
* `violence`, `sexual`, `derogatory`, and `toxic`). This filter level controls how aggressively to
|
|
360
|
+
* filter out potentially harmful content from responses. See the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
|
|
361
|
+
* and the {@link https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#safety-filters | Responsible AI and usage guidelines}
|
|
362
|
+
* for more details.
|
|
363
|
+
*
|
|
364
|
+
* @beta
|
|
365
|
+
*/
|
|
366
|
+
exports.ImagenSafetyFilterLevel = void 0;
|
|
367
|
+
(function (ImagenSafetyFilterLevel) {
|
|
368
|
+
/**
|
|
369
|
+
* The most aggressive filtering level; most strict blocking.
|
|
370
|
+
*/
|
|
371
|
+
ImagenSafetyFilterLevel["BLOCK_LOW_AND_ABOVE"] = "block_low_and_above";
|
|
372
|
+
/**
|
|
373
|
+
* Blocks some sensitive prompts and responses.
|
|
374
|
+
*/
|
|
375
|
+
ImagenSafetyFilterLevel["BLOCK_MEDIUM_AND_ABOVE"] = "block_medium_and_above";
|
|
376
|
+
/**
|
|
377
|
+
* Blocks few sensitive prompts and responses.
|
|
378
|
+
*/
|
|
379
|
+
ImagenSafetyFilterLevel["BLOCK_ONLY_HIGH"] = "block_only_high";
|
|
380
|
+
/**
|
|
381
|
+
* The least aggressive filtering level; blocks very few sensitive prompts and responses.
|
|
382
|
+
*
|
|
383
|
+
* Access to this feature is restricted and may require your case to be reviewed and approved by
|
|
384
|
+
* Cloud support.
|
|
385
|
+
*/
|
|
386
|
+
ImagenSafetyFilterLevel["BLOCK_NONE"] = "block_none";
|
|
387
|
+
})(exports.ImagenSafetyFilterLevel || (exports.ImagenSafetyFilterLevel = {}));
|
|
388
|
+
/**
|
|
389
|
+
* A filter level controlling whether generation of images containing people or faces is allowed.
|
|
390
|
+
*
|
|
391
|
+
* See the <a href="http://firebase.google.com/docs/vertex-ai/generate-images">personGeneration</a>
|
|
392
|
+
* documentation for more details.
|
|
393
|
+
*
|
|
394
|
+
* @beta
|
|
395
|
+
*/
|
|
396
|
+
exports.ImagenPersonFilterLevel = void 0;
|
|
397
|
+
(function (ImagenPersonFilterLevel) {
|
|
398
|
+
/**
|
|
399
|
+
* Disallow generation of images containing people or faces; images of people are filtered out.
|
|
400
|
+
*/
|
|
401
|
+
ImagenPersonFilterLevel["BLOCK_ALL"] = "dont_allow";
|
|
402
|
+
/**
|
|
403
|
+
* Allow generation of images containing adults only; images of children are filtered out.
|
|
404
|
+
*
|
|
405
|
+
* Generation of images containing people or faces may require your use case to be
|
|
406
|
+
* reviewed and approved by Cloud support; see the {@link https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#person-face-gen | Responsible AI and usage guidelines}
|
|
407
|
+
* for more details.
|
|
408
|
+
*/
|
|
409
|
+
ImagenPersonFilterLevel["ALLOW_ADULT"] = "allow_adult";
|
|
410
|
+
/**
|
|
411
|
+
* Allow generation of images containing adults only; images of children are filtered out.
|
|
412
|
+
*
|
|
413
|
+
* Generation of images containing people or faces may require your use case to be
|
|
414
|
+
* reviewed and approved by Cloud support; see the {@link https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#person-face-gen | Responsible AI and usage guidelines}
|
|
415
|
+
* for more details.
|
|
416
|
+
*/
|
|
417
|
+
ImagenPersonFilterLevel["ALLOW_ALL"] = "allow_all";
|
|
418
|
+
})(exports.ImagenPersonFilterLevel || (exports.ImagenPersonFilterLevel = {}));
|
|
419
|
+
/**
|
|
420
|
+
* Aspect ratios for Imagen images.
|
|
421
|
+
*
|
|
422
|
+
* To specify an aspect ratio for generated images, set the `aspectRatio` property in your
|
|
423
|
+
* {@link ImagenGenerationConfig}.
|
|
424
|
+
*
|
|
425
|
+
* See the the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
|
|
426
|
+
* for more details and examples of the supported aspect ratios.
|
|
427
|
+
*
|
|
428
|
+
* @beta
|
|
429
|
+
*/
|
|
430
|
+
exports.ImagenAspectRatio = void 0;
|
|
431
|
+
(function (ImagenAspectRatio) {
|
|
432
|
+
/**
|
|
433
|
+
* Square (1:1) aspect ratio.
|
|
434
|
+
*/
|
|
435
|
+
ImagenAspectRatio["SQUARE"] = "1:1";
|
|
436
|
+
/**
|
|
437
|
+
* Landscape (3:4) aspect ratio.
|
|
438
|
+
*/
|
|
439
|
+
ImagenAspectRatio["LANDSCAPE_3x4"] = "3:4";
|
|
440
|
+
/**
|
|
441
|
+
* Portrait (4:3) aspect ratio.
|
|
442
|
+
*/
|
|
443
|
+
ImagenAspectRatio["PORTRAIT_4x3"] = "4:3";
|
|
444
|
+
/**
|
|
445
|
+
* Landscape (16:9) aspect ratio.
|
|
446
|
+
*/
|
|
447
|
+
ImagenAspectRatio["LANDSCAPE_16x9"] = "16:9";
|
|
448
|
+
/**
|
|
449
|
+
* Portrait (9:16) aspect ratio.
|
|
450
|
+
*/
|
|
451
|
+
ImagenAspectRatio["PORTRAIT_9x16"] = "9:16";
|
|
452
|
+
})(exports.ImagenAspectRatio || (exports.ImagenAspectRatio = {}));
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* @license
|
|
456
|
+
* Copyright 2024 Google LLC
|
|
457
|
+
*
|
|
458
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
459
|
+
* you may not use this file except in compliance with the License.
|
|
460
|
+
* You may obtain a copy of the License at
|
|
461
|
+
*
|
|
462
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
463
|
+
*
|
|
464
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
465
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
466
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
467
|
+
* See the License for the specific language governing permissions and
|
|
468
|
+
* limitations under the License.
|
|
469
|
+
*/
|
|
470
|
+
/**
|
|
471
|
+
* An enum-like object containing constants that represent the supported backends
|
|
472
|
+
* for the Firebase AI SDK.
|
|
473
|
+
* This determines which backend service (Vertex AI Gemini API or Gemini Developer API)
|
|
474
|
+
* the SDK will communicate with.
|
|
475
|
+
*
|
|
476
|
+
* These values are assigned to the `backendType` property within the specific backend
|
|
477
|
+
* configuration objects ({@link GoogleAIBackend} or {@link VertexAIBackend}) to identify
|
|
478
|
+
* which service to target.
|
|
479
|
+
*
|
|
480
|
+
* @public
|
|
481
|
+
*/
|
|
482
|
+
const BackendType = {
|
|
483
|
+
/**
|
|
484
|
+
* Identifies the backend service for the Vertex AI Gemini API provided through Google Cloud.
|
|
485
|
+
* Use this constant when creating a {@link VertexAIBackend} configuration.
|
|
486
|
+
*/
|
|
487
|
+
VERTEX_AI: 'VERTEX_AI',
|
|
488
|
+
/**
|
|
489
|
+
* Identifies the backend service for the Gemini Developer API ({@link https://ai.google/ | Google AI}).
|
|
490
|
+
* Use this constant when creating a {@link GoogleAIBackend} configuration.
|
|
491
|
+
*/
|
|
492
|
+
GOOGLE_AI: 'GOOGLE_AI'
|
|
493
|
+
}; // Using 'as const' makes the string values literal types
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* @license
|
|
497
|
+
* Copyright 2025 Google LLC
|
|
498
|
+
*
|
|
499
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
500
|
+
* you may not use this file except in compliance with the License.
|
|
501
|
+
* You may obtain a copy of the License at
|
|
502
|
+
*
|
|
503
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
504
|
+
*
|
|
505
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
506
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
507
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
508
|
+
* See the License for the specific language governing permissions and
|
|
509
|
+
* limitations under the License.
|
|
510
|
+
*/
|
|
511
|
+
/**
|
|
512
|
+
* Abstract base class representing the configuration for an AI service backend.
|
|
513
|
+
* This class should not be instantiated directly. Use its subclasses; {@link GoogleAIBackend} for
|
|
514
|
+
* the Gemini Developer API (via {@link https://ai.google/ | Google AI}), and
|
|
515
|
+
* {@link VertexAIBackend} for the Vertex AI Gemini API.
|
|
516
|
+
*
|
|
517
|
+
* @public
|
|
518
|
+
*/
|
|
519
|
+
class Backend {
|
|
520
|
+
/**
|
|
521
|
+
* Protected constructor for use by subclasses.
|
|
522
|
+
* @param type - The backend type.
|
|
523
|
+
*/
|
|
524
|
+
constructor(type) {
|
|
525
|
+
this.backendType = type;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Configuration class for the Gemini Developer API.
|
|
530
|
+
*
|
|
531
|
+
* Use this with {@link AIOptions} when initializing the AI service via
|
|
532
|
+
* {@link getAI | getAI()} to specify the Gemini Developer API as the backend.
|
|
533
|
+
*
|
|
534
|
+
* @public
|
|
535
|
+
*/
|
|
536
|
+
class GoogleAIBackend extends Backend {
|
|
537
|
+
/**
|
|
538
|
+
* Creates a configuration object for the Gemini Developer API backend.
|
|
539
|
+
*/
|
|
540
|
+
constructor() {
|
|
541
|
+
super(BackendType.GOOGLE_AI);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Configuration class for the Vertex AI Gemini API.
|
|
546
|
+
*
|
|
547
|
+
* Use this with {@link AIOptions} when initializing the AI service via
|
|
548
|
+
* {@link getAI | getAI()} to specify the Vertex AI Gemini API as the backend.
|
|
549
|
+
*
|
|
550
|
+
* @public
|
|
551
|
+
*/
|
|
552
|
+
class VertexAIBackend extends Backend {
|
|
553
|
+
/**
|
|
554
|
+
* Creates a configuration object for the Vertex AI backend.
|
|
555
|
+
*
|
|
556
|
+
* @param location - The region identifier, defaulting to `us-central1`;
|
|
557
|
+
* see {@link https://firebase.google.com/docs/vertex-ai/locations#available-locations | Vertex AI locations}
|
|
558
|
+
* for a list of supported locations.
|
|
559
|
+
*/
|
|
560
|
+
constructor(location = DEFAULT_LOCATION) {
|
|
561
|
+
super(BackendType.VERTEX_AI);
|
|
562
|
+
if (!location) {
|
|
563
|
+
this.location = DEFAULT_LOCATION;
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
this.location = location;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* @license
|
|
573
|
+
* Copyright 2024 Google LLC
|
|
574
|
+
*
|
|
575
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
576
|
+
* you may not use this file except in compliance with the License.
|
|
577
|
+
* You may obtain a copy of the License at
|
|
578
|
+
*
|
|
579
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
580
|
+
*
|
|
581
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
582
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
583
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
584
|
+
* See the License for the specific language governing permissions and
|
|
585
|
+
* limitations under the License.
|
|
586
|
+
*/
|
|
587
|
+
class AIService {
|
|
588
|
+
constructor(app, backend, authProvider, appCheckProvider) {
|
|
589
|
+
this.app = app;
|
|
590
|
+
this.backend = backend;
|
|
591
|
+
const appCheck = appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.getImmediate({ optional: true });
|
|
592
|
+
const auth = authProvider === null || authProvider === void 0 ? void 0 : authProvider.getImmediate({ optional: true });
|
|
593
|
+
this.auth = auth || null;
|
|
594
|
+
this.appCheck = appCheck || null;
|
|
595
|
+
if (backend instanceof VertexAIBackend) {
|
|
596
|
+
this.location = backend.location;
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
this.location = '';
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
_delete() {
|
|
603
|
+
return Promise.resolve();
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* @license
|
|
609
|
+
* Copyright 2024 Google LLC
|
|
610
|
+
*
|
|
611
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
612
|
+
* you may not use this file except in compliance with the License.
|
|
613
|
+
* You may obtain a copy of the License at
|
|
614
|
+
*
|
|
615
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
616
|
+
*
|
|
617
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
618
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
619
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
620
|
+
* See the License for the specific language governing permissions and
|
|
621
|
+
* limitations under the License.
|
|
622
|
+
*/
|
|
623
|
+
/**
|
|
624
|
+
* Error class for the Firebase AI SDK.
|
|
625
|
+
*
|
|
626
|
+
* @public
|
|
627
|
+
*/
|
|
628
|
+
class AIError extends util.FirebaseError {
|
|
629
|
+
/**
|
|
630
|
+
* Constructs a new instance of the `AIError` class.
|
|
631
|
+
*
|
|
632
|
+
* @param code - The error code from {@link AIErrorCode}.
|
|
633
|
+
* @param message - A human-readable message describing the error.
|
|
634
|
+
* @param customErrorData - Optional error data.
|
|
635
|
+
*/
|
|
636
|
+
constructor(code, message, customErrorData) {
|
|
637
|
+
// Match error format used by FirebaseError from ErrorFactory
|
|
638
|
+
const service = AI_TYPE;
|
|
639
|
+
const fullCode = `${service}/${code}`;
|
|
640
|
+
const fullMessage = `${service}: ${message} (${fullCode})`;
|
|
641
|
+
super(code, fullMessage);
|
|
642
|
+
this.code = code;
|
|
643
|
+
this.customErrorData = customErrorData;
|
|
644
|
+
// FirebaseError initializes a stack trace, but it assumes the error is created from the error
|
|
645
|
+
// factory. Since we break this assumption, we set the stack trace to be originating from this
|
|
646
|
+
// constructor.
|
|
647
|
+
// This is only supported in V8.
|
|
648
|
+
if (Error.captureStackTrace) {
|
|
649
|
+
// Allows us to initialize the stack trace without including the constructor itself at the
|
|
650
|
+
// top level of the stack trace.
|
|
651
|
+
Error.captureStackTrace(this, AIError);
|
|
652
|
+
}
|
|
653
|
+
// Allows instanceof AIError in ES5/ES6
|
|
654
|
+
// https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
|
|
655
|
+
// TODO(dlarocque): Replace this with `new.target`: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
|
|
656
|
+
// which we can now use since we no longer target ES5.
|
|
657
|
+
Object.setPrototypeOf(this, AIError.prototype);
|
|
658
|
+
// Since Error is an interface, we don't inherit toString and so we define it ourselves.
|
|
659
|
+
this.toString = () => fullMessage;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* @license
|
|
665
|
+
* Copyright 2025 Google LLC
|
|
666
|
+
*
|
|
667
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
668
|
+
* you may not use this file except in compliance with the License.
|
|
669
|
+
* You may obtain a copy of the License at
|
|
670
|
+
*
|
|
671
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
672
|
+
*
|
|
673
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
674
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
675
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
676
|
+
* See the License for the specific language governing permissions and
|
|
677
|
+
* limitations under the License.
|
|
678
|
+
*/
|
|
679
|
+
/**
|
|
680
|
+
* Encodes a {@link Backend} into a string that will be used to uniquely identify {@link AI}
|
|
681
|
+
* instances by backend type.
|
|
682
|
+
*
|
|
683
|
+
* @internal
|
|
684
|
+
*/
|
|
685
|
+
function encodeInstanceIdentifier(backend) {
|
|
686
|
+
if (backend instanceof GoogleAIBackend) {
|
|
687
|
+
return `${AI_TYPE}/googleai`;
|
|
688
|
+
}
|
|
689
|
+
else if (backend instanceof VertexAIBackend) {
|
|
690
|
+
return `${AI_TYPE}/vertexai/${backend.location}`;
|
|
691
|
+
}
|
|
692
|
+
else {
|
|
693
|
+
throw new AIError("error" /* AIErrorCode.ERROR */, `Invalid backend: ${JSON.stringify(backend.backendType)}`);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Decodes an instance identifier string into a {@link Backend}.
|
|
698
|
+
*
|
|
699
|
+
* @internal
|
|
700
|
+
*/
|
|
701
|
+
function decodeInstanceIdentifier(instanceIdentifier) {
|
|
702
|
+
const identifierParts = instanceIdentifier.split('/');
|
|
703
|
+
if (identifierParts[0] !== AI_TYPE) {
|
|
704
|
+
throw new AIError("error" /* AIErrorCode.ERROR */, `Invalid instance identifier, unknown prefix '${identifierParts[0]}'`);
|
|
705
|
+
}
|
|
706
|
+
const backendType = identifierParts[1];
|
|
707
|
+
switch (backendType) {
|
|
708
|
+
case 'vertexai':
|
|
709
|
+
const location = identifierParts[2];
|
|
710
|
+
if (!location) {
|
|
711
|
+
throw new AIError("error" /* AIErrorCode.ERROR */, `Invalid instance identifier, unknown location '${instanceIdentifier}'`);
|
|
712
|
+
}
|
|
713
|
+
return new VertexAIBackend(location);
|
|
714
|
+
case 'googleai':
|
|
715
|
+
return new GoogleAIBackend();
|
|
716
|
+
default:
|
|
717
|
+
throw new AIError("error" /* AIErrorCode.ERROR */, `Invalid instance identifier string: '${instanceIdentifier}'`);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* @license
|
|
723
|
+
* Copyright 2025 Google LLC
|
|
724
|
+
*
|
|
725
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
726
|
+
* you may not use this file except in compliance with the License.
|
|
727
|
+
* You may obtain a copy of the License at
|
|
728
|
+
*
|
|
729
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
730
|
+
*
|
|
731
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
732
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
733
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
734
|
+
* See the License for the specific language governing permissions and
|
|
735
|
+
* limitations under the License.
|
|
736
|
+
*/
|
|
737
|
+
/**
|
|
738
|
+
* Base class for Firebase AI model APIs.
|
|
739
|
+
*
|
|
740
|
+
* Instances of this class are associated with a specific Firebase AI {@link Backend}
|
|
741
|
+
* and provide methods for interacting with the configured generative model.
|
|
742
|
+
*
|
|
743
|
+
* @public
|
|
744
|
+
*/
|
|
745
|
+
class AIModel {
|
|
746
|
+
/**
|
|
747
|
+
* Constructs a new instance of the {@link AIModel} class.
|
|
748
|
+
*
|
|
749
|
+
* This constructor should only be called from subclasses that provide
|
|
750
|
+
* a model API.
|
|
751
|
+
*
|
|
752
|
+
* @param ai - an {@link AI} instance.
|
|
753
|
+
* @param modelName - The name of the model being used. It can be in one of the following formats:
|
|
754
|
+
* - `my-model` (short name, will resolve to `publishers/google/models/my-model`)
|
|
755
|
+
* - `models/my-model` (will resolve to `publishers/google/models/my-model`)
|
|
756
|
+
* - `publishers/my-publisher/models/my-model` (fully qualified model name)
|
|
757
|
+
*
|
|
758
|
+
* @throws If the `apiKey` or `projectId` fields are missing in your
|
|
759
|
+
* Firebase config.
|
|
760
|
+
*
|
|
761
|
+
* @internal
|
|
762
|
+
*/
|
|
763
|
+
constructor(ai, modelName) {
|
|
764
|
+
var _a, _b, _c, _d, _e, _f;
|
|
765
|
+
if (!((_b = (_a = ai.app) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.apiKey)) {
|
|
766
|
+
throw new AIError("no-api-key" /* AIErrorCode.NO_API_KEY */, `The "apiKey" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid API key.`);
|
|
767
|
+
}
|
|
768
|
+
else if (!((_d = (_c = ai.app) === null || _c === void 0 ? void 0 : _c.options) === null || _d === void 0 ? void 0 : _d.projectId)) {
|
|
769
|
+
throw new AIError("no-project-id" /* AIErrorCode.NO_PROJECT_ID */, `The "projectId" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid project ID.`);
|
|
770
|
+
}
|
|
771
|
+
else if (!((_f = (_e = ai.app) === null || _e === void 0 ? void 0 : _e.options) === null || _f === void 0 ? void 0 : _f.appId)) {
|
|
772
|
+
throw new AIError("no-app-id" /* AIErrorCode.NO_APP_ID */, `The "appId" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid app ID.`);
|
|
773
|
+
}
|
|
774
|
+
else {
|
|
775
|
+
this._apiSettings = {
|
|
776
|
+
apiKey: ai.app.options.apiKey,
|
|
777
|
+
project: ai.app.options.projectId,
|
|
778
|
+
appId: ai.app.options.appId,
|
|
779
|
+
automaticDataCollectionEnabled: ai.app.automaticDataCollectionEnabled,
|
|
780
|
+
location: ai.location,
|
|
781
|
+
backend: ai.backend
|
|
782
|
+
};
|
|
783
|
+
if (app._isFirebaseServerApp(ai.app) && ai.app.settings.appCheckToken) {
|
|
784
|
+
const token = ai.app.settings.appCheckToken;
|
|
785
|
+
this._apiSettings.getAppCheckToken = () => {
|
|
786
|
+
return Promise.resolve({ token });
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
else if (ai.appCheck) {
|
|
790
|
+
this._apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
|
|
791
|
+
}
|
|
792
|
+
if (ai.auth) {
|
|
793
|
+
this._apiSettings.getAuthToken = () => ai.auth.getToken();
|
|
794
|
+
}
|
|
795
|
+
this.model = AIModel.normalizeModelName(modelName, this._apiSettings.backend.backendType);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Normalizes the given model name to a fully qualified model resource name.
|
|
800
|
+
*
|
|
801
|
+
* @param modelName - The model name to normalize.
|
|
802
|
+
* @returns The fully qualified model resource name.
|
|
803
|
+
*
|
|
804
|
+
* @internal
|
|
805
|
+
*/
|
|
806
|
+
static normalizeModelName(modelName, backendType) {
|
|
807
|
+
if (backendType === BackendType.GOOGLE_AI) {
|
|
808
|
+
return AIModel.normalizeGoogleAIModelName(modelName);
|
|
809
|
+
}
|
|
810
|
+
else {
|
|
811
|
+
return AIModel.normalizeVertexAIModelName(modelName);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* @internal
|
|
816
|
+
*/
|
|
817
|
+
static normalizeGoogleAIModelName(modelName) {
|
|
818
|
+
return `models/${modelName}`;
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* @internal
|
|
822
|
+
*/
|
|
823
|
+
static normalizeVertexAIModelName(modelName) {
|
|
824
|
+
let model;
|
|
825
|
+
if (modelName.includes('/')) {
|
|
826
|
+
if (modelName.startsWith('models/')) {
|
|
827
|
+
// Add 'publishers/google' if the user is only passing in 'models/model-name'.
|
|
828
|
+
model = `publishers/google/${modelName}`;
|
|
829
|
+
}
|
|
830
|
+
else {
|
|
831
|
+
// Any other custom format (e.g. tuned models) must be passed in correctly.
|
|
832
|
+
model = modelName;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
else {
|
|
836
|
+
// If path is not included, assume it's a non-tuned model.
|
|
837
|
+
model = `publishers/google/models/${modelName}`;
|
|
838
|
+
}
|
|
839
|
+
return model;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* @license
|
|
845
|
+
* Copyright 2024 Google LLC
|
|
846
|
+
*
|
|
847
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
848
|
+
* you may not use this file except in compliance with the License.
|
|
849
|
+
* You may obtain a copy of the License at
|
|
850
|
+
*
|
|
851
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
852
|
+
*
|
|
853
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
854
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
855
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
856
|
+
* See the License for the specific language governing permissions and
|
|
857
|
+
* limitations under the License.
|
|
858
|
+
*/
|
|
859
|
+
const logger = new logger$1.Logger('@firebase/vertexai');
|
|
860
|
+
|
|
861
|
+
/**
|
|
862
|
+
* @license
|
|
863
|
+
* Copyright 2024 Google LLC
|
|
864
|
+
*
|
|
865
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
866
|
+
* you may not use this file except in compliance with the License.
|
|
867
|
+
* You may obtain a copy of the License at
|
|
868
|
+
*
|
|
869
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
870
|
+
*
|
|
871
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
872
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
873
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
874
|
+
* See the License for the specific language governing permissions and
|
|
875
|
+
* limitations under the License.
|
|
876
|
+
*/
|
|
877
|
+
var Task;
|
|
878
|
+
(function (Task) {
|
|
879
|
+
Task["GENERATE_CONTENT"] = "generateContent";
|
|
880
|
+
Task["STREAM_GENERATE_CONTENT"] = "streamGenerateContent";
|
|
881
|
+
Task["COUNT_TOKENS"] = "countTokens";
|
|
882
|
+
Task["PREDICT"] = "predict";
|
|
883
|
+
})(Task || (Task = {}));
|
|
884
|
+
class RequestUrl {
|
|
885
|
+
constructor(model, task, apiSettings, stream, requestOptions) {
|
|
886
|
+
this.model = model;
|
|
887
|
+
this.task = task;
|
|
888
|
+
this.apiSettings = apiSettings;
|
|
889
|
+
this.stream = stream;
|
|
890
|
+
this.requestOptions = requestOptions;
|
|
891
|
+
}
|
|
892
|
+
toString() {
|
|
893
|
+
const url = new URL(this.baseUrl); // Throws if the URL is invalid
|
|
894
|
+
url.pathname = `/${this.apiVersion}/${this.modelPath}:${this.task}`;
|
|
895
|
+
url.search = this.queryParams.toString();
|
|
896
|
+
return url.toString();
|
|
897
|
+
}
|
|
898
|
+
get baseUrl() {
|
|
899
|
+
var _a;
|
|
900
|
+
return ((_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.baseUrl) || DEFAULT_BASE_URL;
|
|
901
|
+
}
|
|
902
|
+
get apiVersion() {
|
|
903
|
+
return DEFAULT_API_VERSION; // TODO: allow user-set options if that feature becomes available
|
|
904
|
+
}
|
|
905
|
+
get modelPath() {
|
|
906
|
+
if (this.apiSettings.backend instanceof GoogleAIBackend) {
|
|
907
|
+
return `projects/${this.apiSettings.project}/${this.model}`;
|
|
908
|
+
}
|
|
909
|
+
else if (this.apiSettings.backend instanceof VertexAIBackend) {
|
|
910
|
+
return `projects/${this.apiSettings.project}/locations/${this.apiSettings.backend.location}/${this.model}`;
|
|
911
|
+
}
|
|
912
|
+
else {
|
|
913
|
+
throw new AIError("error" /* AIErrorCode.ERROR */, `Invalid backend: ${JSON.stringify(this.apiSettings.backend)}`);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
get queryParams() {
|
|
917
|
+
const params = new URLSearchParams();
|
|
918
|
+
if (this.stream) {
|
|
919
|
+
params.set('alt', 'sse');
|
|
920
|
+
}
|
|
921
|
+
return params;
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Log language and "fire/version" to x-goog-api-client
|
|
926
|
+
*/
|
|
927
|
+
function getClientHeaders() {
|
|
928
|
+
const loggingTags = [];
|
|
929
|
+
loggingTags.push(`${LANGUAGE_TAG}/${PACKAGE_VERSION}`);
|
|
930
|
+
loggingTags.push(`fire/${PACKAGE_VERSION}`);
|
|
931
|
+
return loggingTags.join(' ');
|
|
932
|
+
}
|
|
933
|
+
async function getHeaders(url) {
|
|
934
|
+
const headers = new Headers();
|
|
935
|
+
headers.append('Content-Type', 'application/json');
|
|
936
|
+
headers.append('x-goog-api-client', getClientHeaders());
|
|
937
|
+
headers.append('x-goog-api-key', url.apiSettings.apiKey);
|
|
938
|
+
if (url.apiSettings.automaticDataCollectionEnabled) {
|
|
939
|
+
headers.append('X-Firebase-Appid', url.apiSettings.appId);
|
|
940
|
+
}
|
|
941
|
+
if (url.apiSettings.getAppCheckToken) {
|
|
942
|
+
const appCheckToken = await url.apiSettings.getAppCheckToken();
|
|
943
|
+
if (appCheckToken) {
|
|
944
|
+
headers.append('X-Firebase-AppCheck', appCheckToken.token);
|
|
945
|
+
if (appCheckToken.error) {
|
|
946
|
+
logger.warn(`Unable to obtain a valid App Check token: ${appCheckToken.error.message}`);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
if (url.apiSettings.getAuthToken) {
|
|
951
|
+
const authToken = await url.apiSettings.getAuthToken();
|
|
952
|
+
if (authToken) {
|
|
953
|
+
headers.append('Authorization', `Firebase ${authToken.accessToken}`);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return headers;
|
|
957
|
+
}
|
|
958
|
+
async function constructRequest(model, task, apiSettings, stream, body, requestOptions) {
|
|
959
|
+
const url = new RequestUrl(model, task, apiSettings, stream, requestOptions);
|
|
960
|
+
return {
|
|
961
|
+
url: url.toString(),
|
|
962
|
+
fetchOptions: {
|
|
963
|
+
method: 'POST',
|
|
964
|
+
headers: await getHeaders(url),
|
|
965
|
+
body
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
async function makeRequest(model, task, apiSettings, stream, body, requestOptions) {
|
|
970
|
+
const url = new RequestUrl(model, task, apiSettings, stream, requestOptions);
|
|
971
|
+
let response;
|
|
972
|
+
let fetchTimeoutId;
|
|
973
|
+
try {
|
|
974
|
+
const request = await constructRequest(model, task, apiSettings, stream, body, requestOptions);
|
|
975
|
+
// Timeout is 180s by default
|
|
976
|
+
const timeoutMillis = (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeout) != null && requestOptions.timeout >= 0
|
|
977
|
+
? requestOptions.timeout
|
|
978
|
+
: DEFAULT_FETCH_TIMEOUT_MS;
|
|
979
|
+
const abortController = new AbortController();
|
|
980
|
+
fetchTimeoutId = setTimeout(() => abortController.abort(), timeoutMillis);
|
|
981
|
+
request.fetchOptions.signal = abortController.signal;
|
|
982
|
+
response = await fetch(request.url, request.fetchOptions);
|
|
983
|
+
if (!response.ok) {
|
|
984
|
+
let message = '';
|
|
985
|
+
let errorDetails;
|
|
986
|
+
try {
|
|
987
|
+
const json = await response.json();
|
|
988
|
+
message = json.error.message;
|
|
989
|
+
if (json.error.details) {
|
|
990
|
+
message += ` ${JSON.stringify(json.error.details)}`;
|
|
991
|
+
errorDetails = json.error.details;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
catch (e) {
|
|
995
|
+
// ignored
|
|
996
|
+
}
|
|
997
|
+
if (response.status === 403 &&
|
|
998
|
+
errorDetails.some((detail) => detail.reason === 'SERVICE_DISABLED') &&
|
|
999
|
+
errorDetails.some((detail) => {
|
|
1000
|
+
var _a, _b;
|
|
1001
|
+
return (_b = (_a = detail.links) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.description.includes('Google developers console API activation');
|
|
1002
|
+
})) {
|
|
1003
|
+
throw new AIError("api-not-enabled" /* AIErrorCode.API_NOT_ENABLED */, `The Firebase AI SDK requires the Firebase AI ` +
|
|
1004
|
+
`API ('firebasevertexai.googleapis.com') to be enabled in your ` +
|
|
1005
|
+
`Firebase project. Enable this API by visiting the Firebase Console ` +
|
|
1006
|
+
`at https://console.firebase.google.com/project/${url.apiSettings.project}/genai/ ` +
|
|
1007
|
+
`and clicking "Get started". If you enabled this API recently, ` +
|
|
1008
|
+
`wait a few minutes for the action to propagate to our systems and ` +
|
|
1009
|
+
`then retry.`, {
|
|
1010
|
+
status: response.status,
|
|
1011
|
+
statusText: response.statusText,
|
|
1012
|
+
errorDetails
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
throw new AIError("fetch-error" /* AIErrorCode.FETCH_ERROR */, `Error fetching from ${url}: [${response.status} ${response.statusText}] ${message}`, {
|
|
1016
|
+
status: response.status,
|
|
1017
|
+
statusText: response.statusText,
|
|
1018
|
+
errorDetails
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
catch (e) {
|
|
1023
|
+
let err = e;
|
|
1024
|
+
if (e.code !== "fetch-error" /* AIErrorCode.FETCH_ERROR */ &&
|
|
1025
|
+
e.code !== "api-not-enabled" /* AIErrorCode.API_NOT_ENABLED */ &&
|
|
1026
|
+
e instanceof Error) {
|
|
1027
|
+
err = new AIError("error" /* AIErrorCode.ERROR */, `Error fetching from ${url.toString()}: ${e.message}`);
|
|
1028
|
+
err.stack = e.stack;
|
|
1029
|
+
}
|
|
1030
|
+
throw err;
|
|
1031
|
+
}
|
|
1032
|
+
finally {
|
|
1033
|
+
if (fetchTimeoutId) {
|
|
1034
|
+
clearTimeout(fetchTimeoutId);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
return response;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
/**
|
|
1041
|
+
* @license
|
|
1042
|
+
* Copyright 2024 Google LLC
|
|
1043
|
+
*
|
|
1044
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1045
|
+
* you may not use this file except in compliance with the License.
|
|
1046
|
+
* You may obtain a copy of the License at
|
|
1047
|
+
*
|
|
1048
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1049
|
+
*
|
|
1050
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1051
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1052
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1053
|
+
* See the License for the specific language governing permissions and
|
|
1054
|
+
* limitations under the License.
|
|
1055
|
+
*/
|
|
1056
|
+
/**
|
|
1057
|
+
* Creates an EnhancedGenerateContentResponse object that has helper functions and
|
|
1058
|
+
* other modifications that improve usability.
|
|
1059
|
+
*/
|
|
1060
|
+
function createEnhancedContentResponse(response) {
|
|
1061
|
+
/**
|
|
1062
|
+
* The Vertex AI backend omits default values.
|
|
1063
|
+
* This causes the `index` property to be omitted from the first candidate in the
|
|
1064
|
+
* response, since it has index 0, and 0 is a default value.
|
|
1065
|
+
* See: https://github.com/firebase/firebase-js-sdk/issues/8566
|
|
1066
|
+
*/
|
|
1067
|
+
if (response.candidates && !response.candidates[0].hasOwnProperty('index')) {
|
|
1068
|
+
response.candidates[0].index = 0;
|
|
1069
|
+
}
|
|
1070
|
+
const responseWithHelpers = addHelpers(response);
|
|
1071
|
+
return responseWithHelpers;
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Adds convenience helper methods to a response object, including stream
|
|
1075
|
+
* chunks (as long as each chunk is a complete GenerateContentResponse JSON).
|
|
1076
|
+
*/
|
|
1077
|
+
function addHelpers(response) {
|
|
1078
|
+
response.text = () => {
|
|
1079
|
+
if (response.candidates && response.candidates.length > 0) {
|
|
1080
|
+
if (response.candidates.length > 1) {
|
|
1081
|
+
logger.warn(`This response had ${response.candidates.length} ` +
|
|
1082
|
+
`candidates. Returning text from the first candidate only. ` +
|
|
1083
|
+
`Access response.candidates directly to use the other candidates.`);
|
|
1084
|
+
}
|
|
1085
|
+
if (hadBadFinishReason(response.candidates[0])) {
|
|
1086
|
+
throw new AIError("response-error" /* AIErrorCode.RESPONSE_ERROR */, `Response error: ${formatBlockErrorMessage(response)}. Response body stored in error.response`, {
|
|
1087
|
+
response
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
return getText(response);
|
|
1091
|
+
}
|
|
1092
|
+
else if (response.promptFeedback) {
|
|
1093
|
+
throw new AIError("response-error" /* AIErrorCode.RESPONSE_ERROR */, `Text not available. ${formatBlockErrorMessage(response)}`, {
|
|
1094
|
+
response
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
return '';
|
|
1098
|
+
};
|
|
1099
|
+
response.inlineDataParts = () => {
|
|
1100
|
+
if (response.candidates && response.candidates.length > 0) {
|
|
1101
|
+
if (response.candidates.length > 1) {
|
|
1102
|
+
logger.warn(`This response had ${response.candidates.length} ` +
|
|
1103
|
+
`candidates. Returning data from the first candidate only. ` +
|
|
1104
|
+
`Access response.candidates directly to use the other candidates.`);
|
|
1105
|
+
}
|
|
1106
|
+
if (hadBadFinishReason(response.candidates[0])) {
|
|
1107
|
+
throw new AIError("response-error" /* AIErrorCode.RESPONSE_ERROR */, `Response error: ${formatBlockErrorMessage(response)}. Response body stored in error.response`, {
|
|
1108
|
+
response
|
|
1109
|
+
});
|
|
1110
|
+
}
|
|
1111
|
+
return getInlineDataParts(response);
|
|
1112
|
+
}
|
|
1113
|
+
else if (response.promptFeedback) {
|
|
1114
|
+
throw new AIError("response-error" /* AIErrorCode.RESPONSE_ERROR */, `Data not available. ${formatBlockErrorMessage(response)}`, {
|
|
1115
|
+
response
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
return undefined;
|
|
1119
|
+
};
|
|
1120
|
+
response.functionCalls = () => {
|
|
1121
|
+
if (response.candidates && response.candidates.length > 0) {
|
|
1122
|
+
if (response.candidates.length > 1) {
|
|
1123
|
+
logger.warn(`This response had ${response.candidates.length} ` +
|
|
1124
|
+
`candidates. Returning function calls from the first candidate only. ` +
|
|
1125
|
+
`Access response.candidates directly to use the other candidates.`);
|
|
1126
|
+
}
|
|
1127
|
+
if (hadBadFinishReason(response.candidates[0])) {
|
|
1128
|
+
throw new AIError("response-error" /* AIErrorCode.RESPONSE_ERROR */, `Response error: ${formatBlockErrorMessage(response)}. Response body stored in error.response`, {
|
|
1129
|
+
response
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
return getFunctionCalls(response);
|
|
1133
|
+
}
|
|
1134
|
+
else if (response.promptFeedback) {
|
|
1135
|
+
throw new AIError("response-error" /* AIErrorCode.RESPONSE_ERROR */, `Function call not available. ${formatBlockErrorMessage(response)}`, {
|
|
1136
|
+
response
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
return undefined;
|
|
1140
|
+
};
|
|
1141
|
+
return response;
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Returns all text found in all parts of first candidate.
|
|
1145
|
+
*/
|
|
1146
|
+
function getText(response) {
|
|
1147
|
+
var _a, _b, _c, _d;
|
|
1148
|
+
const textStrings = [];
|
|
1149
|
+
if ((_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) {
|
|
1150
|
+
for (const part of (_d = (_c = response.candidates) === null || _c === void 0 ? void 0 : _c[0].content) === null || _d === void 0 ? void 0 : _d.parts) {
|
|
1151
|
+
if (part.text) {
|
|
1152
|
+
textStrings.push(part.text);
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
if (textStrings.length > 0) {
|
|
1157
|
+
return textStrings.join('');
|
|
1158
|
+
}
|
|
1159
|
+
else {
|
|
1160
|
+
return '';
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
/**
|
|
1164
|
+
* Returns {@link FunctionCall}s associated with first candidate.
|
|
1165
|
+
*/
|
|
1166
|
+
function getFunctionCalls(response) {
|
|
1167
|
+
var _a, _b, _c, _d;
|
|
1168
|
+
const functionCalls = [];
|
|
1169
|
+
if ((_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) {
|
|
1170
|
+
for (const part of (_d = (_c = response.candidates) === null || _c === void 0 ? void 0 : _c[0].content) === null || _d === void 0 ? void 0 : _d.parts) {
|
|
1171
|
+
if (part.functionCall) {
|
|
1172
|
+
functionCalls.push(part.functionCall);
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
if (functionCalls.length > 0) {
|
|
1177
|
+
return functionCalls;
|
|
1178
|
+
}
|
|
1179
|
+
else {
|
|
1180
|
+
return undefined;
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
/**
|
|
1184
|
+
* Returns {@link InlineDataPart}s in the first candidate if present.
|
|
1185
|
+
*
|
|
1186
|
+
* @internal
|
|
1187
|
+
*/
|
|
1188
|
+
function getInlineDataParts(response) {
|
|
1189
|
+
var _a, _b, _c, _d;
|
|
1190
|
+
const data = [];
|
|
1191
|
+
if ((_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) {
|
|
1192
|
+
for (const part of (_d = (_c = response.candidates) === null || _c === void 0 ? void 0 : _c[0].content) === null || _d === void 0 ? void 0 : _d.parts) {
|
|
1193
|
+
if (part.inlineData) {
|
|
1194
|
+
data.push(part);
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
if (data.length > 0) {
|
|
1199
|
+
return data;
|
|
1200
|
+
}
|
|
1201
|
+
else {
|
|
1202
|
+
return undefined;
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
const badFinishReasons = [exports.FinishReason.RECITATION, exports.FinishReason.SAFETY];
|
|
1206
|
+
function hadBadFinishReason(candidate) {
|
|
1207
|
+
return (!!candidate.finishReason &&
|
|
1208
|
+
badFinishReasons.includes(candidate.finishReason));
|
|
1209
|
+
}
|
|
1210
|
+
function formatBlockErrorMessage(response) {
|
|
1211
|
+
var _a, _b, _c;
|
|
1212
|
+
let message = '';
|
|
1213
|
+
if ((!response.candidates || response.candidates.length === 0) &&
|
|
1214
|
+
response.promptFeedback) {
|
|
1215
|
+
message += 'Response was blocked';
|
|
1216
|
+
if ((_a = response.promptFeedback) === null || _a === void 0 ? void 0 : _a.blockReason) {
|
|
1217
|
+
message += ` due to ${response.promptFeedback.blockReason}`;
|
|
1218
|
+
}
|
|
1219
|
+
if ((_b = response.promptFeedback) === null || _b === void 0 ? void 0 : _b.blockReasonMessage) {
|
|
1220
|
+
message += `: ${response.promptFeedback.blockReasonMessage}`;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
else if ((_c = response.candidates) === null || _c === void 0 ? void 0 : _c[0]) {
|
|
1224
|
+
const firstCandidate = response.candidates[0];
|
|
1225
|
+
if (hadBadFinishReason(firstCandidate)) {
|
|
1226
|
+
message += `Candidate was blocked due to ${firstCandidate.finishReason}`;
|
|
1227
|
+
if (firstCandidate.finishMessage) {
|
|
1228
|
+
message += `: ${firstCandidate.finishMessage}`;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
return message;
|
|
1233
|
+
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Convert a generic successful fetch response body to an Imagen response object
|
|
1236
|
+
* that can be returned to the user. This converts the REST APIs response format to our
|
|
1237
|
+
* APIs representation of a response.
|
|
1238
|
+
*
|
|
1239
|
+
* @internal
|
|
1240
|
+
*/
|
|
1241
|
+
async function handlePredictResponse(response) {
|
|
1242
|
+
var _a;
|
|
1243
|
+
const responseJson = await response.json();
|
|
1244
|
+
const images = [];
|
|
1245
|
+
let filteredReason = undefined;
|
|
1246
|
+
// The backend should always send a non-empty array of predictions if the response was successful.
|
|
1247
|
+
if (!responseJson.predictions || ((_a = responseJson.predictions) === null || _a === void 0 ? void 0 : _a.length) === 0) {
|
|
1248
|
+
throw new AIError("response-error" /* AIErrorCode.RESPONSE_ERROR */, 'No predictions or filtered reason received from Vertex AI. Please report this issue with the full error details at https://github.com/firebase/firebase-js-sdk/issues.');
|
|
1249
|
+
}
|
|
1250
|
+
for (const prediction of responseJson.predictions) {
|
|
1251
|
+
if (prediction.raiFilteredReason) {
|
|
1252
|
+
filteredReason = prediction.raiFilteredReason;
|
|
1253
|
+
}
|
|
1254
|
+
else if (prediction.mimeType && prediction.bytesBase64Encoded) {
|
|
1255
|
+
images.push({
|
|
1256
|
+
mimeType: prediction.mimeType,
|
|
1257
|
+
bytesBase64Encoded: prediction.bytesBase64Encoded
|
|
1258
|
+
});
|
|
1259
|
+
}
|
|
1260
|
+
else if (prediction.mimeType && prediction.gcsUri) {
|
|
1261
|
+
images.push({
|
|
1262
|
+
mimeType: prediction.mimeType,
|
|
1263
|
+
gcsURI: prediction.gcsUri
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
else {
|
|
1267
|
+
throw new AIError("response-error" /* AIErrorCode.RESPONSE_ERROR */, `Predictions array in response has missing properties. Response: ${JSON.stringify(responseJson)}`);
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
return { images, filteredReason };
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
/**
|
|
1274
|
+
* @license
|
|
1275
|
+
* Copyright 2025 Google LLC
|
|
1276
|
+
*
|
|
1277
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1278
|
+
* you may not use this file except in compliance with the License.
|
|
1279
|
+
* You may obtain a copy of the License at
|
|
1280
|
+
*
|
|
1281
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1282
|
+
*
|
|
1283
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1284
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1285
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1286
|
+
* See the License for the specific language governing permissions and
|
|
1287
|
+
* limitations under the License.
|
|
1288
|
+
*/
|
|
1289
|
+
/**
|
|
1290
|
+
* This SDK supports both the Vertex AI Gemini API and the Gemini Developer API (using Google AI).
|
|
1291
|
+
* The public API prioritizes the format used by the Vertex AI Gemini API.
|
|
1292
|
+
* We avoid having two sets of types by translating requests and responses between the two API formats.
|
|
1293
|
+
* This translation allows developers to switch between the Vertex AI Gemini API and the Gemini Developer API
|
|
1294
|
+
* with minimal code changes.
|
|
1295
|
+
*
|
|
1296
|
+
* In here are functions that map requests and responses between the two API formats.
|
|
1297
|
+
* Requests in the Vertex AI format are mapped to the Google AI format before being sent.
|
|
1298
|
+
* Responses from the Google AI backend are mapped back to the Vertex AI format before being returned to the user.
|
|
1299
|
+
*/
|
|
1300
|
+
/**
|
|
1301
|
+
* Maps a Vertex AI {@link GenerateContentRequest} to a format that can be sent to Google AI.
|
|
1302
|
+
*
|
|
1303
|
+
* @param generateContentRequest The {@link GenerateContentRequest} to map.
|
|
1304
|
+
* @returns A {@link GenerateContentResponse} that conforms to the Google AI format.
|
|
1305
|
+
*
|
|
1306
|
+
* @throws If the request contains properties that are unsupported by Google AI.
|
|
1307
|
+
*
|
|
1308
|
+
* @internal
|
|
1309
|
+
*/
|
|
1310
|
+
function mapGenerateContentRequest(generateContentRequest) {
|
|
1311
|
+
var _a, _b;
|
|
1312
|
+
(_a = generateContentRequest.safetySettings) === null || _a === void 0 ? void 0 : _a.forEach(safetySetting => {
|
|
1313
|
+
if (safetySetting.method) {
|
|
1314
|
+
throw new AIError("unsupported" /* AIErrorCode.UNSUPPORTED */, 'SafetySetting.method is not supported in the the Gemini Developer API. Please remove this property.');
|
|
1315
|
+
}
|
|
1316
|
+
});
|
|
1317
|
+
if ((_b = generateContentRequest.generationConfig) === null || _b === void 0 ? void 0 : _b.topK) {
|
|
1318
|
+
const roundedTopK = Math.round(generateContentRequest.generationConfig.topK);
|
|
1319
|
+
if (roundedTopK !== generateContentRequest.generationConfig.topK) {
|
|
1320
|
+
logger.warn('topK in GenerationConfig has been rounded to the nearest integer to match the format for requests to the Gemini Developer API.');
|
|
1321
|
+
generateContentRequest.generationConfig.topK = roundedTopK;
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
return generateContentRequest;
|
|
1325
|
+
}
|
|
1326
|
+
/**
|
|
1327
|
+
* Maps a {@link GenerateContentResponse} from Google AI to the format of the
|
|
1328
|
+
* {@link GenerateContentResponse} that we get from VertexAI that is exposed in the public API.
|
|
1329
|
+
*
|
|
1330
|
+
* @param googleAIResponse The {@link GenerateContentResponse} from Google AI.
|
|
1331
|
+
* @returns A {@link GenerateContentResponse} that conforms to the public API's format.
|
|
1332
|
+
*
|
|
1333
|
+
* @internal
|
|
1334
|
+
*/
|
|
1335
|
+
function mapGenerateContentResponse(googleAIResponse) {
|
|
1336
|
+
const generateContentResponse = {
|
|
1337
|
+
candidates: googleAIResponse.candidates
|
|
1338
|
+
? mapGenerateContentCandidates(googleAIResponse.candidates)
|
|
1339
|
+
: undefined,
|
|
1340
|
+
prompt: googleAIResponse.promptFeedback
|
|
1341
|
+
? mapPromptFeedback(googleAIResponse.promptFeedback)
|
|
1342
|
+
: undefined,
|
|
1343
|
+
usageMetadata: googleAIResponse.usageMetadata
|
|
1344
|
+
};
|
|
1345
|
+
return generateContentResponse;
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* Maps a Vertex AI {@link CountTokensRequest} to a format that can be sent to Google AI.
|
|
1349
|
+
*
|
|
1350
|
+
* @param countTokensRequest The {@link CountTokensRequest} to map.
|
|
1351
|
+
* @param model The model to count tokens with.
|
|
1352
|
+
* @returns A {@link CountTokensRequest} that conforms to the Google AI format.
|
|
1353
|
+
*
|
|
1354
|
+
* @internal
|
|
1355
|
+
*/
|
|
1356
|
+
function mapCountTokensRequest(countTokensRequest, model) {
|
|
1357
|
+
const mappedCountTokensRequest = {
|
|
1358
|
+
generateContentRequest: Object.assign({ model }, countTokensRequest)
|
|
1359
|
+
};
|
|
1360
|
+
return mappedCountTokensRequest;
|
|
1361
|
+
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Maps a Google AI {@link GoogleAIGenerateContentCandidate} to a format that conforms
|
|
1364
|
+
* to the Vertex AI API format.
|
|
1365
|
+
*
|
|
1366
|
+
* @param candidates The {@link GoogleAIGenerateContentCandidate} to map.
|
|
1367
|
+
* @returns A {@link GenerateContentCandidate} that conforms to the Vertex AI format.
|
|
1368
|
+
*
|
|
1369
|
+
* @throws If any {@link Part} in the candidates has a `videoMetadata` property.
|
|
1370
|
+
*
|
|
1371
|
+
* @internal
|
|
1372
|
+
*/
|
|
1373
|
+
function mapGenerateContentCandidates(candidates) {
|
|
1374
|
+
const mappedCandidates = [];
|
|
1375
|
+
let mappedSafetyRatings;
|
|
1376
|
+
if (mappedCandidates) {
|
|
1377
|
+
candidates.forEach(candidate => {
|
|
1378
|
+
var _a;
|
|
1379
|
+
// Map citationSources to citations.
|
|
1380
|
+
let citationMetadata;
|
|
1381
|
+
if (candidate.citationMetadata) {
|
|
1382
|
+
citationMetadata = {
|
|
1383
|
+
citations: candidate.citationMetadata.citationSources
|
|
1384
|
+
};
|
|
1385
|
+
}
|
|
1386
|
+
// Assign missing candidate SafetyRatings properties to their defaults if undefined.
|
|
1387
|
+
if (candidate.safetyRatings) {
|
|
1388
|
+
mappedSafetyRatings = candidate.safetyRatings.map(safetyRating => {
|
|
1389
|
+
var _a, _b, _c;
|
|
1390
|
+
return Object.assign(Object.assign({}, safetyRating), { severity: (_a = safetyRating.severity) !== null && _a !== void 0 ? _a : exports.HarmSeverity.HARM_SEVERITY_UNSUPPORTED, probabilityScore: (_b = safetyRating.probabilityScore) !== null && _b !== void 0 ? _b : 0, severityScore: (_c = safetyRating.severityScore) !== null && _c !== void 0 ? _c : 0 });
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
// videoMetadata is not supported.
|
|
1394
|
+
// Throw early since developers may send a long video as input and only expect to pay
|
|
1395
|
+
// for inference on a small portion of the video.
|
|
1396
|
+
if ((_a = candidate.content) === null || _a === void 0 ? void 0 : _a.parts.some(part => part === null || part === void 0 ? void 0 : part.videoMetadata)) {
|
|
1397
|
+
throw new AIError("unsupported" /* AIErrorCode.UNSUPPORTED */, 'Part.videoMetadata is not supported in the Gemini Developer API. Please remove this property.');
|
|
1398
|
+
}
|
|
1399
|
+
const mappedCandidate = {
|
|
1400
|
+
index: candidate.index,
|
|
1401
|
+
content: candidate.content,
|
|
1402
|
+
finishReason: candidate.finishReason,
|
|
1403
|
+
finishMessage: candidate.finishMessage,
|
|
1404
|
+
safetyRatings: mappedSafetyRatings,
|
|
1405
|
+
citationMetadata,
|
|
1406
|
+
groundingMetadata: candidate.groundingMetadata
|
|
1407
|
+
};
|
|
1408
|
+
mappedCandidates.push(mappedCandidate);
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
return mappedCandidates;
|
|
1412
|
+
}
|
|
1413
|
+
function mapPromptFeedback(promptFeedback) {
|
|
1414
|
+
// Assign missing SafetyRating properties to their defaults if undefined.
|
|
1415
|
+
const mappedSafetyRatings = [];
|
|
1416
|
+
promptFeedback.safetyRatings.forEach(safetyRating => {
|
|
1417
|
+
var _a, _b, _c;
|
|
1418
|
+
mappedSafetyRatings.push({
|
|
1419
|
+
category: safetyRating.category,
|
|
1420
|
+
probability: safetyRating.probability,
|
|
1421
|
+
severity: (_a = safetyRating.severity) !== null && _a !== void 0 ? _a : exports.HarmSeverity.HARM_SEVERITY_UNSUPPORTED,
|
|
1422
|
+
probabilityScore: (_b = safetyRating.probabilityScore) !== null && _b !== void 0 ? _b : 0,
|
|
1423
|
+
severityScore: (_c = safetyRating.severityScore) !== null && _c !== void 0 ? _c : 0,
|
|
1424
|
+
blocked: safetyRating.blocked
|
|
1425
|
+
});
|
|
1426
|
+
});
|
|
1427
|
+
const mappedPromptFeedback = {
|
|
1428
|
+
blockReason: promptFeedback.blockReason,
|
|
1429
|
+
safetyRatings: mappedSafetyRatings,
|
|
1430
|
+
blockReasonMessage: promptFeedback.blockReasonMessage
|
|
1431
|
+
};
|
|
1432
|
+
return mappedPromptFeedback;
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
/**
|
|
1436
|
+
* @license
|
|
1437
|
+
* Copyright 2024 Google LLC
|
|
1438
|
+
*
|
|
1439
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1440
|
+
* you may not use this file except in compliance with the License.
|
|
1441
|
+
* You may obtain a copy of the License at
|
|
1442
|
+
*
|
|
1443
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1444
|
+
*
|
|
1445
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1446
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1447
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1448
|
+
* See the License for the specific language governing permissions and
|
|
1449
|
+
* limitations under the License.
|
|
1450
|
+
*/
|
|
1451
|
+
const responseLineRE = /^data\: (.*)(?:\n\n|\r\r|\r\n\r\n)/;
|
|
1452
|
+
/**
|
|
1453
|
+
* Process a response.body stream from the backend and return an
|
|
1454
|
+
* iterator that provides one complete GenerateContentResponse at a time
|
|
1455
|
+
* and a promise that resolves with a single aggregated
|
|
1456
|
+
* GenerateContentResponse.
|
|
1457
|
+
*
|
|
1458
|
+
* @param response - Response from a fetch call
|
|
1459
|
+
*/
|
|
1460
|
+
function processStream(response, apiSettings) {
|
|
1461
|
+
const inputStream = response.body.pipeThrough(new TextDecoderStream('utf8', { fatal: true }));
|
|
1462
|
+
const responseStream = getResponseStream(inputStream);
|
|
1463
|
+
const [stream1, stream2] = responseStream.tee();
|
|
1464
|
+
return {
|
|
1465
|
+
stream: generateResponseSequence(stream1, apiSettings),
|
|
1466
|
+
response: getResponsePromise(stream2, apiSettings)
|
|
1467
|
+
};
|
|
1468
|
+
}
|
|
1469
|
+
async function getResponsePromise(stream, apiSettings) {
|
|
1470
|
+
const allResponses = [];
|
|
1471
|
+
const reader = stream.getReader();
|
|
1472
|
+
while (true) {
|
|
1473
|
+
const { done, value } = await reader.read();
|
|
1474
|
+
if (done) {
|
|
1475
|
+
let generateContentResponse = aggregateResponses(allResponses);
|
|
1476
|
+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1477
|
+
generateContentResponse = mapGenerateContentResponse(generateContentResponse);
|
|
1478
|
+
}
|
|
1479
|
+
return createEnhancedContentResponse(generateContentResponse);
|
|
1480
|
+
}
|
|
1481
|
+
allResponses.push(value);
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
function generateResponseSequence(stream, apiSettings) {
|
|
1485
|
+
return tslib.__asyncGenerator(this, arguments, function* generateResponseSequence_1() {
|
|
1486
|
+
const reader = stream.getReader();
|
|
1487
|
+
while (true) {
|
|
1488
|
+
const { value, done } = yield tslib.__await(reader.read());
|
|
1489
|
+
if (done) {
|
|
1490
|
+
break;
|
|
1491
|
+
}
|
|
1492
|
+
let enhancedResponse;
|
|
1493
|
+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1494
|
+
enhancedResponse = createEnhancedContentResponse(mapGenerateContentResponse(value));
|
|
1495
|
+
}
|
|
1496
|
+
else {
|
|
1497
|
+
enhancedResponse = createEnhancedContentResponse(value);
|
|
1498
|
+
}
|
|
1499
|
+
yield yield tslib.__await(enhancedResponse);
|
|
1500
|
+
}
|
|
1501
|
+
});
|
|
1502
|
+
}
|
|
1503
|
+
/**
|
|
1504
|
+
* Reads a raw stream from the fetch response and join incomplete
|
|
1505
|
+
* chunks, returning a new stream that provides a single complete
|
|
1506
|
+
* GenerateContentResponse in each iteration.
|
|
1507
|
+
*/
|
|
1508
|
+
function getResponseStream(inputStream) {
|
|
1509
|
+
const reader = inputStream.getReader();
|
|
1510
|
+
const stream = new ReadableStream({
|
|
1511
|
+
start(controller) {
|
|
1512
|
+
let currentText = '';
|
|
1513
|
+
return pump();
|
|
1514
|
+
function pump() {
|
|
1515
|
+
return reader.read().then(({ value, done }) => {
|
|
1516
|
+
if (done) {
|
|
1517
|
+
if (currentText.trim()) {
|
|
1518
|
+
controller.error(new AIError("parse-failed" /* AIErrorCode.PARSE_FAILED */, 'Failed to parse stream'));
|
|
1519
|
+
return;
|
|
1520
|
+
}
|
|
1521
|
+
controller.close();
|
|
1522
|
+
return;
|
|
1523
|
+
}
|
|
1524
|
+
currentText += value;
|
|
1525
|
+
let match = currentText.match(responseLineRE);
|
|
1526
|
+
let parsedResponse;
|
|
1527
|
+
while (match) {
|
|
1528
|
+
try {
|
|
1529
|
+
parsedResponse = JSON.parse(match[1]);
|
|
1530
|
+
}
|
|
1531
|
+
catch (e) {
|
|
1532
|
+
controller.error(new AIError("parse-failed" /* AIErrorCode.PARSE_FAILED */, `Error parsing JSON response: "${match[1]}`));
|
|
1533
|
+
return;
|
|
1534
|
+
}
|
|
1535
|
+
controller.enqueue(parsedResponse);
|
|
1536
|
+
currentText = currentText.substring(match[0].length);
|
|
1537
|
+
match = currentText.match(responseLineRE);
|
|
1538
|
+
}
|
|
1539
|
+
return pump();
|
|
1540
|
+
});
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
});
|
|
1544
|
+
return stream;
|
|
1545
|
+
}
|
|
1546
|
+
/**
|
|
1547
|
+
* Aggregates an array of `GenerateContentResponse`s into a single
|
|
1548
|
+
* GenerateContentResponse.
|
|
1549
|
+
*/
|
|
1550
|
+
function aggregateResponses(responses) {
|
|
1551
|
+
const lastResponse = responses[responses.length - 1];
|
|
1552
|
+
const aggregatedResponse = {
|
|
1553
|
+
promptFeedback: lastResponse === null || lastResponse === void 0 ? void 0 : lastResponse.promptFeedback
|
|
1554
|
+
};
|
|
1555
|
+
for (const response of responses) {
|
|
1556
|
+
if (response.candidates) {
|
|
1557
|
+
for (const candidate of response.candidates) {
|
|
1558
|
+
// Index will be undefined if it's the first index (0), so we should use 0 if it's undefined.
|
|
1559
|
+
// See: https://github.com/firebase/firebase-js-sdk/issues/8566
|
|
1560
|
+
const i = candidate.index || 0;
|
|
1561
|
+
if (!aggregatedResponse.candidates) {
|
|
1562
|
+
aggregatedResponse.candidates = [];
|
|
1563
|
+
}
|
|
1564
|
+
if (!aggregatedResponse.candidates[i]) {
|
|
1565
|
+
aggregatedResponse.candidates[i] = {
|
|
1566
|
+
index: candidate.index
|
|
1567
|
+
};
|
|
1568
|
+
}
|
|
1569
|
+
// Keep overwriting, the last one will be final
|
|
1570
|
+
aggregatedResponse.candidates[i].citationMetadata =
|
|
1571
|
+
candidate.citationMetadata;
|
|
1572
|
+
aggregatedResponse.candidates[i].finishReason = candidate.finishReason;
|
|
1573
|
+
aggregatedResponse.candidates[i].finishMessage =
|
|
1574
|
+
candidate.finishMessage;
|
|
1575
|
+
aggregatedResponse.candidates[i].safetyRatings =
|
|
1576
|
+
candidate.safetyRatings;
|
|
1577
|
+
/**
|
|
1578
|
+
* Candidates should always have content and parts, but this handles
|
|
1579
|
+
* possible malformed responses.
|
|
1580
|
+
*/
|
|
1581
|
+
if (candidate.content && candidate.content.parts) {
|
|
1582
|
+
if (!aggregatedResponse.candidates[i].content) {
|
|
1583
|
+
aggregatedResponse.candidates[i].content = {
|
|
1584
|
+
role: candidate.content.role || 'user',
|
|
1585
|
+
parts: []
|
|
1586
|
+
};
|
|
1587
|
+
}
|
|
1588
|
+
const newPart = {};
|
|
1589
|
+
for (const part of candidate.content.parts) {
|
|
1590
|
+
if (part.text !== undefined) {
|
|
1591
|
+
// The backend can send empty text parts. If these are sent back
|
|
1592
|
+
// (e.g. in chat history), the backend will respond with an error.
|
|
1593
|
+
// To prevent this, ignore empty text parts.
|
|
1594
|
+
if (part.text === '') {
|
|
1595
|
+
continue;
|
|
1596
|
+
}
|
|
1597
|
+
newPart.text = part.text;
|
|
1598
|
+
}
|
|
1599
|
+
if (part.functionCall) {
|
|
1600
|
+
newPart.functionCall = part.functionCall;
|
|
1601
|
+
}
|
|
1602
|
+
if (Object.keys(newPart).length === 0) {
|
|
1603
|
+
throw new AIError("invalid-content" /* AIErrorCode.INVALID_CONTENT */, 'Part should have at least one property, but there are none. This is likely caused ' +
|
|
1604
|
+
'by a malformed response from the backend.');
|
|
1605
|
+
}
|
|
1606
|
+
aggregatedResponse.candidates[i].content.parts.push(newPart);
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
return aggregatedResponse;
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
/**
|
|
1616
|
+
* @license
|
|
1617
|
+
* Copyright 2024 Google LLC
|
|
1618
|
+
*
|
|
1619
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1620
|
+
* you may not use this file except in compliance with the License.
|
|
1621
|
+
* You may obtain a copy of the License at
|
|
1622
|
+
*
|
|
1623
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1624
|
+
*
|
|
1625
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1626
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1627
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1628
|
+
* See the License for the specific language governing permissions and
|
|
1629
|
+
* limitations under the License.
|
|
1630
|
+
*/
|
|
1631
|
+
async function generateContentStream(apiSettings, model, params, requestOptions) {
|
|
1632
|
+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1633
|
+
params = mapGenerateContentRequest(params);
|
|
1634
|
+
}
|
|
1635
|
+
const response = await makeRequest(model, Task.STREAM_GENERATE_CONTENT, apiSettings,
|
|
1636
|
+
/* stream */ true, JSON.stringify(params), requestOptions);
|
|
1637
|
+
return processStream(response, apiSettings); // TODO: Map streaming responses
|
|
1638
|
+
}
|
|
1639
|
+
async function generateContent(apiSettings, model, params, requestOptions) {
|
|
1640
|
+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1641
|
+
params = mapGenerateContentRequest(params);
|
|
1642
|
+
}
|
|
1643
|
+
const response = await makeRequest(model, Task.GENERATE_CONTENT, apiSettings,
|
|
1644
|
+
/* stream */ false, JSON.stringify(params), requestOptions);
|
|
1645
|
+
const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
|
|
1646
|
+
const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
|
|
1647
|
+
return {
|
|
1648
|
+
response: enhancedResponse
|
|
1649
|
+
};
|
|
1650
|
+
}
|
|
1651
|
+
async function processGenerateContentResponse(response, apiSettings) {
|
|
1652
|
+
const responseJson = await response.json();
|
|
1653
|
+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1654
|
+
return mapGenerateContentResponse(responseJson);
|
|
1655
|
+
}
|
|
1656
|
+
else {
|
|
1657
|
+
return responseJson;
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
/**
|
|
1662
|
+
* @license
|
|
1663
|
+
* Copyright 2024 Google LLC
|
|
1664
|
+
*
|
|
1665
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1666
|
+
* you may not use this file except in compliance with the License.
|
|
1667
|
+
* You may obtain a copy of the License at
|
|
1668
|
+
*
|
|
1669
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1670
|
+
*
|
|
1671
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1672
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1673
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1674
|
+
* See the License for the specific language governing permissions and
|
|
1675
|
+
* limitations under the License.
|
|
1676
|
+
*/
|
|
1677
|
+
function formatSystemInstruction(input) {
|
|
1678
|
+
// null or undefined
|
|
1679
|
+
if (input == null) {
|
|
1680
|
+
return undefined;
|
|
1681
|
+
}
|
|
1682
|
+
else if (typeof input === 'string') {
|
|
1683
|
+
return { role: 'system', parts: [{ text: input }] };
|
|
1684
|
+
}
|
|
1685
|
+
else if (input.text) {
|
|
1686
|
+
return { role: 'system', parts: [input] };
|
|
1687
|
+
}
|
|
1688
|
+
else if (input.parts) {
|
|
1689
|
+
if (!input.role) {
|
|
1690
|
+
return { role: 'system', parts: input.parts };
|
|
1691
|
+
}
|
|
1692
|
+
else {
|
|
1693
|
+
return input;
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
function formatNewContent(request) {
|
|
1698
|
+
let newParts = [];
|
|
1699
|
+
if (typeof request === 'string') {
|
|
1700
|
+
newParts = [{ text: request }];
|
|
1701
|
+
}
|
|
1702
|
+
else {
|
|
1703
|
+
for (const partOrString of request) {
|
|
1704
|
+
if (typeof partOrString === 'string') {
|
|
1705
|
+
newParts.push({ text: partOrString });
|
|
1706
|
+
}
|
|
1707
|
+
else {
|
|
1708
|
+
newParts.push(partOrString);
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
return assignRoleToPartsAndValidateSendMessageRequest(newParts);
|
|
1713
|
+
}
|
|
1714
|
+
/**
|
|
1715
|
+
* When multiple Part types (i.e. FunctionResponsePart and TextPart) are
|
|
1716
|
+
* passed in a single Part array, we may need to assign different roles to each
|
|
1717
|
+
* part. Currently only FunctionResponsePart requires a role other than 'user'.
|
|
1718
|
+
* @private
|
|
1719
|
+
* @param parts Array of parts to pass to the model
|
|
1720
|
+
* @returns Array of content items
|
|
1721
|
+
*/
|
|
1722
|
+
function assignRoleToPartsAndValidateSendMessageRequest(parts) {
|
|
1723
|
+
const userContent = { role: 'user', parts: [] };
|
|
1724
|
+
const functionContent = { role: 'function', parts: [] };
|
|
1725
|
+
let hasUserContent = false;
|
|
1726
|
+
let hasFunctionContent = false;
|
|
1727
|
+
for (const part of parts) {
|
|
1728
|
+
if ('functionResponse' in part) {
|
|
1729
|
+
functionContent.parts.push(part);
|
|
1730
|
+
hasFunctionContent = true;
|
|
1731
|
+
}
|
|
1732
|
+
else {
|
|
1733
|
+
userContent.parts.push(part);
|
|
1734
|
+
hasUserContent = true;
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
if (hasUserContent && hasFunctionContent) {
|
|
1738
|
+
throw new AIError("invalid-content" /* AIErrorCode.INVALID_CONTENT */, 'Within a single message, FunctionResponse cannot be mixed with other type of Part in the request for sending chat message.');
|
|
1739
|
+
}
|
|
1740
|
+
if (!hasUserContent && !hasFunctionContent) {
|
|
1741
|
+
throw new AIError("invalid-content" /* AIErrorCode.INVALID_CONTENT */, 'No Content is provided for sending chat message.');
|
|
1742
|
+
}
|
|
1743
|
+
if (hasUserContent) {
|
|
1744
|
+
return userContent;
|
|
1745
|
+
}
|
|
1746
|
+
return functionContent;
|
|
1747
|
+
}
|
|
1748
|
+
function formatGenerateContentInput(params) {
|
|
1749
|
+
let formattedRequest;
|
|
1750
|
+
if (params.contents) {
|
|
1751
|
+
formattedRequest = params;
|
|
1752
|
+
}
|
|
1753
|
+
else {
|
|
1754
|
+
// Array or string
|
|
1755
|
+
const content = formatNewContent(params);
|
|
1756
|
+
formattedRequest = { contents: [content] };
|
|
1757
|
+
}
|
|
1758
|
+
if (params.systemInstruction) {
|
|
1759
|
+
formattedRequest.systemInstruction = formatSystemInstruction(params.systemInstruction);
|
|
1760
|
+
}
|
|
1761
|
+
return formattedRequest;
|
|
1762
|
+
}
|
|
1763
|
+
/**
|
|
1764
|
+
* Convert the user-defined parameters in {@link ImagenGenerationParams} to the format
|
|
1765
|
+
* that is expected from the REST API.
|
|
1766
|
+
*
|
|
1767
|
+
* @internal
|
|
1768
|
+
*/
|
|
1769
|
+
function createPredictRequestBody(prompt, { gcsURI, imageFormat, addWatermark, numberOfImages = 1, negativePrompt, aspectRatio, safetyFilterLevel, personFilterLevel }) {
|
|
1770
|
+
// Properties that are undefined will be omitted from the JSON string that is sent in the request.
|
|
1771
|
+
const body = {
|
|
1772
|
+
instances: [
|
|
1773
|
+
{
|
|
1774
|
+
prompt
|
|
1775
|
+
}
|
|
1776
|
+
],
|
|
1777
|
+
parameters: {
|
|
1778
|
+
storageUri: gcsURI,
|
|
1779
|
+
negativePrompt,
|
|
1780
|
+
sampleCount: numberOfImages,
|
|
1781
|
+
aspectRatio,
|
|
1782
|
+
outputOptions: imageFormat,
|
|
1783
|
+
addWatermark,
|
|
1784
|
+
safetyFilterLevel,
|
|
1785
|
+
personGeneration: personFilterLevel,
|
|
1786
|
+
includeRaiReason: true
|
|
1787
|
+
}
|
|
1788
|
+
};
|
|
1789
|
+
return body;
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
/**
|
|
1793
|
+
* @license
|
|
1794
|
+
* Copyright 2024 Google LLC
|
|
1795
|
+
*
|
|
1796
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1797
|
+
* you may not use this file except in compliance with the License.
|
|
1798
|
+
* You may obtain a copy of the License at
|
|
1799
|
+
*
|
|
1800
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1801
|
+
*
|
|
1802
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1803
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1804
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1805
|
+
* See the License for the specific language governing permissions and
|
|
1806
|
+
* limitations under the License.
|
|
1807
|
+
*/
|
|
1808
|
+
// https://ai.google.dev/api/rest/v1beta/Content#part
|
|
1809
|
+
const VALID_PART_FIELDS = [
|
|
1810
|
+
'text',
|
|
1811
|
+
'inlineData',
|
|
1812
|
+
'functionCall',
|
|
1813
|
+
'functionResponse'
|
|
1814
|
+
];
|
|
1815
|
+
const VALID_PARTS_PER_ROLE = {
|
|
1816
|
+
user: ['text', 'inlineData'],
|
|
1817
|
+
function: ['functionResponse'],
|
|
1818
|
+
model: ['text', 'functionCall'],
|
|
1819
|
+
// System instructions shouldn't be in history anyway.
|
|
1820
|
+
system: ['text']
|
|
1821
|
+
};
|
|
1822
|
+
const VALID_PREVIOUS_CONTENT_ROLES = {
|
|
1823
|
+
user: ['model'],
|
|
1824
|
+
function: ['model'],
|
|
1825
|
+
model: ['user', 'function'],
|
|
1826
|
+
// System instructions shouldn't be in history.
|
|
1827
|
+
system: []
|
|
1828
|
+
};
|
|
1829
|
+
function validateChatHistory(history) {
|
|
1830
|
+
let prevContent = null;
|
|
1831
|
+
for (const currContent of history) {
|
|
1832
|
+
const { role, parts } = currContent;
|
|
1833
|
+
if (!prevContent && role !== 'user') {
|
|
1834
|
+
throw new AIError("invalid-content" /* AIErrorCode.INVALID_CONTENT */, `First Content should be with role 'user', got ${role}`);
|
|
1835
|
+
}
|
|
1836
|
+
if (!POSSIBLE_ROLES.includes(role)) {
|
|
1837
|
+
throw new AIError("invalid-content" /* AIErrorCode.INVALID_CONTENT */, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
|
|
1838
|
+
}
|
|
1839
|
+
if (!Array.isArray(parts)) {
|
|
1840
|
+
throw new AIError("invalid-content" /* AIErrorCode.INVALID_CONTENT */, `Content should have 'parts' but property with an array of Parts`);
|
|
1841
|
+
}
|
|
1842
|
+
if (parts.length === 0) {
|
|
1843
|
+
throw new AIError("invalid-content" /* AIErrorCode.INVALID_CONTENT */, `Each Content should have at least one part`);
|
|
1844
|
+
}
|
|
1845
|
+
const countFields = {
|
|
1846
|
+
text: 0,
|
|
1847
|
+
inlineData: 0,
|
|
1848
|
+
functionCall: 0,
|
|
1849
|
+
functionResponse: 0
|
|
1850
|
+
};
|
|
1851
|
+
for (const part of parts) {
|
|
1852
|
+
for (const key of VALID_PART_FIELDS) {
|
|
1853
|
+
if (key in part) {
|
|
1854
|
+
countFields[key] += 1;
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
const validParts = VALID_PARTS_PER_ROLE[role];
|
|
1859
|
+
for (const key of VALID_PART_FIELDS) {
|
|
1860
|
+
if (!validParts.includes(key) && countFields[key] > 0) {
|
|
1861
|
+
throw new AIError("invalid-content" /* AIErrorCode.INVALID_CONTENT */, `Content with role '${role}' can't contain '${key}' part`);
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
if (prevContent) {
|
|
1865
|
+
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
|
|
1866
|
+
if (!validPreviousContentRoles.includes(prevContent.role)) {
|
|
1867
|
+
throw new AIError("invalid-content" /* AIErrorCode.INVALID_CONTENT */, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
prevContent = currContent;
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
/**
|
|
1875
|
+
* @license
|
|
1876
|
+
* Copyright 2024 Google LLC
|
|
1877
|
+
*
|
|
1878
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1879
|
+
* you may not use this file except in compliance with the License.
|
|
1880
|
+
* You may obtain a copy of the License at
|
|
1881
|
+
*
|
|
1882
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1883
|
+
*
|
|
1884
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1885
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1886
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1887
|
+
* See the License for the specific language governing permissions and
|
|
1888
|
+
* limitations under the License.
|
|
1889
|
+
*/
|
|
1890
|
+
/**
|
|
1891
|
+
* Do not log a message for this error.
|
|
1892
|
+
*/
|
|
1893
|
+
const SILENT_ERROR = 'SILENT_ERROR';
|
|
1894
|
+
/**
|
|
1895
|
+
* ChatSession class that enables sending chat messages and stores
|
|
1896
|
+
* history of sent and received messages so far.
|
|
1897
|
+
*
|
|
1898
|
+
* @public
|
|
1899
|
+
*/
|
|
1900
|
+
class ChatSession {
|
|
1901
|
+
constructor(apiSettings, model, params, requestOptions) {
|
|
1902
|
+
this.model = model;
|
|
1903
|
+
this.params = params;
|
|
1904
|
+
this.requestOptions = requestOptions;
|
|
1905
|
+
this._history = [];
|
|
1906
|
+
this._sendPromise = Promise.resolve();
|
|
1907
|
+
this._apiSettings = apiSettings;
|
|
1908
|
+
if (params === null || params === void 0 ? void 0 : params.history) {
|
|
1909
|
+
validateChatHistory(params.history);
|
|
1910
|
+
this._history = params.history;
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
/**
|
|
1914
|
+
* Gets the chat history so far. Blocked prompts are not added to history.
|
|
1915
|
+
* Neither blocked candidates nor the prompts that generated them are added
|
|
1916
|
+
* to history.
|
|
1917
|
+
*/
|
|
1918
|
+
async getHistory() {
|
|
1919
|
+
await this._sendPromise;
|
|
1920
|
+
return this._history;
|
|
1921
|
+
}
|
|
1922
|
+
/**
|
|
1923
|
+
* Sends a chat message and receives a non-streaming
|
|
1924
|
+
* {@link GenerateContentResult}
|
|
1925
|
+
*/
|
|
1926
|
+
async sendMessage(request) {
|
|
1927
|
+
var _a, _b, _c, _d, _e;
|
|
1928
|
+
await this._sendPromise;
|
|
1929
|
+
const newContent = formatNewContent(request);
|
|
1930
|
+
const generateContentRequest = {
|
|
1931
|
+
safetySettings: (_a = this.params) === null || _a === void 0 ? void 0 : _a.safetySettings,
|
|
1932
|
+
generationConfig: (_b = this.params) === null || _b === void 0 ? void 0 : _b.generationConfig,
|
|
1933
|
+
tools: (_c = this.params) === null || _c === void 0 ? void 0 : _c.tools,
|
|
1934
|
+
toolConfig: (_d = this.params) === null || _d === void 0 ? void 0 : _d.toolConfig,
|
|
1935
|
+
systemInstruction: (_e = this.params) === null || _e === void 0 ? void 0 : _e.systemInstruction,
|
|
1936
|
+
contents: [...this._history, newContent]
|
|
1937
|
+
};
|
|
1938
|
+
let finalResult = {};
|
|
1939
|
+
// Add onto the chain.
|
|
1940
|
+
this._sendPromise = this._sendPromise
|
|
1941
|
+
.then(() => generateContent(this._apiSettings, this.model, generateContentRequest, this.requestOptions))
|
|
1942
|
+
.then(result => {
|
|
1943
|
+
var _a, _b;
|
|
1944
|
+
if (result.response.candidates &&
|
|
1945
|
+
result.response.candidates.length > 0) {
|
|
1946
|
+
this._history.push(newContent);
|
|
1947
|
+
const responseContent = {
|
|
1948
|
+
parts: ((_a = result.response.candidates) === null || _a === void 0 ? void 0 : _a[0].content.parts) || [],
|
|
1949
|
+
// Response seems to come back without a role set.
|
|
1950
|
+
role: ((_b = result.response.candidates) === null || _b === void 0 ? void 0 : _b[0].content.role) || 'model'
|
|
1951
|
+
};
|
|
1952
|
+
this._history.push(responseContent);
|
|
1953
|
+
}
|
|
1954
|
+
else {
|
|
1955
|
+
const blockErrorMessage = formatBlockErrorMessage(result.response);
|
|
1956
|
+
if (blockErrorMessage) {
|
|
1957
|
+
logger.warn(`sendMessage() was unsuccessful. ${blockErrorMessage}. Inspect response object for details.`);
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
finalResult = result;
|
|
1961
|
+
});
|
|
1962
|
+
await this._sendPromise;
|
|
1963
|
+
return finalResult;
|
|
1964
|
+
}
|
|
1965
|
+
/**
|
|
1966
|
+
* Sends a chat message and receives the response as a
|
|
1967
|
+
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
1968
|
+
* and a response promise.
|
|
1969
|
+
*/
|
|
1970
|
+
async sendMessageStream(request) {
|
|
1971
|
+
var _a, _b, _c, _d, _e;
|
|
1972
|
+
await this._sendPromise;
|
|
1973
|
+
const newContent = formatNewContent(request);
|
|
1974
|
+
const generateContentRequest = {
|
|
1975
|
+
safetySettings: (_a = this.params) === null || _a === void 0 ? void 0 : _a.safetySettings,
|
|
1976
|
+
generationConfig: (_b = this.params) === null || _b === void 0 ? void 0 : _b.generationConfig,
|
|
1977
|
+
tools: (_c = this.params) === null || _c === void 0 ? void 0 : _c.tools,
|
|
1978
|
+
toolConfig: (_d = this.params) === null || _d === void 0 ? void 0 : _d.toolConfig,
|
|
1979
|
+
systemInstruction: (_e = this.params) === null || _e === void 0 ? void 0 : _e.systemInstruction,
|
|
1980
|
+
contents: [...this._history, newContent]
|
|
1981
|
+
};
|
|
1982
|
+
const streamPromise = generateContentStream(this._apiSettings, this.model, generateContentRequest, this.requestOptions);
|
|
1983
|
+
// Add onto the chain.
|
|
1984
|
+
this._sendPromise = this._sendPromise
|
|
1985
|
+
.then(() => streamPromise)
|
|
1986
|
+
// This must be handled to avoid unhandled rejection, but jump
|
|
1987
|
+
// to the final catch block with a label to not log this error.
|
|
1988
|
+
.catch(_ignored => {
|
|
1989
|
+
throw new Error(SILENT_ERROR);
|
|
1990
|
+
})
|
|
1991
|
+
.then(streamResult => streamResult.response)
|
|
1992
|
+
.then(response => {
|
|
1993
|
+
if (response.candidates && response.candidates.length > 0) {
|
|
1994
|
+
this._history.push(newContent);
|
|
1995
|
+
const responseContent = Object.assign({}, response.candidates[0].content);
|
|
1996
|
+
// Response seems to come back without a role set.
|
|
1997
|
+
if (!responseContent.role) {
|
|
1998
|
+
responseContent.role = 'model';
|
|
1999
|
+
}
|
|
2000
|
+
this._history.push(responseContent);
|
|
2001
|
+
}
|
|
2002
|
+
else {
|
|
2003
|
+
const blockErrorMessage = formatBlockErrorMessage(response);
|
|
2004
|
+
if (blockErrorMessage) {
|
|
2005
|
+
logger.warn(`sendMessageStream() was unsuccessful. ${blockErrorMessage}. Inspect response object for details.`);
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
})
|
|
2009
|
+
.catch(e => {
|
|
2010
|
+
// Errors in streamPromise are already catchable by the user as
|
|
2011
|
+
// streamPromise is returned.
|
|
2012
|
+
// Avoid duplicating the error message in logs.
|
|
2013
|
+
if (e.message !== SILENT_ERROR) {
|
|
2014
|
+
// Users do not have access to _sendPromise to catch errors
|
|
2015
|
+
// downstream from streamPromise, so they should not throw.
|
|
2016
|
+
logger.error(e);
|
|
2017
|
+
}
|
|
2018
|
+
});
|
|
2019
|
+
return streamPromise;
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
|
|
2023
|
+
/**
|
|
2024
|
+
* @license
|
|
2025
|
+
* Copyright 2024 Google LLC
|
|
2026
|
+
*
|
|
2027
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2028
|
+
* you may not use this file except in compliance with the License.
|
|
2029
|
+
* You may obtain a copy of the License at
|
|
2030
|
+
*
|
|
2031
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2032
|
+
*
|
|
2033
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2034
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2035
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2036
|
+
* See the License for the specific language governing permissions and
|
|
2037
|
+
* limitations under the License.
|
|
2038
|
+
*/
|
|
2039
|
+
async function countTokens(apiSettings, model, params, requestOptions) {
|
|
2040
|
+
let body = '';
|
|
2041
|
+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
2042
|
+
const mappedParams = mapCountTokensRequest(params, model);
|
|
2043
|
+
body = JSON.stringify(mappedParams);
|
|
2044
|
+
}
|
|
2045
|
+
else {
|
|
2046
|
+
body = JSON.stringify(params);
|
|
2047
|
+
}
|
|
2048
|
+
const response = await makeRequest(model, Task.COUNT_TOKENS, apiSettings, false, body, requestOptions);
|
|
2049
|
+
return response.json();
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
/**
|
|
2053
|
+
* @license
|
|
2054
|
+
* Copyright 2024 Google LLC
|
|
2055
|
+
*
|
|
2056
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2057
|
+
* you may not use this file except in compliance with the License.
|
|
2058
|
+
* You may obtain a copy of the License at
|
|
2059
|
+
*
|
|
2060
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2061
|
+
*
|
|
2062
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2063
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2064
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2065
|
+
* See the License for the specific language governing permissions and
|
|
2066
|
+
* limitations under the License.
|
|
2067
|
+
*/
|
|
2068
|
+
/**
|
|
2069
|
+
* Class for generative model APIs.
|
|
2070
|
+
* @public
|
|
2071
|
+
*/
|
|
2072
|
+
class GenerativeModel extends AIModel {
|
|
2073
|
+
constructor(ai, modelParams, requestOptions) {
|
|
2074
|
+
super(ai, modelParams.model);
|
|
2075
|
+
this.generationConfig = modelParams.generationConfig || {};
|
|
2076
|
+
this.safetySettings = modelParams.safetySettings || [];
|
|
2077
|
+
this.tools = modelParams.tools;
|
|
2078
|
+
this.toolConfig = modelParams.toolConfig;
|
|
2079
|
+
this.systemInstruction = formatSystemInstruction(modelParams.systemInstruction);
|
|
2080
|
+
this.requestOptions = requestOptions || {};
|
|
2081
|
+
}
|
|
2082
|
+
/**
|
|
2083
|
+
* Makes a single non-streaming call to the model
|
|
2084
|
+
* and returns an object containing a single {@link GenerateContentResponse}.
|
|
2085
|
+
*/
|
|
2086
|
+
async generateContent(request) {
|
|
2087
|
+
const formattedParams = formatGenerateContentInput(request);
|
|
2088
|
+
return generateContent(this._apiSettings, this.model, Object.assign({ generationConfig: this.generationConfig, safetySettings: this.safetySettings, tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction }, formattedParams), this.requestOptions);
|
|
2089
|
+
}
|
|
2090
|
+
/**
|
|
2091
|
+
* Makes a single streaming call to the model
|
|
2092
|
+
* and returns an object containing an iterable stream that iterates
|
|
2093
|
+
* over all chunks in the streaming response as well as
|
|
2094
|
+
* a promise that returns the final aggregated response.
|
|
2095
|
+
*/
|
|
2096
|
+
async generateContentStream(request) {
|
|
2097
|
+
const formattedParams = formatGenerateContentInput(request);
|
|
2098
|
+
return generateContentStream(this._apiSettings, this.model, Object.assign({ generationConfig: this.generationConfig, safetySettings: this.safetySettings, tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction }, formattedParams), this.requestOptions);
|
|
2099
|
+
}
|
|
2100
|
+
/**
|
|
2101
|
+
* Gets a new {@link ChatSession} instance which can be used for
|
|
2102
|
+
* multi-turn chats.
|
|
2103
|
+
*/
|
|
2104
|
+
startChat(startChatParams) {
|
|
2105
|
+
return new ChatSession(this._apiSettings, this.model, Object.assign({ tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, generationConfig: this.generationConfig, safetySettings: this.safetySettings }, startChatParams), this.requestOptions);
|
|
2106
|
+
}
|
|
2107
|
+
/**
|
|
2108
|
+
* Counts the tokens in the provided request.
|
|
2109
|
+
*/
|
|
2110
|
+
async countTokens(request) {
|
|
2111
|
+
const formattedParams = formatGenerateContentInput(request);
|
|
2112
|
+
return countTokens(this._apiSettings, this.model, formattedParams);
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2115
|
+
|
|
2116
|
+
/**
|
|
2117
|
+
* @license
|
|
2118
|
+
* Copyright 2025 Google LLC
|
|
2119
|
+
*
|
|
2120
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2121
|
+
* you may not use this file except in compliance with the License.
|
|
2122
|
+
* You may obtain a copy of the License at
|
|
2123
|
+
*
|
|
2124
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2125
|
+
*
|
|
2126
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2127
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2128
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2129
|
+
* See the License for the specific language governing permissions and
|
|
2130
|
+
* limitations under the License.
|
|
2131
|
+
*/
|
|
2132
|
+
/**
|
|
2133
|
+
* Class for Imagen model APIs.
|
|
2134
|
+
*
|
|
2135
|
+
* This class provides methods for generating images using the Imagen model.
|
|
2136
|
+
*
|
|
2137
|
+
* @example
|
|
2138
|
+
* ```javascript
|
|
2139
|
+
* const imagen = new ImagenModel(
|
|
2140
|
+
* ai,
|
|
2141
|
+
* {
|
|
2142
|
+
* model: 'imagen-3.0-generate-002'
|
|
2143
|
+
* }
|
|
2144
|
+
* );
|
|
2145
|
+
*
|
|
2146
|
+
* const response = await imagen.generateImages('A photo of a cat');
|
|
2147
|
+
* if (response.images.length > 0) {
|
|
2148
|
+
* console.log(response.images[0].bytesBase64Encoded);
|
|
2149
|
+
* }
|
|
2150
|
+
* ```
|
|
2151
|
+
*
|
|
2152
|
+
* @beta
|
|
2153
|
+
*/
|
|
2154
|
+
class ImagenModel extends AIModel {
|
|
2155
|
+
/**
|
|
2156
|
+
* Constructs a new instance of the {@link ImagenModel} class.
|
|
2157
|
+
*
|
|
2158
|
+
* @param ai - an {@link AI} instance.
|
|
2159
|
+
* @param modelParams - Parameters to use when making requests to Imagen.
|
|
2160
|
+
* @param requestOptions - Additional options to use when making requests.
|
|
2161
|
+
*
|
|
2162
|
+
* @throws If the `apiKey` or `projectId` fields are missing in your
|
|
2163
|
+
* Firebase config.
|
|
2164
|
+
*/
|
|
2165
|
+
constructor(ai, modelParams, requestOptions) {
|
|
2166
|
+
const { model, generationConfig, safetySettings } = modelParams;
|
|
2167
|
+
super(ai, model);
|
|
2168
|
+
this.requestOptions = requestOptions;
|
|
2169
|
+
this.generationConfig = generationConfig;
|
|
2170
|
+
this.safetySettings = safetySettings;
|
|
2171
|
+
}
|
|
2172
|
+
/**
|
|
2173
|
+
* Generates images using the Imagen model and returns them as
|
|
2174
|
+
* base64-encoded strings.
|
|
2175
|
+
*
|
|
2176
|
+
* @param prompt - A text prompt describing the image(s) to generate.
|
|
2177
|
+
* @returns A promise that resolves to an {@link ImagenGenerationResponse}
|
|
2178
|
+
* object containing the generated images.
|
|
2179
|
+
*
|
|
2180
|
+
* @throws If the request to generate images fails. This happens if the
|
|
2181
|
+
* prompt is blocked.
|
|
2182
|
+
*
|
|
2183
|
+
* @remarks
|
|
2184
|
+
* If the prompt was not blocked, but one or more of the generated images were filtered, the
|
|
2185
|
+
* returned object will have a `filteredReason` property.
|
|
2186
|
+
* If all images are filtered, the `images` array will be empty.
|
|
2187
|
+
*
|
|
2188
|
+
* @beta
|
|
2189
|
+
*/
|
|
2190
|
+
async generateImages(prompt) {
|
|
2191
|
+
const body = createPredictRequestBody(prompt, Object.assign(Object.assign({}, this.generationConfig), this.safetySettings));
|
|
2192
|
+
const response = await makeRequest(this.model, Task.PREDICT, this._apiSettings,
|
|
2193
|
+
/* stream */ false, JSON.stringify(body), this.requestOptions);
|
|
2194
|
+
return handlePredictResponse(response);
|
|
2195
|
+
}
|
|
2196
|
+
/**
|
|
2197
|
+
* Generates images to Cloud Storage for Firebase using the Imagen model.
|
|
2198
|
+
*
|
|
2199
|
+
* @internal This method is temporarily internal.
|
|
2200
|
+
*
|
|
2201
|
+
* @param prompt - A text prompt describing the image(s) to generate.
|
|
2202
|
+
* @param gcsURI - The URI of file stored in a Cloud Storage for Firebase bucket.
|
|
2203
|
+
* This should be a directory. For example, `gs://my-bucket/my-directory/`.
|
|
2204
|
+
* @returns A promise that resolves to an {@link ImagenGenerationResponse}
|
|
2205
|
+
* object containing the URLs of the generated images.
|
|
2206
|
+
*
|
|
2207
|
+
* @throws If the request fails to generate images fails. This happens if
|
|
2208
|
+
* the prompt is blocked.
|
|
2209
|
+
*
|
|
2210
|
+
* @remarks
|
|
2211
|
+
* If the prompt was not blocked, but one or more of the generated images were filtered, the
|
|
2212
|
+
* returned object will have a `filteredReason` property.
|
|
2213
|
+
* If all images are filtered, the `images` array will be empty.
|
|
2214
|
+
*/
|
|
2215
|
+
async generateImagesGCS(prompt, gcsURI) {
|
|
2216
|
+
const body = createPredictRequestBody(prompt, Object.assign(Object.assign({ gcsURI }, this.generationConfig), this.safetySettings));
|
|
2217
|
+
const response = await makeRequest(this.model, Task.PREDICT, this._apiSettings,
|
|
2218
|
+
/* stream */ false, JSON.stringify(body), this.requestOptions);
|
|
2219
|
+
return handlePredictResponse(response);
|
|
2220
|
+
}
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
/**
|
|
2224
|
+
* @license
|
|
2225
|
+
* Copyright 2024 Google LLC
|
|
2226
|
+
*
|
|
2227
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2228
|
+
* you may not use this file except in compliance with the License.
|
|
2229
|
+
* You may obtain a copy of the License at
|
|
2230
|
+
*
|
|
2231
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2232
|
+
*
|
|
2233
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2234
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2235
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2236
|
+
* See the License for the specific language governing permissions and
|
|
2237
|
+
* limitations under the License.
|
|
2238
|
+
*/
|
|
2239
|
+
/**
|
|
2240
|
+
* Parent class encompassing all Schema types, with static methods that
|
|
2241
|
+
* allow building specific Schema types. This class can be converted with
|
|
2242
|
+
* `JSON.stringify()` into a JSON string accepted by Vertex AI REST endpoints.
|
|
2243
|
+
* (This string conversion is automatically done when calling SDK methods.)
|
|
2244
|
+
* @public
|
|
2245
|
+
*/
|
|
2246
|
+
class Schema {
|
|
2247
|
+
constructor(schemaParams) {
|
|
2248
|
+
// eslint-disable-next-line guard-for-in
|
|
2249
|
+
for (const paramKey in schemaParams) {
|
|
2250
|
+
this[paramKey] = schemaParams[paramKey];
|
|
2251
|
+
}
|
|
2252
|
+
// Ensure these are explicitly set to avoid TS errors.
|
|
2253
|
+
this.type = schemaParams.type;
|
|
2254
|
+
this.nullable = schemaParams.hasOwnProperty('nullable')
|
|
2255
|
+
? !!schemaParams.nullable
|
|
2256
|
+
: false;
|
|
2257
|
+
}
|
|
2258
|
+
/**
|
|
2259
|
+
* Defines how this Schema should be serialized as JSON.
|
|
2260
|
+
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior
|
|
2261
|
+
* @internal
|
|
2262
|
+
*/
|
|
2263
|
+
toJSON() {
|
|
2264
|
+
const obj = {
|
|
2265
|
+
type: this.type
|
|
2266
|
+
};
|
|
2267
|
+
for (const prop in this) {
|
|
2268
|
+
if (this.hasOwnProperty(prop) && this[prop] !== undefined) {
|
|
2269
|
+
if (prop !== 'required' || this.type === exports.SchemaType.OBJECT) {
|
|
2270
|
+
obj[prop] = this[prop];
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
return obj;
|
|
2275
|
+
}
|
|
2276
|
+
static array(arrayParams) {
|
|
2277
|
+
return new ArraySchema(arrayParams, arrayParams.items);
|
|
2278
|
+
}
|
|
2279
|
+
static object(objectParams) {
|
|
2280
|
+
return new ObjectSchema(objectParams, objectParams.properties, objectParams.optionalProperties);
|
|
2281
|
+
}
|
|
2282
|
+
// eslint-disable-next-line id-blacklist
|
|
2283
|
+
static string(stringParams) {
|
|
2284
|
+
return new StringSchema(stringParams);
|
|
2285
|
+
}
|
|
2286
|
+
static enumString(stringParams) {
|
|
2287
|
+
return new StringSchema(stringParams, stringParams.enum);
|
|
2288
|
+
}
|
|
2289
|
+
static integer(integerParams) {
|
|
2290
|
+
return new IntegerSchema(integerParams);
|
|
2291
|
+
}
|
|
2292
|
+
// eslint-disable-next-line id-blacklist
|
|
2293
|
+
static number(numberParams) {
|
|
2294
|
+
return new NumberSchema(numberParams);
|
|
2295
|
+
}
|
|
2296
|
+
// eslint-disable-next-line id-blacklist
|
|
2297
|
+
static boolean(booleanParams) {
|
|
2298
|
+
return new BooleanSchema(booleanParams);
|
|
2299
|
+
}
|
|
2300
|
+
}
|
|
2301
|
+
/**
|
|
2302
|
+
* Schema class for "integer" types.
|
|
2303
|
+
* @public
|
|
2304
|
+
*/
|
|
2305
|
+
class IntegerSchema extends Schema {
|
|
2306
|
+
constructor(schemaParams) {
|
|
2307
|
+
super(Object.assign({ type: exports.SchemaType.INTEGER }, schemaParams));
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
/**
|
|
2311
|
+
* Schema class for "number" types.
|
|
2312
|
+
* @public
|
|
2313
|
+
*/
|
|
2314
|
+
class NumberSchema extends Schema {
|
|
2315
|
+
constructor(schemaParams) {
|
|
2316
|
+
super(Object.assign({ type: exports.SchemaType.NUMBER }, schemaParams));
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2319
|
+
/**
|
|
2320
|
+
* Schema class for "boolean" types.
|
|
2321
|
+
* @public
|
|
2322
|
+
*/
|
|
2323
|
+
class BooleanSchema extends Schema {
|
|
2324
|
+
constructor(schemaParams) {
|
|
2325
|
+
super(Object.assign({ type: exports.SchemaType.BOOLEAN }, schemaParams));
|
|
2326
|
+
}
|
|
2327
|
+
}
|
|
2328
|
+
/**
|
|
2329
|
+
* Schema class for "string" types. Can be used with or without
|
|
2330
|
+
* enum values.
|
|
2331
|
+
* @public
|
|
2332
|
+
*/
|
|
2333
|
+
class StringSchema extends Schema {
|
|
2334
|
+
constructor(schemaParams, enumValues) {
|
|
2335
|
+
super(Object.assign({ type: exports.SchemaType.STRING }, schemaParams));
|
|
2336
|
+
this.enum = enumValues;
|
|
2337
|
+
}
|
|
2338
|
+
/**
|
|
2339
|
+
* @internal
|
|
2340
|
+
*/
|
|
2341
|
+
toJSON() {
|
|
2342
|
+
const obj = super.toJSON();
|
|
2343
|
+
if (this.enum) {
|
|
2344
|
+
obj['enum'] = this.enum;
|
|
2345
|
+
}
|
|
2346
|
+
return obj;
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
/**
|
|
2350
|
+
* Schema class for "array" types.
|
|
2351
|
+
* The `items` param should refer to the type of item that can be a member
|
|
2352
|
+
* of the array.
|
|
2353
|
+
* @public
|
|
2354
|
+
*/
|
|
2355
|
+
class ArraySchema extends Schema {
|
|
2356
|
+
constructor(schemaParams, items) {
|
|
2357
|
+
super(Object.assign({ type: exports.SchemaType.ARRAY }, schemaParams));
|
|
2358
|
+
this.items = items;
|
|
2359
|
+
}
|
|
2360
|
+
/**
|
|
2361
|
+
* @internal
|
|
2362
|
+
*/
|
|
2363
|
+
toJSON() {
|
|
2364
|
+
const obj = super.toJSON();
|
|
2365
|
+
obj.items = this.items.toJSON();
|
|
2366
|
+
return obj;
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
/**
|
|
2370
|
+
* Schema class for "object" types.
|
|
2371
|
+
* The `properties` param must be a map of `Schema` objects.
|
|
2372
|
+
* @public
|
|
2373
|
+
*/
|
|
2374
|
+
class ObjectSchema extends Schema {
|
|
2375
|
+
constructor(schemaParams, properties, optionalProperties = []) {
|
|
2376
|
+
super(Object.assign({ type: exports.SchemaType.OBJECT }, schemaParams));
|
|
2377
|
+
this.properties = properties;
|
|
2378
|
+
this.optionalProperties = optionalProperties;
|
|
2379
|
+
}
|
|
2380
|
+
/**
|
|
2381
|
+
* @internal
|
|
2382
|
+
*/
|
|
2383
|
+
toJSON() {
|
|
2384
|
+
const obj = super.toJSON();
|
|
2385
|
+
obj.properties = Object.assign({}, this.properties);
|
|
2386
|
+
const required = [];
|
|
2387
|
+
if (this.optionalProperties) {
|
|
2388
|
+
for (const propertyKey of this.optionalProperties) {
|
|
2389
|
+
if (!this.properties.hasOwnProperty(propertyKey)) {
|
|
2390
|
+
throw new AIError("invalid-schema" /* AIErrorCode.INVALID_SCHEMA */, `Property "${propertyKey}" specified in "optionalProperties" does not exist.`);
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
}
|
|
2394
|
+
for (const propertyKey in this.properties) {
|
|
2395
|
+
if (this.properties.hasOwnProperty(propertyKey)) {
|
|
2396
|
+
obj.properties[propertyKey] = this.properties[propertyKey].toJSON();
|
|
2397
|
+
if (!this.optionalProperties.includes(propertyKey)) {
|
|
2398
|
+
required.push(propertyKey);
|
|
2399
|
+
}
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
if (required.length > 0) {
|
|
2403
|
+
obj.required = required;
|
|
2404
|
+
}
|
|
2405
|
+
delete obj.optionalProperties;
|
|
2406
|
+
return obj;
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
/**
|
|
2411
|
+
* @license
|
|
2412
|
+
* Copyright 2025 Google LLC
|
|
2413
|
+
*
|
|
2414
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2415
|
+
* you may not use this file except in compliance with the License.
|
|
2416
|
+
* You may obtain a copy of the License at
|
|
2417
|
+
*
|
|
2418
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2419
|
+
*
|
|
2420
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2421
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2422
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2423
|
+
* See the License for the specific language governing permissions and
|
|
2424
|
+
* limitations under the License.
|
|
2425
|
+
*/
|
|
2426
|
+
/**
|
|
2427
|
+
* Defines the image format for images generated by Imagen.
|
|
2428
|
+
*
|
|
2429
|
+
* Use this class to specify the desired format (JPEG or PNG) and compression quality
|
|
2430
|
+
* for images generated by Imagen. This is typically included as part of
|
|
2431
|
+
* {@link ImagenModelParams}.
|
|
2432
|
+
*
|
|
2433
|
+
* @example
|
|
2434
|
+
* ```javascript
|
|
2435
|
+
* const imagenModelParams = {
|
|
2436
|
+
* // ... other ImagenModelParams
|
|
2437
|
+
* imageFormat: ImagenImageFormat.jpeg(75) // JPEG with a compression level of 75.
|
|
2438
|
+
* }
|
|
2439
|
+
* ```
|
|
2440
|
+
*
|
|
2441
|
+
* @beta
|
|
2442
|
+
*/
|
|
2443
|
+
class ImagenImageFormat {
|
|
2444
|
+
constructor() {
|
|
2445
|
+
this.mimeType = 'image/png';
|
|
2446
|
+
}
|
|
2447
|
+
/**
|
|
2448
|
+
* Creates an {@link ImagenImageFormat} for a JPEG image.
|
|
2449
|
+
*
|
|
2450
|
+
* @param compressionQuality - The level of compression (a number between 0 and 100).
|
|
2451
|
+
* @returns An {@link ImagenImageFormat} object for a JPEG image.
|
|
2452
|
+
*
|
|
2453
|
+
* @beta
|
|
2454
|
+
*/
|
|
2455
|
+
static jpeg(compressionQuality) {
|
|
2456
|
+
if (compressionQuality &&
|
|
2457
|
+
(compressionQuality < 0 || compressionQuality > 100)) {
|
|
2458
|
+
logger.warn(`Invalid JPEG compression quality of ${compressionQuality} specified; the supported range is [0, 100].`);
|
|
2459
|
+
}
|
|
2460
|
+
return { mimeType: 'image/jpeg', compressionQuality };
|
|
2461
|
+
}
|
|
2462
|
+
/**
|
|
2463
|
+
* Creates an {@link ImagenImageFormat} for a PNG image.
|
|
2464
|
+
*
|
|
2465
|
+
* @returns An {@link ImagenImageFormat} object for a PNG image.
|
|
2466
|
+
*
|
|
2467
|
+
* @beta
|
|
2468
|
+
*/
|
|
2469
|
+
static png() {
|
|
2470
|
+
return { mimeType: 'image/png' };
|
|
2471
|
+
}
|
|
2472
|
+
}
|
|
2473
|
+
|
|
2474
|
+
/**
|
|
2475
|
+
* @license
|
|
2476
|
+
* Copyright 2024 Google LLC
|
|
2477
|
+
*
|
|
2478
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2479
|
+
* you may not use this file except in compliance with the License.
|
|
2480
|
+
* You may obtain a copy of the License at
|
|
2481
|
+
*
|
|
2482
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2483
|
+
*
|
|
2484
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2485
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2486
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2487
|
+
* See the License for the specific language governing permissions and
|
|
2488
|
+
* limitations under the License.
|
|
2489
|
+
*/
|
|
2490
|
+
/**
|
|
2491
|
+
* @deprecated Use the new {@link AIModel} instead. The Vertex AI in Firebase SDK has been
|
|
2492
|
+
* replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
|
|
2493
|
+
* services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
|
|
2494
|
+
*
|
|
2495
|
+
* Base class for Firebase AI model APIs.
|
|
2496
|
+
*
|
|
2497
|
+
* @public
|
|
2498
|
+
*/
|
|
2499
|
+
const VertexAIModel = AIModel;
|
|
2500
|
+
/**
|
|
2501
|
+
* @deprecated Use the new {@link AIError} instead. The Vertex AI in Firebase SDK has been
|
|
2502
|
+
* replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
|
|
2503
|
+
* services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
|
|
2504
|
+
*
|
|
2505
|
+
* Error class for the Firebase AI SDK.
|
|
2506
|
+
*
|
|
2507
|
+
* @public
|
|
2508
|
+
*/
|
|
2509
|
+
const VertexAIError = AIError;
|
|
2510
|
+
/**
|
|
2511
|
+
* @deprecated Use the new {@link getAI | getAI()} instead. The Vertex AI in Firebase SDK has been
|
|
2512
|
+
* replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
|
|
2513
|
+
* services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
|
|
2514
|
+
*
|
|
2515
|
+
* Returns a {@link VertexAI} instance for the given app, configured to use the
|
|
2516
|
+
* Vertex AI Gemini API. This instance will be
|
|
2517
|
+
* configured to use the Vertex AI Gemini API.
|
|
2518
|
+
*
|
|
2519
|
+
* @param app - The {@link @firebase/app#FirebaseApp} to use.
|
|
2520
|
+
* @param options - Options to configure the Vertex AI instance, including the location.
|
|
2521
|
+
*
|
|
2522
|
+
* @public
|
|
2523
|
+
*/
|
|
2524
|
+
function getVertexAI(app$1 = app.getApp(), options) {
|
|
2525
|
+
app$1 = util.getModularInstance(app$1);
|
|
2526
|
+
// Dependencies
|
|
2527
|
+
const AIProvider = app._getProvider(app$1, AI_TYPE);
|
|
2528
|
+
const backend = new VertexAIBackend(options === null || options === void 0 ? void 0 : options.location);
|
|
2529
|
+
const identifier = encodeInstanceIdentifier(backend);
|
|
2530
|
+
return AIProvider.getImmediate({
|
|
2531
|
+
identifier
|
|
2532
|
+
});
|
|
2533
|
+
}
|
|
2534
|
+
/**
|
|
2535
|
+
* Returns the default {@link AI} instance that is associated with the provided
|
|
2536
|
+
* {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new instance with the
|
|
2537
|
+
* default settings.
|
|
2538
|
+
*
|
|
2539
|
+
* @example
|
|
2540
|
+
* ```javascript
|
|
2541
|
+
* const ai = getAI(app);
|
|
2542
|
+
* ```
|
|
2543
|
+
*
|
|
2544
|
+
* @example
|
|
2545
|
+
* ```javascript
|
|
2546
|
+
* // Get an AI instance configured to use the Gemini Developer API (via Google AI).
|
|
2547
|
+
* const ai = getAI(app, { backend: new GoogleAIBackend() });
|
|
2548
|
+
* ```
|
|
2549
|
+
*
|
|
2550
|
+
* @example
|
|
2551
|
+
* ```javascript
|
|
2552
|
+
* // Get an AI instance configured to use the Vertex AI Gemini API.
|
|
2553
|
+
* const ai = getAI(app, { backend: new VertexAIBackend() });
|
|
2554
|
+
* ```
|
|
2555
|
+
*
|
|
2556
|
+
* @param app - The {@link @firebase/app#FirebaseApp} to use.
|
|
2557
|
+
* @param options - {@link AIOptions} that configure the AI instance.
|
|
2558
|
+
* @returns The default {@link AI} instance for the given {@link @firebase/app#FirebaseApp}.
|
|
2559
|
+
*
|
|
2560
|
+
* @public
|
|
2561
|
+
*/
|
|
2562
|
+
function getAI(app$1 = app.getApp(), options = { backend: new GoogleAIBackend() }) {
|
|
2563
|
+
app$1 = util.getModularInstance(app$1);
|
|
2564
|
+
// Dependencies
|
|
2565
|
+
const AIProvider = app._getProvider(app$1, AI_TYPE);
|
|
2566
|
+
const identifier = encodeInstanceIdentifier(options.backend);
|
|
2567
|
+
return AIProvider.getImmediate({
|
|
2568
|
+
identifier
|
|
2569
|
+
});
|
|
2570
|
+
}
|
|
2571
|
+
/**
|
|
2572
|
+
* Returns a {@link GenerativeModel} class with methods for inference
|
|
2573
|
+
* and other functionality.
|
|
2574
|
+
*
|
|
2575
|
+
* @public
|
|
2576
|
+
*/
|
|
2577
|
+
function getGenerativeModel(ai, modelParams, requestOptions) {
|
|
2578
|
+
if (!modelParams.model) {
|
|
2579
|
+
throw new AIError("no-model" /* AIErrorCode.NO_MODEL */, `Must provide a model name. Example: getGenerativeModel({ model: 'my-model-name' })`);
|
|
2580
|
+
}
|
|
2581
|
+
return new GenerativeModel(ai, modelParams, requestOptions);
|
|
2582
|
+
}
|
|
2583
|
+
/**
|
|
2584
|
+
* Returns an {@link ImagenModel} class with methods for using Imagen.
|
|
2585
|
+
*
|
|
2586
|
+
* Only Imagen 3 models (named `imagen-3.0-*`) are supported.
|
|
2587
|
+
*
|
|
2588
|
+
* @param ai - An {@link AI} instance.
|
|
2589
|
+
* @param modelParams - Parameters to use when making Imagen requests.
|
|
2590
|
+
* @param requestOptions - Additional options to use when making requests.
|
|
2591
|
+
*
|
|
2592
|
+
* @throws If the `apiKey` or `projectId` fields are missing in your
|
|
2593
|
+
* Firebase config.
|
|
2594
|
+
*
|
|
2595
|
+
* @beta
|
|
2596
|
+
*/
|
|
2597
|
+
function getImagenModel(ai, modelParams, requestOptions) {
|
|
2598
|
+
if (!modelParams.model) {
|
|
2599
|
+
throw new AIError("no-model" /* AIErrorCode.NO_MODEL */, `Must provide a model name. Example: getImagenModel({ model: 'my-model-name' })`);
|
|
2600
|
+
}
|
|
2601
|
+
return new ImagenModel(ai, modelParams, requestOptions);
|
|
2602
|
+
}
|
|
2603
|
+
|
|
2604
|
+
/**
|
|
2605
|
+
* The Firebase AI Web SDK.
|
|
2606
|
+
*
|
|
2607
|
+
* @packageDocumentation
|
|
2608
|
+
*/
|
|
2609
|
+
function registerAI() {
|
|
2610
|
+
app._registerComponent(new component.Component(AI_TYPE, (container, { instanceIdentifier }) => {
|
|
2611
|
+
if (!instanceIdentifier) {
|
|
2612
|
+
throw new AIError("error" /* AIErrorCode.ERROR */, 'AIService instance identifier is undefined.');
|
|
2613
|
+
}
|
|
2614
|
+
const backend = decodeInstanceIdentifier(instanceIdentifier);
|
|
2615
|
+
// getImmediate for FirebaseApp will always succeed
|
|
2616
|
+
const app = container.getProvider('app').getImmediate();
|
|
2617
|
+
const auth = container.getProvider('auth-internal');
|
|
2618
|
+
const appCheckProvider = container.getProvider('app-check-internal');
|
|
2619
|
+
return new AIService(app, backend, auth, appCheckProvider);
|
|
2620
|
+
}, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
|
|
2621
|
+
app.registerVersion(name, version);
|
|
2622
|
+
// BUILD_TARGET will be replaced by values like esm2017, cjs2017, etc during the compilation
|
|
2623
|
+
app.registerVersion(name, version, 'cjs2017');
|
|
2624
|
+
}
|
|
2625
|
+
registerAI();
|
|
2626
|
+
|
|
2627
|
+
exports.AIError = AIError;
|
|
2628
|
+
exports.AIModel = AIModel;
|
|
2629
|
+
exports.ArraySchema = ArraySchema;
|
|
2630
|
+
exports.Backend = Backend;
|
|
2631
|
+
exports.BackendType = BackendType;
|
|
2632
|
+
exports.BooleanSchema = BooleanSchema;
|
|
2633
|
+
exports.ChatSession = ChatSession;
|
|
2634
|
+
exports.GenerativeModel = GenerativeModel;
|
|
2635
|
+
exports.GoogleAIBackend = GoogleAIBackend;
|
|
2636
|
+
exports.ImagenImageFormat = ImagenImageFormat;
|
|
2637
|
+
exports.ImagenModel = ImagenModel;
|
|
2638
|
+
exports.IntegerSchema = IntegerSchema;
|
|
2639
|
+
exports.NumberSchema = NumberSchema;
|
|
2640
|
+
exports.ObjectSchema = ObjectSchema;
|
|
2641
|
+
exports.POSSIBLE_ROLES = POSSIBLE_ROLES;
|
|
2642
|
+
exports.ResponseModality = ResponseModality;
|
|
2643
|
+
exports.Schema = Schema;
|
|
2644
|
+
exports.StringSchema = StringSchema;
|
|
2645
|
+
exports.VertexAIBackend = VertexAIBackend;
|
|
2646
|
+
exports.VertexAIError = VertexAIError;
|
|
2647
|
+
exports.VertexAIModel = VertexAIModel;
|
|
2648
|
+
exports.getAI = getAI;
|
|
2649
|
+
exports.getGenerativeModel = getGenerativeModel;
|
|
2650
|
+
exports.getImagenModel = getImagenModel;
|
|
2651
|
+
exports.getVertexAI = getVertexAI;
|
|
2652
|
+
//# sourceMappingURL=index.cjs.js.map
|