Skip to content

Commit cae765f

Browse files
pashankagsiddh
authored andcommitted
Complete Hybrid inference impl
Fix languageCode parameter in action_code_url (#8912) * Fix languageCode parameter in action_code_url * Add changeset Vaihi add langmodel types. (#8927) * Adding LanguageModel types. These are based off https://github.com/webmachinelearning/prompt-api?tab=readme-ov-file#full-api-surface-in-web-idl * Adding LanguageModel types. * Remove bunch of exports * yarn formatted * after lint Define HybridParams (#8935) Co-authored-by: Erik Eldridge <[email protected]> Adding smoke test for new hybrid params (#8937) * Adding smoke test for new hybrid params * Use the existing name of the model params input --------- Co-authored-by: Erik Eldridge <[email protected]> Moving to in-cloud naming (#8938) Co-authored-by: Erik Eldridge <[email protected]> Moving to string type for the inference mode (#8941) Define ChromeAdapter class (#8942) Co-authored-by: Erik Eldridge <[email protected]> VinF Hybrid Inference: Implement ChromeAdapter (rebased) (#8943) Adding count token impl (#8950) VinF Hybrid Inference #4: ChromeAdapter in stream methods (rebased) (#8949) Define values for Availability enum (#8951) VinF Hybrid Inference: narrow Chrome input type (#8953) Add image inference support (#8954) * Adding image based input for inference * adding image as input to create language model object disable count tokens api for on-device inference (#8962) VinF Hybrid Inference: throw if only_on_device and model is unavailable (#8965)
1 parent 475c81a commit cae765f

26 files changed

+1323
-117
lines changed

.changeset/great-cheetahs-invite.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/auth': patch
3+
---
4+
5+
Fixed: `ActionCodeURL` not populating `languageCode` from the url.

common/api-review/vertexai.api.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export class BooleanSchema extends Schema {
112112

113113
// @public
114114
export class ChatSession {
115-
constructor(apiSettings: ApiSettings, model: string, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
115+
// Warning: (ae-forgotten-export) The symbol "ChromeAdapter" needs to be exported by the entry point index.d.ts
116+
constructor(apiSettings: ApiSettings, model: string, chromeAdapter: ChromeAdapter, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
116117
getHistory(): Promise<Content[]>;
117118
// (undocumented)
118119
model: string;
@@ -392,8 +393,9 @@ export interface GenerativeContentBlob {
392393

393394
// @public
394395
export class GenerativeModel extends AIModel {
395-
constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions);
396+
constructor(ai: AI, modelParams: ModelParams, chromeAdapter: ChromeAdapter, requestOptions?: RequestOptions);
396397
countTokens(request: CountTokensRequest | string | Array<string | Part>): Promise<CountTokensResponse>;
398+
static DEFAULT_HYBRID_IN_CLOUD_MODEL: string;
397399
generateContent(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentResult>;
398400
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentStreamResult>;
399401
// (undocumented)
@@ -415,7 +417,7 @@ export class GenerativeModel extends AIModel {
415417
export function getAI(app?: FirebaseApp, options?: AIOptions): AI;
416418

417419
// @public
418-
export function getGenerativeModel(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions): GenerativeModel;
420+
export function getGenerativeModel(ai: AI, modelParams: ModelParams | HybridParams, requestOptions?: RequestOptions): GenerativeModel;
419421

420422
// @beta
421423
export function getImagenModel(ai: AI, modelParams: ImagenModelParams, requestOptions?: RequestOptions): ImagenModel;
@@ -547,6 +549,22 @@ export enum HarmSeverity {
547549
HARM_SEVERITY_UNSUPPORTED = "HARM_SEVERITY_UNSUPPORTED"
548550
}
549551

552+
// @public
553+
export interface HybridParams {
554+
inCloudParams?: ModelParams;
555+
mode: InferenceMode;
556+
// Warning: (ae-forgotten-export) The symbol "LanguageModelCreateOptions" needs to be exported by the entry point index.d.ts
557+
onDeviceParams?: LanguageModelCreateOptions;
558+
}
559+
560+
// @public
561+
export interface HybridParams {
562+
inCloudParams?: ModelParams;
563+
mode: InferenceMode;
564+
// Warning: (ae-forgotten-export) The symbol "LanguageModelCreateOptions" needs to be exported by the entry point index.d.ts
565+
onDeviceParams?: LanguageModelCreateOptions;
566+
}
567+
550568
// @beta
551569
export enum ImagenAspectRatio {
552570
LANDSCAPE_16x9 = "16:9",
@@ -631,6 +649,9 @@ export interface ImagenSafetySettings {
631649
safetyFilterLevel?: ImagenSafetyFilterLevel;
632650
}
633651

652+
// @public
653+
export type InferenceMode = 'prefer_on_device' | 'only_on_device' | 'only_in_cloud';
654+
634655
// @public
635656
export interface InlineDataPart {
636657
// (undocumented)

docs-devsite/_toc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ toc:
544544
path: /docs/reference/js/vertexai.groundingattribution.md
545545
- title: GroundingMetadata
546546
path: /docs/reference/js/vertexai.groundingmetadata.md
547+
- title: HybridParams
548+
path: /docs/reference/js/vertexai.hybridparams.md
547549
- title: ImagenGCSImage
548550
path: /docs/reference/js/vertexai.imagengcsimage.md
549551
- title: ImagenGenerationConfig

docs-devsite/vertexai.chatsession.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export declare class ChatSession
2222

2323
| Constructor | Modifiers | Description |
2424
| --- | --- | --- |
25-
| [(constructor)(apiSettings, model, params, requestOptions)](./vertexai.chatsession.md#chatsessionconstructor) | | Constructs a new instance of the <code>ChatSession</code> class |
25+
| [(constructor)(apiSettings, model, chromeAdapter, params, requestOptions)](./vertexai.chatsession.md#chatsessionconstructor) | | Constructs a new instance of the <code>ChatSession</code> class |
2626

2727
## Properties
2828

@@ -47,7 +47,7 @@ Constructs a new instance of the `ChatSession` class
4747
<b>Signature:</b>
4848

4949
```typescript
50-
constructor(apiSettings: ApiSettings, model: string, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
50+
constructor(apiSettings: ApiSettings, model: string, chromeAdapter: ChromeAdapter, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
5151
```
5252

5353
#### Parameters
@@ -56,6 +56,7 @@ constructor(apiSettings: ApiSettings, model: string, params?: StartChatParams |
5656
| --- | --- | --- |
5757
| apiSettings | ApiSettings | |
5858
| model | string | |
59+
| chromeAdapter | ChromeAdapter | |
5960
| params | [StartChatParams](./vertexai.startchatparams.md#startchatparams_interface) \| undefined | |
6061
| requestOptions | [RequestOptions](./vertexai.requestoptions.md#requestoptions_interface) \| undefined | |
6162

docs-devsite/vertexai.generativemodel.md

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export declare class GenerativeModel extends AIModel
2929
3030
| Property | Modifiers | Type | Description |
3131
| --- | --- | --- | --- |
32+
| [DEFAULT\_HYBRID\_IN\_CLOUD\_MODEL](./vertexai.generativemodel.md#generativemodeldefault_hybrid_in_cloud_model) | <code>static</code> | string | Defines the name of the default in-cloud model to use for hybrid inference. |
3233
| [generationConfig](./vertexai.generativemodel.md#generativemodelgenerationconfig) | | [GenerationConfig](./vertexai.generationconfig.md#generationconfig_interface) | |
3334
| [requestOptions](./vertexai.generativemodel.md#generativemodelrequestoptions) | | [RequestOptions](./vertexai.requestoptions.md#requestoptions_interface) | |
3435
| [safetySettings](./vertexai.generativemodel.md#generativemodelsafetysettings) | | [SafetySetting](./vertexai.safetysetting.md#safetysetting_interface)<!-- -->\[\] | |
@@ -61,8 +62,19 @@ constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions);
6162
| --- | --- | --- |
6263
| ai | [AI](./vertexai.ai.md#ai_interface) | |
6364
| modelParams | [ModelParams](./vertexai.modelparams.md#modelparams_interface) | |
65+
| chromeAdapter | ChromeAdapter | |
6466
| requestOptions | [RequestOptions](./vertexai.requestoptions.md#requestoptions_interface) | |
6567
68+
## GenerativeModel.DEFAULT\_HYBRID\_IN\_CLOUD\_MODEL
69+
70+
Defines the name of the default in-cloud model to use for hybrid inference.
71+
72+
<b>Signature:</b>
73+
74+
```typescript
75+
static DEFAULT_HYBRID_IN_CLOUD_MODEL: string;
76+
```
77+
6678
## GenerativeModel.generationConfig
6779
6880
<b>Signature:</b>

docs-devsite/vertexai.hybridparams.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Project: /docs/reference/js/_project.yaml
2+
Book: /docs/reference/_book.yaml
3+
page_type: reference
4+
5+
{% comment %}
6+
DO NOT EDIT THIS FILE!
7+
This is generated by the JS SDK team, and any local changes will be
8+
overwritten. Changes should be made in the source code at
9+
https://github.com/firebase/firebase-js-sdk
10+
{% endcomment %}
11+
12+
# HybridParams interface
13+
Toggles hybrid inference.
14+
15+
<b>Signature:</b>
16+
17+
```typescript
18+
export interface HybridParams
19+
```
20+
21+
## Properties
22+
23+
| Property | Type | Description |
24+
| --- | --- | --- |
25+
| [inCloudParams](./vertexai.hybridparams.md#hybridparamsincloudparams) | [ModelParams](./vertexai.modelparams.md#modelparams_interface) | Optional. Specifies advanced params for in-cloud inference. |
26+
| [mode](./vertexai.hybridparams.md#hybridparamsmode) | [InferenceMode](./vertexai.md#inferencemode) | Specifies on-device or in-cloud inference. Defaults to prefer on-device. |
27+
| [onDeviceParams](./vertexai.hybridparams.md#hybridparamsondeviceparams) | LanguageModelCreateOptions | Optional. Specifies advanced params for on-device inference. |
28+
29+
## HybridParams.inCloudParams
30+
31+
Optional. Specifies advanced params for in-cloud inference.
32+
33+
<b>Signature:</b>
34+
35+
```typescript
36+
inCloudParams?: ModelParams;
37+
```
38+
39+
## HybridParams.mode
40+
41+
Specifies on-device or in-cloud inference. Defaults to prefer on-device.
42+
43+
<b>Signature:</b>
44+
45+
```typescript
46+
mode: InferenceMode;
47+
```
48+
49+
## HybridParams.onDeviceParams
50+
51+
Optional. Specifies advanced params for on-device inference.
52+
53+
<b>Signature:</b>
54+
55+
```typescript
56+
onDeviceParams?: LanguageModelCreateOptions;
57+
```

docs-devsite/vertexai.md

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ The Firebase AI Web SDK.
9898
| [GenerativeContentBlob](./vertexai.generativecontentblob.md#generativecontentblob_interface) | Interface for sending an image. |
9999
| [GroundingAttribution](./vertexai.groundingattribution.md#groundingattribution_interface) | |
100100
| [GroundingMetadata](./vertexai.groundingmetadata.md#groundingmetadata_interface) | Metadata returned to client when grounding is enabled. |
101+
| [HybridParams](./vertexai.hybridparams.md#hybridparams_interface) | Toggles hybrid inference. |
101102
| [ImagenGCSImage](./vertexai.imagengcsimage.md#imagengcsimage_interface) | An image generated by Imagen, stored in a Cloud Storage for Firebase bucket.<!-- -->This feature is not available yet. |
102103
| [ImagenGenerationConfig](./vertexai.imagengenerationconfig.md#imagengenerationconfig_interface) | <b><i>(Public Preview)</i></b> Configuration options for generating images with Imagen.<!-- -->See the [documentation](http://firebase.google.com/docs/vertex-ai/generate-images-imagen) for more details. |
103104
| [ImagenGenerationResponse](./vertexai.imagengenerationresponse.md#imagengenerationresponse_interface) | <b><i>(Public Preview)</i></b> The response from a request to generate images with Imagen. |

e2e/sample-apps/modular.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import {
5858
onValue,
5959
off
6060
} from 'firebase/database';
61-
import { getGenerativeModel, getVertexAI, VertexAI } from 'firebase/vertexai';
61+
import { getGenerativeModel, getVertexAI } from 'firebase/vertexai';
6262
import { getDataConnect, DataConnect } from 'firebase/data-connect';
6363

6464
/**
@@ -313,9 +313,15 @@ function callPerformance(app) {
313313
async function callVertexAI(app) {
314314
console.log('[VERTEXAI] start');
315315
const vertexAI = getVertexAI(app);
316-
const model = getGenerativeModel(vertexAI, { model: 'gemini-1.5-flash' });
317-
const result = await model.countTokens('abcdefg');
318-
console.log(`[VERTEXAI] counted tokens: ${result.totalTokens}`);
316+
const model = getGenerativeModel(vertexAI, {
317+
mode: 'only_on_device'
318+
});
319+
const singleResult = await model.generateContent([
320+
{ text: 'describe the following:' },
321+
{ text: 'the mojave desert' }
322+
]);
323+
console.log(`Generated text: ${singleResult.response.text()}`);
324+
console.log(`[VERTEXAI] end`);
319325
}
320326

321327
/**
@@ -341,18 +347,18 @@ async function main() {
341347
const app = initializeApp(config);
342348
setLogLevel('warn');
343349

344-
callAppCheck(app);
345-
await authLogin(app);
346-
await callStorage(app);
347-
await callFirestore(app);
348-
await callDatabase(app);
349-
await callMessaging(app);
350-
callAnalytics(app);
351-
callPerformance(app);
352-
await callFunctions(app);
350+
// callAppCheck(app);
351+
// await authLogin(app);
352+
// await callStorage(app);
353+
// await callFirestore(app);
354+
// await callDatabase(app);
355+
// await callMessaging(app);
356+
// callAnalytics(app);
357+
// callPerformance(app);
358+
// await callFunctions(app);
353359
await callVertexAI(app);
354-
callDataConnect(app);
355-
await authLogout(app);
360+
// callDataConnect(app);
361+
// await authLogout(app);
356362
console.log('DONE');
357363
}
358364

packages/auth/src/core/action_code_url.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('core/action_code_url', () => {
3030
'oobCode=CODE&mode=signIn&apiKey=API_KEY&' +
3131
'continueUrl=' +
3232
encodeURIComponent(continueUrl) +
33-
'&languageCode=en&tenantId=TENANT_ID&state=bla';
33+
'&lang=en&tenantId=TENANT_ID&state=bla';
3434
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
3535
expect(actionCodeUrl!.operation).to.eq(ActionCodeOperation.EMAIL_SIGNIN);
3636
expect(actionCodeUrl!.code).to.eq('CODE');
@@ -46,7 +46,7 @@ describe('core/action_code_url', () => {
4646
const actionLink =
4747
'https://www.example.com/finishSignIn?' +
4848
'oobCode=CODE&mode=signIn&apiKey=API_KEY&' +
49-
'languageCode=en';
49+
'lang=en';
5050
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
5151
expect(actionCodeUrl!.operation).to.eq(
5252
ActionCodeOperation.EMAIL_SIGNIN
@@ -57,7 +57,7 @@ describe('core/action_code_url', () => {
5757
const actionLink =
5858
'https://www.example.com/finishSignIn?' +
5959
'oobCode=CODE&mode=verifyAndChangeEmail&apiKey=API_KEY&' +
60-
'languageCode=en';
60+
'lang=en';
6161
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
6262
expect(actionCodeUrl!.operation).to.eq(
6363
ActionCodeOperation.VERIFY_AND_CHANGE_EMAIL
@@ -68,7 +68,7 @@ describe('core/action_code_url', () => {
6868
const actionLink =
6969
'https://www.example.com/finishSignIn?' +
7070
'oobCode=CODE&mode=verifyEmail&apiKey=API_KEY&' +
71-
'languageCode=en';
71+
'lang=en';
7272
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
7373
expect(actionCodeUrl!.operation).to.eq(
7474
ActionCodeOperation.VERIFY_EMAIL
@@ -79,7 +79,7 @@ describe('core/action_code_url', () => {
7979
const actionLink =
8080
'https://www.example.com/finishSignIn?' +
8181
'oobCode=CODE&mode=recoverEmail&apiKey=API_KEY&' +
82-
'languageCode=en';
82+
'lang=en';
8383
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
8484
expect(actionCodeUrl!.operation).to.eq(
8585
ActionCodeOperation.RECOVER_EMAIL
@@ -90,7 +90,7 @@ describe('core/action_code_url', () => {
9090
const actionLink =
9191
'https://www.example.com/finishSignIn?' +
9292
'oobCode=CODE&mode=resetPassword&apiKey=API_KEY&' +
93-
'languageCode=en';
93+
'lang=en';
9494
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
9595
expect(actionCodeUrl!.operation).to.eq(
9696
ActionCodeOperation.PASSWORD_RESET
@@ -101,7 +101,7 @@ describe('core/action_code_url', () => {
101101
const actionLink =
102102
'https://www.example.com/finishSignIn?' +
103103
'oobCode=CODE&mode=revertSecondFactorAddition&apiKey=API_KEY&' +
104-
'languageCode=en';
104+
'lang=en';
105105
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
106106
expect(actionCodeUrl!.operation).to.eq(
107107
ActionCodeOperation.REVERT_SECOND_FACTOR_ADDITION

packages/auth/src/core/action_code_url.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const enum QueryField {
2929
API_KEY = 'apiKey',
3030
CODE = 'oobCode',
3131
CONTINUE_URL = 'continueUrl',
32-
LANGUAGE_CODE = 'languageCode',
32+
LANGUAGE_CODE = 'lang',
3333
MODE = 'mode',
3434
TENANT_ID = 'tenantId'
3535
}

packages/vertexai/src/api.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ describe('Top level API', () => {
101101
expect(genModel).to.be.an.instanceOf(GenerativeModel);
102102
expect(genModel.model).to.equal('publishers/google/models/my-model');
103103
});
104+
it('getGenerativeModel with HybridParams sets a default model', () => {
105+
const genModel = getGenerativeModel(fakeVertexAI, {
106+
mode: 'only_on_device'
107+
});
108+
expect(genModel.model).to.equal(
109+
`publishers/google/models/${GenerativeModel.DEFAULT_HYBRID_IN_CLOUD_MODEL}`
110+
);
111+
});
112+
it('getGenerativeModel with HybridParams honors a model override', () => {
113+
const genModel = getGenerativeModel(fakeVertexAI, {
114+
mode: 'prefer_on_device',
115+
inCloudParams: { model: 'my-model' }
116+
});
117+
expect(genModel.model).to.equal('publishers/google/models/my-model');
118+
});
104119
it('getImagenModel throws if no model is provided', () => {
105120
try {
106121
getImagenModel(fakeAI, {} as ImagenModelParams);

0 commit comments

Comments
 (0)