Skip to content

Commit 07c75ea

Browse files
authored
VinF Hybrid Inference: support structured output (#9009)
1 parent e859c03 commit 07c75ea

File tree

11 files changed

+222
-58
lines changed

11 files changed

+222
-58
lines changed

common/api-review/vertexai.api.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,7 @@ export enum HarmSeverity {
553553
export interface HybridParams {
554554
inCloudParams?: ModelParams;
555555
mode: InferenceMode;
556-
// Warning: (ae-forgotten-export) The symbol "LanguageModelCreateOptions" needs to be exported by the entry point index.d.ts
557-
onDeviceParams?: LanguageModelCreateOptions;
556+
onDeviceParams?: OnDeviceParams;
558557
}
559558

560559
// @beta
@@ -718,6 +717,18 @@ export interface ObjectSchemaInterface extends SchemaInterface {
718717
type: SchemaType.OBJECT;
719718
}
720719

720+
// @public
721+
export interface OnDeviceParams {
722+
// Warning: (ae-forgotten-export) The symbol "LanguageModelCreateOptions" needs to be exported by the entry point index.d.ts
723+
//
724+
// (undocumented)
725+
createOptions?: LanguageModelCreateOptions;
726+
// Warning: (ae-forgotten-export) The symbol "LanguageModelPromptOptions" needs to be exported by the entry point index.d.ts
727+
//
728+
// (undocumented)
729+
promptOptions?: LanguageModelPromptOptions;
730+
}
731+
721732
// @public
722733
export type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart;
723734

docs-devsite/_toc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ toc:
562562
path: /docs/reference/js/vertexai.objectschema.md
563563
- title: ObjectSchemaInterface
564564
path: /docs/reference/js/vertexai.objectschemainterface.md
565+
- title: OnDeviceParams
566+
path: /docs/reference/js/vertexai.ondeviceparams.md
565567
- title: PromptFeedback
566568
path: /docs/reference/js/vertexai.promptfeedback.md
567569
- title: RequestOptions

docs-devsite/vertexai.hybridparams.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface HybridParams
2424
| --- | --- | --- |
2525
| [inCloudParams](./vertexai.hybridparams.md#hybridparamsincloudparams) | [ModelParams](./vertexai.modelparams.md#modelparams_interface) | Optional. Specifies advanced params for in-cloud inference. |
2626
| [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. |
27+
| [onDeviceParams](./vertexai.hybridparams.md#hybridparamsondeviceparams) | [OnDeviceParams](./vertexai.ondeviceparams.md#ondeviceparams_interface) | Optional. Specifies advanced params for on-device inference. |
2828

2929
## HybridParams.inCloudParams
3030

@@ -53,5 +53,5 @@ Optional. Specifies advanced params for on-device inference.
5353
<b>Signature:</b>
5454

5555
```typescript
56-
onDeviceParams?: LanguageModelCreateOptions;
56+
onDeviceParams?: OnDeviceParams;
5757
```

docs-devsite/vertexai.md

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ The Firebase AI Web SDK.
108108
| [ModalityTokenCount](./vertexai.modalitytokencount.md#modalitytokencount_interface) | Represents token counting info for a single modality. |
109109
| [ModelParams](./vertexai.modelparams.md#modelparams_interface) | Params passed to [getGenerativeModel()](./vertexai.md#getgenerativemodel_c63f46a)<!-- -->. |
110110
| [ObjectSchemaInterface](./vertexai.objectschemainterface.md#objectschemainterface_interface) | Interface for [ObjectSchema](./vertexai.objectschema.md#objectschema_class) class. |
111+
| [OnDeviceParams](./vertexai.ondeviceparams.md#ondeviceparams_interface) | Encapsulates configuration for on-device inference. |
111112
| [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>. |
112113
| [RequestOptions](./vertexai.requestoptions.md#requestoptions_interface) | Params passed to [getGenerativeModel()](./vertexai.md#getgenerativemodel_c63f46a)<!-- -->. |
113114
| [RetrievedContextAttribution](./vertexai.retrievedcontextattribution.md#retrievedcontextattribution_interface) | |
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
# OnDeviceParams interface
13+
Encapsulates configuration for on-device inference.
14+
15+
<b>Signature:</b>
16+
17+
```typescript
18+
export interface OnDeviceParams
19+
```
20+
21+
## Properties
22+
23+
| Property | Type | Description |
24+
| --- | --- | --- |
25+
| [createOptions](./vertexai.ondeviceparams.md#ondeviceparamscreateoptions) | LanguageModelCreateOptions | |
26+
| [promptOptions](./vertexai.ondeviceparams.md#ondeviceparamspromptoptions) | LanguageModelPromptOptions | |
27+
28+
## OnDeviceParams.createOptions
29+
30+
<b>Signature:</b>
31+
32+
```typescript
33+
createOptions?: LanguageModelCreateOptions;
34+
```
35+
36+
## OnDeviceParams.promptOptions
37+
38+
<b>Signature:</b>
39+
40+
```typescript
41+
promptOptions?: LanguageModelPromptOptions;
42+
```

e2e/sample-apps/modular.js

+35-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 } from 'firebase/vertexai';
61+
import { getGenerativeModel, getVertexAI, Schema } from 'firebase/vertexai';
6262
import { getDataConnect, DataConnect } from 'firebase/data-connect';
6363

6464
/**
@@ -313,23 +313,43 @@ function callPerformance(app) {
313313
async function callVertexAI(app) {
314314
console.log('[VERTEXAI] start');
315315
const vertexAI = getVertexAI(app);
316-
const model = getGenerativeModel(vertexAI, {
317-
mode: 'prefer_on_device'
316+
317+
const jsonSchema = Schema.object({
318+
properties: {
319+
characters: Schema.array({
320+
items: Schema.object({
321+
properties: {
322+
name: Schema.string(),
323+
accessory: Schema.string(),
324+
age: Schema.number(),
325+
species: Schema.string()
326+
},
327+
optionalProperties: ['accessory']
328+
})
329+
})
330+
}
318331
});
319-
const singleResult = await model.generateContent([
320-
{ text: 'describe this 20 x 20 px image in two words' },
321-
{
322-
inlineData: {
323-
mimeType: 'image/heic',
324-
data: 'AAAAGGZ0eXBoZWljAAAAAGhlaWNtaWYxAAAB7G1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAHBpY3QAAAAAAAAAAAAAAAAAAAAAJGRpbmYAAAAcZHJlZgAAAAAAAAABAAAADHVybCAAAAABAAAADnBpdG0AAAAAAAEAAAA4aWluZgAAAAAAAgAAABVpbmZlAgAAAAABAABodmMxAAAAABVpbmZlAgAAAQACAABFeGlmAAAAABppcmVmAAAAAAAAAA5jZHNjAAIAAQABAAABD2lwcnAAAADtaXBjbwAAABNjb2xybmNseAACAAIABoAAAAAMY2xsaQDLAEAAAAAUaXNwZQAAAAAAAAAUAAAADgAAAChjbGFwAAAAFAAAAAEAAAANAAAAAQAAAAAAAAAB/8AAAACAAAAAAAAJaXJvdAAAAAAQcGl4aQAAAAADCAgIAAAAcWh2Y0MBA3AAAACwAAAAAAAe8AD8/fj4AAALA6AAAQAXQAEMAf//A3AAAAMAsAAAAwAAAwAecCShAAEAI0IBAQNwAAADALAAAAMAAAMAHqAUIEHAjw1iHuRZVNwICBgCogABAAlEAcBhcshAUyQAAAAaaXBtYQAAAAAAAAABAAEHgQIDhIUGhwAAACxpbG9jAAAAAEQAAAIAAQAAAAEAAAJsAAABDAACAAAAAQAAAhQAAABYAAAAAW1kYXQAAAAAAAABdAAAAAZFeGlmAABNTQAqAAAACAAEARIAAwAAAAEAAQAAARoABQAAAAEAAAA+ARsABQAAAAEAAABGASgAAwAAAAEAAgAAAAAAAAAAAEgAAAABAAAASAAAAAEAAAEIKAGvoR8wDimTiRYUbALiHkU3ZdZ8DXAcSrRB9GARtVQHvnCE0LEyBGAyb5P4eYr6JAK5UxNX10WNlARq3ZpcGeVD+Xom6LodYasuZKKtDHCz/xnswOtC/ksZzVKhtWQqGvkXcsJnLYqWevNkacnccQ95jbHJBg9nXub69jAAN3xhNOXxjGSxaG9QvES5R7sYICEojRjLF5OB5K3v+okQAwfgWpz/u21ayideOgOZQLAyBkKOv7ymLNCagiPWTlHAuy/3qR1Q7m2ERFaxKIAbLSkIVO/P8m8+anKxhzhC//L8NMAUoF+Sf3aEH9O41fwLc+PlcbrDrjgY2EboD3cn9DyN32Rum2Ym'
332+
333+
const model = getGenerativeModel(vertexAI, {
334+
// mode: 'prefer_on_device',
335+
mode: 'only_in_cloud',
336+
inCloudParams: {
337+
generationConfig: {
338+
responseMimeType: 'application/json',
339+
responseSchema: jsonSchema
340+
}
341+
},
342+
onDeviceParams: {
343+
promptOptions: {
344+
responseConstraint: jsonSchema
325345
}
326346
}
327-
]);
328-
console.log(`Generated text: ${singleResult.response.text()}`);
329-
const chat = model.startChat();
330-
let chatResult = await chat.sendMessage('describe red in two words');
331-
chatResult = await chat.sendMessage('describe blue');
332-
console.log('Chat history:', await chat.getHistory());
347+
});
348+
349+
const singleResult = await model.generateContent(
350+
"For use in a children's card game, generate 10 animal-based characters."
351+
);
352+
console.log(`Generated text:`, JSON.parse(singleResult.response.text()));
333353
console.log(`[VERTEXAI] end`);
334354
}
335355

e2e/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module.exports = [
8888
stats: {
8989
colors: true
9090
},
91-
devtool: 'source-map',
91+
devtool: 'eval-source-map',
9292
devServer: {
9393
static: './build'
9494
}

0 commit comments

Comments
 (0)