@firebase/ai 2.5.0-canary.0800a8bed → 2.5.0-canary.180b1ad9b
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 +101 -0
- package/dist/ai.d.ts +125 -0
- package/dist/esm/index.esm.js +324 -105
- package/dist/esm/index.esm.js.map +1 -1
- package/dist/esm/src/api.d.ts +23 -1
- package/dist/esm/src/backend.d.ts +24 -0
- package/dist/esm/src/constants.d.ts +1 -0
- package/dist/esm/src/methods/generate-content.d.ts +2 -0
- package/dist/esm/src/models/template-generative-model.d.ts +64 -0
- package/dist/esm/src/models/template-imagen-model.d.ts +51 -0
- package/dist/esm/src/models/utils.d.ts +26 -0
- package/dist/esm/src/requests/request.d.ts +33 -15
- package/dist/index.cjs.js +327 -104
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.node.cjs.js +327 -104
- package/dist/index.node.cjs.js.map +1 -1
- package/dist/index.node.mjs +324 -105
- package/dist/index.node.mjs.map +1 -1
- package/dist/src/api.d.ts +23 -1
- package/dist/src/backend.d.ts +24 -0
- package/dist/src/constants.d.ts +1 -0
- package/dist/src/methods/generate-content.d.ts +2 -0
- package/dist/src/models/template-generative-model.d.ts +64 -0
- package/dist/src/models/template-imagen-model.d.ts +51 -0
- package/dist/src/models/utils.d.ts +26 -0
- package/dist/src/requests/request.d.ts +33 -15
- package/package.json +8 -8
package/dist/index.node.cjs.js
CHANGED
|
@@ -8,7 +8,7 @@ var util = require('@firebase/util');
|
|
|
8
8
|
var logger$1 = require('@firebase/logger');
|
|
9
9
|
|
|
10
10
|
var name = "@firebase/ai";
|
|
11
|
-
var version = "2.5.0-canary.
|
|
11
|
+
var version = "2.5.0-canary.180b1ad9b";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* @license
|
|
@@ -770,6 +770,18 @@ class GoogleAIBackend extends Backend {
|
|
|
770
770
|
constructor() {
|
|
771
771
|
super(BackendType.GOOGLE_AI);
|
|
772
772
|
}
|
|
773
|
+
/**
|
|
774
|
+
* @internal
|
|
775
|
+
*/
|
|
776
|
+
_getModelPath(project, model) {
|
|
777
|
+
return `/${DEFAULT_API_VERSION}/projects/${project}/${model}`;
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* @internal
|
|
781
|
+
*/
|
|
782
|
+
_getTemplatePath(project, templateId) {
|
|
783
|
+
return `/${DEFAULT_API_VERSION}/projects/${project}/templates/${templateId}`;
|
|
784
|
+
}
|
|
773
785
|
}
|
|
774
786
|
/**
|
|
775
787
|
* Configuration class for the Vertex AI Gemini API.
|
|
@@ -796,6 +808,18 @@ class VertexAIBackend extends Backend {
|
|
|
796
808
|
this.location = location;
|
|
797
809
|
}
|
|
798
810
|
}
|
|
811
|
+
/**
|
|
812
|
+
* @internal
|
|
813
|
+
*/
|
|
814
|
+
_getModelPath(project, model) {
|
|
815
|
+
return `/${DEFAULT_API_VERSION}/projects/${project}/locations/${this.location}/${model}`;
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* @internal
|
|
819
|
+
*/
|
|
820
|
+
_getTemplatePath(project, templateId) {
|
|
821
|
+
return `/${DEFAULT_API_VERSION}/projects/${project}/locations/${this.location}/templates/${templateId}`;
|
|
822
|
+
}
|
|
799
823
|
}
|
|
800
824
|
|
|
801
825
|
/**
|
|
@@ -927,6 +951,67 @@ function factory(container, { instanceIdentifier }) {
|
|
|
927
951
|
return new AIService(app, backend, auth, appCheckProvider);
|
|
928
952
|
}
|
|
929
953
|
|
|
954
|
+
/**
|
|
955
|
+
* @license
|
|
956
|
+
* Copyright 2025 Google LLC
|
|
957
|
+
*
|
|
958
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
959
|
+
* you may not use this file except in compliance with the License.
|
|
960
|
+
* You may obtain a copy of the License at
|
|
961
|
+
*
|
|
962
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
963
|
+
*
|
|
964
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
965
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
966
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
967
|
+
* See the License for the specific language governing permissions and
|
|
968
|
+
* limitations under the License.
|
|
969
|
+
*/
|
|
970
|
+
/**
|
|
971
|
+
* Initializes an {@link ApiSettings} object from an {@link AI} instance.
|
|
972
|
+
*
|
|
973
|
+
* If this is a Server App, the {@link ApiSettings} object's `getAppCheckToken()` will resolve
|
|
974
|
+
* with the `FirebaseServerAppSettings.appCheckToken`, instead of requiring that an App Check
|
|
975
|
+
* instance is initialized.
|
|
976
|
+
*/
|
|
977
|
+
function initApiSettings(ai) {
|
|
978
|
+
if (!ai.app?.options?.apiKey) {
|
|
979
|
+
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.`);
|
|
980
|
+
}
|
|
981
|
+
else if (!ai.app?.options?.projectId) {
|
|
982
|
+
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.`);
|
|
983
|
+
}
|
|
984
|
+
else if (!ai.app?.options?.appId) {
|
|
985
|
+
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.`);
|
|
986
|
+
}
|
|
987
|
+
const apiSettings = {
|
|
988
|
+
apiKey: ai.app.options.apiKey,
|
|
989
|
+
project: ai.app.options.projectId,
|
|
990
|
+
appId: ai.app.options.appId,
|
|
991
|
+
automaticDataCollectionEnabled: ai.app.automaticDataCollectionEnabled,
|
|
992
|
+
location: ai.location,
|
|
993
|
+
backend: ai.backend
|
|
994
|
+
};
|
|
995
|
+
if (app._isFirebaseServerApp(ai.app) && ai.app.settings.appCheckToken) {
|
|
996
|
+
const token = ai.app.settings.appCheckToken;
|
|
997
|
+
apiSettings.getAppCheckToken = () => {
|
|
998
|
+
return Promise.resolve({ token });
|
|
999
|
+
};
|
|
1000
|
+
}
|
|
1001
|
+
else if (ai.appCheck) {
|
|
1002
|
+
if (ai.options?.useLimitedUseAppCheckTokens) {
|
|
1003
|
+
apiSettings.getAppCheckToken = () => ai.appCheck.getLimitedUseToken();
|
|
1004
|
+
}
|
|
1005
|
+
else {
|
|
1006
|
+
apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
if (ai.auth) {
|
|
1010
|
+
apiSettings.getAuthToken = () => ai.auth.getToken();
|
|
1011
|
+
}
|
|
1012
|
+
return apiSettings;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
930
1015
|
/**
|
|
931
1016
|
* @license
|
|
932
1017
|
* Copyright 2025 Google LLC
|
|
@@ -970,43 +1055,8 @@ class AIModel {
|
|
|
970
1055
|
* @internal
|
|
971
1056
|
*/
|
|
972
1057
|
constructor(ai, modelName) {
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
}
|
|
976
|
-
else if (!ai.app?.options?.projectId) {
|
|
977
|
-
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.`);
|
|
978
|
-
}
|
|
979
|
-
else if (!ai.app?.options?.appId) {
|
|
980
|
-
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.`);
|
|
981
|
-
}
|
|
982
|
-
else {
|
|
983
|
-
this._apiSettings = {
|
|
984
|
-
apiKey: ai.app.options.apiKey,
|
|
985
|
-
project: ai.app.options.projectId,
|
|
986
|
-
appId: ai.app.options.appId,
|
|
987
|
-
automaticDataCollectionEnabled: ai.app.automaticDataCollectionEnabled,
|
|
988
|
-
location: ai.location,
|
|
989
|
-
backend: ai.backend
|
|
990
|
-
};
|
|
991
|
-
if (app._isFirebaseServerApp(ai.app) && ai.app.settings.appCheckToken) {
|
|
992
|
-
const token = ai.app.settings.appCheckToken;
|
|
993
|
-
this._apiSettings.getAppCheckToken = () => {
|
|
994
|
-
return Promise.resolve({ token });
|
|
995
|
-
};
|
|
996
|
-
}
|
|
997
|
-
else if (ai.appCheck) {
|
|
998
|
-
if (ai.options?.useLimitedUseAppCheckTokens) {
|
|
999
|
-
this._apiSettings.getAppCheckToken = () => ai.appCheck.getLimitedUseToken();
|
|
1000
|
-
}
|
|
1001
|
-
else {
|
|
1002
|
-
this._apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
if (ai.auth) {
|
|
1006
|
-
this._apiSettings.getAuthToken = () => ai.auth.getToken();
|
|
1007
|
-
}
|
|
1008
|
-
this.model = AIModel.normalizeModelName(modelName, this._apiSettings.backend.backendType);
|
|
1009
|
-
}
|
|
1058
|
+
this._apiSettings = initApiSettings(ai);
|
|
1059
|
+
this.model = AIModel.normalizeModelName(modelName, this._apiSettings.backend.backendType);
|
|
1010
1060
|
}
|
|
1011
1061
|
/**
|
|
1012
1062
|
* Normalizes the given model name to a fully qualified model resource name.
|
|
@@ -1073,7 +1123,7 @@ const logger = new logger$1.Logger('@firebase/vertexai');
|
|
|
1073
1123
|
|
|
1074
1124
|
/**
|
|
1075
1125
|
* @license
|
|
1076
|
-
* Copyright
|
|
1126
|
+
* Copyright 2025 Google LLC
|
|
1077
1127
|
*
|
|
1078
1128
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1079
1129
|
* you may not use this file except in compliance with the License.
|
|
@@ -1087,47 +1137,33 @@ const logger = new logger$1.Logger('@firebase/vertexai');
|
|
|
1087
1137
|
* See the License for the specific language governing permissions and
|
|
1088
1138
|
* limitations under the License.
|
|
1089
1139
|
*/
|
|
1090
|
-
|
|
1091
|
-
(
|
|
1092
|
-
|
|
1093
|
-
Task["STREAM_GENERATE_CONTENT"] = "streamGenerateContent";
|
|
1094
|
-
Task["COUNT_TOKENS"] = "countTokens";
|
|
1095
|
-
Task["PREDICT"] = "predict";
|
|
1096
|
-
})(Task || (Task = {}));
|
|
1097
|
-
class RequestUrl {
|
|
1098
|
-
constructor(model, task, apiSettings, stream, requestOptions) {
|
|
1099
|
-
this.model = model;
|
|
1100
|
-
this.task = task;
|
|
1101
|
-
this.apiSettings = apiSettings;
|
|
1102
|
-
this.stream = stream;
|
|
1103
|
-
this.requestOptions = requestOptions;
|
|
1140
|
+
class RequestURL {
|
|
1141
|
+
constructor(params) {
|
|
1142
|
+
this.params = params;
|
|
1104
1143
|
}
|
|
1105
1144
|
toString() {
|
|
1106
1145
|
const url = new URL(this.baseUrl); // Throws if the URL is invalid
|
|
1107
|
-
url.pathname =
|
|
1146
|
+
url.pathname = this.pathname;
|
|
1108
1147
|
url.search = this.queryParams.toString();
|
|
1109
1148
|
return url.toString();
|
|
1110
1149
|
}
|
|
1111
|
-
get
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
get modelPath() {
|
|
1118
|
-
if (this.apiSettings.backend instanceof GoogleAIBackend) {
|
|
1119
|
-
return `projects/${this.apiSettings.project}/${this.model}`;
|
|
1120
|
-
}
|
|
1121
|
-
else if (this.apiSettings.backend instanceof VertexAIBackend) {
|
|
1122
|
-
return `projects/${this.apiSettings.project}/locations/${this.apiSettings.backend.location}/${this.model}`;
|
|
1150
|
+
get pathname() {
|
|
1151
|
+
// We need to construct a different URL if the request is for server side prompt templates,
|
|
1152
|
+
// since the URL patterns are different. Server side prompt templates expect a templateId
|
|
1153
|
+
// instead of a model name.
|
|
1154
|
+
if (this.params.templateId) {
|
|
1155
|
+
return `${this.params.apiSettings.backend._getTemplatePath(this.params.apiSettings.project, this.params.templateId)}:${this.params.task}`;
|
|
1123
1156
|
}
|
|
1124
1157
|
else {
|
|
1125
|
-
|
|
1158
|
+
return `${this.params.apiSettings.backend._getModelPath(this.params.apiSettings.project, this.params.model)}:${this.params.task}`;
|
|
1126
1159
|
}
|
|
1127
1160
|
}
|
|
1161
|
+
get baseUrl() {
|
|
1162
|
+
return this.params.requestOptions?.baseUrl ?? `https://${DEFAULT_DOMAIN}`;
|
|
1163
|
+
}
|
|
1128
1164
|
get queryParams() {
|
|
1129
1165
|
const params = new URLSearchParams();
|
|
1130
|
-
if (this.stream) {
|
|
1166
|
+
if (this.params.stream) {
|
|
1131
1167
|
params.set('alt', 'sse');
|
|
1132
1168
|
}
|
|
1133
1169
|
return params;
|
|
@@ -1167,12 +1203,12 @@ async function getHeaders(url) {
|
|
|
1167
1203
|
const headers = new Headers();
|
|
1168
1204
|
headers.append('Content-Type', 'application/json');
|
|
1169
1205
|
headers.append('x-goog-api-client', getClientHeaders());
|
|
1170
|
-
headers.append('x-goog-api-key', url.apiSettings.apiKey);
|
|
1171
|
-
if (url.apiSettings.automaticDataCollectionEnabled) {
|
|
1172
|
-
headers.append('X-Firebase-Appid', url.apiSettings.appId);
|
|
1206
|
+
headers.append('x-goog-api-key', url.params.apiSettings.apiKey);
|
|
1207
|
+
if (url.params.apiSettings.automaticDataCollectionEnabled) {
|
|
1208
|
+
headers.append('X-Firebase-Appid', url.params.apiSettings.appId);
|
|
1173
1209
|
}
|
|
1174
|
-
if (url.apiSettings.getAppCheckToken) {
|
|
1175
|
-
const appCheckToken = await url.apiSettings.getAppCheckToken();
|
|
1210
|
+
if (url.params.apiSettings.getAppCheckToken) {
|
|
1211
|
+
const appCheckToken = await url.params.apiSettings.getAppCheckToken();
|
|
1176
1212
|
if (appCheckToken) {
|
|
1177
1213
|
headers.append('X-Firebase-AppCheck', appCheckToken.token);
|
|
1178
1214
|
if (appCheckToken.error) {
|
|
@@ -1180,39 +1216,33 @@ async function getHeaders(url) {
|
|
|
1180
1216
|
}
|
|
1181
1217
|
}
|
|
1182
1218
|
}
|
|
1183
|
-
if (url.apiSettings.getAuthToken) {
|
|
1184
|
-
const authToken = await url.apiSettings.getAuthToken();
|
|
1219
|
+
if (url.params.apiSettings.getAuthToken) {
|
|
1220
|
+
const authToken = await url.params.apiSettings.getAuthToken();
|
|
1185
1221
|
if (authToken) {
|
|
1186
1222
|
headers.append('Authorization', `Firebase ${authToken.accessToken}`);
|
|
1187
1223
|
}
|
|
1188
1224
|
}
|
|
1189
1225
|
return headers;
|
|
1190
1226
|
}
|
|
1191
|
-
async function
|
|
1192
|
-
const url = new
|
|
1193
|
-
return {
|
|
1194
|
-
url: url.toString(),
|
|
1195
|
-
fetchOptions: {
|
|
1196
|
-
method: 'POST',
|
|
1197
|
-
headers: await getHeaders(url),
|
|
1198
|
-
body
|
|
1199
|
-
}
|
|
1200
|
-
};
|
|
1201
|
-
}
|
|
1202
|
-
async function makeRequest(model, task, apiSettings, stream, body, requestOptions) {
|
|
1203
|
-
const url = new RequestUrl(model, task, apiSettings, stream, requestOptions);
|
|
1227
|
+
async function makeRequest(requestUrlParams, body) {
|
|
1228
|
+
const url = new RequestURL(requestUrlParams);
|
|
1204
1229
|
let response;
|
|
1205
1230
|
let fetchTimeoutId;
|
|
1206
1231
|
try {
|
|
1207
|
-
const
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1232
|
+
const fetchOptions = {
|
|
1233
|
+
method: 'POST',
|
|
1234
|
+
headers: await getHeaders(url),
|
|
1235
|
+
body
|
|
1236
|
+
};
|
|
1237
|
+
// Timeout is 180s by default.
|
|
1238
|
+
const timeoutMillis = requestUrlParams.requestOptions?.timeout != null &&
|
|
1239
|
+
requestUrlParams.requestOptions.timeout >= 0
|
|
1240
|
+
? requestUrlParams.requestOptions.timeout
|
|
1211
1241
|
: DEFAULT_FETCH_TIMEOUT_MS;
|
|
1212
1242
|
const abortController = new AbortController();
|
|
1213
1243
|
fetchTimeoutId = setTimeout(() => abortController.abort(), timeoutMillis);
|
|
1214
|
-
|
|
1215
|
-
response = await fetch(
|
|
1244
|
+
fetchOptions.signal = abortController.signal;
|
|
1245
|
+
response = await fetch(url.toString(), fetchOptions);
|
|
1216
1246
|
if (!response.ok) {
|
|
1217
1247
|
let message = '';
|
|
1218
1248
|
let errorDetails;
|
|
@@ -1234,7 +1264,7 @@ async function makeRequest(model, task, apiSettings, stream, body, requestOption
|
|
|
1234
1264
|
throw new AIError(AIErrorCode.API_NOT_ENABLED, `The Firebase AI SDK requires the Firebase AI ` +
|
|
1235
1265
|
`API ('firebasevertexai.googleapis.com') to be enabled in your ` +
|
|
1236
1266
|
`Firebase project. Enable this API by visiting the Firebase Console ` +
|
|
1237
|
-
`at https://console.firebase.google.com/project/${url.apiSettings.project}/genai/ ` +
|
|
1267
|
+
`at https://console.firebase.google.com/project/${url.params.apiSettings.project}/genai/ ` +
|
|
1238
1268
|
`and clicking "Get started". If you enabled this API recently, ` +
|
|
1239
1269
|
`wait a few minutes for the action to propagate to our systems and ` +
|
|
1240
1270
|
`then retry.`, {
|
|
@@ -1975,8 +2005,13 @@ async function generateContentStreamOnCloud(apiSettings, model, params, requestO
|
|
|
1975
2005
|
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1976
2006
|
params = mapGenerateContentRequest(params);
|
|
1977
2007
|
}
|
|
1978
|
-
return makeRequest(
|
|
1979
|
-
|
|
2008
|
+
return makeRequest({
|
|
2009
|
+
task: "streamGenerateContent" /* Task.STREAM_GENERATE_CONTENT */,
|
|
2010
|
+
model,
|
|
2011
|
+
apiSettings,
|
|
2012
|
+
stream: true,
|
|
2013
|
+
requestOptions
|
|
2014
|
+
}, JSON.stringify(params));
|
|
1980
2015
|
}
|
|
1981
2016
|
async function generateContentStream(apiSettings, model, params, chromeAdapter, requestOptions) {
|
|
1982
2017
|
const callResult = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContentStream(params), () => generateContentStreamOnCloud(apiSettings, model, params, requestOptions));
|
|
@@ -1986,8 +2021,37 @@ async function generateContentOnCloud(apiSettings, model, params, requestOptions
|
|
|
1986
2021
|
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1987
2022
|
params = mapGenerateContentRequest(params);
|
|
1988
2023
|
}
|
|
1989
|
-
return makeRequest(
|
|
1990
|
-
|
|
2024
|
+
return makeRequest({
|
|
2025
|
+
model,
|
|
2026
|
+
task: "generateContent" /* Task.GENERATE_CONTENT */,
|
|
2027
|
+
apiSettings,
|
|
2028
|
+
stream: false,
|
|
2029
|
+
requestOptions
|
|
2030
|
+
}, JSON.stringify(params));
|
|
2031
|
+
}
|
|
2032
|
+
async function templateGenerateContent(apiSettings, templateId, templateParams, requestOptions) {
|
|
2033
|
+
const response = await makeRequest({
|
|
2034
|
+
task: "templateGenerateContent" /* ServerPromptTemplateTask.TEMPLATE_GENERATE_CONTENT */,
|
|
2035
|
+
templateId,
|
|
2036
|
+
apiSettings,
|
|
2037
|
+
stream: false,
|
|
2038
|
+
requestOptions
|
|
2039
|
+
}, JSON.stringify(templateParams));
|
|
2040
|
+
const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
|
|
2041
|
+
const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
|
|
2042
|
+
return {
|
|
2043
|
+
response: enhancedResponse
|
|
2044
|
+
};
|
|
2045
|
+
}
|
|
2046
|
+
async function templateGenerateContentStream(apiSettings, templateId, templateParams, requestOptions) {
|
|
2047
|
+
const response = await makeRequest({
|
|
2048
|
+
task: "templateStreamGenerateContent" /* ServerPromptTemplateTask.TEMPLATE_STREAM_GENERATE_CONTENT */,
|
|
2049
|
+
templateId,
|
|
2050
|
+
apiSettings,
|
|
2051
|
+
stream: true,
|
|
2052
|
+
requestOptions
|
|
2053
|
+
}, JSON.stringify(templateParams));
|
|
2054
|
+
return processStream(response, apiSettings);
|
|
1991
2055
|
}
|
|
1992
2056
|
async function generateContent(apiSettings, model, params, chromeAdapter, requestOptions) {
|
|
1993
2057
|
const callResult = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContent(params), () => generateContentOnCloud(apiSettings, model, params, requestOptions));
|
|
@@ -2399,7 +2463,13 @@ async function countTokensOnCloud(apiSettings, model, params, requestOptions) {
|
|
|
2399
2463
|
else {
|
|
2400
2464
|
body = JSON.stringify(params);
|
|
2401
2465
|
}
|
|
2402
|
-
const response = await makeRequest(
|
|
2466
|
+
const response = await makeRequest({
|
|
2467
|
+
model,
|
|
2468
|
+
task: "countTokens" /* Task.COUNT_TOKENS */,
|
|
2469
|
+
apiSettings,
|
|
2470
|
+
stream: false,
|
|
2471
|
+
requestOptions
|
|
2472
|
+
}, body);
|
|
2403
2473
|
return response.json();
|
|
2404
2474
|
}
|
|
2405
2475
|
async function countTokens(apiSettings, model, params, chromeAdapter, requestOptions) {
|
|
@@ -2952,8 +3022,13 @@ class ImagenModel extends AIModel {
|
|
|
2952
3022
|
...this.generationConfig,
|
|
2953
3023
|
...this.safetySettings
|
|
2954
3024
|
});
|
|
2955
|
-
const response = await makeRequest(
|
|
2956
|
-
|
|
3025
|
+
const response = await makeRequest({
|
|
3026
|
+
task: "predict" /* Task.PREDICT */,
|
|
3027
|
+
model: this.model,
|
|
3028
|
+
apiSettings: this._apiSettings,
|
|
3029
|
+
stream: false,
|
|
3030
|
+
requestOptions: this.requestOptions
|
|
3031
|
+
}, JSON.stringify(body));
|
|
2957
3032
|
return handlePredictResponse(response);
|
|
2958
3033
|
}
|
|
2959
3034
|
/**
|
|
@@ -2981,8 +3056,13 @@ class ImagenModel extends AIModel {
|
|
|
2981
3056
|
...this.generationConfig,
|
|
2982
3057
|
...this.safetySettings
|
|
2983
3058
|
});
|
|
2984
|
-
const response = await makeRequest(
|
|
2985
|
-
|
|
3059
|
+
const response = await makeRequest({
|
|
3060
|
+
task: "predict" /* Task.PREDICT */,
|
|
3061
|
+
model: this.model,
|
|
3062
|
+
apiSettings: this._apiSettings,
|
|
3063
|
+
stream: false,
|
|
3064
|
+
requestOptions: this.requestOptions
|
|
3065
|
+
}, JSON.stringify(body));
|
|
2986
3066
|
return handlePredictResponse(response);
|
|
2987
3067
|
}
|
|
2988
3068
|
}
|
|
@@ -3134,6 +3214,121 @@ class WebSocketHandlerImpl {
|
|
|
3134
3214
|
}
|
|
3135
3215
|
}
|
|
3136
3216
|
|
|
3217
|
+
/**
|
|
3218
|
+
* @license
|
|
3219
|
+
* Copyright 2025 Google LLC
|
|
3220
|
+
*
|
|
3221
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3222
|
+
* you may not use this file except in compliance with the License.
|
|
3223
|
+
* You may obtain a copy of the License at
|
|
3224
|
+
*
|
|
3225
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
3226
|
+
*
|
|
3227
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
3228
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
3229
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
3230
|
+
* See the License for the specific language governing permissions and
|
|
3231
|
+
* limitations under the License.
|
|
3232
|
+
*/
|
|
3233
|
+
/**
|
|
3234
|
+
* {@link GenerativeModel} APIs that execute on a server-side template.
|
|
3235
|
+
*
|
|
3236
|
+
* This class should only be instantiated with {@link getTemplateGenerativeModel}.
|
|
3237
|
+
*
|
|
3238
|
+
* @beta
|
|
3239
|
+
*/
|
|
3240
|
+
class TemplateGenerativeModel {
|
|
3241
|
+
/**
|
|
3242
|
+
* @hideconstructor
|
|
3243
|
+
*/
|
|
3244
|
+
constructor(ai, requestOptions) {
|
|
3245
|
+
this.requestOptions = requestOptions || {};
|
|
3246
|
+
this._apiSettings = initApiSettings(ai);
|
|
3247
|
+
}
|
|
3248
|
+
/**
|
|
3249
|
+
* Makes a single non-streaming call to the model and returns an object
|
|
3250
|
+
* containing a single {@link GenerateContentResponse}.
|
|
3251
|
+
*
|
|
3252
|
+
* @param templateId - The ID of the server-side template to execute.
|
|
3253
|
+
* @param templateVariables - A key-value map of variables to populate the
|
|
3254
|
+
* template with.
|
|
3255
|
+
*
|
|
3256
|
+
* @beta
|
|
3257
|
+
*/
|
|
3258
|
+
async generateContent(templateId, templateVariables // anything!
|
|
3259
|
+
) {
|
|
3260
|
+
return templateGenerateContent(this._apiSettings, templateId, { inputs: templateVariables }, this.requestOptions);
|
|
3261
|
+
}
|
|
3262
|
+
/**
|
|
3263
|
+
* Makes a single streaming call to the model and returns an object
|
|
3264
|
+
* containing an iterable stream that iterates over all chunks in the
|
|
3265
|
+
* streaming response as well as a promise that returns the final aggregated
|
|
3266
|
+
* response.
|
|
3267
|
+
*
|
|
3268
|
+
* @param templateId - The ID of the server-side template to execute.
|
|
3269
|
+
* @param templateVariables - A key-value map of variables to populate the
|
|
3270
|
+
* template with.
|
|
3271
|
+
*
|
|
3272
|
+
* @beta
|
|
3273
|
+
*/
|
|
3274
|
+
async generateContentStream(templateId, templateVariables) {
|
|
3275
|
+
return templateGenerateContentStream(this._apiSettings, templateId, { inputs: templateVariables }, this.requestOptions);
|
|
3276
|
+
}
|
|
3277
|
+
}
|
|
3278
|
+
|
|
3279
|
+
/**
|
|
3280
|
+
* @license
|
|
3281
|
+
* Copyright 2025 Google LLC
|
|
3282
|
+
*
|
|
3283
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3284
|
+
* you may not use this file except in compliance with the License.
|
|
3285
|
+
* You may obtain a copy of the License at
|
|
3286
|
+
*
|
|
3287
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
3288
|
+
*
|
|
3289
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
3290
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
3291
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
3292
|
+
* See the License for the specific language governing permissions and
|
|
3293
|
+
* limitations under the License.
|
|
3294
|
+
*/
|
|
3295
|
+
/**
|
|
3296
|
+
* Class for Imagen model APIs that execute on a server-side template.
|
|
3297
|
+
*
|
|
3298
|
+
* This class should only be instantiated with {@link getTemplateImagenModel}.
|
|
3299
|
+
*
|
|
3300
|
+
* @beta
|
|
3301
|
+
*/
|
|
3302
|
+
class TemplateImagenModel {
|
|
3303
|
+
/**
|
|
3304
|
+
* @hideconstructor
|
|
3305
|
+
*/
|
|
3306
|
+
constructor(ai, requestOptions) {
|
|
3307
|
+
this.requestOptions = requestOptions || {};
|
|
3308
|
+
this._apiSettings = initApiSettings(ai);
|
|
3309
|
+
}
|
|
3310
|
+
/**
|
|
3311
|
+
* Makes a single call to the model and returns an object containing a single
|
|
3312
|
+
* {@link ImagenGenerationResponse}.
|
|
3313
|
+
*
|
|
3314
|
+
* @param templateId - The ID of the server-side template to execute.
|
|
3315
|
+
* @param templateVariables - A key-value map of variables to populate the
|
|
3316
|
+
* template with.
|
|
3317
|
+
*
|
|
3318
|
+
* @beta
|
|
3319
|
+
*/
|
|
3320
|
+
async generateImages(templateId, templateVariables) {
|
|
3321
|
+
const response = await makeRequest({
|
|
3322
|
+
task: "templatePredict" /* ServerPromptTemplateTask.TEMPLATE_PREDICT */,
|
|
3323
|
+
templateId,
|
|
3324
|
+
apiSettings: this._apiSettings,
|
|
3325
|
+
stream: false,
|
|
3326
|
+
requestOptions: this.requestOptions
|
|
3327
|
+
}, JSON.stringify({ inputs: templateVariables }));
|
|
3328
|
+
return handlePredictResponse(response);
|
|
3329
|
+
}
|
|
3330
|
+
}
|
|
3331
|
+
|
|
3137
3332
|
/**
|
|
3138
3333
|
* @license
|
|
3139
3334
|
* Copyright 2024 Google LLC
|
|
@@ -3925,6 +4120,30 @@ function getLiveGenerativeModel(ai, modelParams) {
|
|
|
3925
4120
|
const webSocketHandler = new WebSocketHandlerImpl();
|
|
3926
4121
|
return new LiveGenerativeModel(ai, modelParams, webSocketHandler);
|
|
3927
4122
|
}
|
|
4123
|
+
/**
|
|
4124
|
+
* Returns a {@link TemplateGenerativeModel} class for executing server-side
|
|
4125
|
+
* templates.
|
|
4126
|
+
*
|
|
4127
|
+
* @param ai - An {@link AI} instance.
|
|
4128
|
+
* @param requestOptions - Additional options to use when making requests.
|
|
4129
|
+
*
|
|
4130
|
+
* @beta
|
|
4131
|
+
*/
|
|
4132
|
+
function getTemplateGenerativeModel(ai, requestOptions) {
|
|
4133
|
+
return new TemplateGenerativeModel(ai, requestOptions);
|
|
4134
|
+
}
|
|
4135
|
+
/**
|
|
4136
|
+
* Returns a {@link TemplateImagenModel} class for executing server-side
|
|
4137
|
+
* Imagen templates.
|
|
4138
|
+
*
|
|
4139
|
+
* @param ai - An {@link AI} instance.
|
|
4140
|
+
* @param requestOptions - Additional options to use when making requests.
|
|
4141
|
+
*
|
|
4142
|
+
* @beta
|
|
4143
|
+
*/
|
|
4144
|
+
function getTemplateImagenModel(ai, requestOptions) {
|
|
4145
|
+
return new TemplateImagenModel(ai, requestOptions);
|
|
4146
|
+
}
|
|
3928
4147
|
|
|
3929
4148
|
/**
|
|
3930
4149
|
* The Firebase AI Web SDK.
|
|
@@ -3979,11 +4198,15 @@ exports.ResponseModality = ResponseModality;
|
|
|
3979
4198
|
exports.Schema = Schema;
|
|
3980
4199
|
exports.SchemaType = SchemaType;
|
|
3981
4200
|
exports.StringSchema = StringSchema;
|
|
4201
|
+
exports.TemplateGenerativeModel = TemplateGenerativeModel;
|
|
4202
|
+
exports.TemplateImagenModel = TemplateImagenModel;
|
|
3982
4203
|
exports.URLRetrievalStatus = URLRetrievalStatus;
|
|
3983
4204
|
exports.VertexAIBackend = VertexAIBackend;
|
|
3984
4205
|
exports.getAI = getAI;
|
|
3985
4206
|
exports.getGenerativeModel = getGenerativeModel;
|
|
3986
4207
|
exports.getImagenModel = getImagenModel;
|
|
3987
4208
|
exports.getLiveGenerativeModel = getLiveGenerativeModel;
|
|
4209
|
+
exports.getTemplateGenerativeModel = getTemplateGenerativeModel;
|
|
4210
|
+
exports.getTemplateImagenModel = getTemplateImagenModel;
|
|
3988
4211
|
exports.startAudioConversation = startAudioConversation;
|
|
3989
4212
|
//# sourceMappingURL=index.node.cjs.js.map
|