@firebase/ai 1.4.0-canary.c0617a341 → 1.4.0-canary.ec5f37403

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.
@@ -17,8 +17,8 @@
17
17
  import { FirebaseApp } from '@firebase/app';
18
18
  import { AI_TYPE } from './constants';
19
19
  import { AIService } from './service';
20
- import { AI, AIOptions, VertexAI, VertexAIOptions } from './public-types';
21
- import { ImagenModelParams, ModelParams, RequestOptions, AIErrorCode } from './types';
20
+ import { AI, AIOptions } from './public-types';
21
+ import { ImagenModelParams, ModelParams, RequestOptions } from './types';
22
22
  import { AIError } from './errors';
23
23
  import { AIModel, GenerativeModel, ImagenModel } from './models';
24
24
  export { ChatSession } from './methods/chat-session';
@@ -26,47 +26,11 @@ export * from './requests/schema-builder';
26
26
  export { ImagenImageFormat } from './requests/imagen-image-format';
27
27
  export { AIModel, GenerativeModel, ImagenModel, AIError };
28
28
  export { Backend, VertexAIBackend, GoogleAIBackend } from './backend';
29
- export { AIErrorCode as VertexAIErrorCode };
30
- /**
31
- * @deprecated Use the new {@link AIModel} instead. The Vertex AI in Firebase SDK has been
32
- * replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
33
- * services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
34
- *
35
- * Base class for Firebase AI model APIs.
36
- *
37
- * @public
38
- */
39
- export declare const VertexAIModel: typeof AIModel;
40
- /**
41
- * @deprecated Use the new {@link AIError} instead. The Vertex AI in Firebase SDK has been
42
- * replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
43
- * services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
44
- *
45
- * Error class for the Firebase AI SDK.
46
- *
47
- * @public
48
- */
49
- export declare const VertexAIError: typeof AIError;
50
29
  declare module '@firebase/component' {
51
30
  interface NameServiceMapping {
52
31
  [AI_TYPE]: AIService;
53
32
  }
54
33
  }
55
- /**
56
- * @deprecated Use the new {@link getAI | getAI()} instead. The Vertex AI in Firebase SDK has been
57
- * replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
58
- * services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
59
- *
60
- * Returns a {@link VertexAI} instance for the given app, configured to use the
61
- * Vertex AI Gemini API. This instance will be
62
- * configured to use the Vertex AI Gemini API.
63
- *
64
- * @param app - The {@link @firebase/app#FirebaseApp} to use.
65
- * @param options - Options to configure the Vertex AI instance, including the location.
66
- *
67
- * @public
68
- */
69
- export declare function getVertexAI(app?: FirebaseApp, options?: VertexAIOptions): VertexAI;
70
34
  /**
71
35
  * Returns the default {@link AI} instance that is associated with the provided
72
36
  * {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new instance with the
@@ -17,24 +17,6 @@
17
17
  import { FirebaseApp } from '@firebase/app';
18
18
  import { Backend } from './backend';
19
19
  export * from './types';
20
- /**
21
- * @deprecated Use the new {@link AI | AI} instead. The Vertex AI in Firebase SDK has been
22
- * replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
23
- * services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
24
- *
25
- * An instance of the Firebase AI SDK.
26
- *
27
- * @public
28
- */
29
- export type VertexAI = AI;
30
- /**
31
- * Options when initializing the Firebase AI SDK.
32
- *
33
- * @public
34
- */
35
- export interface VertexAIOptions {
36
- location?: string;
37
- }
38
20
  /**
39
21
  * An instance of the Firebase AI SDK.
40
22
  *
@@ -14,10 +14,10 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- import { TypedSchema } from '../requests/schema-builder';
17
+ import { ObjectSchema, TypedSchema } from '../requests/schema-builder';
18
18
  import { Content, Part } from './content';
19
19
  import { FunctionCallingMode, HarmBlockMethod, HarmBlockThreshold, HarmCategory, ResponseModality } from './enums';
20
- import { ObjectSchemaInterface, SchemaRequest } from './schema';
20
+ import { ObjectSchemaRequest, SchemaRequest } from './schema';
21
21
  /**
22
22
  * Base parameters for a number of methods.
23
23
  * @public
@@ -101,6 +101,10 @@ export interface GenerationConfig {
101
101
  * @beta
102
102
  */
103
103
  responseModalities?: ResponseModality[];
104
+ /**
105
+ * Configuration for "thinking" behavior of compatible Gemini models.
106
+ */
107
+ thinkingConfig?: ThinkingConfig;
104
108
  }
