@@ -113,14 +113,15 @@ const vertexAI = new VertexAI({
113
113
location : LOCATION ,
114
114
} ) ;
115
115
116
+ const TEXT_MODEL_NAME = 'gemini-1.0-pro' ;
116
117
const generativeTextModel = vertexAI . getGenerativeModel ( {
117
- model : 'gemini-1.0-pro' ,
118
+ model : TEXT_MODEL_NAME ,
118
119
generationConfig : {
119
120
maxOutputTokens : 256 ,
120
121
} ,
121
122
} ) ;
122
123
const generativeTextModelPreview = vertexAI . preview . getGenerativeModel ( {
123
- model : 'gemini-1.0-pro' ,
124
+ model : TEXT_MODEL_NAME ,
124
125
generationConfig : {
125
126
maxOutputTokens : 256 ,
126
127
} ,
@@ -138,12 +139,6 @@ const generativeTextModelWithPrefixPreview =
138
139
maxOutputTokens : 256 ,
139
140
} ,
140
141
} ) ;
141
- const textModelNoOutputLimit = vertexAI . getGenerativeModel ( {
142
- model : 'gemini-1.0-pro' ,
143
- } ) ;
144
- const textModelNoOutputLimitPreview = vertexAI . preview . getGenerativeModel ( {
145
- model : 'gemini-1.0-pro' ,
146
- } ) ;
147
142
const generativeVisionModel = vertexAI . getGenerativeModel ( {
148
143
model : 'gemini-1.0-pro-vision' ,
149
144
} ) ;
@@ -442,6 +437,70 @@ describe('generateContentStream', () => {
442
437
expect ( item . candidates [ 0 ] . functionCalls ! ) . toEqual ( functionCalls ! ) ;
443
438
}
444
439
} ) ;
440
+ it ( 'should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel' , async ( ) => {
441
+ const generativeTextModel = vertexAI . getGenerativeModel ( {
442
+ model : TEXT_MODEL_NAME ,
443
+ tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
444
+ } ) ;
445
+ const result = await generativeTextModel . generateContentStream ( {
446
+ contents : [ { role : 'user' , parts : [ { text : 'Why is the sky blue?' } ] } ] ,
447
+ } ) ;
448
+ const response = await result . response ;
449
+ const groundingMetadata = response . candidates [ 0 ] . groundingMetadata ;
450
+ expect ( ! ! groundingMetadata ) . toBeTruthy ( ) ;
451
+ if ( groundingMetadata ) {
452
+ expect ( ! ! groundingMetadata . groundingAttributions ) . toBeTruthy ( ) ;
453
+ expect ( ! ! groundingMetadata . webSearchQueries ) . toBeTruthy ( ) ;
454
+ }
455
+ } ) ;
456
+ it ( 'should return grounding metadata when passed GoogleSearchRetriever in generateContent' , async ( ) => {
457
+ const generativeTextModel = vertexAI . getGenerativeModel ( {
458
+ model : TEXT_MODEL_NAME ,
459
+ } ) ;
460
+ const result = await generativeTextModel . generateContentStream ( {
461
+ contents : [ { role : 'user' , parts : [ { text : 'Why is the sky blue?' } ] } ] ,
462
+ tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
463
+ } ) ;
464
+ const response = await result . response ;
465
+ const groundingMetadata = response . candidates [ 0 ] . groundingMetadata ;
466
+ expect ( ! ! groundingMetadata ) . toBeTruthy ( ) ;
467
+ if ( groundingMetadata ) {
468
+ expect ( ! ! groundingMetadata . groundingAttributions ) . toBeTruthy ( ) ;
469
+ expect ( ! ! groundingMetadata . webSearchQueries ) . toBeTruthy ( ) ;
470
+ }
471
+ } ) ;
472
+ it ( 'in preview should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel' , async ( ) => {
473
+ const generativeTextModel = vertexAI . preview . getGenerativeModel ( {
474
+ model : TEXT_MODEL_NAME ,
475
+ tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
476
+ } ) ;
477
+ const result = await generativeTextModel . generateContentStream ( {
478
+ contents : [ { role : 'user' , parts : [ { text : 'Why is the sky blue?' } ] } ] ,
479
+ } ) ;
480
+ const response = await result . response ;
481
+ const groundingMetadata = response . candidates [ 0 ] . groundingMetadata ;
482
+ expect ( ! ! groundingMetadata ) . toBeTruthy ( ) ;
483
+ if ( groundingMetadata ) {
484
+ expect ( ! ! groundingMetadata . groundingAttributions ) . toBeTruthy ( ) ;
485
+ expect ( ! ! groundingMetadata . webSearchQueries ) . toBeTruthy ( ) ;
486
+ }
487
+ } ) ;
488
+ it ( 'in preview should return grounding metadata when passed GoogleSearchRetriever in generateContent' , async ( ) => {
489
+ const generativeTextModel = vertexAI . preview . getGenerativeModel ( {
490
+ model : TEXT_MODEL_NAME ,
491
+ } ) ;
492
+ const result = await generativeTextModel . generateContentStream ( {
493
+ contents : [ { role : 'user' , parts : [ { text : 'Why is the sky blue?' } ] } ] ,
494
+ tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
495
+ } ) ;
496
+ const response = await result . response ;
497
+ const groundingMetadata = response . candidates [ 0 ] . groundingMetadata ;
498
+ expect ( ! ! groundingMetadata ) . toBeTruthy ( ) ;
499
+ if ( groundingMetadata ) {
500
+ expect ( ! ! groundingMetadata . groundingAttributions ) . toBeTruthy ( ) ;
501
+ expect ( ! ! groundingMetadata . webSearchQueries ) . toBeTruthy ( ) ;
502
+ }
503
+ } ) ;
445
504
} ) ;
446
505
447
506
describe ( 'generateContent' , ( ) => {
@@ -467,7 +526,7 @@ describe('generateContent', () => {
467
526
} ) ;
468
527
it ( 'should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel' , async ( ) => {
469
528
const generativeTextModel = vertexAI . getGenerativeModel ( {
470
- model : 'gemini-pro' ,
529
+ model : TEXT_MODEL_NAME ,
471
530
tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
472
531
} ) ;
473
532
const result = await generativeTextModel . generateContent ( {
@@ -483,7 +542,7 @@ describe('generateContent', () => {
483
542
} ) ;
484
543
it ( 'should return grounding metadata when passed GoogleSearchRetriever in generateContent' , async ( ) => {
485
544
const generativeTextModel = vertexAI . getGenerativeModel ( {
486
- model : 'gemini-pro' ,
545
+ model : TEXT_MODEL_NAME ,
487
546
} ) ;
488
547
const result = await generativeTextModel . generateContent ( {
489
548
contents : [ { role : 'user' , parts : [ { text : 'Why is the sky blue?' } ] } ] ,
@@ -499,7 +558,7 @@ describe('generateContent', () => {
499
558
} ) ;
500
559
it ( 'in preview should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel' , async ( ) => {
501
560
const generativeTextModel = vertexAI . preview . getGenerativeModel ( {
502
- model : 'gemini-pro' ,
561
+ model : TEXT_MODEL_NAME ,
503
562
tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
504
563
} ) ;
505
564
const result = await generativeTextModel . generateContent ( {
@@ -515,7 +574,7 @@ describe('generateContent', () => {
515
574
} ) ;
516
575
it ( 'in preview should return grounding metadata when passed GoogleSearchRetriever in generateContent' , async ( ) => {
517
576
const generativeTextModel = vertexAI . preview . getGenerativeModel ( {
518
- model : 'gemini-pro' ,
577
+ model : TEXT_MODEL_NAME ,
519
578
} ) ;
520
579
const result = await generativeTextModel . generateContent ( {
521
580
contents : [ { role : 'user' , parts : [ { text : 'Why is the sky blue?' } ] } ] ,
@@ -630,7 +689,7 @@ describe('sendMessage', () => {
630
689
} ) ;
631
690
it ( 'should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel' , async ( ) => {
632
691
const generativeTextModel = vertexAI . getGenerativeModel ( {
633
- model : 'gemini-pro' ,
692
+ model : TEXT_MODEL_NAME ,
634
693
tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
635
694
} ) ;
636
695
const chat = generativeTextModel . startChat ( ) ;
@@ -645,7 +704,7 @@ describe('sendMessage', () => {
645
704
} ) ;
646
705
it ( 'should return grounding metadata when passed GoogleSearchRetriever in startChat' , async ( ) => {
647
706
const generativeTextModel = vertexAI . getGenerativeModel ( {
648
- model : 'gemini-pro' ,
707
+ model : TEXT_MODEL_NAME ,
649
708
} ) ;
650
709
const chat = generativeTextModel . startChat ( {
651
710
tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
@@ -661,7 +720,7 @@ describe('sendMessage', () => {
661
720
} ) ;
662
721
it ( 'in preview should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel' , async ( ) => {
663
722
const generativeTextModel = vertexAI . preview . getGenerativeModel ( {
664
- model : 'gemini-pro' ,
723
+ model : TEXT_MODEL_NAME ,
665
724
tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
666
725
} ) ;
667
726
const chat = generativeTextModel . startChat ( ) ;
@@ -676,7 +735,7 @@ describe('sendMessage', () => {
676
735
} ) ;
677
736
it ( 'in preview should return grounding metadata when passed GoogleSearchRetriever in startChat' , async ( ) => {
678
737
const generativeTextModel = vertexAI . preview . getGenerativeModel ( {
679
- model : 'gemini-pro' ,
738
+ model : TEXT_MODEL_NAME ,
680
739
} ) ;
681
740
const chat = generativeTextModel . startChat ( {
682
741
tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
@@ -828,6 +887,68 @@ describe('sendMessageStream', () => {
828
887
JSON . stringify ( response2 . candidates [ 0 ] . content . parts [ 0 ] . text )
829
888
) . toContain ( WEATHER_FORECAST ) ;
830
889
} ) ;
890
+ it ( 'should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel' , async ( ) => {
891
+ const generativeTextModel = vertexAI . getGenerativeModel ( {
892
+ model : TEXT_MODEL_NAME ,
893
+ tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
894
+ } ) ;
895
+ const chat = generativeTextModel . startChat ( ) ;
896
+ const result = await chat . sendMessageStream ( 'Why is the sky blue?' ) ;
897
+ const response = await result . response ;
898
+ const groundingMetadata = response . candidates [ 0 ] . groundingMetadata ;
899
+ expect ( ! ! groundingMetadata ) . toBeTruthy ( ) ;
900
+ if ( groundingMetadata ) {
901
+ expect ( ! ! groundingMetadata . groundingAttributions ) . toBeTruthy ( ) ;
902
+ expect ( ! ! groundingMetadata . webSearchQueries ) . toBeTruthy ( ) ;
903
+ }
904
+ } ) ;
905
+ it ( 'should return grounding metadata when passed GoogleSearchRetriever in startChat' , async ( ) => {
906
+ const generativeTextModel = vertexAI . getGenerativeModel ( {
907
+ model : TEXT_MODEL_NAME ,
908
+ } ) ;
909
+ const chat = generativeTextModel . startChat ( {
910
+ tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
911
+ } ) ;
912
+ const result = await chat . sendMessageStream ( 'Why is the sky blue?' ) ;
913
+ const response = await result . response ;
914
+ const groundingMetadata = response . candidates [ 0 ] . groundingMetadata ;
915
+ expect ( ! ! groundingMetadata ) . toBeTruthy ( ) ;
916
+ if ( groundingMetadata ) {
917
+ expect ( ! ! groundingMetadata . groundingAttributions ) . toBeTruthy ( ) ;
918
+ expect ( ! ! groundingMetadata . webSearchQueries ) . toBeTruthy ( ) ;
919
+ }
920
+ } ) ;
921
+ it ( 'in preview should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel' , async ( ) => {
922
+ const generativeTextModel = vertexAI . preview . getGenerativeModel ( {
923
+ model : TEXT_MODEL_NAME ,
924
+ tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
925
+ } ) ;
926
+ const chat = generativeTextModel . startChat ( ) ;
927
+ const result = await chat . sendMessageStream ( 'Why is the sky blue?' ) ;
928
+ const response = await result . response ;
929
+ const groundingMetadata = response . candidates [ 0 ] . groundingMetadata ;
930
+ expect ( ! ! groundingMetadata ) . toBeTruthy ( ) ;
931
+ if ( groundingMetadata ) {
932
+ expect ( ! ! groundingMetadata . groundingAttributions ) . toBeTruthy ( ) ;
933
+ expect ( ! ! groundingMetadata . webSearchQueries ) . toBeTruthy ( ) ;
934
+ }
935
+ } ) ;
936
+ it ( 'in preview should return grounding metadata when passed GoogleSearchRetriever in startChat' , async ( ) => {
937
+ const generativeTextModel = vertexAI . preview . getGenerativeModel ( {
938
+ model : TEXT_MODEL_NAME ,
939
+ } ) ;
940
+ const chat = generativeTextModel . startChat ( {
941
+ tools : TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL ,
942
+ } ) ;
943
+ const result = await chat . sendMessageStream ( 'Why is the sky blue?' ) ;
944
+ const response = await result . response ;
945
+ const groundingMetadata = response . candidates [ 0 ] . groundingMetadata ;
946
+ expect ( ! ! groundingMetadata ) . toBeTruthy ( ) ;
947
+ if ( groundingMetadata ) {
948
+ expect ( ! ! groundingMetadata . groundingAttributions ) . toBeTruthy ( ) ;
949
+ expect ( ! ! groundingMetadata . webSearchQueries ) . toBeTruthy ( ) ;
950
+ }
951
+ } ) ;
831
952
} ) ;
832
953
833
954
describe ( 'countTokens' , ( ) => {
0 commit comments