Skip to content

Commit f06cbf9

Browse files
authored
feat(ai): server prompt templates (#9351)
1 parent 6abe529 commit f06cbf9

30 files changed

+1759
-530
lines changed

.changeset/metal-ties-cry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'firebase': minor
3+
'@firebase/ai': minor
4+
---
5+
6+
Add support for server prompt templates.

common/api-review/ai.api.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export interface AudioTranscriptionConfig {
100100
export abstract class Backend {
101101
protected constructor(type: BackendType);
102102
readonly backendType: BackendType;
103+
// @internal (undocumented)
104+
abstract _getModelPath(project: string, model: string): string;
105+
// @internal (undocumented)
106+
abstract _getTemplatePath(project: string, templateId: string): string;
103107
}
104108

105109
// @public
@@ -567,9 +571,19 @@ export function getImagenModel(ai: AI, modelParams: ImagenModelParams, requestOp
567571
// @beta
568572
export function getLiveGenerativeModel(ai: AI, modelParams: LiveModelParams): LiveGenerativeModel;
569573

574+
// @beta
575+
export function getTemplateGenerativeModel(ai: AI, requestOptions?: RequestOptions): TemplateGenerativeModel;
576+
577+
// @beta
578+
export function getTemplateImagenModel(ai: AI, requestOptions?: RequestOptions): TemplateImagenModel;
579+
570580
// @public
571581
export class GoogleAIBackend extends Backend {
572582
constructor();
583+
// @internal (undocumented)
584+
_getModelPath(project: string, model: string): string;
585+
// @internal (undocumented)
586+
_getTemplatePath(project: string, templateId: string): string;
573587
}
574588

575589
// Warning: (ae-internal-missing-underscore) The name "GoogleAICitationMetadata" should be prefixed with an underscore because the declaration is marked as @internal
@@ -1314,6 +1328,25 @@ export class StringSchema extends Schema {
13141328
toJSON(): SchemaRequest;
13151329
}
13161330

1331+
// @beta
1332+
export class TemplateGenerativeModel {
1333+
constructor(ai: AI, requestOptions?: RequestOptions);
1334+
// @internal (undocumented)
1335+
_apiSettings: ApiSettings;
1336+
generateContent(templateId: string, templateVariables: object): Promise<GenerateContentResult>;
1337+
generateContentStream(templateId: string, templateVariables: object): Promise<GenerateContentStreamResult>;
1338+
requestOptions?: RequestOptions;
1339+
}
1340+
1341+
// @beta
1342+
export class TemplateImagenModel {
1343+
constructor(ai: AI, requestOptions?: RequestOptions);
1344+
// @internal (undocumented)
1345+
_apiSettings: ApiSettings;
1346+
generateImages(templateId: string, templateVariables: object): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
1347+
requestOptions?: RequestOptions;
1348+
}
1349+
13171350
// @public
13181351
export interface TextPart {
13191352
// (undocumented)
@@ -1412,6 +1445,10 @@ export interface UsageMetadata {
14121445
// @public
14131446
export class VertexAIBackend extends Backend {
14141447
constructor(location?: string);
1448+
// @internal (undocumented)
1449+
_getModelPath(project: string, model: string): string;
1450+
// @internal (undocumented)
1451+
_getTemplatePath(project: string, templateId: string): string;
14151452
readonly location: string;
14161453
}
14171454

docs-devsite/_toc.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ toc:
198198
path: /docs/reference/js/ai.startchatparams.md
199199
- title: StringSchema
200200
path: /docs/reference/js/ai.stringschema.md
201+
- title: TemplateGenerativeModel
202+
path: /docs/reference/js/ai.templategenerativemodel.md
203+
- title: TemplateImagenModel
204+
path: /docs/reference/js/ai.templateimagenmodel.md
201205
- title: TextPart
202206
path: /docs/reference/js/ai.textpart.md
203207
- title: ThinkingConfig

docs-devsite/ai.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ The Firebase AI Web SDK.
2222
| [getGenerativeModel(ai, modelParams, requestOptions)](./ai.md#getgenerativemodel_c63f46a) | Returns a [GenerativeModel](./ai.generativemodel.md#generativemodel_class) class with methods for inference and other functionality. |
2323
| [getImagenModel(ai, modelParams, requestOptions)](./ai.md#getimagenmodel_e1f6645) | Returns an [ImagenModel](./ai.imagenmodel.md#imagenmodel_class) class with methods for using Imagen.<!-- -->Only Imagen 3 models (named <code>imagen-3.0-*</code>) are supported. |
2424
| [getLiveGenerativeModel(ai, modelParams)](./ai.md#getlivegenerativemodel_f2099ac) | <b><i>(Public Preview)</i></b> Returns a [LiveGenerativeModel](./ai.livegenerativemodel.md#livegenerativemodel_class) class for real-time, bidirectional communication.<!-- -->The Live API is only supported in modern browser windows and Node &gt;<!-- -->= 22. |
25+
| [getTemplateGenerativeModel(ai, requestOptions)](./ai.md#gettemplategenerativemodel_9476bbc) | <b><i>(Public Preview)</i></b> Returns a [TemplateGenerativeModel](./ai.templategenerativemodel.md#templategenerativemodel_class) class for executing server-side templates. |
26+
| [getTemplateImagenModel(ai, requestOptions)](./ai.md#gettemplateimagenmodel_9476bbc) | <b><i>(Public Preview)</i></b> Returns a [TemplateImagenModel](./ai.templateimagenmodel.md#templateimagenmodel_class) class for executing server-side Imagen templates. |
2527
| <b>function(liveSession, ...)</b> |
2628
| [startAudioConversation(liveSession, options)](./ai.md#startaudioconversation_01c8e7f) | <b><i>(Public Preview)</i></b> Starts a real-time, bidirectional audio conversation with the model. This helper function manages the complexities of microphone access, audio recording, playback, and interruptions. |
2729

@@ -47,6 +49,8 @@ The Firebase AI Web SDK.
4749
| [ObjectSchema](./ai.objectschema.md#objectschema_class) | Schema class for "object" types. The <code>properties</code> param must be a map of <code>Schema</code> objects. |
4850
| [Schema](./ai.schema.md#schema_class) | Parent class encompassing all Schema types, with static methods that allow building specific Schema types. This class can be converted with <code>JSON.stringify()</code> into a JSON string accepted by Vertex AI REST endpoints. (This string conversion is automatically done when calling SDK methods.) |
4951
| [StringSchema](./ai.stringschema.md#stringschema_class) | Schema class for "string" types. Can be used with or without enum values. |
52+
| [TemplateGenerativeModel](./ai.templategenerativemodel.md#templategenerativemodel_class) | <b><i>(Public Preview)</i></b> [GenerativeModel](./ai.generativemodel.md#generativemodel_class) APIs that execute on a server-side template.<!-- -->This class should only be instantiated with [getTemplateGenerativeModel()](./ai.md#gettemplategenerativemodel_9476bbc)<!-- -->. |
53+
| [TemplateImagenModel](./ai.templateimagenmodel.md#templateimagenmodel_class) | <b><i>(Public Preview)</i></b> Class for Imagen model APIs that execute on a server-side template.<!-- -->This class should only be instantiated with [getTemplateImagenModel()](./ai.md#gettemplateimagenmodel_9476bbc)<!-- -->. |
5054
| [VertexAIBackend](./ai.vertexaibackend.md#vertexaibackend_class) | Configuration class for the Vertex AI Gemini API.<!-- -->Use this with [AIOptions](./ai.aioptions.md#aioptions_interface) when initializing the AI service via [getAI()](./ai.md#getai_a94a413) to specify the Vertex AI Gemini API as the backend. |
5155

5256
## Interfaces
@@ -341,6 +345,54 @@ export declare function getLiveGenerativeModel(ai: AI, modelParams: LiveModelPar
341345

342346
If the `apiKey` or `projectId` fields are missing in your Firebase config.
343347

348+
### getTemplateGenerativeModel(ai, requestOptions) {:#gettemplategenerativemodel_9476bbc}
349+
350+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
351+
>
352+
353+
Returns a [TemplateGenerativeModel](./ai.templategenerativemodel.md#templategenerativemodel_class) class for executing server-side templates.
354+
355+
<b>Signature:</b>
356+
357+
```typescript
358+
export declare function getTemplateGenerativeModel(ai: AI, requestOptions?: RequestOptions): TemplateGenerativeModel;
359+
```
360+
361+
#### Parameters
362+
363+
| Parameter | Type | Description |
364+
| --- | --- | --- |
365+
| ai | [AI](./ai.ai.md#ai_interface) | An [AI](./ai.ai.md#ai_interface) instance. |
366+
| requestOptions | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) | Additional options to use when making requests. |
367+
368+
<b>Returns:</b>
369+
370+
[TemplateGenerativeModel](./ai.templategenerativemodel.md#templategenerativemodel_class)
371+
372+
### getTemplateImagenModel(ai, requestOptions) {:#gettemplateimagenmodel_9476bbc}
373+
374+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
375+
>
376+
377+
Returns a [TemplateImagenModel](./ai.templateimagenmodel.md#templateimagenmodel_class) class for executing server-side Imagen templates.
378+
379+
<b>Signature:</b>
380+
381+
```typescript
382+
export declare function getTemplateImagenModel(ai: AI, requestOptions?: RequestOptions): TemplateImagenModel;
383+
```
384+
385+
#### Parameters
386+
387+
| Parameter | Type | Description |
388+
| --- | --- | --- |
389+
| ai | [AI](./ai.ai.md#ai_interface) | An [AI](./ai.ai.md#ai_interface) instance. |
390+
| requestOptions | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) | Additional options to use when making requests. |
391+
392+
<b>Returns:</b>
393+
394+
[TemplateImagenModel](./ai.templateimagenmodel.md#templateimagenmodel_class)
395+
344396
## function(liveSession, ...)
345397

346398
### startAudioConversation(liveSession, options) {:#startaudioconversation_01c8e7f}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
# TemplateGenerativeModel class
13+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
14+
>
15+
16+
[GenerativeModel](./ai.generativemodel.md#generativemodel_class) APIs that execute on a server-side template.
17+
18+
This class should only be instantiated with [getTemplateGenerativeModel()](./ai.md#gettemplategenerativemodel_9476bbc)<!-- -->.
19+
20+
<b>Signature:</b>
21+
22+
```typescript
23+
export declare class TemplateGenerativeModel
24+
```
25+
26+
## Constructors
27+
28+
| Constructor | Modifiers | Description |
29+
| --- | --- | --- |
30+
| [(constructor)(ai, requestOptions)](./ai.templategenerativemodel.md#templategenerativemodelconstructor) | | <b><i>(Public Preview)</i></b> Constructs a new instance of the <code>TemplateGenerativeModel</code> class |
31+
32+
## Properties
33+
34+
| Property | Modifiers | Type | Description |
35+
| --- | --- | --- | --- |
36+
| [requestOptions](./ai.templategenerativemodel.md#templategenerativemodelrequestoptions) | | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) | <b><i>(Public Preview)</i></b> Additional options to use when making requests. |
37+
38+
## Methods
39+
40+
| Method | Modifiers | Description |
41+
| --- | --- | --- |
42+
| [generateContent(templateId, templateVariables)](./ai.templategenerativemodel.md#templategenerativemodelgeneratecontent) | | <b><i>(Public Preview)</i></b> Makes a single non-streaming call to the model and returns an object containing a single [GenerateContentResponse](./ai.generatecontentresponse.md#generatecontentresponse_interface)<!-- -->. |
43+
| [generateContentStream(templateId, templateVariables)](./ai.templategenerativemodel.md#templategenerativemodelgeneratecontentstream) | | <b><i>(Public Preview)</i></b> Makes a single streaming call to the model and returns an object containing an iterable stream that iterates over all chunks in the streaming response as well as a promise that returns the final aggregated response. |
44+
45+
## TemplateGenerativeModel.(constructor)
46+
47+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
48+
>
49+
50+
Constructs a new instance of the `TemplateGenerativeModel` class
51+
52+
<b>Signature:</b>
53+
54+
```typescript
55+
constructor(ai: AI, requestOptions?: RequestOptions);
56+
```
57+
58+
#### Parameters
59+
60+
| Parameter | Type | Description |
61+
| --- | --- | --- |
62+
| ai | [AI](./ai.ai.md#ai_interface) | |
63+
| requestOptions | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) | |
64+
65+
## TemplateGenerativeModel.requestOptions
66+
67+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
68+
>
69+
70+
Additional options to use when making requests.
71+
72+
<b>Signature:</b>
73+
74+
```typescript
75+
requestOptions?: RequestOptions;
76+
```
77+
78+
## TemplateGenerativeModel.generateContent()
79+
80+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
81+
>
82+
83+
Makes a single non-streaming call to the model and returns an object containing a single [GenerateContentResponse](./ai.generatecontentresponse.md#generatecontentresponse_interface)<!-- -->.
84+
85+
<b>Signature:</b>
86+
87+
```typescript
88+
generateContent(templateId: string, templateVariables: object): Promise<GenerateContentResult>;
89+
```
90+
91+
#### Parameters
92+
93+
| Parameter | Type | Description |
94+
| --- | --- | --- |
95+
| templateId | string | The ID of the server-side template to execute. |
96+
| templateVariables | object | A key-value map of variables to populate the template with. |
97+
98+
<b>Returns:</b>
99+
100+
Promise&lt;[GenerateContentResult](./ai.generatecontentresult.md#generatecontentresult_interface)<!-- -->&gt;
101+
102+
## TemplateGenerativeModel.generateContentStream()
103+
104+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
105+
>
106+
107+
Makes a single streaming call to the model and returns an object containing an iterable stream that iterates over all chunks in the streaming response as well as a promise that returns the final aggregated response.
108+
109+
<b>Signature:</b>
110+
111+
```typescript
112+
generateContentStream(templateId: string, templateVariables: object): Promise<GenerateContentStreamResult>;
113+
```
114+
115+
#### Parameters
116+
117+
| Parameter | Type | Description |
118+
| --- | --- | --- |
119+
| templateId | string | The ID of the server-side template to execute. |
120+
| templateVariables | object | A key-value map of variables to populate the template with. |
121+
122+
<b>Returns:</b>
123+
124+
Promise&lt;[GenerateContentStreamResult](./ai.generatecontentstreamresult.md#generatecontentstreamresult_interface)<!-- -->&gt;
125+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
# TemplateImagenModel class
13+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
14+
>
15+
16+
Class for Imagen model APIs that execute on a server-side template.
17+
18+
This class should only be instantiated with [getTemplateImagenModel()](./ai.md#gettemplateimagenmodel_9476bbc)<!-- -->.
19+
20+
<b>Signature:</b>
21+
22+
```typescript
23+
export declare class TemplateImagenModel
24+
```
25+
26+
## Constructors
27+
28+
| Constructor | Modifiers | Description |
29+
| --- | --- | --- |
30+
| [(constructor)(ai, requestOptions)](./ai.templateimagenmodel.md#templateimagenmodelconstructor) | | <b><i>(Public Preview)</i></b> Constructs a new instance of the <code>TemplateImagenModel</code> class |
31+
32+
## Properties
33+
34+
| Property | Modifiers | Type | Description |
35+
| --- | --- | --- | --- |
36+
| [requestOptions](./ai.templateimagenmodel.md#templateimagenmodelrequestoptions) | | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) | <b><i>(Public Preview)</i></b> Additional options to use when making requests. |
37+
38+
## Methods
39+
40+
| Method | Modifiers | Description |
41+
| --- | --- | --- |
42+
| [generateImages(templateId, templateVariables)](./ai.templateimagenmodel.md#templateimagenmodelgenerateimages) | | <b><i>(Public Preview)</i></b> Makes a single call to the model and returns an object containing a single [ImagenGenerationResponse](./ai.imagengenerationresponse.md#imagengenerationresponse_interface)<!-- -->. |
43+
44+
## TemplateImagenModel.(constructor)
45+
46+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
47+
>
48+
49+
Constructs a new instance of the `TemplateImagenModel` class
50+
51+
<b>Signature:</b>
52+
53+
```typescript
54+
constructor(ai: AI, requestOptions?: RequestOptions);
55+
```
56+
57+
#### Parameters
58+
59+
| Parameter | Type | Description |
60+
| --- | --- | --- |
61+
| ai | [AI](./ai.ai.md#ai_interface) | |
62+
| requestOptions | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) | |
63+
64+
## TemplateImagenModel.requestOptions
65+
66+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
67+
>
68+
69+
Additional options to use when making requests.
70+
71+
<b>Signature:</b>
72+
73+
```typescript
74+
requestOptions?: RequestOptions;
75+
```
76+
77+
## TemplateImagenModel.generateImages()
78+
79+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
80+
>
81+
82+
Makes a single call to the model and returns an object containing a single [ImagenGenerationResponse](./ai.imagengenerationresponse.md#imagengenerationresponse_interface)<!-- -->.
83+
84+
<b>Signature:</b>
85+
86+
```typescript
87+
generateImages(templateId: string, templateVariables: object): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
88+
```
89+
90+
#### Parameters
91+
92+
| Parameter | Type | Description |
93+
| --- | --- | --- |
94+
| templateId | string | The ID of the server-side template to execute. |
95+
| templateVariables | object | A key-value map of variables to populate the template with. |
96+
97+
<b>Returns:</b>
98+
99+
Promise&lt;[ImagenGenerationResponse](./ai.imagengenerationresponse.md#imagengenerationresponse_interface)<!-- -->&lt;[ImagenInlineImage](./ai.imageninlineimage.md#imageninlineimage_interface)<!-- -->&gt;&gt;
100+

packages/ai/integration/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function formatConfigAsString(config: { ai: AI; model: string }): string {
4444

4545
const backends: readonly Backend[] = [
4646
new GoogleAIBackend(),
47-
new VertexAIBackend()
47+
new VertexAIBackend('global')
4848
];
4949

5050
const backendNames: Map<BackendType, string> = new Map([

0 commit comments

Comments
 (0)