Skip to content

Commit 9c8e864

Browse files
authored
feat(ai): Chat history and auto function calling for server templates (#9763)
1 parent 6cbe865 commit 9c8e864

24 files changed

Lines changed: 2018 additions & 392 deletions

.changeset/slimy-carpets-flow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/ai': minor
3+
'firebase': minor
4+
---
5+
6+
Add `startChat()` for `TemplateGenerativeModel`.

.changeset/smooth-apes-lick.md

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

common/api-review/ai.api.md

Lines changed: 122 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,15 @@ export class BooleanSchema extends Schema {
140140
}
141141

142142
// @public
143-
export class ChatSession {
143+
export class ChatSession extends ChatSessionBase<StartChatParams, GenerateContentRequest, FunctionDeclarationsTool> {
144144
// Warning: (ae-incompatible-release-tags) The symbol "__constructor" is marked as @public, but its signature references "ChromeAdapter" which is marked as @beta
145145
constructor(apiSettings: ApiSettings, model: string, chromeAdapter?: ChromeAdapter | undefined, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
146146
// @internal
147-
_callFunctionsAsNeeded(functionCalls: FunctionCall[]): Promise<FunctionResponsePart[]>;
147+
_callGenerateContent(formattedRequest: GenerateContentRequest, singleRequestOptions?: RequestOptions): Promise<GenerateContentResult>;
148148
// @internal
149-
_formatRequest(incomingContent: Content, tempHistory: Content[]): GenerateContentRequest;
149+
_callGenerateContentStream(formattedRequest: GenerateContentRequest, singleRequestOptions?: RequestOptions): Promise<GenerateContentStreamResult>;
150150
// @internal
151-
_getCallableFunctionCalls(response?: GenerateContentResponse): FunctionCall[] | undefined;
152-
getHistory(): Promise<Content[]>;
151+
_formatRequest(incomingContent: Content, tempHistory: Content[]): GenerateContentRequest;
153152
// (undocumented)
154153
model: string;
155154
// (undocumented)
@@ -158,7 +157,40 @@ export class ChatSession {
158157
requestOptions?: RequestOptions | undefined;
159158
sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
160159
sendMessageStream(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
161-
}
160+
}
161+
162+
// Warning: (ae-incompatible-release-tags) The symbol "ChatSessionBase" is marked as @public, but its signature references "StartTemplateChatParams" which is marked as @beta
163+
// Warning: (ae-incompatible-release-tags) The symbol "ChatSessionBase" is marked as @public, but its signature references "TemplateFunctionDeclarationsTool" which is marked as @beta
164+
//
165+
// @public
166+
export abstract class ChatSessionBase<ParamsType extends StartChatParams | StartTemplateChatParams, RequestType, FunctionDeclarationsToolType extends FunctionDeclarationsTool | TemplateFunctionDeclarationsTool> {
167+
constructor(apiSettings: ApiSettings, params?: ParamsType | undefined, requestOptions?: RequestOptions | undefined);
168+
// (undocumented)
169+
protected _apiSettings: ApiSettings;
170+
// @internal
171+
_callFunctionsAsNeeded(functionCalls: FunctionCall[]): Promise<FunctionResponsePart[]>;
172+
// @internal
173+
abstract _callGenerateContent(formattedRequest: RequestType, singleRequestOptions?: RequestOptions): Promise<GenerateContentResult>;
174+
// @internal
175+
abstract _callGenerateContentStream(formattedRequest: RequestType, singleRequestOptions?: RequestOptions): Promise<GenerateContentStreamResult>;
176+
// @internal
177+
abstract _formatRequest(incomingContent: Content, tempHistory: Content[]): RequestType;
178+
// @internal
179+
_getCallableFunctionCalls(response?: GenerateContentResponse): FunctionCall[] | undefined;
180+
getHistory(): Promise<Content[]>;
181+
// (undocumented)
182+
protected _history: Content[];
183+
// (undocumented)
184+
params?: ParamsType | undefined;
185+
// (undocumented)
186+
requestOptions?: RequestOptions | undefined;
187+
// @internal
188+
_sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
189+
// @internal
190+
_sendMessageStream(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
191+
// @internal
192+
protected _sendPromise: Promise<void>;
193+
}
162194

163195
// @beta
164196
export interface ChromeAdapter {
@@ -1157,7 +1189,7 @@ export interface PromptFeedback {
11571189
// @public
11581190
export interface RequestOptions {
11591191
baseUrl?: string;
1160-
maxSequentalFunctionCalls?: number;
1192+
maxSequentialFunctionCalls?: number;
11611193
timeout?: number;
11621194
}
11631195

@@ -1340,6 +1372,14 @@ export interface StartChatParams extends BaseParams {
13401372
tools?: Tool[];
13411373
}
13421374

1375+
// @beta
1376+
export interface StartTemplateChatParams extends Omit<StartChatParams, 'tools'> {
1377+
templateId: string;
1378+
templateVariables?: Record<string, unknown>;
1379+
// (undocumented)
1380+
tools?: TemplateTool[];
1381+
}
1382+
13431383
// @public
13441384
export class StringSchema extends Schema {
13451385
constructor(schemaParams?: SchemaParams, enumValues?: string[]);
@@ -1349,14 +1389,76 @@ export class StringSchema extends Schema {
13491389
toJSON(): SchemaRequest;
13501390
}
13511391

1392+
// Warning: (ae-incompatible-release-tags) The symbol "TemplateChatSession" is marked as @beta, but its signature references "TemplateRequestInternal" which is marked as @internal
1393+
//
1394+
// @beta
1395+
export class TemplateChatSession extends ChatSessionBase<StartTemplateChatParams, TemplateRequestInternal, TemplateFunctionDeclarationsTool> {
1396+
constructor(apiSettings: ApiSettings, params: StartTemplateChatParams, requestOptions?: RequestOptions | undefined);
1397+
// @internal
1398+
_callGenerateContent(formattedRequest: TemplateRequestInternal, singleRequestOptions?: RequestOptions): Promise<GenerateContentResult>;
1399+
// @internal
1400+
_callGenerateContentStream(formattedRequest: TemplateRequestInternal, singleRequestOptions?: RequestOptions): Promise<GenerateContentStreamResult>;
1401+
// @internal
1402+
_formatRequest(incomingContent: Content, tempHistory: Content[]): TemplateRequestInternal;
1403+
// (undocumented)
1404+
params: StartTemplateChatParams;
1405+
// (undocumented)
1406+
requestOptions?: RequestOptions | undefined;
1407+
sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
1408+
sendMessageStream(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
1409+
}
1410+
1411+
// @beta
1412+
export interface TemplateFunctionDeclaration {
1413+
description?: never;
1414+
functionReference?: Function;
1415+
name: string;
1416+
parameters?: ObjectSchema | ObjectSchemaRequest;
1417+
}
1418+
1419+
// Warning: (ae-internal-missing-underscore) The name "TemplateFunctionDeclarationInternal" should be prefixed with an underscore because the declaration is marked as @internal
1420+
//
1421+
// @internal (undocumented)
1422+
export interface TemplateFunctionDeclarationInternal extends Omit<TemplateFunctionDeclaration, 'parameters'> {
1423+
// (undocumented)
1424+
inputSchema?: ObjectSchema | ObjectSchemaRequest;
1425+
}
1426+
1427+
// @beta
1428+
export interface TemplateFunctionDeclarationsTool {
1429+
functionDeclarations?: TemplateFunctionDeclaration[];
1430+
}
1431+
1432+
// Warning: (ae-internal-missing-underscore) The name "TemplateFunctionDeclarationsToolInternal" should be prefixed with an underscore because the declaration is marked as @internal
1433+
//
1434+
// @internal
1435+
export interface TemplateFunctionDeclarationsToolInternal {
1436+
templateFunctions?: TemplateFunctionDeclarationInternal[];
1437+
}
1438+
1439+
// @beta
1440+
export interface TemplateGenerateContentRequest {
1441+
// (undocumented)
1442+
[key: string]: unknown;
1443+
// (undocumented)
1444+
history?: Content[];
1445+
// (undocumented)
1446+
inputs?: Record<string, unknown>;
1447+
// (undocumented)
1448+
toolConfig?: ToolConfig;
1449+
// (undocumented)
1450+
tools?: TemplateFunctionDeclarationsTool[];
1451+
}
1452+
13521453
// @beta
13531454
export class TemplateGenerativeModel {
13541455
constructor(ai: AI, requestOptions?: RequestOptions);
13551456
// @internal (undocumented)
13561457
_apiSettings: ApiSettings;
1357-
generateContent(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
1358-
generateContentStream(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
1458+
generateContent(templateId: string, templateVariables: Record<string, unknown>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
1459+
generateContentStream(templateId: string, templateVariables: Record<string, unknown>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
13591460
requestOptions?: RequestOptions;
1461+
startChat(params: StartTemplateChatParams): TemplateChatSession;
13601462
}
13611463

13621464
// @public @deprecated
@@ -1369,6 +1471,17 @@ export class TemplateImagenModel {
13691471
requestOptions?: RequestOptions;
13701472
}
13711473

1474+
// Warning: (ae-internal-missing-underscore) The name "TemplateRequestInternal" should be prefixed with an underscore because the declaration is marked as @internal
1475+
//
1476+
// @internal
1477+
export interface TemplateRequestInternal extends Omit<TemplateGenerateContentRequest, 'tools'> {
1478+
// (undocumented)
1479+
tools?: TemplateFunctionDeclarationsToolInternal[];
1480+
}
1481+
1482+
// @beta
1483+
export type TemplateTool = TemplateFunctionDeclarationsTool;
1484+
13721485
// @public
13731486
export interface TextPart {
13741487
// (undocumented)

docs-devsite/_toc.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ toc:
2828
path: /docs/reference/js/ai.booleanschema.md
2929
- title: ChatSession
3030
path: /docs/reference/js/ai.chatsession.md
31+
- title: ChatSessionBase
32+
path: /docs/reference/js/ai.chatsessionbase.md
3133
- title: ChromeAdapter
3234
path: /docs/reference/js/ai.chromeadapter.md
3335
- title: Citation
@@ -200,8 +202,18 @@ toc:
200202
path: /docs/reference/js/ai.startaudioconversationoptions.md
201203
- title: StartChatParams
202204
path: /docs/reference/js/ai.startchatparams.md
205+
- title: StartTemplateChatParams
206+
path: /docs/reference/js/ai.starttemplatechatparams.md
203207
- title: StringSchema
204208
path: /docs/reference/js/ai.stringschema.md
209+
- title: TemplateChatSession
210+
path: /docs/reference/js/ai.templatechatsession.md
211+
- title: TemplateFunctionDeclaration
212+
path: /docs/reference/js/ai.templatefunctiondeclaration.md
213+
- title: TemplateFunctionDeclarationsTool
214+
path: /docs/reference/js/ai.templatefunctiondeclarationstool.md
215+
- title: TemplateGenerateContentRequest
216+
path: /docs/reference/js/ai.templategeneratecontentrequest.md
205217
- title: TemplateGenerativeModel
206218
path: /docs/reference/js/ai.templategenerativemodel.md
207219
- title: TemplateImagenModel

docs-devsite/ai.chatsession.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ ChatSession class that enables sending chat messages and stores history of sent
1515
<b>Signature:</b>
1616

1717
```typescript
18-
export declare class ChatSession
18+
export declare class ChatSession extends ChatSessionBase<StartChatParams, GenerateContentRequest, FunctionDeclarationsTool>
1919
```
20+
<b>Extends:</b> [ChatSessionBase](./ai.chatsessionbase.md#chatsessionbase_class)<!-- -->&lt;[StartChatParams](./ai.startchatparams.md#startchatparams_interface)<!-- -->, [GenerateContentRequest](./ai.generatecontentrequest.md#generatecontentrequest_interface)<!-- -->, [FunctionDeclarationsTool](./ai.functiondeclarationstool.md#functiondeclarationstool_interface)<!-- -->&gt;
2021
2122
## Constructors
2223
@@ -36,7 +37,6 @@ export declare class ChatSession
3637
3738
| Method | Modifiers | Description |
3839
| --- | --- | --- |
39-
| [getHistory()](./ai.chatsession.md#chatsessiongethistory) | | Gets the chat history so far. Blocked prompts are not added to history. Neither blocked candidates nor the prompts that generated them are added to history. |
4040
| [sendMessage(request, singleRequestOptions)](./ai.chatsession.md#chatsessionsendmessage) | | Sends a chat message and receives a non-streaming [GenerateContentResult](./ai.generatecontentresult.md#generatecontentresult_interface) |
4141
| [sendMessageStream(request, singleRequestOptions)](./ai.chatsession.md#chatsessionsendmessagestream) | | Sends a chat message and receives the response as a [GenerateContentStreamResult](./ai.generatecontentstreamresult.md#generatecontentstreamresult_interface) containing an iterable stream and a response promise. |
4242
@@ -84,19 +84,6 @@ params?: StartChatParams | undefined;
8484
requestOptions?: RequestOptions | undefined;
8585
```
8686
87-
## ChatSession.getHistory()
88-
89-
Gets the chat history so far. Blocked prompts are not added to history. Neither blocked candidates nor the prompts that generated them are added to history.
90-
91-
<b>Signature:</b>
92-
93-
```typescript
94-
getHistory(): Promise<Content[]>;
95-
```
96-
<b>Returns:</b>
97-
98-
Promise&lt;[Content](./ai.content.md#content_interface)<!-- -->\[\]&gt;
99-
10087
## ChatSession.sendMessage()
10188
10289
Sends a chat message and receives a non-streaming [GenerateContentResult](./ai.generatecontentresult.md#generatecontentresult_interface)

docs-devsite/ai.chatsessionbase.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
# ChatSessionBase class
13+
Base class for various `ChatSession` classes that enables sending chat messages and stores history of sent and received messages so far.
14+
15+
<b>Signature:</b>
16+
17+
```typescript
18+
export declare abstract class ChatSessionBase<ParamsType extends StartChatParams | StartTemplateChatParams, RequestType, FunctionDeclarationsToolType extends FunctionDeclarationsTool | TemplateFunctionDeclarationsTool>
19+
```
20+
21+
## Constructors
22+
23+
| Constructor | Modifiers | Description |
24+
| --- | --- | --- |
25+
| [(constructor)(apiSettings, params, requestOptions)](./ai.chatsessionbase.md#chatsessionbaseconstructor) | | Constructs a new instance of the <code>ChatSessionBase</code> class |
26+
27+
## Properties
28+
29+
| Property | Modifiers | Type | Description |
30+
| --- | --- | --- | --- |
31+
| [\_apiSettings](./ai.chatsessionbase.md#chatsessionbase_apisettings) | | ApiSettings | |
32+
| [\_history](./ai.chatsessionbase.md#chatsessionbase_history) | | [Content](./ai.content.md#content_interface)<!-- -->\[\] | |
33+
| [params](./ai.chatsessionbase.md#chatsessionbaseparams) | | ParamsType \| undefined | |
34+
| [requestOptions](./ai.chatsessionbase.md#chatsessionbaserequestoptions) | | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) \| undefined | |
35+
36+
## Methods
37+
38+
| Method | Modifiers | Description |
39+
| --- | --- | --- |
40+
| [getHistory()](./ai.chatsessionbase.md#chatsessionbasegethistory) | | Gets the chat history so far. Blocked prompts are not added to history. Neither blocked candidates nor the prompts that generated them are added to history. |
41+
42+
## ChatSessionBase.(constructor)
43+
44+
Constructs a new instance of the `ChatSessionBase` class
45+
46+
<b>Signature:</b>
47+
48+
```typescript
49+
constructor(apiSettings: ApiSettings, params?: ParamsType | undefined, requestOptions?: RequestOptions | undefined);
50+
```
51+
52+
#### Parameters
53+
54+
| Parameter | Type | Description |
55+
| --- | --- | --- |
56+
| apiSettings | ApiSettings | |
57+
| params | ParamsType \| undefined | |
58+
| requestOptions | [RequestOptions](./ai.requestoptions.md#requestoptions_interface) \| undefined | |
59+
60+
## ChatSessionBase.\_apiSettings
61+
62+
<b>Signature:</b>
63+
64+
```typescript
65+
protected _apiSettings: ApiSettings;
66+
```
67+
68+
## ChatSessionBase.\_history
69+
70+
<b>Signature:</b>
71+
72+
```typescript
73+
protected _history: Content[];
74+
```
75+
76+
## ChatSessionBase.params
77+
78+
<b>Signature:</b>
79+
80+
```typescript
81+
params?: ParamsType | undefined;
82+
```
83+
84+
## ChatSessionBase.requestOptions
85+
86+
<b>Signature:</b>
87+
88+
```typescript
89+
requestOptions?: RequestOptions | undefined;
90+
```
91+
92+
## ChatSessionBase.getHistory()
93+
94+
Gets the chat history so far. Blocked prompts are not added to history. Neither blocked candidates nor the prompts that generated them are added to history.
95+
96+
<b>Signature:</b>
97+
98+
```typescript
99+
getHistory(): Promise<Content[]>;
100+
```
101+
<b>Returns:</b>
102+
103+
Promise&lt;[Content](./ai.content.md#content_interface)<!-- -->\[\]&gt;
104+

0 commit comments

Comments
 (0)