@firebase/ai 2.0.0 → 2.1.0-canary.02280d747

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.
Files changed (51) hide show
  1. package/dist/ai-public.d.ts +217 -9
  2. package/dist/ai.d.ts +220 -10
  3. package/dist/esm/index.esm.js +394 -34
  4. package/dist/esm/index.esm.js.map +1 -1
  5. package/dist/esm/src/api.d.ts +2 -2
  6. package/dist/esm/src/constants.d.ts +4 -0
  7. package/dist/esm/src/index.d.ts +3 -0
  8. package/dist/esm/src/methods/chat-session.d.ts +3 -1
  9. package/dist/esm/src/methods/chrome-adapter.d.ts +120 -0
  10. package/dist/esm/src/methods/count-tokens.d.ts +3 -1
  11. package/dist/esm/src/methods/generate-content.d.ts +3 -2
  12. package/dist/esm/src/models/ai-model.d.ts +1 -1
  13. package/dist/esm/src/models/generative-model.d.ts +3 -1
  14. package/dist/esm/src/public-types.d.ts +10 -1
  15. package/dist/esm/src/service.d.ts +4 -1
  16. package/dist/esm/src/types/chrome-adapter.d.ts +56 -0
  17. package/dist/esm/src/types/enums.d.ts +20 -1
  18. package/dist/esm/src/types/imagen/requests.d.ts +2 -2
  19. package/dist/esm/src/types/imagen/responses.d.ts +1 -0
  20. package/dist/esm/src/types/index.d.ts +2 -0
  21. package/dist/esm/src/types/language-model.d.ts +117 -0
  22. package/dist/esm/src/types/requests.d.ts +35 -2
  23. package/dist/esm/src/types/responses.d.ts +1 -1
  24. package/dist/esm/src/types/schema.d.ts +1 -1
  25. package/dist/index.cjs.js +395 -33
  26. package/dist/index.cjs.js.map +1 -1
  27. package/dist/index.node.cjs.js +382 -22
  28. package/dist/index.node.cjs.js.map +1 -1
  29. package/dist/index.node.mjs +382 -23
  30. package/dist/index.node.mjs.map +1 -1
  31. package/dist/src/api.d.ts +2 -2
  32. package/dist/src/constants.d.ts +4 -0
  33. package/dist/src/index.d.ts +3 -0
  34. package/dist/src/methods/chat-session.d.ts +3 -1
  35. package/dist/src/methods/chrome-adapter.d.ts +120 -0
  36. package/dist/src/methods/count-tokens.d.ts +3 -1
  37. package/dist/src/methods/generate-content.d.ts +3 -2
  38. package/dist/src/models/ai-model.d.ts +1 -1
  39. package/dist/src/models/generative-model.d.ts +3 -1
  40. package/dist/src/public-types.d.ts +10 -1
  41. package/dist/src/service.d.ts +4 -1
  42. package/dist/src/types/chrome-adapter.d.ts +56 -0
  43. package/dist/src/types/enums.d.ts +20 -1
  44. package/dist/src/types/imagen/requests.d.ts +2 -2
  45. package/dist/src/types/imagen/responses.d.ts +1 -0
  46. package/dist/src/types/index.d.ts +2 -0
  47. package/dist/src/types/language-model.d.ts +117 -0
  48. package/dist/src/types/requests.d.ts +35 -2
  49. package/dist/src/types/responses.d.ts +1 -1
  50. package/dist/src/types/schema.d.ts +1 -1
  51. package/package.json +8 -8
@@ -4,10 +4,17 @@
4
4
  * @packageDocumentation
5
5
  */
6
6
 
7
+ import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
7
8
  import { AppCheckTokenResult } from '@firebase/app-check-interop-types';
9
+ import { ComponentContainer } from '@firebase/component';
8
10
  import { FirebaseApp } from '@firebase/app';
11
+ import { FirebaseAppCheckInternal } from '@firebase/app-check-interop-types';
12
+ import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
13
+ import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
9
14
  import { FirebaseAuthTokenData } from '@firebase/auth-interop-types';
10
15
  import { FirebaseError } from '@firebase/util';
