Skip to content

Firebase AI Hybrid Inference Implementation #9029

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 6 commits into
base: main
Choose a base branch
from
Open
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
79 changes: 76 additions & 3 deletions common/api-review/ai.api.md
Original file line number Diff line number Diff line change
@@ -112,7 +112,8 @@ export class BooleanSchema extends Schema {

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

// @public
export class GenerativeModel extends AIModel {
constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions);
constructor(ai: AI, modelParams: ModelParams, chromeAdapter: ChromeAdapter, 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)
@@ -418,7 +420,7 @@ export class GenerativeModel extends AIModel {
export function getAI(app?: FirebaseApp, options?: AIOptions): AI;

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

// @beta
export function getImagenModel(ai: AI, modelParams: ImagenModelParams, requestOptions?: RequestOptions): ImagenModel;
@@ -550,6 +552,13 @@ export enum HarmSeverity {
HARM_SEVERITY_UNSUPPORTED = "HARM_SEVERITY_UNSUPPORTED"
}

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

// @beta
export enum ImagenAspectRatio {
LANDSCAPE_16x9 = "16:9",
@@ -634,6 +643,9 @@ export interface ImagenSafetySettings {
safetyFilterLevel?: ImagenSafetyFilterLevel;
}

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

// @public
export interface InlineDataPart {
// (undocumented)
@@ -652,6 +664,57 @@ export class IntegerSchema extends Schema {
constructor(schemaParams?: SchemaParams);
}

// @public (undocumented)
export interface LanguageModelCreateCoreOptions {
// (undocumented)
expectedInputs?: LanguageModelExpected[];
// (undocumented)
temperature?: number;
// (undocumented)
topK?: number;
}

// @public (undocumented)
export interface LanguageModelCreateOptions extends LanguageModelCreateCoreOptions {
// (undocumented)
initialPrompts?: LanguageModelMessage[];
// (undocumented)
signal?: AbortSignal;
}

// @public (undocumented)
export interface LanguageModelExpected {
// (undocumented)
languages?: string[];
// (undocumented)
type: LanguageModelMessageType;
}

// @public (undocumented)
export interface LanguageModelMessage {
// (undocumented)
content: LanguageModelMessageContent[];
// (undocumented)
role: LanguageModelMessageRole;
}

// @public (undocumented)
export interface LanguageModelMessageContent {
// (undocumented)
type: LanguageModelMessageType;
// (undocumented)
value: LanguageModelMessageContentValue;
}

// @public (undocumented)
export type LanguageModelMessageContentValue = ImageBitmapSource | AudioBuffer | BufferSource | string;

// @public (undocumented)
export type LanguageModelMessageRole = 'system' | 'user' | 'assistant';

// @public (undocumented)
export type LanguageModelMessageType = 'text' | 'image' | 'audio';

// @public
export enum Modality {
AUDIO = "AUDIO",
@@ -708,6 +771,16 @@ export interface ObjectSchemaInterface extends SchemaInterface {
type: SchemaType.OBJECT;
}

// @public
export interface OnDeviceParams {
// (undocumented)
createOptions?: LanguageModelCreateOptions;
// Warning: (ae-forgotten-export) The symbol "LanguageModelPromptOptions" needs to be exported by the entry point index.d.ts
//
// (undocumented)
promptOptions?: LanguageModelPromptOptions;
}

// @public
export type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart;

14 changes: 14 additions & 0 deletions docs-devsite/_toc.yaml
Original file line number Diff line number Diff line change
@@ -80,6 +80,8 @@ toc:
path: /docs/reference/js/ai.groundingattribution.md
- title: GroundingMetadata
path: /docs/reference/js/ai.groundingmetadata.md
- title: HybridParams
path: /docs/reference/js/ai.hybridparams.md
- title: ImagenGCSImage
path: /docs/reference/js/ai.imagengcsimage.md
- title: ImagenGenerationConfig
@@ -100,6 +102,16 @@ toc:
path: /docs/reference/js/ai.inlinedatapart.md
- title: IntegerSchema
path: /docs/reference/js/ai.integerschema.md
- title: LanguageModelCreateCoreOptions
path: /docs/reference/js/ai.languagemodelcreatecoreoptions.md
- title: LanguageModelCreateOptions
path: /docs/reference/js/ai.languagemodelcreateoptions.md
- title: LanguageModelExpected
path: /docs/reference/js/ai.languagemodelexpected.md
- title: LanguageModelMessage
path: /docs/reference/js/ai.languagemodelmessage.md
- title: LanguageModelMessageContent
path: /docs/reference/js/ai.languagemodelmessagecontent.md
- title: ModalityTokenCount
path: /docs/reference/js/ai.modalitytokencount.md
- title: ModelParams
@@ -110,6 +122,8 @@ toc:
path: /docs/reference/js/ai.objectschema.md
- title: ObjectSchemaInterface
path: /docs/reference/js/ai.objectschemainterface.md
- title: OnDeviceParams
path: /docs/reference/js/ai.ondeviceparams.md
- title: PromptFeedback
path: /docs/reference/js/ai.promptfeedback.md
- title: RequestOptions
5 changes: 3 additions & 2 deletions docs-devsite/ai.chatsession.md
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ export declare class ChatSession

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

## Properties

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

```typescript
constructor(apiSettings: ApiSettings, model: string, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
constructor(apiSettings: ApiSettings, model: string, chromeAdapter: ChromeAdapter, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
```

#### Parameters
@@ -56,6 +56,7 @@ constructor(apiSettings: ApiSettings, model: string, params?: StartChatParams |
| --- | --- | --- |
| apiSettings | ApiSettings | |
| model | string | |
| chromeAdapter | ChromeAdapter | |
| params | [StartChatParams](./ai.startchatparams.md#startchatparams_interface) \| undefined | |
| requestOptions | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) \| undefined | |

16 changes: 14 additions & 2 deletions docs-devsite/ai.generativemodel.md
Original file line number Diff line number Diff line change
@@ -23,12 +23,13 @@ export declare class GenerativeModel extends AIModel

| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(ai, modelParams, requestOptions)](./ai.generativemodel.md#generativemodelconstructor) | | Constructs a new instance of the <code>GenerativeModel</code> class |
| [(constructor)(ai, modelParams, chromeAdapter, requestOptions)](./ai.generativemodel.md#generativemodelconstructor) | | Constructs a new instance of the <code>GenerativeModel</code> class |

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [DEFAULT\_HYBRID\_IN\_CLOUD\_MODEL](./ai.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](./ai.generativemodel.md#generativemodelgenerationconfig) | | [GenerationConfig](./ai.generationconfig.md#generationconfig_interface) | |
| [requestOptions](./ai.generativemodel.md#generativemodelrequestoptions) | | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) | |
| [safetySettings](./ai.generativemodel.md#generativemodelsafetysettings) | | [SafetySetting](./ai.safetysetting.md#safetysetting_interface)<!-- -->\[\] | |
@@ -52,7 +53,7 @@ Constructs a new instance of the `GenerativeModel` class
<b>Signature:</b>

```typescript
constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions);
constructor(ai: AI, modelParams: ModelParams, chromeAdapter: ChromeAdapter, requestOptions?: RequestOptions);
```

#### Parameters
@@ -61,8 +62,19 @@ constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions);
| --- | --- | --- |
| ai | [AI](./ai.ai.md#ai_interface) | |
| modelParams | [ModelParams](./ai.modelparams.md#modelparams_interface) | |
| chromeAdapter | ChromeAdapter | |
| requestOptions | [RequestOptions](./ai.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>
57 changes: 57 additions & 0 deletions docs-devsite/ai.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](./ai.hybridparams.md#hybridparamsincloudparams) | [ModelParams](./ai.modelparams.md#modelparams_interface) | Optional. Specifies advanced params for in-cloud inference. |
| [mode](./ai.hybridparams.md#hybridparamsmode) | [InferenceMode](./ai.md#inferencemode) | Specifies on-device or in-cloud inference. Defaults to prefer on-device. |
| [onDeviceParams](./ai.hybridparams.md#hybridparamsondeviceparams) | [OnDeviceParams](./ai.ondeviceparams.md#ondeviceparams_interface) | 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?: OnDeviceParams;
```
49 changes: 49 additions & 0 deletions docs-devsite/ai.languagemodelcreatecoreoptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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 %}

# LanguageModelCreateCoreOptions interface
<b>Signature:</b>

```typescript
export interface LanguageModelCreateCoreOptions
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [expectedInputs](./ai.languagemodelcreatecoreoptions.md#languagemodelcreatecoreoptionsexpectedinputs) | [LanguageModelExpected](./ai.languagemodelexpected.md#languagemodelexpected_interface)<!-- -->\[\] | |
| [temperature](./ai.languagemodelcreatecoreoptions.md#languagemodelcreatecoreoptionstemperature) | number | |
| [topK](./ai.languagemodelcreatecoreoptions.md#languagemodelcreatecoreoptionstopk) | number | |

## LanguageModelCreateCoreOptions.expectedInputs

<b>Signature:</b>

```typescript
expectedInputs?: LanguageModelExpected[];
```

## LanguageModelCreateCoreOptions.temperature

<b>Signature:</b>

```typescript
temperature?: number;
```

## LanguageModelCreateCoreOptions.topK

<b>Signature:</b>

```typescript
topK?: number;
```
41 changes: 41 additions & 0 deletions docs-devsite/ai.languagemodelcreateoptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 %}

# LanguageModelCreateOptions interface
<b>Signature:</b>

```typescript
export interface LanguageModelCreateOptions extends LanguageModelCreateCoreOptions
```
<b>Extends:</b> [LanguageModelCreateCoreOptions](./ai.languagemodelcreatecoreoptions.md#languagemodelcreatecoreoptions_interface)

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [initialPrompts](./ai.languagemodelcreateoptions.md#languagemodelcreateoptionsinitialprompts) | [LanguageModelMessage](./ai.languagemodelmessage.md#languagemodelmessage_interface)<!-- -->\[\] | |
| [signal](./ai.languagemodelcreateoptions.md#languagemodelcreateoptionssignal) | AbortSignal | |

## LanguageModelCreateOptions.initialPrompts

<b>Signature:</b>

```typescript
initialPrompts?: LanguageModelMessage[];
```

## LanguageModelCreateOptions.signal

<b>Signature:</b>

```typescript
signal?: AbortSignal;
```
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.