@firebase/ai 1.4.1-canary.2b5731292 → 1.4.1-canary.5200f7bb7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-public.d.ts +34 -26
- package/dist/ai.d.ts +37 -26
- package/dist/esm/{index.esm2017.js → index.esm.js} +170 -93
- package/dist/esm/index.esm.js.map +1 -0
- package/dist/esm/src/requests/schema-builder.d.ts +23 -4
- package/dist/esm/src/types/requests.d.ts +1 -1
- package/dist/esm/src/types/responses.d.ts +0 -14
- package/dist/esm/src/types/schema.d.ts +12 -6
- package/dist/index.cjs.js +169 -91
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.node.cjs.js +169 -91
- package/dist/index.node.cjs.js.map +1 -1
- package/dist/index.node.mjs +169 -92
- package/dist/index.node.mjs.map +1 -1
- package/dist/src/requests/schema-builder.d.ts +23 -4
- package/dist/src/types/requests.d.ts +1 -1
- package/dist/src/types/responses.d.ts +0 -14
- package/dist/src/types/schema.d.ts +12 -6
- package/package.json +12 -12
- package/dist/esm/index.esm2017.js.map +0 -1
|
@@ -2,10 +2,9 @@ import { _isFirebaseServerApp, _getProvider, getApp, _registerComponent, registe
|
|
|
2
2
|
import { Component } from '@firebase/component';
|
|
3
3
|
import { FirebaseError, getModularInstance } from '@firebase/util';
|
|
4
4
|
import { Logger } from '@firebase/logger';
|
|
5
|
-
import { __asyncGenerator, __await } from 'tslib';
|
|
6
5
|
|
|
7
6
|
var name = "@firebase/ai";
|
|
8
|
-
var version = "1.4.1-canary.
|
|
7
|
+
var version = "1.4.1-canary.5200f7bb7";
|
|
9
8
|
|
|
10
9
|
/**
|
|
11
10
|
* @license
|
|
@@ -626,8 +625,8 @@ class AIService {
|
|
|
626
625
|
constructor(app, backend, authProvider, appCheckProvider) {
|
|
627
626
|
this.app = app;
|
|
628
627
|
this.backend = backend;
|
|
629
|
-
const appCheck = appCheckProvider
|
|
630
|
-
const auth = authProvider
|
|
628
|
+
const appCheck = appCheckProvider?.getImmediate({ optional: true });
|
|
629
|
+
const auth = authProvider?.getImmediate({ optional: true });
|
|
631
630
|
this.auth = auth || null;
|
|
632
631
|
this.appCheck = appCheck || null;
|
|
633
632
|
if (backend instanceof VertexAIBackend) {
|
|
@@ -799,14 +798,13 @@ class AIModel {
|
|
|
799
798
|
* @internal
|
|
800
799
|
*/
|
|
801
800
|
constructor(ai, modelName) {
|
|
802
|
-
|
|
803
|
-
if (!((_b = (_a = ai.app) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.apiKey)) {
|
|
801
|
+
if (!ai.app?.options?.apiKey) {
|
|
804
802
|
throw new AIError(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.`);
|
|
805
803
|
}
|
|
806
|
-
else if (!
|
|
804
|
+
else if (!ai.app?.options?.projectId) {
|
|
807
805
|
throw new AIError(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.`);
|
|
808
806
|
}
|
|
809
|
-
else if (!
|
|
807
|
+
else if (!ai.app?.options?.appId) {
|
|
810
808
|
throw new AIError(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.`);
|
|
811
809
|
}
|
|
812
810
|
else {
|
|
@@ -934,8 +932,7 @@ class RequestUrl {
|
|
|
934
932
|
return url.toString();
|
|
935
933
|
}
|
|
936
934
|
get baseUrl() {
|
|
937
|
-
|
|
938
|
-
return ((_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.baseUrl) || DEFAULT_BASE_URL;
|
|
935
|
+
return this.requestOptions?.baseUrl || DEFAULT_BASE_URL;
|
|
939
936
|
}
|
|
940
937
|
get apiVersion() {
|
|
941
938
|
return DEFAULT_API_VERSION; // TODO: allow user-set options if that feature becomes available
|
|
@@ -1011,7 +1008,7 @@ async function makeRequest(model, task, apiSettings, stream, body, requestOption
|
|
|
1011
1008
|
try {
|
|
1012
1009
|
const request = await constructRequest(model, task, apiSettings, stream, body, requestOptions);
|
|
1013
1010
|
// Timeout is 180s by default
|
|
1014
|
-
const timeoutMillis =
|
|
1011
|
+
const timeoutMillis = requestOptions?.timeout != null && requestOptions.timeout >= 0
|
|
1015
1012
|
? requestOptions.timeout
|
|
1016
1013
|
: DEFAULT_FETCH_TIMEOUT_MS;
|
|
1017
1014
|
const abortController = new AbortController();
|
|
@@ -1035,10 +1032,7 @@ async function makeRequest(model, task, apiSettings, stream, body, requestOption
|
|
|
1035
1032
|
if (response.status === 403 &&
|
|
1036
1033
|
errorDetails &&
|
|
1037
1034
|
errorDetails.some((detail) => detail.reason === 'SERVICE_DISABLED') &&
|
|
1038
|
-
errorDetails.some((detail) => {
|
|
1039
|
-
var _a, _b;
|
|
1040
|
-
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');
|
|
1041
|
-
})) {
|
|
1035
|
+
errorDetails.some((detail) => detail.links?.[0]?.description.includes('Google developers console API activation'))) {
|
|
1042
1036
|
throw new AIError(AIErrorCode.API_NOT_ENABLED, `The Firebase AI SDK requires the Firebase AI ` +
|
|
1043
1037
|
`API ('firebasevertexai.googleapis.com') to be enabled in your ` +
|
|
1044
1038
|
`Firebase project. Enable this API by visiting the Firebase Console ` +
|
|
@@ -1183,10 +1177,9 @@ function addHelpers(response) {
|
|
|
1183
1177
|
* Returns all text found in all parts of first candidate.
|
|
1184
1178
|
*/
|
|
1185
1179
|
function getText(response) {
|
|
1186
|
-
var _a, _b, _c, _d;
|
|
1187
1180
|
const textStrings = [];
|
|
1188
|
-
if (
|
|
1189
|
-
for (const part of
|
|
1181
|
+
if (response.candidates?.[0].content?.parts) {
|
|
1182
|
+
for (const part of response.candidates?.[0].content?.parts) {
|
|
1190
1183
|
if (part.text) {
|
|
1191
1184
|
textStrings.push(part.text);
|
|
1192
1185
|
}
|
|
@@ -1203,10 +1196,9 @@ function getText(response) {
|
|
|
1203
1196
|
* Returns {@link FunctionCall}s associated with first candidate.
|
|
1204
1197
|
*/
|
|
1205
1198
|
function getFunctionCalls(response) {
|
|
1206
|
-
var _a, _b, _c, _d;
|
|
1207
1199
|
const functionCalls = [];
|
|
1208
|
-
if (
|
|
1209
|
-
for (const part of
|
|
1200
|
+
if (response.candidates?.[0].content?.parts) {
|
|
1201
|
+
for (const part of response.candidates?.[0].content?.parts) {
|
|
1210
1202
|
if (part.functionCall) {
|
|
1211
1203
|
functionCalls.push(part.functionCall);
|
|
1212
1204
|
}
|
|
@@ -1225,10 +1217,9 @@ function getFunctionCalls(response) {
|
|
|
1225
1217
|
* @internal
|
|
1226
1218
|
*/
|
|
1227
1219
|
function getInlineDataParts(response) {
|
|
1228
|
-
var _a, _b, _c, _d;
|
|
1229
1220
|
const data = [];
|
|
1230
|
-
if (
|
|
1231
|
-
for (const part of
|
|
1221
|
+
if (response.candidates?.[0].content?.parts) {
|
|
1222
|
+
for (const part of response.candidates?.[0].content?.parts) {
|
|
1232
1223
|
if (part.inlineData) {
|
|
1233
1224
|
data.push(part);
|
|
1234
1225
|
}
|
|
@@ -1247,19 +1238,18 @@ function hadBadFinishReason(candidate) {
|
|
|
1247
1238
|
badFinishReasons.some(reason => reason === candidate.finishReason));
|
|
1248
1239
|
}
|
|
1249
1240
|
function formatBlockErrorMessage(response) {
|
|
1250
|
-
var _a, _b, _c;
|
|
1251
1241
|
let message = '';
|
|
1252
1242
|
if ((!response.candidates || response.candidates.length === 0) &&
|
|
1253
1243
|
response.promptFeedback) {
|
|
1254
1244
|
message += 'Response was blocked';
|
|
1255
|
-
if (
|
|
1245
|
+
if (response.promptFeedback?.blockReason) {
|
|
1256
1246
|
message += ` due to ${response.promptFeedback.blockReason}`;
|
|
1257
1247
|
}
|
|
1258
|
-
if (
|
|
1248
|
+
if (response.promptFeedback?.blockReasonMessage) {
|
|
1259
1249
|
message += `: ${response.promptFeedback.blockReasonMessage}`;
|
|
1260
1250
|
}
|
|
1261
1251
|
}
|
|
1262
|
-
else if (
|
|
1252
|
+
else if (response.candidates?.[0]) {
|
|
1263
1253
|
const firstCandidate = response.candidates[0];
|
|
1264
1254
|
if (hadBadFinishReason(firstCandidate)) {
|
|
1265
1255
|
message += `Candidate was blocked due to ${firstCandidate.finishReason}`;
|
|
@@ -1278,12 +1268,11 @@ function formatBlockErrorMessage(response) {
|
|
|
1278
1268
|
* @internal
|
|
1279
1269
|
*/
|
|
1280
1270
|
async function handlePredictResponse(response) {
|
|
1281
|
-
var _a;
|
|
1282
1271
|
const responseJson = await response.json();
|
|
1283
1272
|
const images = [];
|
|
1284
1273
|
let filteredReason = undefined;
|
|
1285
1274
|
// The backend should always send a non-empty array of predictions if the response was successful.
|
|
1286
|
-
if (!responseJson.predictions ||
|
|
1275
|
+
if (!responseJson.predictions || responseJson.predictions?.length === 0) {
|
|
1287
1276
|
throw new AIError(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.');
|
|
1288
1277
|
}
|
|
1289
1278
|
for (const prediction of responseJson.predictions) {
|
|
@@ -1347,13 +1336,12 @@ async function handlePredictResponse(response) {
|
|
|
1347
1336
|
* @internal
|
|
1348
1337
|
*/
|
|
1349
1338
|
function mapGenerateContentRequest(generateContentRequest) {
|
|
1350
|
-
|
|
1351
|
-
(_a = generateContentRequest.safetySettings) === null || _a === void 0 ? void 0 : _a.forEach(safetySetting => {
|
|
1339
|
+
generateContentRequest.safetySettings?.forEach(safetySetting => {
|
|
1352
1340
|
if (safetySetting.method) {
|
|
1353
1341
|
throw new AIError(AIErrorCode.UNSUPPORTED, 'SafetySetting.method is not supported in the the Gemini Developer API. Please remove this property.');
|
|
1354
1342
|
}
|
|
1355
1343
|
});
|
|
1356
|
-
if (
|
|
1344
|
+
if (generateContentRequest.generationConfig?.topK) {
|
|
1357
1345
|
const roundedTopK = Math.round(generateContentRequest.generationConfig.topK);
|
|
1358
1346
|
if (roundedTopK !== generateContentRequest.generationConfig.topK) {
|
|
1359
1347
|
logger.warn('topK in GenerationConfig has been rounded to the nearest integer to match the format for requests to the Gemini Developer API.');
|
|
@@ -1394,7 +1382,10 @@ function mapGenerateContentResponse(googleAIResponse) {
|
|
|
1394
1382
|
*/
|
|
1395
1383
|
function mapCountTokensRequest(countTokensRequest, model) {
|
|
1396
1384
|
const mappedCountTokensRequest = {
|
|
1397
|
-
generateContentRequest:
|
|
1385
|
+
generateContentRequest: {
|
|
1386
|
+
model,
|
|
1387
|
+
...countTokensRequest
|
|
1388
|
+
}
|
|
1398
1389
|
};
|
|
1399
1390
|
return mappedCountTokensRequest;
|
|
1400
1391
|
}
|
|
@@ -1414,7 +1405,6 @@ function mapGenerateContentCandidates(candidates) {
|
|
|
1414
1405
|
let mappedSafetyRatings;
|
|
1415
1406
|
if (mappedCandidates) {
|
|
1416
1407
|
candidates.forEach(candidate => {
|
|
1417
|
-
var _a;
|
|
1418
1408
|
// Map citationSources to citations.
|
|
1419
1409
|
let citationMetadata;
|
|
1420
1410
|
if (candidate.citationMetadata) {
|
|
@@ -1425,14 +1415,18 @@ function mapGenerateContentCandidates(candidates) {
|
|
|
1425
1415
|
// Assign missing candidate SafetyRatings properties to their defaults if undefined.
|
|
1426
1416
|
if (candidate.safetyRatings) {
|
|
1427
1417
|
mappedSafetyRatings = candidate.safetyRatings.map(safetyRating => {
|
|
1428
|
-
|
|
1429
|
-
|
|
1418
|
+
return {
|
|
1419
|
+
...safetyRating,
|
|
1420
|
+
severity: safetyRating.severity ?? HarmSeverity.HARM_SEVERITY_UNSUPPORTED,
|
|
1421
|
+
probabilityScore: safetyRating.probabilityScore ?? 0,
|
|
1422
|
+
severityScore: safetyRating.severityScore ?? 0
|
|
1423
|
+
};
|
|
1430
1424
|
});
|
|
1431
1425
|
}
|
|
1432
1426
|
// videoMetadata is not supported.
|
|
1433
1427
|
// Throw early since developers may send a long video as input and only expect to pay
|
|
1434
1428
|
// for inference on a small portion of the video.
|
|
1435
|
-
if (
|
|
1429
|
+
if (candidate.content?.parts.some(part => part?.videoMetadata)) {
|
|
1436
1430
|
throw new AIError(AIErrorCode.UNSUPPORTED, 'Part.videoMetadata is not supported in the Gemini Developer API. Please remove this property.');
|
|
1437
1431
|
}
|
|
1438
1432
|
const mappedCandidate = {
|
|
@@ -1453,13 +1447,12 @@ function mapPromptFeedback(promptFeedback) {
|
|
|
1453
1447
|
// Assign missing SafetyRating properties to their defaults if undefined.
|
|
1454
1448
|
const mappedSafetyRatings = [];
|
|
1455
1449
|
promptFeedback.safetyRatings.forEach(safetyRating => {
|
|
1456
|
-
var _a, _b, _c;
|
|
1457
1450
|
mappedSafetyRatings.push({
|
|
1458
1451
|
category: safetyRating.category,
|
|
1459
1452
|
probability: safetyRating.probability,
|
|
1460
|
-
severity:
|
|
1461
|
-
probabilityScore:
|
|
1462
|
-
severityScore:
|
|
1453
|
+
severity: safetyRating.severity ?? HarmSeverity.HARM_SEVERITY_UNSUPPORTED,
|
|
1454
|
+
probabilityScore: safetyRating.probabilityScore ?? 0,
|
|
1455
|
+
severityScore: safetyRating.severityScore ?? 0,
|
|
1463
1456
|
blocked: safetyRating.blocked
|
|
1464
1457
|
});
|
|
1465
1458
|
});
|
|
@@ -1520,24 +1513,22 @@ async function getResponsePromise(stream, apiSettings) {
|
|
|
1520
1513
|
allResponses.push(value);
|
|
1521
1514
|
}
|
|
1522
1515
|
}
|
|
1523
|
-
function generateResponseSequence(stream, apiSettings) {
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
break;
|
|
1530
|
-
}
|
|
1531
|
-
let enhancedResponse;
|
|
1532
|
-
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1533
|
-
enhancedResponse = createEnhancedContentResponse(mapGenerateContentResponse(value));
|
|
1534
|
-
}
|
|
1535
|
-
else {
|
|
1536
|
-
enhancedResponse = createEnhancedContentResponse(value);
|
|
1537
|
-
}
|
|
1538
|
-
yield yield __await(enhancedResponse);
|
|
1516
|
+
async function* generateResponseSequence(stream, apiSettings) {
|
|
1517
|
+
const reader = stream.getReader();
|
|
1518
|
+
while (true) {
|
|
1519
|
+
const { value, done } = await reader.read();
|
|
1520
|
+
if (done) {
|
|
1521
|
+
break;
|
|
1539
1522
|
}
|
|
1540
|
-
|
|
1523
|
+
let enhancedResponse;
|
|
1524
|
+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1525
|
+
enhancedResponse = createEnhancedContentResponse(mapGenerateContentResponse(value));
|
|
1526
|
+
}
|
|
1527
|
+
else {
|
|
1528
|
+
enhancedResponse = createEnhancedContentResponse(value);
|
|
1529
|
+
}
|
|
1530
|
+
yield enhancedResponse;
|
|
1531
|
+
}
|
|
1541
1532
|
}
|
|
1542
1533
|
/**
|
|
1543
1534
|
* Reads a raw stream from the fetch response and join incomplete
|
|
@@ -1589,7 +1580,7 @@ function getResponseStream(inputStream) {
|
|
|
1589
1580
|
function aggregateResponses(responses) {
|
|
1590
1581
|
const lastResponse = responses[responses.length - 1];
|
|
1591
1582
|
const aggregatedResponse = {
|
|
1592
|
-
promptFeedback: lastResponse
|
|
1583
|
+
promptFeedback: lastResponse?.promptFeedback
|
|
1593
1584
|
};
|
|
1594
1585
|
for (const response of responses) {
|
|
1595
1586
|
if (response.candidates) {
|
|
@@ -1946,7 +1937,7 @@ class ChatSession {
|
|
|
1946
1937
|
this._history = [];
|
|
1947
1938
|
this._sendPromise = Promise.resolve();
|
|
1948
1939
|
this._apiSettings = apiSettings;
|
|
1949
|
-
if (params
|
|
1940
|
+
if (params?.history) {
|
|
1950
1941
|
validateChatHistory(params.history);
|
|
1951
1942
|
this._history = params.history;
|
|
1952
1943
|
}
|
|
@@ -1965,15 +1956,14 @@ class ChatSession {
|
|
|
1965
1956
|
* {@link GenerateContentResult}
|
|
1966
1957
|
*/
|
|
1967
1958
|
async sendMessage(request) {
|
|
1968
|
-
var _a, _b, _c, _d, _e;
|
|
1969
1959
|
await this._sendPromise;
|
|
1970
1960
|
const newContent = formatNewContent(request);
|
|
1971
1961
|
const generateContentRequest = {
|
|
1972
|
-
safetySettings:
|
|
1973
|
-
generationConfig:
|
|
1974
|
-
tools:
|
|
1975
|
-
toolConfig:
|
|
1976
|
-
systemInstruction:
|
|
1962
|
+
safetySettings: this.params?.safetySettings,
|
|
1963
|
+
generationConfig: this.params?.generationConfig,
|
|
1964
|
+
tools: this.params?.tools,
|
|
1965
|
+
toolConfig: this.params?.toolConfig,
|
|
1966
|
+
systemInstruction: this.params?.systemInstruction,
|
|
1977
1967
|
contents: [...this._history, newContent]
|
|
1978
1968
|
};
|
|
1979
1969
|
let finalResult = {};
|
|
@@ -1981,14 +1971,13 @@ class ChatSession {
|
|
|
1981
1971
|
this._sendPromise = this._sendPromise
|
|
1982
1972
|
.then(() => generateContent(this._apiSettings, this.model, generateContentRequest, this.requestOptions))
|
|
1983
1973
|
.then(result => {
|
|
1984
|
-
var _a, _b;
|
|
1985
1974
|
if (result.response.candidates &&
|
|
1986
1975
|
result.response.candidates.length > 0) {
|
|
1987
1976
|
this._history.push(newContent);
|
|
1988
1977
|
const responseContent = {
|
|
1989
|
-
parts:
|
|
1978
|
+
parts: result.response.candidates?.[0].content.parts || [],
|
|
1990
1979
|
// Response seems to come back without a role set.
|
|
1991
|
-
role:
|
|
1980
|
+
role: result.response.candidates?.[0].content.role || 'model'
|
|
1992
1981
|
};
|
|
1993
1982
|
this._history.push(responseContent);
|
|
1994
1983
|
}
|
|
@@ -2009,15 +1998,14 @@ class ChatSession {
|
|
|
2009
1998
|
* and a response promise.
|
|
2010
1999
|
*/
|
|
2011
2000
|
async sendMessageStream(request) {
|
|
2012
|
-
var _a, _b, _c, _d, _e;
|
|
2013
2001
|
await this._sendPromise;
|
|
2014
2002
|
const newContent = formatNewContent(request);
|
|
2015
2003
|
const generateContentRequest = {
|
|
2016
|
-
safetySettings:
|
|
2017
|
-
generationConfig:
|
|
2018
|
-
tools:
|
|
2019
|
-
toolConfig:
|
|
2020
|
-
systemInstruction:
|
|
2004
|
+
safetySettings: this.params?.safetySettings,
|
|
2005
|
+
generationConfig: this.params?.generationConfig,
|
|
2006
|
+
tools: this.params?.tools,
|
|
2007
|
+
toolConfig: this.params?.toolConfig,
|
|
2008
|
+
systemInstruction: this.params?.systemInstruction,
|
|
2021
2009
|
contents: [...this._history, newContent]
|
|
2022
2010
|
};
|
|
2023
2011
|
const streamPromise = generateContentStream(this._apiSettings, this.model, generateContentRequest, this.requestOptions);
|
|
@@ -2033,7 +2021,7 @@ class ChatSession {
|
|
|
2033
2021
|
.then(response => {
|
|
2034
2022
|
if (response.candidates && response.candidates.length > 0) {
|
|
2035
2023
|
this._history.push(newContent);
|
|
2036
|
-
const responseContent =
|
|
2024
|
+
const responseContent = { ...response.candidates[0].content };
|
|
2037
2025
|
// Response seems to come back without a role set.
|
|
2038
2026
|
if (!responseContent.role) {
|
|
2039
2027
|
responseContent.role = 'model';
|
|
@@ -2126,7 +2114,14 @@ class GenerativeModel extends AIModel {
|
|
|
2126
2114
|
*/
|
|
2127
2115
|
async generateContent(request) {
|
|
2128
2116
|
const formattedParams = formatGenerateContentInput(request);
|
|
2129
|
-
return generateContent(this._apiSettings, this.model,
|
|
2117
|
+
return generateContent(this._apiSettings, this.model, {
|
|
2118
|
+
generationConfig: this.generationConfig,
|
|
2119
|
+
safetySettings: this.safetySettings,
|
|
2120
|
+
tools: this.tools,
|
|
2121
|
+
toolConfig: this.toolConfig,
|
|
2122
|
+
systemInstruction: this.systemInstruction,
|
|
2123
|
+
...formattedParams
|
|
2124
|
+
}, this.requestOptions);
|
|
2130
2125
|
}
|
|
2131
2126
|
/**
|
|
2132
2127
|
* Makes a single streaming call to the model
|
|
@@ -2136,14 +2131,33 @@ class GenerativeModel extends AIModel {
|
|
|
2136
2131
|
*/
|
|
2137
2132
|
async generateContentStream(request) {
|
|
2138
2133
|
const formattedParams = formatGenerateContentInput(request);
|
|
2139
|
-
return generateContentStream(this._apiSettings, this.model,
|
|
2134
|
+
return generateContentStream(this._apiSettings, this.model, {
|
|
2135
|
+
generationConfig: this.generationConfig,
|
|
2136
|
+
safetySettings: this.safetySettings,
|
|
2137
|
+
tools: this.tools,
|
|
2138
|
+
toolConfig: this.toolConfig,
|
|
2139
|
+
systemInstruction: this.systemInstruction,
|
|
2140
|
+
...formattedParams
|
|
2141
|
+
}, this.requestOptions);
|
|
2140
2142
|
}
|
|
2141
2143
|
/**
|
|
2142
2144
|
* Gets a new {@link ChatSession} instance which can be used for
|
|
2143
2145
|
* multi-turn chats.
|
|
2144
2146
|
*/
|
|
2145
2147
|
startChat(startChatParams) {
|
|
2146
|
-
return new ChatSession(this._apiSettings, this.model,
|
|
2148
|
+
return new ChatSession(this._apiSettings, this.model, {
|
|
2149
|
+
tools: this.tools,
|
|
2150
|
+
toolConfig: this.toolConfig,
|
|
2151
|
+
systemInstruction: this.systemInstruction,
|
|
2152
|
+
generationConfig: this.generationConfig,
|
|
2153
|
+
safetySettings: this.safetySettings,
|
|
2154
|
+
/**
|
|
2155
|
+
* Overrides params inherited from GenerativeModel with those explicitly set in the
|
|
2156
|
+
* StartChatParams. For example, if startChatParams.generationConfig is set, it'll override
|
|
2157
|
+
* this.generationConfig.
|
|
2158
|
+
*/
|
|
2159
|
+
...startChatParams
|
|
2160
|
+
}, this.requestOptions);
|
|
2147
2161
|
}
|
|
2148
2162
|
/**
|
|
2149
2163
|
* Counts the tokens in the provided request.
|
|
@@ -2229,7 +2243,10 @@ class ImagenModel extends AIModel {
|
|
|
2229
2243
|
* @beta
|
|
2230
2244
|
*/
|
|
2231
2245
|
async generateImages(prompt) {
|
|
2232
|
-
const body = createPredictRequestBody(prompt,
|
|
2246
|
+
const body = createPredictRequestBody(prompt, {
|
|
2247
|
+
...this.generationConfig,
|
|
2248
|
+
...this.safetySettings
|
|
2249
|
+
});
|
|
2233
2250
|
const response = await makeRequest(this.model, Task.PREDICT, this._apiSettings,
|
|
2234
2251
|
/* stream */ false, JSON.stringify(body), this.requestOptions);
|
|
2235
2252
|
return handlePredictResponse(response);
|
|
@@ -2254,7 +2271,11 @@ class ImagenModel extends AIModel {
|
|
|
2254
2271
|
* If all images are filtered, the `images` array will be empty.
|
|
2255
2272
|
*/
|
|
2256
2273
|
async generateImagesGCS(prompt, gcsURI) {
|
|
2257
|
-
const body = createPredictRequestBody(prompt,
|
|
2274
|
+
const body = createPredictRequestBody(prompt, {
|
|
2275
|
+
gcsURI,
|
|
2276
|
+
...this.generationConfig,
|
|
2277
|
+
...this.safetySettings
|
|
2278
|
+
});
|
|
2258
2279
|
const response = await makeRequest(this.model, Task.PREDICT, this._apiSettings,
|
|
2259
2280
|
/* stream */ false, JSON.stringify(body), this.requestOptions);
|
|
2260
2281
|
return handlePredictResponse(response);
|
|
@@ -2286,12 +2307,19 @@ class ImagenModel extends AIModel {
|
|
|
2286
2307
|
*/
|
|
2287
2308
|
class Schema {
|
|
2288
2309
|
constructor(schemaParams) {
|
|
2310
|
+
// TODO(dlarocque): Enforce this with union types
|
|
2311
|
+
if (!schemaParams.type && !schemaParams.anyOf) {
|
|
2312
|
+
throw new AIError(AIErrorCode.INVALID_SCHEMA, "A schema must have either a 'type' or an 'anyOf' array of sub-schemas.");
|
|
2313
|
+
}
|
|
2289
2314
|
// eslint-disable-next-line guard-for-in
|
|
2290
2315
|
for (const paramKey in schemaParams) {
|
|
2291
2316
|
this[paramKey] = schemaParams[paramKey];
|
|
2292
2317
|
}
|
|
2293
2318
|
// Ensure these are explicitly set to avoid TS errors.
|
|
2294
2319
|
this.type = schemaParams.type;
|
|
2320
|
+
this.format = schemaParams.hasOwnProperty('format')
|
|
2321
|
+
? schemaParams.format
|
|
2322
|
+
: undefined;
|
|
2295
2323
|
this.nullable = schemaParams.hasOwnProperty('nullable')
|
|
2296
2324
|
? !!schemaParams.nullable
|
|
2297
2325
|
: false;
|
|
@@ -2338,6 +2366,9 @@ class Schema {
|
|
|
2338
2366
|
static boolean(booleanParams) {
|
|
2339
2367
|
return new BooleanSchema(booleanParams);
|
|
2340
2368
|
}
|
|
2369
|
+
static anyOf(anyOfParams) {
|
|
2370
|
+
return new AnyOfSchema(anyOfParams);
|
|
2371
|
+
}
|
|
2341
2372
|
}
|
|
2342
2373
|
/**
|
|
2343
2374
|
* Schema class for "integer" types.
|
|
@@ -2345,7 +2376,10 @@ class Schema {
|
|
|
2345
2376
|
*/
|
|
2346
2377
|
class IntegerSchema extends Schema {
|
|
2347
2378
|
constructor(schemaParams) {
|
|
2348
|
-
super(
|
|
2379
|
+
super({
|
|
2380
|
+
type: SchemaType.INTEGER,
|
|
2381
|
+
...schemaParams
|
|
2382
|
+
});
|
|
2349
2383
|
}
|
|
2350
2384
|
}
|
|
2351
2385
|
/**
|
|
@@ -2354,7 +2388,10 @@ class IntegerSchema extends Schema {
|
|
|
2354
2388
|
*/
|
|
2355
2389
|
class NumberSchema extends Schema {
|
|
2356
2390
|
constructor(schemaParams) {
|
|
2357
|
-
super(
|
|
2391
|
+
super({
|
|
2392
|
+
type: SchemaType.NUMBER,
|
|
2393
|
+
...schemaParams
|
|
2394
|
+
});
|
|
2358
2395
|
}
|
|
2359
2396
|
}
|
|
2360
2397
|
/**
|
|
@@ -2363,7 +2400,10 @@ class NumberSchema extends Schema {
|
|
|
2363
2400
|
*/
|
|
2364
2401
|
class BooleanSchema extends Schema {
|
|
2365
2402
|
constructor(schemaParams) {
|
|
2366
|
-
super(
|
|
2403
|
+
super({
|
|
2404
|
+
type: SchemaType.BOOLEAN,
|
|
2405
|
+
...schemaParams
|
|
2406
|
+
});
|
|
2367
2407
|
}
|
|
2368
2408
|
}
|
|
2369
2409
|
/**
|
|
@@ -2373,7 +2413,10 @@ class BooleanSchema extends Schema {
|
|
|
2373
2413
|
*/
|
|
2374
2414
|
class StringSchema extends Schema {
|
|
2375
2415
|
constructor(schemaParams, enumValues) {
|
|
2376
|
-
super(
|
|
2416
|
+
super({
|
|
2417
|
+
type: SchemaType.STRING,
|
|
2418
|
+
...schemaParams
|
|
2419
|
+
});
|
|
2377
2420
|
this.enum = enumValues;
|
|
2378
2421
|
}
|
|
2379
2422
|
/**
|
|
@@ -2395,7 +2438,10 @@ class StringSchema extends Schema {
|
|
|
2395
2438
|
*/
|
|
2396
2439
|
class ArraySchema extends Schema {
|
|
2397
2440
|
constructor(schemaParams, items) {
|
|
2398
|
-
super(
|
|
2441
|
+
super({
|
|
2442
|
+
type: SchemaType.ARRAY,
|
|
2443
|
+
...schemaParams
|
|
2444
|
+
});
|
|
2399
2445
|
this.items = items;
|
|
2400
2446
|
}
|
|
2401
2447
|
/**
|
|
@@ -2414,7 +2460,10 @@ class ArraySchema extends Schema {
|
|
|
2414
2460
|
*/
|
|
2415
2461
|
class ObjectSchema extends Schema {
|
|
2416
2462
|
constructor(schemaParams, properties, optionalProperties = []) {
|
|
2417
|
-
super(
|
|
2463
|
+
super({
|
|
2464
|
+
type: SchemaType.OBJECT,
|
|
2465
|
+
...schemaParams
|
|
2466
|
+
});
|
|
2418
2467
|
this.properties = properties;
|
|
2419
2468
|
this.optionalProperties = optionalProperties;
|
|
2420
2469
|
}
|
|
@@ -2423,7 +2472,7 @@ class ObjectSchema extends Schema {
|
|
|
2423
2472
|
*/
|
|
2424
2473
|
toJSON() {
|
|
2425
2474
|
const obj = super.toJSON();
|
|
2426
|
-
obj.properties =
|
|
2475
|
+
obj.properties = { ...this.properties };
|
|
2427
2476
|
const required = [];
|
|
2428
2477
|
if (this.optionalProperties) {
|
|
2429
2478
|
for (const propertyKey of this.optionalProperties) {
|
|
@@ -2447,6 +2496,34 @@ class ObjectSchema extends Schema {
|
|
|
2447
2496
|
return obj;
|
|
2448
2497
|
}
|
|
2449
2498
|
}
|
|
2499
|
+
/**
|
|
2500
|
+
* Schema class representing a value that can conform to any of the provided sub-schemas. This is
|
|
2501
|
+
* useful when a field can accept multiple distinct types or structures.
|
|
2502
|
+
* @public
|
|
2503
|
+
*/
|
|
2504
|
+
class AnyOfSchema extends Schema {
|
|
2505
|
+
constructor(schemaParams) {
|
|
2506
|
+
if (schemaParams.anyOf.length === 0) {
|
|
2507
|
+
throw new AIError(AIErrorCode.INVALID_SCHEMA, "The 'anyOf' array must not be empty.");
|
|
2508
|
+
}
|
|
2509
|
+
super({
|
|
2510
|
+
...schemaParams,
|
|
2511
|
+
type: undefined // anyOf schemas do not have an explicit type
|
|
2512
|
+
});
|
|
2513
|
+
this.anyOf = schemaParams.anyOf;
|
|
2514
|
+
}
|
|
2515
|
+
/**
|
|
2516
|
+
* @internal
|
|
2517
|
+
*/
|
|
2518
|
+
toJSON() {
|
|
2519
|
+
const obj = super.toJSON();
|
|
2520
|
+
// Ensure the 'anyOf' property contains serialized SchemaRequest objects.
|
|
2521
|
+
if (this.anyOf && Array.isArray(this.anyOf)) {
|
|
2522
|
+
obj.anyOf = this.anyOf.map(s => s.toJSON());
|
|
2523
|
+
}
|
|
2524
|
+
return obj;
|
|
2525
|
+
}
|
|
2526
|
+
}
|
|
2450
2527
|
|
|
2451
2528
|
/**
|
|
2452
2529
|
* @license
|
|
@@ -2616,10 +2693,10 @@ function registerAI() {
|
|
|
2616
2693
|
return new AIService(app, backend, auth, appCheckProvider);
|
|
2617
2694
|
}, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
|
|
2618
2695
|
registerVersion(name, version);
|
|
2619
|
-
// BUILD_TARGET will be replaced by values like
|
|
2620
|
-
registerVersion(name, version, '
|
|
2696
|
+
// BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation
|
|
2697
|
+
registerVersion(name, version, 'esm2020');
|
|
2621
2698
|
}
|
|
2622
2699
|
registerAI();
|
|
2623
2700
|
|
|
2624
|
-
export { AIError, AIErrorCode, AIModel, ArraySchema, Backend, BackendType, BlockReason, BooleanSchema, ChatSession, FinishReason, FunctionCallingMode, GenerativeModel, GoogleAIBackend, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, ImagenAspectRatio, ImagenImageFormat, ImagenModel, ImagenPersonFilterLevel, ImagenSafetyFilterLevel, IntegerSchema, Modality, NumberSchema, ObjectSchema, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, VertexAIBackend, getAI, getGenerativeModel, getImagenModel };
|
|
2625
|
-
//# sourceMappingURL=index.
|
|
2701
|
+
export { AIError, AIErrorCode, AIModel, AnyOfSchema, ArraySchema, Backend, BackendType, BlockReason, BooleanSchema, ChatSession, FinishReason, FunctionCallingMode, GenerativeModel, GoogleAIBackend, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, ImagenAspectRatio, ImagenImageFormat, ImagenModel, ImagenPersonFilterLevel, ImagenSafetyFilterLevel, IntegerSchema, Modality, NumberSchema, ObjectSchema, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, VertexAIBackend, getAI, getGenerativeModel, getImagenModel };
|
|
2702
|
+
//# sourceMappingURL=index.esm.js.map
|