Skip to content

VinF Hybrid Inference #1: Update the API #8874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: vertexai-hybridinference-integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion common/api-review/vertexai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ export interface GenerativeContentBlob {
export class GenerativeModel extends VertexAIModel {
constructor(vertexAI: VertexAI, modelParams: ModelParams, requestOptions?: RequestOptions);
countTokens(request: CountTokensRequest | string | Array<string | Part>): Promise<CountTokensResponse>;
static DEFAULT_HYBRID_IN_CLOUD_MODEL: string;
generateContent(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentResult>;
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentStreamResult>;
// (undocumented)
Expand All @@ -344,7 +345,7 @@ export class GenerativeModel extends VertexAIModel {
}

// @public
export function getGenerativeModel(vertexAI: VertexAI, modelParams: ModelParams, requestOptions?: RequestOptions): GenerativeModel;
export function getGenerativeModel(vertexAI: VertexAI, modelParams: ModelParams | HybridParams, requestOptions?: RequestOptions): GenerativeModel;

// @beta
export function getImagenModel(vertexAI: VertexAI, modelParams: ImagenModelParams, requestOptions?: RequestOptions): ImagenModel;
Expand Down Expand Up @@ -416,6 +417,13 @@ export enum HarmSeverity {
HARM_SEVERITY_NEGLIGIBLE = "HARM_SEVERITY_NEGLIGIBLE"
}

// @public
export interface HybridParams {
inCloudParams?: ModelParams;
mode: InferenceMode;
onDeviceParams?: AILanguageModelCreateOptionsWithSystemPrompt;
}

// @beta
export enum ImagenAspectRatio {
LANDSCAPE_16x9 = "16:9",
Expand Down Expand Up @@ -500,6 +508,9 @@ export interface ImagenSafetySettings {
safetyFilterLevel?: ImagenSafetyFilterLevel;
}

// @public
export type InferenceMode = 'prefer_on_device' | 'only_on_device' | 'only_in_cloud';

// @public
export interface InlineDataPart {
// (undocumented)
Expand Down
2 changes: 2 additions & 0 deletions docs-devsite/_toc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ toc:
path: /docs/reference/js/vertexai.groundingattribution.md
- title: GroundingMetadata
path: /docs/reference/js/vertexai.groundingmetadata.md
- title: HybridParams
path: /docs/reference/js/vertexai.hybridparams.md
- title: ImagenGCSImage
path: /docs/reference/js/vertexai.imagengcsimage.md
- title: ImagenGenerationConfig
Expand Down
11 changes: 11 additions & 0 deletions docs-devsite/vertexai.generativemodel.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export declare class GenerativeModel extends VertexAIModel

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [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. |
| [generationConfig](./vertexai.generativemodel.md#generativemodelgenerationconfig) | | [GenerationConfig](./vertexai.generationconfig.md#generationconfig_interface) | |
| [requestOptions](./vertexai.generativemodel.md#generativemodelrequestoptions) | | [RequestOptions](./vertexai.requestoptions.md#requestoptions_interface) | |
| [safetySettings](./vertexai.generativemodel.md#generativemodelsafetysettings) | | [SafetySetting](./vertexai.safetysetting.md#safetysetting_interface)<!-- -->\[\] | |
Expand Down Expand Up @@ -63,6 +64,16 @@ constructor(vertexAI: VertexAI, modelParams: ModelParams, requestOptions?: Reque
| modelParams | [ModelParams](./vertexai.modelparams.md#modelparams_interface) | |
| requestOptions | [RequestOptions](./vertexai.requestoptions.md#requestoptions_interface) | |

## GenerativeModel.DEFAULT\_HYBRID\_IN\_CLOUD\_MODEL

Defines the name of the default in-cloud model to use for hybrid inference.

<b>Signature:</b>

```typescript
static DEFAULT_HYBRID_IN_CLOUD_MODEL: string;
```

## GenerativeModel.generationConfig

<b>Signature:</b>
Expand Down
57 changes: 57 additions & 0 deletions docs-devsite/vertexai.hybridparams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Project: /docs/reference/js/_project.yaml
Book: /docs/reference/_book.yaml
page_type: reference

{% comment %}
DO NOT EDIT THIS FILE!
This is generated by the JS SDK team, and any local changes will be
overwritten. Changes should be made in the source code at
https://github.com/firebase/firebase-js-sdk
{% endcomment %}

# HybridParams interface
Toggles hybrid inference.

<b>Signature:</b>

```typescript
export interface HybridParams
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [inCloudParams](./vertexai.hybridparams.md#hybridparamsincloudparams) | [ModelParams](./vertexai.modelparams.md#modelparams_interface) | Optional. Specifies advanced params for in-cloud inference. |
| [mode](./vertexai.hybridparams.md#hybridparamsmode) | [InferenceMode](./vertexai.md#inferencemode) | Specifies on-device or in-cloud inference. Defaults to prefer on-device. |
| [onDeviceParams](./vertexai.hybridparams.md#hybridparamsondeviceparams) | AILanguageModelCreateOptionsWithSystemPrompt | Optional. Specifies advanced params for on-device inference. |

## HybridParams.inCloudParams

Optional. Specifies advanced params for in-cloud inference.

<b>Signature:</b>

```typescript
inCloudParams?: ModelParams;
```

## HybridParams.mode

Specifies on-device or in-cloud inference. Defaults to prefer on-device.

<b>Signature:</b>

```typescript
mode: InferenceMode;
```

## HybridParams.onDeviceParams

Optional. Specifies advanced params for on-device inference.

<b>Signature:</b>

```typescript
onDeviceParams?: AILanguageModelCreateOptionsWithSystemPrompt;
```
24 changes: 18 additions & 6 deletions docs-devsite/vertexai.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The Vertex AI in Firebase Web SDK.
| <b>function(app, ...)</b> |
| [getVertexAI(app, options)](./vertexai.md#getvertexai_04094cf) | Returns a <code>[VertexAI](./vertexai.vertexai.md#vertexai_interface)</code> instance for the given app. |
| <b>function(vertexAI, ...)</b> |
| [getGenerativeModel(vertexAI, modelParams, requestOptions)](./vertexai.md#getgenerativemodel_e3037c9) | Returns a <code>[GenerativeModel](./vertexai.generativemodel.md#generativemodel_class)</code> class with methods for inference and other functionality. |
| [getGenerativeModel(vertexAI, modelParams, requestOptions)](./vertexai.md#getgenerativemodel_8dbc150) | Returns a <code>[GenerativeModel](./vertexai.generativemodel.md#generativemodel_class)</code> class with methods for inference and other functionality. |
| [getImagenModel(vertexAI, modelParams, requestOptions)](./vertexai.md#getimagenmodel_812c375) | <b><i>(Public Preview)</i></b> Returns an <code>[ImagenModel](./vertexai.imagenmodel.md#imagenmodel_class)</code> class with methods for using Imagen.<!-- -->Only Imagen 3 models (named <code>imagen-3.0-*</code>) are supported. |

## Classes
Expand Down Expand Up @@ -91,6 +91,7 @@ The Vertex AI in Firebase Web SDK.
| [GenerativeContentBlob](./vertexai.generativecontentblob.md#generativecontentblob_interface) | Interface for sending an image. |
| [GroundingAttribution](./vertexai.groundingattribution.md#groundingattribution_interface) | |
| [GroundingMetadata](./vertexai.groundingmetadata.md#groundingmetadata_interface) | Metadata returned to client when grounding is enabled. |
| [HybridParams](./vertexai.hybridparams.md#hybridparams_interface) | Toggles hybrid inference. |
| [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. |
| [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. |
| [ImagenGenerationResponse](./vertexai.imagengenerationresponse.md#imagengenerationresponse_interface) | <b><i>(Public Preview)</i></b> The response from a request to generate images with Imagen. |
Expand All @@ -99,10 +100,10 @@ The Vertex AI in Firebase Web SDK.
| [ImagenSafetySettings](./vertexai.imagensafetysettings.md#imagensafetysettings_interface) | <b><i>(Public Preview)</i></b> Settings for controlling the aggressiveness of filtering out sensitive content.<!-- -->See the [documentation](http://firebase.google.com/docs/vertex-ai/generate-images) for more details. |
| [InlineDataPart](./vertexai.inlinedatapart.md#inlinedatapart_interface) | Content part interface if the part represents an image. |
| [ModalityTokenCount](./vertexai.modalitytokencount.md#modalitytokencount_interface) | Represents token counting info for a single modality. |
| [ModelParams](./vertexai.modelparams.md#modelparams_interface) | Params passed to <code>[getGenerativeModel()](./vertexai.md#getgenerativemodel_e3037c9)</code>. |
| [ModelParams](./vertexai.modelparams.md#modelparams_interface) | Params passed to <code>[getGenerativeModel()](./vertexai.md#getgenerativemodel_8dbc150)</code>. |
| [ObjectSchemaInterface](./vertexai.objectschemainterface.md#objectschemainterface_interface) | Interface for <code>[ObjectSchema](./vertexai.objectschema.md#objectschema_class)</code> class. |
| [PromptFeedback](./vertexai.promptfeedback.md#promptfeedback_interface) | If the prompt was blocked, this will be populated with <code>blockReason</code> and the relevant <code>safetyRatings</code>. |
| [RequestOptions](./vertexai.requestoptions.md#requestoptions_interface) | Params passed to <code>[getGenerativeModel()](./vertexai.md#getgenerativemodel_e3037c9)</code>. |
| [RequestOptions](./vertexai.requestoptions.md#requestoptions_interface) | Params passed to <code>[getGenerativeModel()](./vertexai.md#getgenerativemodel_8dbc150)</code>. |
| [RetrievedContextAttribution](./vertexai.retrievedcontextattribution.md#retrievedcontextattribution_interface) | |
| [SafetyRating](./vertexai.safetyrating.md#safetyrating_interface) | A safety rating associated with a <code>[GenerateContentCandidate](./vertexai.generatecontentcandidate.md#generatecontentcandidate_interface)</code> |
| [SafetySetting](./vertexai.safetysetting.md#safetysetting_interface) | Safety setting that can be sent as part of request parameters. |
Expand Down Expand Up @@ -130,6 +131,7 @@ The Vertex AI in Firebase Web SDK.

| Type Alias | Description |
| --- | --- |
| [InferenceMode](./vertexai.md#inferencemode) | Determines whether inference happens on-device or in-cloud. |
| [Part](./vertexai.md#part) | Content part - includes text, image/video, or function call/response part types. |
| [Role](./vertexai.md#role) | Role is the producer of the content. |
| [Tool](./vertexai.md#tool) | Defines a tool that model can call to access external knowledge. |
Expand Down Expand Up @@ -160,22 +162,22 @@ export declare function getVertexAI(app?: FirebaseApp, options?: VertexAIOptions

## function(vertexAI, ...)

### getGenerativeModel(vertexAI, modelParams, requestOptions) {:#getgenerativemodel_e3037c9}
### getGenerativeModel(vertexAI, modelParams, requestOptions) {:#getgenerativemodel_8dbc150}

Returns a <code>[GenerativeModel](./vertexai.generativemodel.md#generativemodel_class)</code> class with methods for inference and other functionality.

<b>Signature:</b>

```typescript
export declare function getGenerativeModel(vertexAI: VertexAI, modelParams: ModelParams, requestOptions?: RequestOptions): GenerativeModel;
export declare function getGenerativeModel(vertexAI: VertexAI, modelParams: ModelParams | HybridParams, requestOptions?: RequestOptions): GenerativeModel;
```

#### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| vertexAI | [VertexAI](./vertexai.vertexai.md#vertexai_interface) | |
| modelParams | [ModelParams](./vertexai.modelparams.md#modelparams_interface) | |
| modelParams | [ModelParams](./vertexai.modelparams.md#modelparams_interface) \| [HybridParams](./vertexai.hybridparams.md#hybridparams_interface) | |
| requestOptions | [RequestOptions](./vertexai.requestoptions.md#requestoptions_interface) | |

<b>Returns:</b>
Expand Down Expand Up @@ -223,6 +225,16 @@ Possible roles.
POSSIBLE_ROLES: readonly ["user", "model", "function", "system"]
```

## InferenceMode

Determines whether inference happens on-device or in-cloud.

<b>Signature:</b>

```typescript
export type InferenceMode = 'prefer_on_device' | 'only_on_device' | 'only_in_cloud';
```

## Part

Content part - includes text, image/video, or function call/response part types.
Expand Down
2 changes: 1 addition & 1 deletion docs-devsite/vertexai.modelparams.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ https://github.com/firebase/firebase-js-sdk
{% endcomment %}

# ModelParams interface
Params passed to <code>[getGenerativeModel()](./vertexai.md#getgenerativemodel_e3037c9)</code>.
Params passed to <code>[getGenerativeModel()](./vertexai.md#getgenerativemodel_8dbc150)</code>.

<b>Signature:</b>

Expand Down
2 changes: 1 addition & 1 deletion docs-devsite/vertexai.requestoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ https://github.com/firebase/firebase-js-sdk
{% endcomment %}

# RequestOptions interface
Params passed to <code>[getGenerativeModel()](./vertexai.md#getgenerativemodel_e3037c9)</code>.
Params passed to <code>[getGenerativeModel()](./vertexai.md#getgenerativemodel_8dbc150)</code>.

<b>Signature:</b>

Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@
"type": "git",
"url": "git+https://github.com/firebase/firebase-js-sdk.git"
},
"workspaces": [
"packages/*",
"integration/*",
"repo-scripts/*"
],
"workspaces": {
"packages": [
"packages/*",
"integration/*",
"repo-scripts/*"
],
"nohoist": [
"**/vertexai/@types/dom-chromium-ai"
]
},
"devDependencies": {
"@babel/core": "7.26.8",
"@babel/plugin-transform-modules-commonjs": "7.26.3",
Expand Down
3 changes: 2 additions & 1 deletion packages/vertexai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"devDependencies": {
"@firebase/app": "0.11.3",
"@rollup/plugin-json": "6.1.0",
"@types/dom-chromium-ai": "0.0.6",
"rollup": "2.79.2",
"rollup-plugin-replace": "2.2.0",
"rollup-plugin-typescript2": "0.36.0",
Expand All @@ -78,4 +79,4 @@
],
"reportDir": "./coverage/node"
}
}
}
15 changes: 15 additions & 0 deletions packages/vertexai/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ describe('Top level API', () => {
expect(genModel).to.be.an.instanceOf(GenerativeModel);
expect(genModel.model).to.equal('publishers/google/models/my-model');
});
it('getGenerativeModel with HybridParams sets a default model', () => {
const genModel = getGenerativeModel(fakeVertexAI, {
mode: 'only_on_device'
});
expect(genModel.model).to.equal(
`publishers/google/models/${GenerativeModel.DEFAULT_HYBRID_IN_CLOUD_MODEL}`
);
});
it('getGenerativeModel with HybridParams honors a model override', () => {
const genModel = getGenerativeModel(fakeVertexAI, {
mode: 'only_in_cloud',
inCloudParams: { model: 'my-model' }
});
expect(genModel.model).to.equal('publishers/google/models/my-model');
});
it('getImagenModel throws if no model is provided', () => {
try {
getImagenModel(fakeVertexAI, {} as ImagenModelParams);
Expand Down
18 changes: 15 additions & 3 deletions packages/vertexai/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { VertexAIService } from './service';
import { VertexAI, VertexAIOptions } from './public-types';
import {
ImagenModelParams,
HybridParams,
ModelParams,
RequestOptions,
VertexAIErrorCode
Expand Down Expand Up @@ -70,16 +71,27 @@ export function getVertexAI(
*/
export function getGenerativeModel(
vertexAI: VertexAI,
modelParams: ModelParams,
modelParams: ModelParams | HybridParams,
requestOptions?: RequestOptions
): GenerativeModel {
if (!modelParams.model) {
// Uses the existence of HybridParams.mode to clarify the type of the modelParams input.
const hybridParams = modelParams as HybridParams;
let inCloudParams: ModelParams;
if (hybridParams.mode) {
inCloudParams = hybridParams.inCloudParams || {
model: GenerativeModel.DEFAULT_HYBRID_IN_CLOUD_MODEL
};
} else {
inCloudParams = modelParams as ModelParams;
}

if (!inCloudParams.model) {
throw new VertexAIError(
VertexAIErrorCode.NO_MODEL,
`Must provide a model name. Example: getGenerativeModel({ model: 'my-model-name' })`
);
}
return new GenerativeModel(vertexAI, modelParams, requestOptions);
return new GenerativeModel(vertexAI, inCloudParams, requestOptions);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/vertexai/src/models/generative-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ import { VertexAIModel } from './vertexai-model';
* @public
*/
export class GenerativeModel extends VertexAIModel {
/**
* Defines the name of the default in-cloud model to use for hybrid inference.
*/
static DEFAULT_HYBRID_IN_CLOUD_MODEL = 'gemini-2.0-flash-lite';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we avoid defining a default model? If this model string is deprecated in the future, and a user is using an old version of the SDK, would using this default result in an error?

generationConfig: GenerationConfig;
safetySettings: SafetySetting[];
requestOptions?: RequestOptions;
Expand Down
25 changes: 25 additions & 0 deletions packages/vertexai/src/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,28 @@ export interface FunctionCallingConfig {
mode?: FunctionCallingMode;
allowedFunctionNames?: string[];
}

/**
* Toggles hybrid inference.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the @public tag here and in other JSDoc comments for public APIs?

*/
export interface HybridParams {
/**
* Specifies on-device or in-cloud inference. Defaults to prefer on-device.
*/
mode: InferenceMode;
/**
* Optional. Specifies advanced params for on-device inference.
*/
onDeviceParams?: AILanguageModelCreateOptionsWithSystemPrompt;
/**
* Optional. Specifies advanced params for in-cloud inference.
*/
inCloudParams?: ModelParams;
}
/**
* Determines whether inference happens on-device or in-cloud.
*/
export type InferenceMode =
| 'prefer_on_device'
| 'only_on_device'
| 'only_in_cloud';
3 changes: 2 additions & 1 deletion repo-scripts/changelog-generator/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"strict": true,
"outDir": "dist",
"lib": [
"ESNext"
"ESNext",
"dom"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this file changed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, adding the @types/dom-chromium-ai dependency causes the postsubmit script to fail. I'll try reproing the issue on a stand-alone branch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created PR #8916 to repro in CI env.

],
"module": "CommonJS",
"moduleResolution": "node",
Expand Down
6 changes: 4 additions & 2 deletions scripts/release/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ const writeFile = promisify(_writeFile);

const {
workspaces: rawWorkspaces
}: { workspaces: string[] } = require(`${root}/package.json`);
const workspaces = rawWorkspaces.map(workspace => `${root}/${workspace}`);
}: { workspaces: { packages: string[] } } = require(`${root}/package.json`);
const workspaces = rawWorkspaces.packages.map(
workspace => `${root}/${workspace}`
);

export function mapWorkspaceToPackages(
workspaces: string[]
Expand Down
Loading
Loading