Skip to content

Commit 33dd398

Browse files
committed
Add test for template chat and function calling
1 parent 1666650 commit 33dd398

4 files changed

Lines changed: 547 additions & 0 deletions

File tree

common/api-review/ai.api.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,22 @@ export interface StartChatParams extends BaseParams {
13401340
tools?: Tool[];
13411341
}
13421342

1343+
// @public
1344+
export interface StartTemplateChatParams extends BaseParams {
1345+
// (undocumented)
1346+
history?: Content[];
1347+
// (undocumented)
1348+
systemInstruction?: string | Part | Content;
1349+
// (undocumented)
1350+
templateId: string;
1351+
// (undocumented)
1352+
templateVariables?: Record<string, unknown>;
1353+
// (undocumented)
1354+
toolConfig?: ToolConfig;
1355+
// (undocumented)
1356+
tools?: TemplateTool[];
1357+
}
1358+
13431359
// @public
13441360
export class StringSchema extends Schema {
13451361
constructor(schemaParams?: SchemaParams, enumValues?: string[]);
@@ -1349,6 +1365,37 @@ export class StringSchema extends Schema {
13491365
toJSON(): SchemaRequest;
13501366
}
13511367

1368+
// @public
1369+
export class TemplateChatSession {
1370+
constructor(apiSettings: ApiSettings, params: StartTemplateChatParams, requestOptions?: RequestOptions | undefined);
1371+
// @internal
1372+
_callFunctionsAsNeeded(functionCalls: FunctionCall[]): Promise<FunctionResponsePart[]>;
1373+
// @internal
1374+
_formatRequest(incomingContent: Content, tempHistory: Content[]): object;
1375+
// @internal
1376+
_getCallableFunctionCalls(response?: GenerateContentResponse): FunctionCall[] | undefined;
1377+
getHistory(): Promise<Content[]>;
1378+
// (undocumented)
1379+
params: StartTemplateChatParams;
1380+
// (undocumented)
1381+
requestOptions?: RequestOptions | undefined;
1382+
sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
1383+
sendMessageStream(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
1384+
}
1385+
1386+
// @public
1387+
export interface TemplateFunctionDeclaration {
1388+
description?: never;
1389+
functionReference?: Function;
1390+
name: string;
1391+
parameters?: ObjectSchema | ObjectSchemaRequest;
1392+
}
1393+
1394+
// @public
1395+
export interface TemplateFunctionDeclarationsTool {
1396+
functionDeclarations?: TemplateFunctionDeclaration[];
1397+
}
1398+
13521399
// @beta
13531400
export class TemplateGenerativeModel {
13541401
constructor(ai: AI, requestOptions?: RequestOptions);
@@ -1357,6 +1404,7 @@ export class TemplateGenerativeModel {
13571404
generateContent(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
13581405
generateContentStream(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
13591406
requestOptions?: RequestOptions;
1407+
startChat(params: StartTemplateChatParams): TemplateChatSession;
13601408
}
13611409

13621410
// @beta
@@ -1368,6 +1416,9 @@ export class TemplateImagenModel {
13681416
requestOptions?: RequestOptions;
13691417
}
13701418

1419+
// @public
1420+
export type TemplateTool = TemplateFunctionDeclarationsTool;
1421+
13711422
// @public
13721423
export interface TextPart {
13731424
// (undocumented)

packages/ai/integration/prompt-templates.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,49 @@ describe('Prompt templates', function () {
4444
expect(response.text()).to.contain('John'); // Template asks to address directly by name
4545
});
4646
});
47+
describe('Template Chat Session', () => {
48+
it('successfully chats', async () => {
49+
const model = getTemplateGenerativeModel(testConfig.ai);
50+
const chat = model.startChat({
51+
templateId: `chat-prompt-${templateBackendSuffix(
52+
testConfig.ai.backend.backendType
53+
)}`,
54+
templateVariables: { name: 'John' }
55+
});
56+
const result = await chat.sendMessage('Hello');
57+
expect(result.response.text()).to.be.a('string');
58+
});
59+
60+
it('successfully calls functions', async () => {
61+
const model = getTemplateGenerativeModel(testConfig.ai);
62+
const chat = model.startChat({
63+
templateId: `function-calling-prompt-${templateBackendSuffix(
64+
testConfig.ai.backend.backendType
65+
)}`,
66+
templateVariables: {},
67+
tools: [
68+
{
69+
functionDeclarations: [
70+
{
71+
name: 'getWeather',
72+
parameters: {
73+
type: 'object',
74+
properties: {
75+
location: { type: 'string' }
76+
}
77+
},
78+
functionReference: () => {
79+
return { temperature: 72 };
80+
}
81+
}
82+
]
83+
}
84+
]
85+
});
86+
const result = await chat.sendMessage('What is the weather in Paris?');
87+
expect(result.response.text()).to.be.a('string');
88+
});
89+
});
4790
describe('Imagen model', async () => {
4891
it('successfully generates images', async () => {
4992
const model = getTemplateImagenModel(testConfig.ai);

0 commit comments

Comments
 (0)