@@ -20,6 +20,7 @@ import {
20
20
ClientError ,
21
21
FunctionDeclarationsTool ,
22
22
GoogleSearchRetrievalTool ,
23
+ Part ,
23
24
TextPart ,
24
25
VertexAI ,
25
26
} from '../src' ;
@@ -361,7 +362,7 @@ describe('generateContentStream', () => {
361
362
) ;
362
363
} ) ;
363
364
364
- it ( 'should return a FunctionCall or text when passed a FunctionDeclaration or FunctionResponse' , async ( ) => {
365
+ it ( 'should return a text when passed a FunctionDeclaration or FunctionResponse' , async ( ) => {
365
366
const request = {
366
367
contents : [
367
368
{ role : 'user' , parts : [ { text : 'What is the weather in Boston?' } ] } ,
@@ -381,7 +382,7 @@ describe('generateContentStream', () => {
381
382
) ;
382
383
}
383
384
} ) ;
384
- it ( 'in preview should return a FunctionCall or text when passed a FunctionDeclaration or FunctionResponse' , async ( ) => {
385
+ it ( 'in preview should return a text when passed a FunctionDeclaration or FunctionResponse' , async ( ) => {
385
386
const request = {
386
387
contents : [
387
388
{ role : 'user' , parts : [ { text : 'What is the weather in Boston?' } ] } ,
@@ -401,6 +402,46 @@ describe('generateContentStream', () => {
401
402
) ;
402
403
}
403
404
} ) ;
405
+ it ( 'should return a FunctionCall when passed a FunctionDeclaration' , async ( ) => {
406
+ const request = {
407
+ contents : [
408
+ { role : 'user' , parts : [ { text : 'What is the weather in Boston?' } ] } ,
409
+ ] ,
410
+ tools : TOOLS_WITH_FUNCTION_DECLARATION ,
411
+ } ;
412
+ const streamingResp =
413
+ await generativeTextModel . generateContentStream ( request ) ;
414
+ for await ( const item of streamingResp . stream ) {
415
+ expect ( item . candidates [ 0 ] ) . toBeTruthy (
416
+ `sys test failure on generateContentStream, for item ${ item } `
417
+ ) ;
418
+ const functionCalls = item . candidates [ 0 ] . content . parts
419
+ . filter ( part => ! ! part . functionCall )
420
+ . map ( part => part . functionCall ! ) ;
421
+ expect ( functionCalls ) . toHaveSize ( 1 ) ;
422
+ expect ( item . candidates [ 0 ] . functionCalls ! ) . toEqual ( functionCalls ! ) ;
423
+ }
424
+ } ) ;
425
+ it ( 'in preview should return a FunctionCall when passed a FunctionDeclaration' , async ( ) => {
426
+ const request = {
427
+ contents : [
428
+ { role : 'user' , parts : [ { text : 'What is the weather in Boston?' } ] } ,
429
+ ] ,
430
+ tools : TOOLS_WITH_FUNCTION_DECLARATION ,
431
+ } ;
432
+ const streamingResp =
433
+ await generativeTextModelPreview . generateContentStream ( request ) ;
434
+ for await ( const item of streamingResp . stream ) {
435
+ expect ( item . candidates [ 0 ] ) . toBeTruthy (
436
+ `sys test failure on generateContentStream in preview, for item ${ item } `
437
+ ) ;
438
+ const functionCalls = item . candidates [ 0 ] . content . parts
439
+ . filter ( part => ! ! part . functionCall )
440
+ . map ( part => part . functionCall ! ) ;
441
+ expect ( functionCalls ) . toHaveSize ( 1 ) ;
442
+ expect ( item . candidates [ 0 ] . functionCalls ! ) . toEqual ( functionCalls ! ) ;
443
+ }
444
+ } ) ;
404
445
} ) ;
405
446
406
447
describe ( 'generateContent' , ( ) => {
@@ -488,6 +529,79 @@ describe('generateContent', () => {
488
529
expect ( ! ! groundingMetadata . webSearchQueries ) . toBeTruthy ( ) ;
489
530
}
490
531
} ) ;
532
+ it ( 'should return a text when passed a FunctionDeclaration or FunctionResponse' , async ( ) => {
533
+ const request = {
534
+ contents : [
535
+ { role : 'user' , parts : [ { text : 'What is the weather in Boston?' } ] } ,
536
+ { role : 'model' , parts : FUNCTION_CALL } ,
537
+ { role : 'function' , parts : FUNCTION_RESPONSE_PART } ,
538
+ ] ,
539
+ tools : TOOLS_WITH_FUNCTION_DECLARATION ,
540
+ } ;
541
+ const resp = await generativeTextModel . generateContent ( request ) ;
542
+
543
+ expect ( resp . response . candidates [ 0 ] ) . toBeTruthy (
544
+ `sys test failure on generateContentStream, for resp ${ resp } `
545
+ ) ;
546
+ expect (
547
+ resp . response . candidates [ 0 ] . content . parts [ 0 ] . text ?. toLowerCase ( )
548
+ ) . toContain ( WEATHER_FORECAST ) ;
549
+ } ) ;
550
+ it ( 'in preview should return a text when passed a FunctionDeclaration or FunctionResponse' , async ( ) => {
551
+ const request = {
552
+ contents : [
553
+ { role : 'user' , parts : [ { text : 'What is the weather in Boston?' } ] } ,
554
+ { role : 'model' , parts : FUNCTION_CALL } ,
555
+ { role : 'function' , parts : FUNCTION_RESPONSE_PART } ,
556
+ ] ,
557
+ tools : TOOLS_WITH_FUNCTION_DECLARATION ,
558
+ } ;
559
+ const resp = await generativeTextModelPreview . generateContent ( request ) ;
560
+ expect ( resp . response . candidates [ 0 ] ) . toBeTruthy (
561
+ `sys test failure on generateContentStream in preview, for resp ${ resp } `
562
+ ) ;
563
+ const functionCalls = resp . response . candidates [ 0 ] . content . parts
564
+ . filter ( ( part : Part ) => ! ! part . functionCall )
565
+ . map ( ( part : Part ) => part . functionCall ! ) ;
566
+ expect (
567
+ resp . response . candidates [ 0 ] . content . parts [ 0 ] . text ?. toLowerCase ( )
568
+ ) . toContain ( WEATHER_FORECAST ) ;
569
+ } ) ;
570
+ it ( 'should return a FunctionCall when passed a FunctionDeclaration' , async ( ) => {
571
+ const request = {
572
+ contents : [
573
+ { role : 'user' , parts : [ { text : 'What is the weather in Boston?' } ] } ,
574
+ ] ,
575
+ tools : TOOLS_WITH_FUNCTION_DECLARATION ,
576
+ } ;
577
+ const resp = await generativeTextModel . generateContent ( request ) ;
578
+
579
+ expect ( resp . response . candidates [ 0 ] ) . toBeTruthy (
580
+ `sys test failure on generateContentStream, for resp ${ resp } `
581
+ ) ;
582
+ const functionCalls = resp . response . candidates [ 0 ] . content . parts
583
+ . filter ( ( part : Part ) => ! ! part . functionCall )
584
+ . map ( ( part : Part ) => part . functionCall ! ) ;
585
+ expect ( functionCalls ) . toHaveSize ( 1 ) ;
586
+ expect ( resp . response . candidates [ 0 ] . functionCalls ! ) . toEqual ( functionCalls ! ) ;
587
+ } ) ;
588
+ it ( 'in preview should return a FunctionCall when passed a FunctionDeclaration' , async ( ) => {
589
+ const request = {
590
+ contents : [
591
+ { role : 'user' , parts : [ { text : 'What is the weather in Boston?' } ] } ,
592
+ ] ,
593
+ tools : TOOLS_WITH_FUNCTION_DECLARATION ,
594
+ } ;
595
+ const resp = await generativeTextModelPreview . generateContent ( request ) ;
596
+ expect ( resp . response . candidates [ 0 ] ) . toBeTruthy (
597
+ `sys test failure on generateContentStream in preview, for resp ${ resp } `
598
+ ) ;
599
+ const functionCalls = resp . response . candidates [ 0 ] . content . parts
600
+ . filter ( ( part : Part ) => ! ! part . functionCall )
601
+ . map ( ( part : Part ) => part . functionCall ! ) ;
602
+ expect ( functionCalls ) . toHaveSize ( 1 ) ;
603
+ expect ( resp . response . candidates [ 0 ] . functionCalls ! ) . toEqual ( functionCalls ! ) ;
604
+ } ) ;
491
605
} ) ;
492
606
493
607
describe ( 'sendMessage' , ( ) => {
0 commit comments