105
109
  /**
106
110
  * Params for {@link GenerativeModel.startChat}.
@@ -149,7 +153,7 @@ export interface RequestOptions {
149
153
  * Defines a tool that model can call to access external knowledge.
150
154
  * @public
151
155
  */
152
- export declare type Tool = FunctionDeclarationsTool;
156
+ export type Tool = FunctionDeclarationsTool | GoogleSearchTool;
153
157
  /**
154
158
  * Structured representation of a function declaration as defined by the
155
159
  * {@link https://spec.openapis.org/oas/v3.0.3 | OpenAPI 3.0 specification}.
@@ -159,7 +163,7 @@ export declare type Tool = FunctionDeclarationsTool;
159
163
  * as a Tool by the model and executed by the client.
160
164
  * @public
161
165
  */
162
- export declare interface FunctionDeclaration {
166
+ export interface FunctionDeclaration {
163
167
  /**
164
168
  * The name of the function to call. Must start with a letter or an
165
169
  * underscore. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with
@@ -176,7 +180,41 @@ export declare interface FunctionDeclaration {
176
180
  * format. Reflects the Open API 3.03 Parameter Object. Parameter names are
177
181
  * case-sensitive. For a function with no parameters, this can be left unset.
178
182
  */
179
- parameters?: ObjectSchemaInterface;
183
+ parameters?: ObjectSchema | ObjectSchemaRequest;
184
+ }
185
+ /**
186
+ * A tool that allows a Gemini model to connect to Google Search to access and incorporate
187
+ * up-to-date information from the web into its responses.
188
+ *
189
+ * Important: If using Grounding with Google Search, you are required to comply with the
190
+ * "Grounding with Google Search" usage requirements for your chosen API provider: {@link https://ai.google.dev/gemini-api/terms#grounding-with-google-search | Gemini Developer API}
191
+ * or Vertex AI Gemini API (see {@link https://cloud.google.com/terms/service-terms | Service Terms}
192
+ * section within the Service Specific Terms).
193
+ *
194
+ * @public
195
+ */
196
+ export interface GoogleSearchTool {
197
+ /**
198
+ * Specifies the Google Search configuration.
199
+ * Currently, this is an empty object, but it's reserved for future configuration options.
200
+ * Specifies the Google Search configuration. Currently, this is an empty object, but it's
201
+ * reserved for future configuration options.
202
+ *
203
+ * When using this feature, you are required to comply with the "Grounding with Google Search"
204
+ * usage requirements for your chosen API provider: {@link https://ai.google.dev/gemini-api/terms#grounding-with-google-search | Gemini Developer API}
205
+ * or Vertex AI Gemini API (see {@link https://cloud.google.com/terms/service-terms | Service Terms}
206
+ * section within the Service Specific Terms).
207
+ */
208
+ googleSearch: GoogleSearch;
209
+ }
210
+ /**
211
+ * Specifies the Google Search configuration.
212
+ *
213
+ * @remarks Currently, this is an empty object, but it's reserved for future configuration options.
214
+ *
215
+ * @public
216
+ */
217
+ export interface GoogleSearch {
180
218
  }
181
219
  /**
182
220
  * A `FunctionDeclarationsTool` is a piece of code that enables the system to
@@ -184,7 +222,7 @@ export declare interface FunctionDeclaration {
184
222
  * outside of knowledge and scope of the model.
185
223
  * @public
186
224
  */
187
- export declare interface FunctionDeclarationsTool {
225
+ export interface FunctionDeclarationsTool {
188
226
  /**
189
227
  * Optional. One or more function declarations
190
228
  * to be passed to the model along with the current user query. Model may
@@ -211,3 +249,27 @@ export interface FunctionCallingConfig {
211
249
  mode?: FunctionCallingMode;
212
250
  allowedFunctionNames?: string[];
213
251
  }
252
+ /**
253
+ * Configuration for "thinking" behavior of compatible Gemini models.
254
+ *
255
+ * Certain models utilize a thinking process before generating a response. This allows them to
256
+ * reason through complex problems and plan a more coherent and accurate answer.
257
+ *
258
+ * @public
259
+ */
260
+ export interface ThinkingConfig {
261
+ /**
262
+ * The thinking budget, in tokens.
263
+ *
264
+ * This parameter sets an upper limit on the number of tokens the model can use for its internal
265
+ * "thinking" process. A higher budget may result in higher quality responses for complex tasks
266
+ * but can also increase latency and cost.
267
+ *
268
+ * If you don't specify a budget, the model will determine the appropriate amount
269
+ * of thinking based on the complexity of the prompt.
270
+ *
271
+ * An error will be thrown if you set a thinking budget for a model that does not support this
272
+ * feature or if the specified budget is not within the model's supported range.
273
+ */
274
+ thinkingBudget?: number;
275
+ }
@@ -78,6 +78,10 @@ export interface GenerateContentResponse {
78
78
  export interface UsageMetadata {
79
79
  promptTokenCount: number;
80
80
  candidatesTokenCount: number;
81
+ /**
82
+ * The number of tokens used by the model's internal "thinking" process.
83
+ */
84
+ thoughtsTokenCount?: number;
81
85
  totalTokenCount: number;
82
86
  promptTokensDetails?: ModalityTokenCount[];
83
87
  candidatesTokensDetails?: ModalityTokenCount[];
@@ -151,34 +155,168 @@ export interface Citation {
151
155
  publicationDate?: Date;
152
156
  }
153
157
  /**
154
- * Metadata returned to client when grounding is enabled.
158
+ * Metadata returned when grounding is enabled.
159
+ *
160
+ * Currently, only Grounding with Google Search is supported (see {@link GoogleSearchTool}).
161
+ *
162
+ * Important: If using Grounding with Google Search, you are required to comply with the
163
+ * "Grounding with Google Search" usage requirements for your chosen API provider: {@link https://ai.google.dev/gemini-api/terms#grounding-with-google-search | Gemini Developer API}
164
+ * or Vertex AI Gemini API (see {@link https://cloud.google.com/terms/service-terms | Service Terms}
165
+ * section within the Service Specific Terms).
166
+ *
155
167
  * @public
156
168
  */
157
169
  export interface GroundingMetadata {
170
+ /**
171
+ * Google Search entry point for web searches. This contains an HTML/CSS snippet that must be
172
+ * embedded in an app to display a Google Search entry point for follow-up web searches related to
173
+ * a model's “Grounded Response”.
174
+ */
175
+ searchEntryPoint?: SearchEntrypoint;
176
+ /**
177
+ * A list of {@link GroundingChunk} objects. Each chunk represents a piece of retrieved content
178
+ * (for example, from a web page). that the model used to ground its response.
179
+ */
180
+ groundingChunks?: GroundingChunk[];
181
+ /**
182
+ * A list of {@link GroundingSupport} objects. Each object details how specific segments of the
183
+ * model's response are supported by the `groundingChunks`.
184
+ */
185
+ groundingSupports?: GroundingSupport[];
186
+ /**
187
+ * A list of web search queries that the model performed to gather the grounding information.
188
+ * These can be used to allow users to explore the search results themselves.
189
+ */
158
190
  webSearchQueries?: string[];
191
+ /**
192
+ * @deprecated Use {@link GroundingSupport} instead.
193
+ */
159
194
  retrievalQueries?: string[];
160
195
  /**
161
- * @deprecated
196
+ * @deprecated Use {@link GroundingChunk} instead.
162
197
  */
163
198
  groundingAttributions: GroundingAttribution[];
164
199
  }
165
200
  /**
166
- * @deprecated
201
+ * Google search entry point.
202
+ *
167
203
  * @public
168
204
  */
169
- export interface GroundingAttribution {
170
- segment: Segment;
171
- confidenceScore?: number;
172
- web?: WebAttribution;
173
- retrievedContext?: RetrievedContextAttribution;
205
+ export interface SearchEntrypoint {
206
+ /**
207
+ * HTML/CSS snippet that must be embedded in a web page. The snippet is designed to avoid
208
+ * undesired interaction with the rest of the page's CSS.
209
+ *
210
+ * To ensure proper rendering and prevent CSS conflicts, it is recommended
211
+ * to encapsulate this `renderedContent` within a shadow DOM when embedding it
212
+ * into a webpage. See {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM | MDN: Using shadow DOM}.
213
+ *
214
+ * @example
215
+ * ```javascript
216
+ * const container = document.createElement('div');
217
+ * document.body.appendChild(container);
218
+ * container.attachShadow({ mode: 'open' }).innerHTML = renderedContent;
219
+ * ```
220
+ */
221
+ renderedContent?: string;
222
+ }
223
+ /**
224
+ * Represents a chunk of retrieved data that supports a claim in the model's response. This is part
225
+ * of the grounding information provided when grounding is enabled.
226
+ *
227
+ * @public
228
+ */
229
+ export interface GroundingChunk {
230
+ /**
231
+ * Contains details if the grounding chunk is from a web source.
232
+ */
233
+ web?: WebGroundingChunk;
174
234
  }
175
235
  /**
236
+ * A grounding chunk from the web.
237
+ *
238
+ * Important: If using Grounding with Google Search, you are required to comply with the
239
+ * {@link https://cloud.google.com/terms/service-terms | Service Specific Terms} for "Grounding with Google Search".
240
+ *
241
+ * @public
242
+ */
243
+ export interface WebGroundingChunk {
244
+ /**
245
+ * The URI of the retrieved web page.
246
+ */
247
+ uri?: string;
248
+ /**
249
+ * The title of the retrieved web page.
250
+ */
251
+ title?: string;
252
+ /**
253
+ * The domain of the original URI from which the content was retrieved.
254
+ *
255
+ * This property is only supported in the Vertex AI Gemini API ({@link VertexAIBackend}).
256
+ * When using the Gemini Developer API ({@link GoogleAIBackend}), this property will be
257
+ * `undefined`.
258
+ */
259
+ domain?: string;
260
+ }
261
+ /**
262
+ * Provides information about how a specific segment of the model's response is supported by the
263
+ * retrieved grounding chunks.
264
+ *
265
+ * @public
266
+ */
267
+ export interface GroundingSupport {
268
+ /**
269
+ * Specifies the segment of the model's response content that this grounding support pertains to.
270
+ */
271
+ segment?: Segment;
272
+ /**
273
+ * A list of indices that refer to specific {@link GroundingChunk} objects within the
274
+ * {@link GroundingMetadata.groundingChunks} array. These referenced chunks
275
+ * are the sources that support the claim made in the associated `segment` of the response.
276
+ * For example, an array `[1, 3, 4]` means that `groundingChunks[1]`, `groundingChunks[3]`,
277
+ * and `groundingChunks[4]` are the retrieved content supporting this part of the response.
278
+ */
279
+ groundingChunkIndices?: number[];
280
+ }
281
+ /**
282
+ * Represents a specific segment within a {@link Content} object, often used to
283
+ * pinpoint the exact location of text or data that grounding information refers to.
284
+ *
176
285
  * @public
177
286
  */
178
287
  export interface Segment {
288
+ /**
289
+ * The zero-based index of the {@link Part} object within the `parts` array
290
+ * of its parent {@link Content} object. This identifies which part of the
291
+ * content the segment belongs to.
292
+ */
179
293
  partIndex: number;
294
+ /**
295
+ * The zero-based start index of the segment within the specified `Part`,
296
+ * measured in UTF-8 bytes. This offset is inclusive, starting from 0 at the
297
+ * beginning of the part's content (e.g., `Part.text`).
298
+ */
180
299
  startIndex: number;
300
+ /**
301
+ * The zero-based end index of the segment within the specified `Part`,
302
+ * measured in UTF-8 bytes. This offset is exclusive, meaning the character
303
+ * at this index is not included in the segment.
304
+ */
181
305
  endIndex: number;
306
+ /**
307
+ * The text corresponding to the segment from the response.
308
+ */
309
+ text: string;
310
+ }
311
+ /**
312
+ * @deprecated
313
+ * @public
314
+ */
315
+ export interface GroundingAttribution {
316
+ segment: Segment;
317
+ confidenceScore?: number;
318
+ web?: WebAttribution;
319
+ retrievedContext?: RetrievedContextAttribution;
182
320
  }
183
321
  /**
184
322
  * @public
@@ -243,11 +381,10 @@ export interface CountTokensResponse {
243
381
  */
244
382
  totalTokens: number;
245
383
  /**
384
+ * @deprecated Use `totalTokens` instead. This property is undefined when using models greater than `gemini-1.5-*`.
385
+ *
246
386
  * The total number of billable characters counted across all instances
247
387
  * from the request.
248
- *
249
- * This property is only supported when using the Vertex AI Gemini API ({@link VertexAIBackend}).
250
- * When using the Gemini Developer API ({@link GoogleAIBackend}), this property is not supported and will default to 0.
251
388
  */
252
389
  totalBillableCharacters?: number;
253
390
  /**
@@ -109,10 +109,18 @@ export interface SchemaInterface extends SchemaShared<SchemaInterface> {
109
109
  type: SchemaType;
110
110
  }
111
111
  /**
112
- * Interface for {@link ObjectSchema} class.
112
+ * Interface for JSON parameters in {@link SchemaType.OBJECT} format when
113
+ * not using the {@link ObjectSchema} helper.
113
114
  * @public
114
115
  */
115
- export interface ObjectSchemaInterface extends SchemaInterface {
116
+ export interface ObjectSchemaRequest extends SchemaRequest {
116
117
  type: SchemaType.OBJECT;
117
- optionalProperties?: string[];
118
+ /**
119
+ * This is not a property accepted in the final request to the backend, but is
120
+ * a client-side convenience property that is only usable by constructing
121
+ * a schema through the `Schema.object()` helper method. Populating this
122
+ * property will cause response errors if the object is not wrapped with
123
+ * `Schema.object()`.
124
+ */
125
+ optionalProperties?: never;
118
126
  }
package/dist/index.cjs.js CHANGED
@@ -9,7 +9,7 @@ var logger$1 = require('@firebase/logger');
9
9
  var tslib = require('tslib');
10
10
 
11
11
  var name = "@firebase/ai";
12
- var version = "1.4.0-canary.c0617a341";
12
+ var version = "1.4.0-canary.ec5f37403";
13
13
 
14
14
  /**
15
15
  * @license
@@ -1000,6 +1000,7 @@ async function makeRequest(model, task, apiSettings, stream, body, requestOption
1000
1000
  // ignored
1001
1001
  }
1002
1002
  if (response.status === 403 &&
1003
+ errorDetails &&
1003
1004
  errorDetails.some((detail) => detail.reason === 'SERVICE_DISABLED') &&
1004
1005
  errorDetails.some((detail) => {
1005
1006
  var _a, _b;
@@ -1579,6 +1580,8 @@ function aggregateResponses(responses) {
1579
1580
  candidate.finishMessage;
1580
1581
  aggregatedResponse.candidates[i].safetyRatings =
1581
1582
  candidate.safetyRatings;
1583
+ aggregatedResponse.candidates[i].groundingMetadata =
1584
+ candidate.groundingMetadata;
1582
1585
  /**
1583
1586
  * Candidates should always have content and parts, but this handles
1584
1587
  * possible malformed responses.
@@ -2492,50 +2495,6 @@ class ImagenImageFormat {
2492
2495
  * See the License for the specific language governing permissions and
2493
2496
  * limitations under the License.
2494
2497
  */
2495
- /**
2496
- * @deprecated Use the new {@link AIModel} instead. The Vertex AI in Firebase SDK has been
2497
- * replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
2498
- * services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
2499
- *
2500
- * Base class for Firebase AI model APIs.
2501
- *
2502
- * @public
2503
- */
2504
- const VertexAIModel = AIModel;
2505
- /**
2506
- * @deprecated Use the new {@link AIError} instead. The Vertex AI in Firebase SDK has been
2507
- * replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
2508
- * services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
2509
- *
2510
- * Error class for the Firebase AI SDK.
2511
- *
2512
- * @public
2513
- */
2514
- const VertexAIError = AIError;
2515
- /**
2516
- * @deprecated Use the new {@link getAI | getAI()} instead. The Vertex AI in Firebase SDK has been
2517
- * replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
2518
- * services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
2519
- *
2520
- * Returns a {@link VertexAI} instance for the given app, configured to use the
2521
- * Vertex AI Gemini API. This instance will be
2522
- * configured to use the Vertex AI Gemini API.
2523
- *
2524
- * @param app - The {@link @firebase/app#FirebaseApp} to use.
2525
- * @param options - Options to configure the Vertex AI instance, including the location.
2526
- *
2527
- * @public
2528
- */
2529
- function getVertexAI(app$1 = app.getApp(), options) {
2530
- app$1 = util.getModularInstance(app$1);
2531
- // Dependencies
2532
- const AIProvider = app._getProvider(app$1, AI_TYPE);
2533
- const backend = new VertexAIBackend(options === null || options === void 0 ? void 0 : options.location);
2534
- const identifier = encodeInstanceIdentifier(backend);
2535
- return AIProvider.getImmediate({
2536
- identifier
2537
- });
2538
- }
2539
2498
  /**
2540
2499
  * Returns the default {@link AI} instance that is associated with the provided
2541
2500
  * {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new instance with the
@@ -2648,10 +2607,7 @@ exports.ResponseModality = ResponseModality;
2648
2607
  exports.Schema = Schema;
2649
2608
  exports.StringSchema = StringSchema;
2650
2609
  exports.VertexAIBackend = VertexAIBackend;
2651
- exports.VertexAIError = VertexAIError;
2652
- exports.VertexAIModel = VertexAIModel;
2653
2610
  exports.getAI = getAI;
2654
2611
  exports.getGenerativeModel = getGenerativeModel;
2655
2612
  exports.getImagenModel = getImagenModel;
2656
- exports.getVertexAI = getVertexAI;
2657
2613
  //# sourceMappingURL=index.cjs.js.map