Skip to content

Commit 47f8521

Browse files
authored
feat(ai): Automatic function calling (#9489)
1 parent 7b1e6ec commit 47f8521

File tree

15 files changed

+1224
-87
lines changed

15 files changed

+1224
-87
lines changed
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+
Added automatic function calling capability when using `ChatSession`.

common/api-review/ai.api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ export class BooleanSchema extends Schema {
143143
export class ChatSession {
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);
146+
// @internal
147+
_callFunctionsAsNeeded(functionCalls: FunctionCall[]): Promise<FunctionResponsePart[]>;
148+
// @internal
149+
_formatRequest(incomingContent: Content, tempHistory: Content[]): GenerateContentRequest;
150+
// @internal
151+
_getCallableFunctionCalls(response?: GenerateContentResponse): FunctionCall[] | undefined;
146152
getHistory(): Promise<Content[]>;
147153
// (undocumented)
148154
model: string;
@@ -407,6 +413,7 @@ export interface FunctionCallPart {
407413
// @public
408414
export interface FunctionDeclaration {
409415
description: string;
416+
functionReference?: Function;
410417
name: string;
411418
parameters?: ObjectSchema | ObjectSchemaRequest;
412419
}
@@ -422,6 +429,8 @@ export interface FunctionResponse {
422429
// (undocumented)
423430
name: string;
424431
// (undocumented)
432+
parts?: Part[];
433+
// (undocumented)
425434
response: object;
426435
}
427436

@@ -1145,6 +1154,7 @@ export interface PromptFeedback {
11451154
// @public
11461155
export interface RequestOptions {
11471156
baseUrl?: string;
1157+
maxSequentalFunctionCalls?: number;
11481158
timeout?: number;
11491159
}
11501160

docs-devsite/ai.functiondeclaration.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface FunctionDeclaration
2323
| Property | Type | Description |
2424
| --- | --- | --- |
2525
| [description](./ai.functiondeclaration.md#functiondeclarationdescription) | string | Description and purpose of the function. Model uses it to decide how and whether to call the function. |
26+
| [functionReference](./ai.functiondeclaration.md#functiondeclarationfunctionreference) | Function | Reference to an actual function to call. Specifying this will cause the function to be called automatically when requested by the model. |
2627
| [name](./ai.functiondeclaration.md#functiondeclarationname) | string | The name of the function to call. Must start with a letter or an underscore. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a max length of 64. |
2728
| [parameters](./ai.functiondeclaration.md#functiondeclarationparameters) | [ObjectSchema](./ai.objectschema.md#objectschema_class) \| [ObjectSchemaRequest](./ai.objectschemarequest.md#objectschemarequest_interface) | Optional. Describes the parameters to this function in JSON Schema Object format. Reflects the Open API 3.03 Parameter Object. Parameter names are case-sensitive. For a function with no parameters, this can be left unset. |
2829

@@ -36,6 +37,16 @@ Description and purpose of the function. Model uses it to decide how and whether
3637
description: string;
3738
```
3839

40+
## FunctionDeclaration.functionReference
41+
42+
Reference to an actual function to call. Specifying this will cause the function to be called automatically when requested by the model.
43+
44+
<b>Signature:</b>
45+
46+
```typescript
47+
functionReference?: Function;
48+
```
49+
3950
## FunctionDeclaration.name
4051

4152
The name of the function to call. Must start with a letter or an underscore. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a max length of 64.

docs-devsite/ai.functionresponse.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface FunctionResponse
2424
| --- | --- | --- |
2525
| [id](./ai.functionresponse.md#functionresponseid) | string | The id of the [FunctionCall](./ai.functioncall.md#functioncall_interface)<!-- -->. |
2626
| [name](./ai.functionresponse.md#functionresponsename) | string | |
27+
| [parts](./ai.functionresponse.md#functionresponseparts) | [Part](./ai.md#part)<!-- -->\[\] | |
2728
| [response](./ai.functionresponse.md#functionresponseresponse) | object | |
2829

2930
## FunctionResponse.id
@@ -46,6 +47,14 @@ id?: string;
4647
name: string;
4748
```
4849

50+
## FunctionResponse.parts
51+
52+
<b>Signature:</b>
53+
54+
```typescript
55+
parts?: Part[];
56+
```
57+
4958
## FunctionResponse.response
5059

5160
<b>Signature:</b>

docs-devsite/ai.requestoptions.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface RequestOptions
2323
| Property | Type | Description |
2424
| --- | --- | --- |
2525
| [baseUrl](./ai.requestoptions.md#requestoptionsbaseurl) | string | Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com, which is the [Firebase AI Logic API](https://console.cloud.google.com/apis/library/firebasevertexai.googleapis.com?project=_) (used regardless of your chosen Gemini API provider). |
26+
| [maxSequentalFunctionCalls](./ai.requestoptions.md#requestoptionsmaxsequentalfunctioncalls) | number | Limits amount of sequential function calls the SDK can make during automatic function calling, in order to prevent infinite loops. If not specified, this value defaults to 10.<!-- -->When it reaches this limit, it will return the last response received from the model, whether it is a text response or further function calls. |
2627
| [timeout](./ai.requestoptions.md#requestoptionstimeout) | number | Request timeout in milliseconds. Defaults to 180 seconds (180000ms). |
2728

2829
## RequestOptions.baseUrl
@@ -35,6 +36,18 @@ Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com, whic
3536
baseUrl?: string;
3637
```
3738

39+
## RequestOptions.maxSequentalFunctionCalls
40+
41+
Limits amount of sequential function calls the SDK can make during automatic function calling, in order to prevent infinite loops. If not specified, this value defaults to 10.
42+
43+
When it reaches this limit, it will return the last response received from the model, whether it is a text response or further function calls.
44+
45+
<b>Signature:</b>
46+
47+
```typescript
48+
maxSequentalFunctionCalls?: number;
49+
```
50+
3851
## RequestOptions.timeout
3952

4053
Request timeout in milliseconds. Defaults to 180 seconds (180000ms).

0 commit comments

Comments
 (0)