16
+ import { InstanceFactoryOptions } from '@firebase/component';
17
+ import { Provider } from '@firebase/component';
11
18
 
12
19
  /**
13
20
  * An instance of the Firebase AI SDK.
@@ -27,6 +34,10 @@ export declare interface AI {
27
34
  * Vertex AI Gemini API (using {@link VertexAIBackend}).
28
35
  */
29
36
  backend: Backend;
37
+ /**
38
+ * Options applied to this {@link AI} instance.
39
+ */
40
+ options?: AIOptions;
30
41
  /**
31
42
  * @deprecated use `AI.backend.location` instead.
32
43
  *
@@ -125,8 +136,26 @@ export declare abstract class AIModel {
125
136
  export declare interface AIOptions {
126
137
  /**
127
138
  * The backend configuration to use for the AI service instance.
139
+ * Defaults to the Gemini Developer API backend ({@link GoogleAIBackend}).
140
+ */
141
+ backend?: Backend;
142
+ /**
143
+ * Whether to use App Check limited use tokens. Defaults to false.
128
144
  */
145
+ useLimitedUseAppCheckTokens?: boolean;
146
+ }
147
+
148
+ declare class AIService implements AI, _FirebaseService {
149
+ app: FirebaseApp;
129
150
  backend: Backend;
151
+ auth: FirebaseAuthInternal | null;
152
+ appCheck: FirebaseAppCheckInternal | null;
153
+ _options?: Omit<AIOptions, 'backend'>;
154
+ location: string;
155
+ constructor(app: FirebaseApp, backend: Backend, authProvider?: Provider<FirebaseAuthInternalName>, appCheckProvider?: Provider<AppCheckInternalComponentName>);
156
+ _delete(): Promise<void>;
157
+ set options(optionsToSet: AIOptions);
158
+ get options(): AIOptions | undefined;
130
159
  }
131
160
 
132
161
  /**
@@ -275,12 +304,13 @@ export declare class BooleanSchema extends Schema {
275
304
  */
