@firebase/ai 1.4.1-canary.2d720995d → 1.4.1-canary.f11b55294
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/esm/{index.esm.js → index.esm2017.js} +92 -131
- package/dist/esm/index.esm2017.js.map +1 -0
- package/dist/index.cjs.js +91 -130
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.node.cjs.js +91 -130
- package/dist/index.node.cjs.js.map +1 -1
- package/dist/index.node.mjs +91 -130
- package/dist/index.node.mjs.map +1 -1
- package/package.json +12 -12
- package/dist/esm/index.esm.js.map +0 -1
package/dist/index.node.cjs.js
CHANGED
|
@@ -6,9 +6,10 @@ var app = require('@firebase/app');
|
|
|
6
6
|
var component = require('@firebase/component');
|
|
7
7
|
var util = require('@firebase/util');
|
|
8
8
|
var logger$1 = require('@firebase/logger');
|
|
9
|
+
var tslib = require('tslib');
|
|
9
10
|
|
|
10
11
|
var name = "@firebase/ai";
|
|
11
|
-
var version = "1.4.1-canary.
|
|
12
|
+
var version = "1.4.1-canary.f11b55294";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* @license
|
|
@@ -629,8 +630,8 @@ class AIService {
|
|
|
629
630
|
constructor(app, backend, authProvider, appCheckProvider) {
|
|
630
631
|
this.app = app;
|
|
631
632
|
this.backend = backend;
|
|
632
|
-
const appCheck = appCheckProvider
|
|
633
|
-
const auth = authProvider
|
|
633
|
+
const appCheck = appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.getImmediate({ optional: true });
|
|
634
|
+
const auth = authProvider === null || authProvider === void 0 ? void 0 : authProvider.getImmediate({ optional: true });
|
|
634
635
|
this.auth = auth || null;
|
|
635
636
|
this.appCheck = appCheck || null;
|
|
636
637
|
if (backend instanceof VertexAIBackend) {
|
|
@@ -802,13 +803,14 @@ class AIModel {
|
|
|
802
803
|
* @internal
|
|
803
804
|
*/
|
|
804
805
|
constructor(ai, modelName) {
|
|
805
|
-
|
|
806
|
+
var _a, _b, _c, _d, _e, _f;
|
|
807
|
+
if (!((_b = (_a = ai.app) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.apiKey)) {
|
|
806
808
|
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.`);
|
|
807
809
|
}
|
|
808
|
-
else if (!ai.app
|
|
810
|
+
else if (!((_d = (_c = ai.app) === null || _c === void 0 ? void 0 : _c.options) === null || _d === void 0 ? void 0 : _d.projectId)) {
|
|
809
811
|
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.`);
|
|
810
812
|
}
|
|
811
|
-
else if (!ai.app
|
|
813
|
+
else if (!((_f = (_e = ai.app) === null || _e === void 0 ? void 0 : _e.options) === null || _f === void 0 ? void 0 : _f.appId)) {
|
|
812
814
|
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.`);
|
|
813
815
|
}
|
|
814
816
|
else {
|
|
@@ -936,7 +938,8 @@ class RequestUrl {
|
|
|
936
938
|
return url.toString();
|
|
937
939
|
}
|
|
938
940
|
get baseUrl() {
|
|
939
|
-
|
|
941
|
+
var _a;
|
|
942
|
+
return ((_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.baseUrl) || DEFAULT_BASE_URL;
|
|
940
943
|
}
|
|
941
944
|
get apiVersion() {
|
|
942
945
|
return DEFAULT_API_VERSION; // TODO: allow user-set options if that feature becomes available
|
|
@@ -1012,7 +1015,7 @@ async function makeRequest(model, task, apiSettings, stream, body, requestOption
|
|
|
1012
1015
|
try {
|
|
1013
1016
|
const request = await constructRequest(model, task, apiSettings, stream, body, requestOptions);
|
|
1014
1017
|
// Timeout is 180s by default
|
|
1015
|
-
const timeoutMillis = requestOptions
|
|
1018
|
+
const timeoutMillis = (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeout) != null && requestOptions.timeout >= 0
|
|
1016
1019
|
? requestOptions.timeout
|
|
1017
1020
|
: DEFAULT_FETCH_TIMEOUT_MS;
|
|
1018
1021
|
const abortController = new AbortController();
|
|
@@ -1036,7 +1039,10 @@ async function makeRequest(model, task, apiSettings, stream, body, requestOption
|
|
|
1036
1039
|
if (response.status === 403 &&
|
|
1037
1040
|
errorDetails &&
|
|
1038
1041
|
errorDetails.some((detail) => detail.reason === 'SERVICE_DISABLED') &&
|
|
1039
|
-
errorDetails.some((detail) =>
|
|
1042
|
+
errorDetails.some((detail) => {
|
|
1043
|
+
var _a, _b;
|
|
1044
|
+
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');
|
|
1045
|
+
})) {
|
|
1040
1046
|
throw new AIError(AIErrorCode.API_NOT_ENABLED, `The Firebase AI SDK requires the Firebase AI ` +
|
|
1041
1047
|
`API ('firebasevertexai.googleapis.com') to be enabled in your ` +
|
|
1042
1048
|
`Firebase project. Enable this API by visiting the Firebase Console ` +
|
|
@@ -1181,9 +1187,10 @@ function addHelpers(response) {
|
|
|
1181
1187
|
* Returns all text found in all parts of first candidate.
|
|
1182
1188
|
*/
|
|
1183
1189
|
function getText(response) {
|
|
1190
|
+
var _a, _b, _c, _d;
|
|
1184
1191
|
const textStrings = [];
|
|
1185
|
-
if (response.candidates
|
|
1186
|
-
for (const part of response.candidates
|
|
1192
|
+
if ((_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) {
|
|
1193
|
+
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) {
|
|
1187
1194
|
if (part.text) {
|
|
1188
1195
|
textStrings.push(part.text);
|
|
1189
1196
|
}
|
|
@@ -1200,9 +1207,10 @@ function getText(response) {
|
|
|
1200
1207
|
* Returns {@link FunctionCall}s associated with first candidate.
|
|
1201
1208
|
*/
|
|
1202
1209
|
function getFunctionCalls(response) {
|
|
1210
|
+
var _a, _b, _c, _d;
|
|
1203
1211
|
const functionCalls = [];
|
|
1204
|
-
if (response.candidates
|
|
1205
|
-
for (const part of response.candidates
|
|
1212
|
+
if ((_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) {
|
|
1213
|
+
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) {
|
|
1206
1214
|
if (part.functionCall) {
|
|
1207
1215
|
functionCalls.push(part.functionCall);
|
|
1208
1216
|
}
|
|
@@ -1221,9 +1229,10 @@ function getFunctionCalls(response) {
|
|
|
1221
1229
|
* @internal
|
|
1222
1230
|
*/
|
|
1223
1231
|
function getInlineDataParts(response) {
|
|
1232
|
+
var _a, _b, _c, _d;
|
|
1224
1233
|
const data = [];
|
|
1225
|
-
if (response.candidates
|
|
1226
|
-
for (const part of response.candidates
|
|
1234
|
+
if ((_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) {
|
|
1235
|
+
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) {
|
|
1227
1236
|
if (part.inlineData) {
|
|
1228
1237
|
data.push(part);
|
|
1229
1238
|
}
|
|
@@ -1242,18 +1251,19 @@ function hadBadFinishReason(candidate) {
|
|
|
1242
1251
|
badFinishReasons.some(reason => reason === candidate.finishReason));
|
|
1243
1252
|
}
|
|
1244
1253
|
function formatBlockErrorMessage(response) {
|
|
1254
|
+
var _a, _b, _c;
|
|
1245
1255
|
let message = '';
|
|
1246
1256
|
if ((!response.candidates || response.candidates.length === 0) &&
|
|
1247
1257
|
response.promptFeedback) {
|
|
1248
1258
|
message += 'Response was blocked';
|
|
1249
|
-
if (response.promptFeedback
|
|
1259
|
+
if ((_a = response.promptFeedback) === null || _a === void 0 ? void 0 : _a.blockReason) {
|
|
1250
1260
|
message += ` due to ${response.promptFeedback.blockReason}`;
|
|
1251
1261
|
}
|
|
1252
|
-
if (response.promptFeedback
|
|
1262
|
+
if ((_b = response.promptFeedback) === null || _b === void 0 ? void 0 : _b.blockReasonMessage) {
|
|
1253
1263
|
message += `: ${response.promptFeedback.blockReasonMessage}`;
|
|
1254
1264
|
}
|
|
1255
1265
|
}
|
|
1256
|
-
else if (response.candidates
|
|
1266
|
+
else if ((_c = response.candidates) === null || _c === void 0 ? void 0 : _c[0]) {
|
|
1257
1267
|
const firstCandidate = response.candidates[0];
|
|
1258
1268
|
if (hadBadFinishReason(firstCandidate)) {
|
|
1259
1269
|
message += `Candidate was blocked due to ${firstCandidate.finishReason}`;
|
|
@@ -1272,11 +1282,12 @@ function formatBlockErrorMessage(response) {
|
|
|
1272
1282
|
* @internal
|
|
1273
1283
|
*/
|
|
1274
1284
|
async function handlePredictResponse(response) {
|
|
1285
|
+
var _a;
|
|
1275
1286
|
const responseJson = await response.json();
|
|
1276
1287
|
const images = [];
|
|
1277
1288
|
let filteredReason = undefined;
|
|
1278
1289
|
// The backend should always send a non-empty array of predictions if the response was successful.
|
|
1279
|
-
if (!responseJson.predictions || responseJson.predictions
|
|
1290
|
+
if (!responseJson.predictions || ((_a = responseJson.predictions) === null || _a === void 0 ? void 0 : _a.length) === 0) {
|
|
1280
1291
|
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.');
|
|
1281
1292
|
}
|
|
1282
1293
|
for (const prediction of responseJson.predictions) {
|
|
@@ -1340,12 +1351,13 @@ async function handlePredictResponse(response) {
|
|
|
1340
1351
|
* @internal
|
|
1341
1352
|
*/
|
|
1342
1353
|
function mapGenerateContentRequest(generateContentRequest) {
|
|
1343
|
-
|
|
1354
|
+
var _a, _b;
|
|
1355
|
+
(_a = generateContentRequest.safetySettings) === null || _a === void 0 ? void 0 : _a.forEach(safetySetting => {
|
|
1344
1356
|
if (safetySetting.method) {
|
|
1345
1357
|
throw new AIError(AIErrorCode.UNSUPPORTED, 'SafetySetting.method is not supported in the the Gemini Developer API. Please remove this property.');
|
|
1346
1358
|
}
|
|
1347
1359
|
});
|
|
1348
|
-
if (generateContentRequest.generationConfig
|
|
1360
|
+
if ((_b = generateContentRequest.generationConfig) === null || _b === void 0 ? void 0 : _b.topK) {
|
|
1349
1361
|
const roundedTopK = Math.round(generateContentRequest.generationConfig.topK);
|
|
1350
1362
|
if (roundedTopK !== generateContentRequest.generationConfig.topK) {
|
|
1351
1363
|
logger.warn('topK in GenerationConfig has been rounded to the nearest integer to match the format for requests to the Gemini Developer API.');
|
|
@@ -1386,10 +1398,7 @@ function mapGenerateContentResponse(googleAIResponse) {
|
|
|
1386
1398
|
*/
|
|
1387
1399
|
function mapCountTokensRequest(countTokensRequest, model) {
|
|
1388
1400
|
const mappedCountTokensRequest = {
|
|
1389
|
-
generateContentRequest: {
|
|
1390
|
-
model,
|
|
1391
|
-
...countTokensRequest
|
|
1392
|
-
}
|
|
1401
|
+
generateContentRequest: Object.assign({ model }, countTokensRequest)
|
|
1393
1402
|
};
|
|
1394
1403
|
return mappedCountTokensRequest;
|
|
1395
1404
|
}
|
|
@@ -1409,6 +1418,7 @@ function mapGenerateContentCandidates(candidates) {
|
|
|
1409
1418
|
let mappedSafetyRatings;
|
|
1410
1419
|
if (mappedCandidates) {
|
|
1411
1420
|
candidates.forEach(candidate => {
|
|
1421
|
+
var _a;
|
|
1412
1422
|
// Map citationSources to citations.
|
|
1413
1423
|
let citationMetadata;
|
|
1414
1424
|
if (candidate.citationMetadata) {
|
|
@@ -1419,18 +1429,14 @@ function mapGenerateContentCandidates(candidates) {
|
|
|
1419
1429
|
// Assign missing candidate SafetyRatings properties to their defaults if undefined.
|
|
1420
1430
|
if (candidate.safetyRatings) {
|
|
1421
1431
|
mappedSafetyRatings = candidate.safetyRatings.map(safetyRating => {
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
severity: safetyRating.severity ?? HarmSeverity.HARM_SEVERITY_UNSUPPORTED,
|
|
1425
|
-
probabilityScore: safetyRating.probabilityScore ?? 0,
|
|
1426
|
-
severityScore: safetyRating.severityScore ?? 0
|
|
1427
|
-
};
|
|
1432
|
+
var _a, _b, _c;
|
|
1433
|
+
return Object.assign(Object.assign({}, safetyRating), { severity: (_a = safetyRating.severity) !== null && _a !== void 0 ? _a : HarmSeverity.HARM_SEVERITY_UNSUPPORTED, probabilityScore: (_b = safetyRating.probabilityScore) !== null && _b !== void 0 ? _b : 0, severityScore: (_c = safetyRating.severityScore) !== null && _c !== void 0 ? _c : 0 });
|
|
1428
1434
|
});
|
|
1429
1435
|
}
|
|
1430
1436
|
// videoMetadata is not supported.
|
|
1431
1437
|
// Throw early since developers may send a long video as input and only expect to pay
|
|
1432
1438
|
// for inference on a small portion of the video.
|
|
1433
|
-
if (candidate.content
|
|
1439
|
+
if ((_a = candidate.content) === null || _a === void 0 ? void 0 : _a.parts.some(part => part === null || part === void 0 ? void 0 : part.videoMetadata)) {
|
|
1434
1440
|
throw new AIError(AIErrorCode.UNSUPPORTED, 'Part.videoMetadata is not supported in the Gemini Developer API. Please remove this property.');
|
|
1435
1441
|
}
|
|
1436
1442
|
const mappedCandidate = {
|
|
@@ -1451,12 +1457,13 @@ function mapPromptFeedback(promptFeedback) {
|
|
|
1451
1457
|
// Assign missing SafetyRating properties to their defaults if undefined.
|
|
1452
1458
|
const mappedSafetyRatings = [];
|
|
1453
1459
|
promptFeedback.safetyRatings.forEach(safetyRating => {
|
|
1460
|
+
var _a, _b, _c;
|
|
1454
1461
|
mappedSafetyRatings.push({
|
|
1455
1462
|
category: safetyRating.category,
|
|
1456
1463
|
probability: safetyRating.probability,
|
|
1457
|
-
severity: safetyRating.severity
|
|
1458
|
-
probabilityScore: safetyRating.probabilityScore
|
|
1459
|
-
severityScore: safetyRating.severityScore
|
|
1464
|
+
severity: (_a = safetyRating.severity) !== null && _a !== void 0 ? _a : HarmSeverity.HARM_SEVERITY_UNSUPPORTED,
|
|
1465
|
+
probabilityScore: (_b = safetyRating.probabilityScore) !== null && _b !== void 0 ? _b : 0,
|
|
1466
|
+
severityScore: (_c = safetyRating.severityScore) !== null && _c !== void 0 ? _c : 0,
|
|
1460
1467
|
blocked: safetyRating.blocked
|
|
1461
1468
|
});
|
|
1462
1469
|
});
|
|
@@ -1517,22 +1524,24 @@ async function getResponsePromise(stream, apiSettings) {
|
|
|
1517
1524
|
allResponses.push(value);
|
|
1518
1525
|
}
|
|
1519
1526
|
}
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1527
|
+
function generateResponseSequence(stream, apiSettings) {
|
|
1528
|
+
return tslib.__asyncGenerator(this, arguments, function* generateResponseSequence_1() {
|
|
1529
|
+
const reader = stream.getReader();
|
|
1530
|
+
while (true) {
|
|
1531
|
+
const { value, done } = yield tslib.__await(reader.read());
|
|
1532
|
+
if (done) {
|
|
1533
|
+
break;
|
|
1534
|
+
}
|
|
1535
|
+
let enhancedResponse;
|
|
1536
|
+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1537
|
+
enhancedResponse = createEnhancedContentResponse(mapGenerateContentResponse(value));
|
|
1538
|
+
}
|
|
1539
|
+
else {
|
|
1540
|
+
enhancedResponse = createEnhancedContentResponse(value);
|
|
1541
|
+
}
|
|
1542
|
+
yield yield tslib.__await(enhancedResponse);
|
|
1533
1543
|
}
|
|
1534
|
-
|
|
1535
|
-
}
|
|
1544
|
+
});
|
|
1536
1545
|
}
|
|
1537
1546
|
/**
|
|
1538
1547
|
* Reads a raw stream from the fetch response and join incomplete
|
|
@@ -1584,7 +1593,7 @@ function getResponseStream(inputStream) {
|
|
|
1584
1593
|
function aggregateResponses(responses) {
|
|
1585
1594
|
const lastResponse = responses[responses.length - 1];
|
|
1586
1595
|
const aggregatedResponse = {
|
|
1587
|
-
promptFeedback: lastResponse
|
|
1596
|
+
promptFeedback: lastResponse === null || lastResponse === void 0 ? void 0 : lastResponse.promptFeedback
|
|
1588
1597
|
};
|
|
1589
1598
|
for (const response of responses) {
|
|
1590
1599
|
if (response.candidates) {
|
|
@@ -1941,7 +1950,7 @@ class ChatSession {
|
|
|
1941
1950
|
this._history = [];
|
|
1942
1951
|
this._sendPromise = Promise.resolve();
|
|
1943
1952
|
this._apiSettings = apiSettings;
|
|
1944
|
-
if (params
|
|
1953
|
+
if (params === null || params === void 0 ? void 0 : params.history) {
|
|
1945
1954
|
validateChatHistory(params.history);
|
|
1946
1955
|
this._history = params.history;
|
|
1947
1956
|
}
|
|
@@ -1960,14 +1969,15 @@ class ChatSession {
|
|
|
1960
1969
|
* {@link GenerateContentResult}
|
|
1961
1970
|
*/
|
|
1962
1971
|
async sendMessage(request) {
|
|
1972
|
+
var _a, _b, _c, _d, _e;
|
|
1963
1973
|
await this._sendPromise;
|
|
1964
1974
|
const newContent = formatNewContent(request);
|
|
1965
1975
|
const generateContentRequest = {
|
|
1966
|
-
safetySettings: this.params
|
|
1967
|
-
generationConfig: this.params
|
|
1968
|
-
tools: this.params
|
|
1969
|
-
toolConfig: this.params
|
|
1970
|
-
systemInstruction: this.params
|
|
1976
|
+
safetySettings: (_a = this.params) === null || _a === void 0 ? void 0 : _a.safetySettings,
|
|
1977
|
+
generationConfig: (_b = this.params) === null || _b === void 0 ? void 0 : _b.generationConfig,
|
|
1978
|
+
tools: (_c = this.params) === null || _c === void 0 ? void 0 : _c.tools,
|
|
1979
|
+
toolConfig: (_d = this.params) === null || _d === void 0 ? void 0 : _d.toolConfig,
|
|
1980
|
+
systemInstruction: (_e = this.params) === null || _e === void 0 ? void 0 : _e.systemInstruction,
|
|
1971
1981
|
contents: [...this._history, newContent]
|
|
1972
1982
|
};
|
|
1973
1983
|
let finalResult = {};
|
|
@@ -1975,13 +1985,14 @@ class ChatSession {
|
|
|
1975
1985
|
this._sendPromise = this._sendPromise
|
|
1976
1986
|
.then(() => generateContent(this._apiSettings, this.model, generateContentRequest, this.requestOptions))
|
|
1977
1987
|
.then(result => {
|
|
1988
|
+
var _a, _b;
|
|
1978
1989
|
if (result.response.candidates &&
|
|
1979
1990
|
result.response.candidates.length > 0) {
|
|
1980
1991
|
this._history.push(newContent);
|
|
1981
1992
|
const responseContent = {
|
|
1982
|
-
parts: result.response.candidates
|
|
1993
|
+
parts: ((_a = result.response.candidates) === null || _a === void 0 ? void 0 : _a[0].content.parts) || [],
|
|
1983
1994
|
// Response seems to come back without a role set.
|
|
1984
|
-
role: result.response.candidates
|
|
1995
|
+
role: ((_b = result.response.candidates) === null || _b === void 0 ? void 0 : _b[0].content.role) || 'model'
|
|
1985
1996
|
};
|
|
1986
1997
|
this._history.push(responseContent);
|
|
1987
1998
|
}
|
|
@@ -2002,14 +2013,15 @@ class ChatSession {
|
|
|
2002
2013
|
* and a response promise.
|
|
2003
2014
|
*/
|
|
2004
2015
|
async sendMessageStream(request) {
|
|
2016
|
+
var _a, _b, _c, _d, _e;
|
|
2005
2017
|
await this._sendPromise;
|
|
2006
2018
|
const newContent = formatNewContent(request);
|
|
2007
2019
|
const generateContentRequest = {
|
|
2008
|
-
safetySettings: this.params
|
|
2009
|
-
generationConfig: this.params
|
|
2010
|
-
tools: this.params
|
|
2011
|
-
toolConfig: this.params
|
|
2012
|
-
systemInstruction: this.params
|
|
2020
|
+
safetySettings: (_a = this.params) === null || _a === void 0 ? void 0 : _a.safetySettings,
|
|
2021
|
+
generationConfig: (_b = this.params) === null || _b === void 0 ? void 0 : _b.generationConfig,
|
|
2022
|
+
tools: (_c = this.params) === null || _c === void 0 ? void 0 : _c.tools,
|
|
2023
|
+
toolConfig: (_d = this.params) === null || _d === void 0 ? void 0 : _d.toolConfig,
|
|
2024
|
+
systemInstruction: (_e = this.params) === null || _e === void 0 ? void 0 : _e.systemInstruction,
|
|
2013
2025
|
contents: [...this._history, newContent]
|
|
2014
2026
|
};
|
|
2015
2027
|
const streamPromise = generateContentStream(this._apiSettings, this.model, generateContentRequest, this.requestOptions);
|
|
@@ -2025,7 +2037,7 @@ class ChatSession {
|
|
|
2025
2037
|
.then(response => {
|
|
2026
2038
|
if (response.candidates && response.candidates.length > 0) {
|
|
2027
2039
|
this._history.push(newContent);
|
|
2028
|
-
const responseContent = {
|
|
2040
|
+
const responseContent = Object.assign({}, response.candidates[0].content);
|
|
2029
2041
|
// Response seems to come back without a role set.
|
|
2030
2042
|
if (!responseContent.role) {
|
|
2031
2043
|
responseContent.role = 'model';
|
|
@@ -2118,14 +2130,7 @@ class GenerativeModel extends AIModel {
|
|
|
2118
2130
|
*/
|
|
2119
2131
|
async generateContent(request) {
|
|
2120
2132
|
const formattedParams = formatGenerateContentInput(request);
|
|
2121
|
-
return generateContent(this._apiSettings, this.model, {
|
|
2122
|
-
generationConfig: this.generationConfig,
|
|
2123
|
-
safetySettings: this.safetySettings,
|
|
2124
|
-
tools: this.tools,
|
|
2125
|
-
toolConfig: this.toolConfig,
|
|
2126
|
-
systemInstruction: this.systemInstruction,
|
|
2127
|
-
...formattedParams
|
|
2128
|
-
}, this.requestOptions);
|
|
2133
|
+
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);
|
|
2129
2134
|
}
|
|
2130
2135
|
/**
|
|
2131
2136
|
* Makes a single streaming call to the model
|
|
@@ -2135,33 +2140,14 @@ class GenerativeModel extends AIModel {
|
|
|
2135
2140
|
*/
|
|
2136
2141
|
async generateContentStream(request) {
|
|
2137
2142
|
const formattedParams = formatGenerateContentInput(request);
|
|
2138
|
-
return generateContentStream(this._apiSettings, this.model, {
|
|
2139
|
-
generationConfig: this.generationConfig,
|
|
2140
|
-
safetySettings: this.safetySettings,
|
|
2141
|
-
tools: this.tools,
|
|
2142
|
-
toolConfig: this.toolConfig,
|
|
2143
|
-
systemInstruction: this.systemInstruction,
|
|
2144
|
-
...formattedParams
|
|
2145
|
-
}, this.requestOptions);
|
|
2143
|
+
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);
|
|
2146
2144
|
}
|
|
2147
2145
|
/**
|
|
2148
2146
|
* Gets a new {@link ChatSession} instance which can be used for
|
|
2149
2147
|
* multi-turn chats.
|
|
2150
2148
|
*/
|
|
2151
2149
|
startChat(startChatParams) {
|
|
2152
|
-
return new ChatSession(this._apiSettings, this.model, {
|
|
2153
|
-
tools: this.tools,
|
|
2154
|
-
toolConfig: this.toolConfig,
|
|
2155
|
-
systemInstruction: this.systemInstruction,
|
|
2156
|
-
generationConfig: this.generationConfig,
|
|
2157
|
-
safetySettings: this.safetySettings,
|
|
2158
|
-
/**
|
|
2159
|
-
* Overrides params inherited from GenerativeModel with those explicitly set in the
|
|
2160
|
-
* StartChatParams. For example, if startChatParams.generationConfig is set, it'll override
|
|
2161
|
-
* this.generationConfig.
|
|
2162
|
-
*/
|
|
2163
|
-
...startChatParams
|
|
2164
|
-
}, this.requestOptions);
|
|
2150
|
+
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);
|
|
2165
2151
|
}
|
|
2166
2152
|
/**
|
|
2167
2153
|
* Counts the tokens in the provided request.
|
|
@@ -2247,10 +2233,7 @@ class ImagenModel extends AIModel {
|
|
|
2247
2233
|
* @beta
|
|
2248
2234
|
*/
|
|
2249
2235
|
async generateImages(prompt) {
|
|
2250
|
-
const body = createPredictRequestBody(prompt, {
|
|
2251
|
-
...this.generationConfig,
|
|
2252
|
-
...this.safetySettings
|
|
2253
|
-
});
|
|
2236
|
+
const body = createPredictRequestBody(prompt, Object.assign(Object.assign({}, this.generationConfig), this.safetySettings));
|
|
2254
2237
|
const response = await makeRequest(this.model, Task.PREDICT, this._apiSettings,
|
|
2255
2238
|
/* stream */ false, JSON.stringify(body), this.requestOptions);
|
|
2256
2239
|
return handlePredictResponse(response);
|
|
@@ -2275,11 +2258,7 @@ class ImagenModel extends AIModel {
|
|
|
2275
2258
|
* If all images are filtered, the `images` array will be empty.
|
|
2276
2259
|
*/
|
|
2277
2260
|
async generateImagesGCS(prompt, gcsURI) {
|
|
2278
|
-
const body = createPredictRequestBody(prompt, {
|
|
2279
|
-
gcsURI,
|
|
2280
|
-
...this.generationConfig,
|
|
2281
|
-
...this.safetySettings
|
|
2282
|
-
});
|
|
2261
|
+
const body = createPredictRequestBody(prompt, Object.assign(Object.assign({ gcsURI }, this.generationConfig), this.safetySettings));
|
|
2283
2262
|
const response = await makeRequest(this.model, Task.PREDICT, this._apiSettings,
|
|
2284
2263
|
/* stream */ false, JSON.stringify(body), this.requestOptions);
|
|
2285
2264
|
return handlePredictResponse(response);
|
|
@@ -2370,10 +2349,7 @@ class Schema {
|
|
|
2370
2349
|
*/
|
|
2371
2350
|
class IntegerSchema extends Schema {
|
|
2372
2351
|
constructor(schemaParams) {
|
|
2373
|
-
super({
|
|
2374
|
-
type: SchemaType.INTEGER,
|
|
2375
|
-
...schemaParams
|
|
2376
|
-
});
|
|
2352
|
+
super(Object.assign({ type: SchemaType.INTEGER }, schemaParams));
|
|
2377
2353
|
}
|
|
2378
2354
|
}
|
|
2379
2355
|
/**
|
|
@@ -2382,10 +2358,7 @@ class IntegerSchema extends Schema {
|
|
|
2382
2358
|
*/
|
|
2383
2359
|
class NumberSchema extends Schema {
|
|
2384
2360
|
constructor(schemaParams) {
|
|
2385
|
-
super({
|
|
2386
|
-
type: SchemaType.NUMBER,
|
|
2387
|
-
...schemaParams
|
|
2388
|
-
});
|
|
2361
|
+
super(Object.assign({ type: SchemaType.NUMBER }, schemaParams));
|
|
2389
2362
|
}
|
|
2390
2363
|
}
|
|
2391
2364
|
/**
|
|
@@ -2394,10 +2367,7 @@ class NumberSchema extends Schema {
|
|
|
2394
2367
|
*/
|
|
2395
2368
|
class BooleanSchema extends Schema {
|
|
2396
2369
|
constructor(schemaParams) {
|
|
2397
|
-
super({
|
|
2398
|
-
type: SchemaType.BOOLEAN,
|
|
2399
|
-
...schemaParams
|
|
2400
|
-
});
|
|
2370
|
+
super(Object.assign({ type: SchemaType.BOOLEAN }, schemaParams));
|
|
2401
2371
|
}
|
|
2402
2372
|
}
|
|
2403
2373
|
/**
|
|
@@ -2407,10 +2377,7 @@ class BooleanSchema extends Schema {
|
|
|
2407
2377
|
*/
|
|
2408
2378
|
class StringSchema extends Schema {
|
|
2409
2379
|
constructor(schemaParams, enumValues) {
|
|
2410
|
-
super({
|
|
2411
|
-
type: SchemaType.STRING,
|
|
2412
|
-
...schemaParams
|
|
2413
|
-
});
|
|
2380
|
+
super(Object.assign({ type: SchemaType.STRING }, schemaParams));
|
|
2414
2381
|
this.enum = enumValues;
|
|
2415
2382
|
}
|
|
2416
2383
|
/**
|
|
@@ -2432,10 +2399,7 @@ class StringSchema extends Schema {
|
|
|
2432
2399
|
*/
|
|
2433
2400
|
class ArraySchema extends Schema {
|
|
2434
2401
|
constructor(schemaParams, items) {
|
|
2435
|
-
super({
|
|
2436
|
-
type: SchemaType.ARRAY,
|
|
2437
|
-
...schemaParams
|
|
2438
|
-
});
|
|
2402
|
+
super(Object.assign({ type: SchemaType.ARRAY }, schemaParams));
|
|
2439
2403
|
this.items = items;
|
|
2440
2404
|
}
|
|
2441
2405
|
/**
|
|
@@ -2454,10 +2418,7 @@ class ArraySchema extends Schema {
|
|
|
2454
2418
|
*/
|
|
2455
2419
|
class ObjectSchema extends Schema {
|
|
2456
2420
|
constructor(schemaParams, properties, optionalProperties = []) {
|
|
2457
|
-
super({
|
|
2458
|
-
type: SchemaType.OBJECT,
|
|
2459
|
-
...schemaParams
|
|
2460
|
-
});
|
|
2421
|
+
super(Object.assign({ type: SchemaType.OBJECT }, schemaParams));
|
|
2461
2422
|
this.properties = properties;
|
|
2462
2423
|
this.optionalProperties = optionalProperties;
|
|
2463
2424
|
}
|
|
@@ -2466,7 +2427,7 @@ class ObjectSchema extends Schema {
|
|
|
2466
2427
|
*/
|
|
2467
2428
|
toJSON() {
|
|
2468
2429
|
const obj = super.toJSON();
|
|
2469
|
-
obj.properties = {
|
|
2430
|
+
obj.properties = Object.assign({}, this.properties);
|
|
2470
2431
|
const required = [];
|
|
2471
2432
|
if (this.optionalProperties) {
|
|
2472
2433
|
for (const propertyKey of this.optionalProperties) {
|
|
@@ -2659,8 +2620,8 @@ function registerAI() {
|
|
|
2659
2620
|
return new AIService(app, backend, auth, appCheckProvider);
|
|
2660
2621
|
}, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
|
|
2661
2622
|
app.registerVersion(name, version, 'node');
|
|
2662
|
-
// BUILD_TARGET will be replaced by values like
|
|
2663
|
-
app.registerVersion(name, version, '
|
|
2623
|
+
// BUILD_TARGET will be replaced by values like esm2017, cjs2017, etc during the compilation
|
|
2624
|
+
app.registerVersion(name, version, 'cjs2017');
|
|
2664
2625
|
}
|
|
2665
2626
|
registerAI();
|
|
2666
2627
|
|