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 4 commits into
base: main
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
92 changes: 89 additions & 3 deletions common/api-review/ai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -652,6 +664,70 @@ export class IntegerSchema extends Schema {
constructor(schemaParams?: SchemaParams);
}

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

// @public (undocumented)
export interface LanguageModelCreateOptions extends LanguageModelCreateCoreOptions {
// (undocumented)
initialPrompts?: LanguageModelInitialPrompts;
// (undocumented)
signal?: AbortSignal;
// (undocumented)
systemPrompt?: string;
}

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

// @public (undocumented)
export type LanguageModelInitialPrompts = LanguageModelMessage[] | LanguageModelMessageShorthand[];

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

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

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

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

// @public (undocumented)
export interface LanguageModelMessageShorthand {
// (undocumented)
content: string;
// (undocumented)
role: LanguageModelMessageRole;
}

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

// @public
export enum Modality {
AUDIO = "AUDIO",
Expand Down Expand Up @@ -708,6 +784,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;

Expand Down
16 changes: 16 additions & 0 deletions docs-devsite/_toc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -100,6 +102,18 @@ 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: LanguageModelExpectedInput
path: /docs/reference/js/ai.languagemodelexpectedinput.md
- title: LanguageModelMessage
path: /docs/reference/js/ai.languagemodelmessage.md
- title: LanguageModelMessageContent
path: /docs/reference/js/ai.languagemodelmessagecontent.md
- title: LanguageModelMessageShorthand
path: /docs/reference/js/ai.languagemodelmessageshorthand.md
- title: ModalityTokenCount
path: /docs/reference/js/ai.modalitytokencount.md
- title: ModelParams
Expand All @@ -110,6 +124,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
Expand Down
5 changes: 3 additions & 2 deletions docs-devsite/ai.chatsession.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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 | |

Expand Down
16 changes: 14 additions & 2 deletions docs-devsite/ai.generativemodel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)<!-- -->\[\] | |
Expand All @@ -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
Expand All @@ -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>
Expand Down
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) | [LanguageModelExpectedInput](./ai.languagemodelexpectedinput.md#languagemodelexpectedinput_interface)<!-- -->\[\] | |
| [temperature](./ai.languagemodelcreatecoreoptions.md#languagemodelcreatecoreoptionstemperature) | number | |
| [topK](./ai.languagemodelcreatecoreoptions.md#languagemodelcreatecoreoptionstopk) | number | |

## LanguageModelCreateCoreOptions.expectedInputs

<b>Signature:</b>

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

## LanguageModelCreateCoreOptions.temperature

<b>Signature:</b>

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

## LanguageModelCreateCoreOptions.topK

<b>Signature:</b>

```typescript
topK?: number;
```
Loading
Loading