276
305
  export declare class ChatSession {
277
306
  model: string;
307
+ private chromeAdapter?;
278
308
  params?: StartChatParams | undefined;
279
309
  requestOptions?: RequestOptions | undefined;
280
310
  private _apiSettings;
281
311
  private _history;
282
312
  private _sendPromise;
283
- constructor(apiSettings: ApiSettings, model: string, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
313
+ constructor(apiSettings: ApiSettings, model: string, chromeAdapter?: ChromeAdapter | undefined, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
284
314
  /**
285
315
  * Gets the chat history so far. Blocked prompts are not added to history.
286
316
  * Neither blocked candidates nor the prompts that generated them are added
@@ -300,6 +330,43 @@ export declare class ChatSession {
300
330
  sendMessageStream(request: string | Array<string | Part>): Promise<GenerateContentStreamResult>;
301
331
  }
302
332
 
333
+ /**
334
+ * <b>(EXPERIMENTAL)</b> Defines an inference "backend" that uses Chrome's on-device model,
335
+ * and encapsulates logic for detecting when on-device inference is
336
+ * possible.
337
+ *
338
+ * These methods should not be called directly by the user.
339
+ *
340
+ * @public
341
+ */
342
+ export declare interface ChromeAdapter {
343
+ /**
344
+ * Checks if the on-device model is capable of handling a given
345
+ * request.
346
+ * @param request - A potential request to be passed to the model.
347
+ */
348
+ isAvailable(request: GenerateContentRequest): Promise<boolean>;
349
+ /**
350
+ * Generates content using on-device inference.
351
+ *
352
+ * @remarks
353
+ * This is comparable to {@link GenerativeModel.generateContent} for generating
354
+ * content using in-cloud inference.
355
+ * @param request - a standard Firebase AI {@link GenerateContentRequest}
356
+ */
357
+ generateContent(request: GenerateContentRequest): Promise<Response>;
358
+ /**
359
+ * Generates a content stream using on-device inference.
360
+ *
361
+ * @remarks
362
+ * This is comparable to {@link GenerativeModel.generateContentStream} for generating
363
+ * a content stream using in-cloud inference.
364
+ * @param request - a standard Firebase AI {@link GenerateContentRequest}
365
+ */
366
+ generateContentStream(request: GenerateContentRequest): Promise<Response>;
367
+ /* Excluded from this release type: countTokens */
368
+ }
369
+
303
370
  /**
304
371
  * A single citation.
305
372
  * @public
@@ -449,6 +516,8 @@ export declare interface ErrorDetails {
449
516
  [key: string]: unknown;
450
517
  }
451
518
 
519
+ export declare function factory(container: ComponentContainer, { instanceIdentifier }: InstanceFactoryOptions): AIService;
520
+
452
521
  /**
453
522
  * Data pointing to a file uploaded on Google Cloud Storage.
454
523
  * @public
@@ -519,6 +588,8 @@ export declare const FinishReason: {
519
588
  */
520
589
  export declare type FinishReason = (typeof FinishReason)[keyof typeof FinishReason];
521
590
 
591
+ /* Excluded from this release type: _FirebaseService */
592
+
522
593
  /**
523
594
  * A predicted {@link FunctionCall} returned from the model
524
595
  * that contains a string representing the {@link FunctionDeclaration.name}
@@ -561,6 +632,9 @@ export declare const FunctionCallingMode: {
561
632
  readonly NONE: "NONE";
562
633
  };
563
634
 
635
+ /**
636
+ * @public
637
+ */
564
638
  export declare type FunctionCallingMode = (typeof FunctionCallingMode)[keyof typeof FunctionCallingMode];
565
639
 
566
640
  /**
@@ -770,13 +844,14 @@ export declare interface GenerativeContentBlob {
770
844
  * @public
771
845
  */
772
846
  export declare class GenerativeModel extends AIModel {
847
+ private chromeAdapter?;
773
848
  generationConfig: GenerationConfig;
774
849
  safetySettings: SafetySetting[];
775
850
  requestOptions?: RequestOptions;
776
851
  tools?: Tool[];
777
852
  toolConfig?: ToolConfig;
778
853
  systemInstruction?: Content;
779
- constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions);
854
+ constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions, chromeAdapter?: ChromeAdapter | undefined);
780
855
  /**
781
856
  * Makes a single non-streaming call to the model
782
857
  * and returns an object containing a single {@link GenerateContentResponse}.
@@ -836,7 +911,7 @@ export declare function getAI(app?: FirebaseApp, options?: AIOptions): AI;
836
911
  *
837
912
  * @public
838
913
  */
839
- export declare function getGenerativeModel(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions): GenerativeModel;
914
+ export declare function getGenerativeModel(ai: AI, modelParams: ModelParams | HybridParams, requestOptions?: RequestOptions): GenerativeModel;
840
915
 
841
916
  /**
842
917
  * Returns an {@link ImagenModel} class with methods for using Imagen.
@@ -942,7 +1017,7 @@ export declare interface GroundingMetadata {
942
1017
  /**
943
1018
  * Google Search entry point for web searches. This contains an HTML/CSS snippet that must be
944
1019
  * embedded in an app to display a Google Search entry point for follow-up web searches related to
945
- * a model's Grounded Response”.
1020
+ * a model's "Grounded Response".
946
1021
  */
947
1022
  searchEntryPoint?: SearchEntrypoint;
948
1023
  /**
@@ -1033,7 +1108,7 @@ export declare const HarmBlockThreshold: {
1033
1108
  readonly BLOCK_NONE: "BLOCK_NONE";
1034
1109
  /**
1035
1110
  * All content will be allowed. This is the same as `BLOCK_NONE`, but the metadata corresponding
1036
- * to the {@link HarmCategory} will not be present in the response.
1111
+ * to the {@link (HarmCategory:type)} will not be present in the response.
1037
1112
  */
1038
1113
  readonly OFF: "OFF";
1039
1114
  };
@@ -1126,13 +1201,33 @@ export declare const HarmSeverity: {
1126
1201
  */
1127
1202
  export declare type HarmSeverity = (typeof HarmSeverity)[keyof typeof HarmSeverity];
1128
1203
 
1204
+ /**
1205
+ * <b>(EXPERIMENTAL)</b>
1206
+ * Configures hybrid inference.
1207
+ * @public
1208
+ */
1209
+ export declare interface HybridParams {
1210
+ /**
1211
+ * Specifies on-device or in-cloud inference. Defaults to prefer on-device.
1212
+ */
1213
+ mode: InferenceMode;
1214
+ /**
1215
+ * Optional. Specifies advanced params for on-device inference.
1216
+ */
1217
+ onDeviceParams?: OnDeviceParams;
1218
+ /**
1219
+ * Optional. Specifies advanced params for in-cloud inference.
1220
+ */
1221
+ inCloudParams?: ModelParams;
1222
+ }
1223
+
1129
1224
  /**
1130
1225
  * Aspect ratios for Imagen images.
1131
1226
  *
1132
1227
  * To specify an aspect ratio for generated images, set the `aspectRatio` property in your
1133
1228
  * {@link ImagenGenerationConfig}.
1134
1229
  *
1135
- * See the the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
1230
+ * See the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
1136
1231
  * for more details and examples of the supported aspect ratios.
1137
1232
  *
1138
1233
  * @beta
@@ -1166,7 +1261,7 @@ export declare const ImagenAspectRatio: {
1166
1261
  * To specify an aspect ratio for generated images, set the `aspectRatio` property in your
1167
1262
  * {@link ImagenGenerationConfig}.
1168
1263
  *
1169
- * See the the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
1264
+ * See the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
1170
1265
  * for more details and examples of the supported aspect ratios.
1171
1266
  *
1172
1267
  * @beta
@@ -1177,6 +1272,7 @@ export declare type ImagenAspectRatio = (typeof ImagenAspectRatio)[keyof typeof
1177
1272
  * An image generated by Imagen, stored in a Cloud Storage for Firebase bucket.
1178
1273
  *
1179
1274
  * This feature is not available yet.
1275
+ * @beta
1180
1276
  */
1181
1277
  export declare interface ImagenGCSImage {
1182
1278
  /**
@@ -1571,6 +1667,24 @@ export declare interface ImagenSafetySettings {
1571
1667
  personFilterLevel?: ImagenPersonFilterLevel;
1572
1668
  }
1573
1669
 
1670
+ /**
1671
+ * <b>(EXPERIMENTAL)</b>
1672
+ * Determines whether inference happens on-device or in-cloud.
1673
+ * @public
1674
+ */
1675
+ export declare const InferenceMode: {
1676
+ readonly PREFER_ON_DEVICE: "prefer_on_device";
1677
+ readonly ONLY_ON_DEVICE: "only_on_device";
1678
+ readonly ONLY_IN_CLOUD: "only_in_cloud";
1679
+ };
1680
+
1681
+ /**
1682
+ * <b>(EXPERIMENTAL)</b>
1683
+ * Determines whether inference happens on-device or in-cloud.
1684
+ * @public
1685
+ */
1686
+ export declare type InferenceMode = (typeof InferenceMode)[keyof typeof InferenceMode];
1687
+
1574
1688
  /**
1575
1689
  * Content part interface if the part represents an image.
1576
1690
  * @public
@@ -1594,6 +1708,86 @@ export declare class IntegerSchema extends Schema {
1594
1708
  constructor(schemaParams?: SchemaParams);
1595
1709
  }
1596
1710
 
1711
+ /**
1712
+ * <b>(EXPERIMENTAL)</b>
1713
+ * Configures the creation of an on-device language model session.
1714
+ * @public
1715
+ */
1716
+ export declare interface LanguageModelCreateCoreOptions {
1717
+ topK?: number;
1718
+ temperature?: number;
1719
+ expectedInputs?: LanguageModelExpected[];
1720
+ }
1721
+
1722
+ /**
1723
+ * <b>(EXPERIMENTAL)</b>
1724
+ * Configures the creation of an on-device language model session.
1725
+ * @public
1726
+ */
1727
+ export declare interface LanguageModelCreateOptions extends LanguageModelCreateCoreOptions {
1728
+ signal?: AbortSignal;
1729
+ initialPrompts?: LanguageModelMessage[];
1730
+ }
1731
+
1732
+ /**
1733
+ * <b>(EXPERIMENTAL)</b>
1734
+ * Options for the expected inputs for an on-device language model.
1735
+ * @public
1736
+ */ export declare interface LanguageModelExpected {
1737
+ type: LanguageModelMessageType;
1738
+ languages?: string[];
1739
+ }
1740
+
1741
+ /**
1742
+ * <b>(EXPERIMENTAL)</b>
1743
+ * An on-device language model message.
1744
+ * @public
1745
+ */
1746
+ export declare interface LanguageModelMessage {
1747
+ role: LanguageModelMessageRole;
1748
+ content: LanguageModelMessageContent[];
1749
+ }
1750
+
1751
+ /**
1752
+ * <b>(EXPERIMENTAL)</b>
1753
+ * An on-device language model content object.
1754
+ * @public
1755
+ */
1756
+ export declare interface LanguageModelMessageContent {
1757
+ type: LanguageModelMessageType;
1758
+ value: LanguageModelMessageContentValue;
1759
+ }
1760
+
1761
+ /**
1762
+ * <b>(EXPERIMENTAL)</b>
1763
+ * Content formats that can be provided as on-device message content.
1764
+ * @public
1765
+ */
1766
+ export declare type LanguageModelMessageContentValue = ImageBitmapSource | AudioBuffer | BufferSource | string;
1767
+
1768
+ /**
1769
+ * <b>(EXPERIMENTAL)</b>
1770
+ * Allowable roles for on-device language model usage.
1771
+ * @public
1772
+ */
1773
+ export declare type LanguageModelMessageRole = 'system' | 'user' | 'assistant';
1774
+
1775
+ /**
1776
+ * <b>(EXPERIMENTAL)</b>
1777
+ * Allowable types for on-device language model messages.
1778
+ * @public
1779
+ */
1780
+ export declare type LanguageModelMessageType = 'text' | 'image' | 'audio';
1781
+
1782
+ /**
1783
+ * <b>(EXPERIMENTAL)</b>
1784
+ * Options for an on-device language model prompt.
1785
+ * @public
1786
+ */
1787
+ export declare interface LanguageModelPromptOptions {
1788
+ responseConstraint?: object;
1789
+ }
1790
+
1597
1791
  /**
1598
1792
  * Content part modality.
1599
1793
  * @public
@@ -1679,7 +1873,7 @@ export declare class ObjectSchema extends Schema {
1679
1873
  }
1680
1874
 
1681
1875
  /**
1682
- * Interface for JSON parameters in a schema of {@link SchemaType}
1876
+ * Interface for JSON parameters in a schema of {@link (SchemaType:type)}
1683
1877
  * "object" when not using the `Schema.object()` helper.
1684
1878
  * @public
1685
1879
  */
@@ -1695,6 +1889,17 @@ export declare interface ObjectSchemaRequest extends SchemaRequest {
1695
1889
  optionalProperties?: never;
1696
1890
  }
1697
1891
 
1892
+ /**
1893
+ * <b>(EXPERIMENTAL)</b>
1894
+ * Encapsulates configuration for on-device inference.
1895
+ *
1896
+ * @public
1897
+ */
1898
+ export declare interface OnDeviceParams {
1899
+ createOptions?: LanguageModelCreateOptions;
1900
+ promptOptions?: LanguageModelPromptOptions;
1901
+ }
1902
+
1698
1903
  /**
1699
1904
  * Content part - includes text, image/video, or function call/response
1700
1905
  * part types.
@@ -1734,7 +1939,10 @@ export declare interface RequestOptions {
1734
1939
  */
1735
1940
  timeout?: number;
1736
1941
  /**
1737
- * Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com
1942
+ * Base url for endpoint. Defaults to
1943
+ * https://firebasevertexai.googleapis.com, which is the
1944
+ * {@link https://console.cloud.google.com/apis/library/firebasevertexai.googleapis.com?project=_ | Firebase AI Logic API}
1945
+ * (used regardless of your chosen Gemini API provider).
1738
1946
  */
1739
1947
  baseUrl?: string;
1740
1948
  }